usart.c 4.0 KB

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