1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- #include "usart.h"
- void USART_GPIO_Config(u32 RCC_APBnPeriphn, u8 GPIO_AF_USARTn,GPIO_TypeDef *GPIOx, u16 PIN, u8 GPIO_PinSource)
- {
- RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn,ENABLE);
-
- GPIO_PinAFConfig(GPIOx, GPIO_PinSource, GPIO_AF_USARTn); //GPIOA10复用为USART1
-
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
- GPIO_Init(GPIOx,&GPIO_InitStructure);
- }
- void USART_NVIC_Config(s8 NVIC_IRQChannel, u8 pre, u8 sub)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART_MOD_Config(void (*ClockCmd)(uint32_t, FunctionalState), u32 RCC_APBnPeriphn, USART_TypeDef* USARTn, u32 baudrate)
- {
- (*ClockCmd)(RCC_APBnPeriphn, ENABLE);
-
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = baudrate; //波特率设置
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
- USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
- USART_InitStructure.USART_Mode = USART_Mode_Rx; //收发模式
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
-
- USART_Init(USARTn, &USART_InitStructure);
-
- USART_ITConfig(USARTn, USART_IT_RXNE , ENABLE);
- USART_ITConfig(USARTn, USART_IT_IDLE , ENABLE);
-
- USART_Cmd(USARTn, ENABLE);
- }
|