usart.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "sys.h"
  2. #include "usart.h"
  3. //////////////////////////////////////////////////////////////////////////////////
  4. //如果使用ucos,则包括下面的头文件即可.
  5. #if SYSTEM_SUPPORT_OS
  6. #include "FreeRTOS.h" //FreeRTOS使用
  7. #endif
  8. //////////////////////////////////////////////////////////////////
  9. //加入以下代码,支持printf函数,而不需要选择use MicroLIB
  10. #if 1
  11. #pragma import(__use_no_semihosting)
  12. //标准库需要的支持函数
  13. struct __FILE
  14. {
  15. int handle;
  16. };
  17. FILE __stdout;
  18. //定义_sys_exit()以避免使用半主机模式
  19. void _sys_exit(int x)
  20. {
  21. x = x;
  22. }
  23. //重定义fputc函数
  24. int fputc(int ch, FILE *f)
  25. {
  26. while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
  27. USART1->DR = (u8) ch;
  28. return ch;
  29. }
  30. #endif
  31. /*使用microLib的方法*/
  32. /*
  33. int fputc(int ch, FILE *f)
  34. {
  35. USART_SendData(USART1, (uint8_t) ch);
  36. while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {}
  37. return ch;
  38. }
  39. int GetKey (void) {
  40. while (!(USART1->SR & USART_FLAG_RXNE));
  41. return ((int)(USART1->DR & 0x1FF));
  42. }
  43. */
  44. #if EN_USART1_RX //如果使能了接收
  45. //串口1中断服务程序
  46. //注意,读取USARTx->SR能避免莫名其妙的错误
  47. u8 USART_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.
  48. //接收状态
  49. //bit15, 接收完成标志
  50. //bit14, 接收到0x0d
  51. //bit13~0, 接收到的有效字节数目
  52. u16 USART_RX_STA=0; //接收状态标记
  53. void uart_init(u32 bound){
  54. //GPIO端口设置
  55. GPIO_InitTypeDef GPIO_InitStructure;
  56. USART_InitTypeDef USART_InitStructure;
  57. NVIC_InitTypeDef NVIC_InitStructure;
  58. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
  59. //USART1_TX GPIOA.9
  60. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  61. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  62. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  63. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  64. //USART1_RX GPIOA.10初始化
  65. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  66. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  67. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  68. //Usart1 NVIC 配置
  69. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  70. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  71. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  72. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  73. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  74. //USART 初始化设置
  75. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  76. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  77. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  78. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  79. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  80. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  81. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  82. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  83. USART_Cmd(USART1, ENABLE); //使能串口1
  84. }
  85. void USART1_IRQHandler(void) //串口1中断服务程序
  86. {
  87. u8 Res;
  88. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
  89. {
  90. Res =USART_ReceiveData(USART1); //读取接收到的数据
  91. if((USART_RX_STA&0x8000)==0)//接收未完成
  92. {
  93. if(USART_RX_STA&0x4000)//接收到了0x0d
  94. {
  95. if(Res!=0x0a)USART_RX_STA=0;//接收错误,重新开始
  96. else USART_RX_STA|=0x8000; //接收完成了
  97. }
  98. else //还没收到0X0D
  99. {
  100. if(Res==0x0d)USART_RX_STA|=0x4000;
  101. else
  102. {
  103. USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
  104. USART_RX_STA++;
  105. if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//接收数据错误,重新开始接收
  106. }
  107. }
  108. }
  109. }
  110. }
  111. #endif