uart.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include "uart.h"
  2. #include "TIM.h"
  3. Uart_Data_Struct Uart_dat = { 0 ,0 , 1 , 0 ,0 , 0 , 0 };
  4. void USART1_Init(u32 Baud)
  5. {
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. USART_InitTypeDef USART_InitStructure;
  8. NVIC_InitTypeDef NVIC_InitStructure;
  9. //--使能串口IO时钟,串口外设时钟
  10. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
  11. //--配置PA9-UART1-TX
  12. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //PA9
  13. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
  14. GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  15. GPIO_Init(GPIOA,&GPIO_InitStructure);
  16. //--配置PA10-UART1-RX
  17. GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //PA10
  18. GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  19. GPIO_Init(GPIOA,&GPIO_InitStructure);
  20. //--USART1-NVIC优先级配置
  21. NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn; //中断通道号
  22. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority/=3; //抢占优先级3
  23. NVIC_InitStructure.NVIC_IRQChannelSubPriority=3; //响应优先级3
  24. NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //使能IRQ中断
  25. NVIC_Init(&NVIC_InitStructure);
  26. //--USART1-参数配置
  27. USART_InitStructure.USART_BaudRate=Baud; //波特率
  28. USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //无硬件数据流控制
  29. USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //收发模式
  30. USART_InitStructure.USART_Parity=USART_Parity_No; //无奇偶校验
  31. USART_InitStructure.USART_StopBits=USART_StopBits_1; //一位停止位
  32. USART_InitStructure.USART_WordLength=USART_WordLength_8b; //8位数据格式
  33. USART_Init(USART1,&USART_InitStructure);
  34. //--使能USART1接收中断
  35. USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  36. //--使能USART1
  37. USART_Cmd(USART1,ENABLE);
  38. }
  39. //--发送一个字符--
  40. /***************** 发送一个字符 **********************/
  41. void USART_SendByte( USART_TypeDef * pUSARTx, uint8_t ch)
  42. {
  43. /* 发送一个字节数据到USART */
  44. USART_SendData(pUSARTx,ch);
  45. /* 等待发送数据寄存器为空 */
  46. while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
  47. }
  48. /***************** 发送字符串 **********************/
  49. void USART_SendString( USART_TypeDef * pUSARTx, char *str)
  50. {
  51. while( *str)
  52. {
  53. USART_SendByte(pUSARTx,*str++);
  54. }
  55. }
  56. /********USART发送一个缓冲区的数据**********/
  57. void USART_SendBuffDat(USART_TypeDef* uartx, u8 *BUFF, u8 len)
  58. {
  59. while(len--)
  60. {
  61. USART_SendByte(uartx,*BUFF++);
  62. }
  63. }
  64. /*----清除UART结构体的属性----*/
  65. void Clear_Uart_Dat( Uart_Data_Struct *uart_dat_struct )
  66. {
  67. uint16_t i ;
  68. for( i = 0; i < uart_dat_struct->Receive_Num ; i++)
  69. {
  70. uart_dat_struct->Recv_Buff[i] = 0;
  71. uart_dat_struct->Send_Buff[i] = 0;
  72. }
  73. uart_dat_struct->Receive_OK = 0;
  74. uart_dat_struct->Receive_Num = 0;
  75. uart_dat_struct->Receive_Byte_Flag = 0;
  76. uart_dat_struct->WaitTime = 0;
  77. }
  78. /*******串口1中断服务函数******/
  79. void USART1_IRQHandler(void) //函数在启动头文件中
  80. {
  81. //--接收中断发生
  82. if(USART_GetITStatus(USART1,USART_IT_RXNE))
  83. {
  84. USART_ClearFlag(USART1,USART_IT_RXNE); //清中断
  85. Uart_dat.WaitTime = 0;
  86. if(( Uart_dat.Receive_Num < UART_BUFF_MAX_SIZE ) && (!Uart_dat.Receive_OK) )
  87. {
  88. Uart_dat.Recv_Buff[ Uart_dat.Receive_Num++ ] = USART_ReceiveData( USART1 );
  89. Uart_dat.Receive_Byte_Flag = 1;
  90. }
  91. }
  92. }