usart.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "usart.h"
  2. void USART_GPIO_Config(u32 RCC_APBnPeriphn, u8 GPIO_AF_USARTn,GPIO_TypeDef *GPIOx, u16 PIN, u8 GPIO_PinSource)
  3. {
  4. RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn,ENABLE);
  5. GPIO_PinAFConfig(GPIOx, GPIO_PinSource, GPIO_AF_USARTn); //GPIOA10复用为USART1
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. GPIO_InitStructure.GPIO_Pin = PIN;
  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
  9. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
  10. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
  11. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
  12. GPIO_Init(GPIOx,&GPIO_InitStructure);
  13. }
  14. void USART_NVIC_Config(s8 NVIC_IRQChannel, u8 pre, u8 sub)
  15. {
  16. NVIC_InitTypeDef NVIC_InitStructure;
  17. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel;
  18. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre;
  19. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub;
  20. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  21. NVIC_Init(&NVIC_InitStructure);
  22. }
  23. void USART_MOD_Config(void (*ClockCmd)(uint32_t, FunctionalState), u32 RCC_APBnPeriphn, USART_TypeDef* USARTn, u32 baudrate)
  24. {
  25. (*ClockCmd)(RCC_APBnPeriphn, ENABLE);
  26. USART_InitTypeDef USART_InitStructure;
  27. USART_InitStructure.USART_BaudRate = baudrate; //波特率设置
  28. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  29. USART_InitStructure.USART_StopBits = USART_StopBits_1; //一个停止位
  30. USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验位
  31. USART_InitStructure.USART_Mode = USART_Mode_Rx; //收发模式
  32. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  33. USART_Init(USARTn, &USART_InitStructure);
  34. USART_ITConfig(USARTn, USART_IT_RXNE , ENABLE);
  35. USART_ITConfig(USARTn, USART_IT_IDLE , ENABLE);
  36. USART_Cmd(USARTn, ENABLE);
  37. }