usart.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "sys.h"
  2. #include "usart.h"
  3. #include "string.h"
  4. #include <stdarg.h>
  5. #define Usart1ReadLen 50
  6. //值
  7. char Usart1ReadBuff[Usart1ReadLen]={0}; //接收数据缓存
  8. u32 Usart1ReadCnt = 0;//串口1接收到的数据个数
  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. void uart_init(u32 bound){
  31. //GPIO端口设置
  32. GPIO_InitTypeDef GPIO_InitStructure;
  33. USART_InitTypeDef USART_InitStructure;
  34. NVIC_InitTypeDef NVIC_InitStructure;
  35. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
  36. //USART1_TX GPIOA.9
  37. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  38. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  39. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  40. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
  41. //USART1_RX GPIOA.10初始化
  42. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
  43. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  44. GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
  45. //Usart1 NVIC 配置
  46. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  47. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
  48. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
  49. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  50. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
  51. //USART 初始化设置
  52. USART_InitStructure.USART_BaudRate = bound;//串口波特率
  53. USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
  54. USART_InitStructure.USART_StopBits = USART_StopBits_1;//一个停止位
  55. USART_InitStructure.USART_Parity = USART_Parity_No;//无奇偶校验位
  56. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
  57. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
  58. USART_Init(USART1, &USART_InitStructure); //初始化串口1
  59. USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
  60. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断
  61. USART_Cmd(USART1, ENABLE); //使能串口1
  62. }
  63. void UsartSendData(u8 *p,char len)
  64. {
  65. while(len--)
  66. {
  67. while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
  68. USART_SendData(USART1, *p++);
  69. }
  70. }
  71. extern u8 jr_flag;
  72. extern u8 mode_flag;
  73. void USART1_IRQHandler(void) //串口1中断服务程序
  74. {
  75. u8 Res,rxes;
  76. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾)
  77. {
  78. Res =USART_ReceiveData(USART1); //读取接收到的数据
  79. if(Usart1ReadCnt < Usart1ReadLen-1)
  80. {
  81. Usart1ReadBuff[Usart1ReadCnt] = Res;
  82. }
  83. else
  84. {
  85. Usart1ReadCnt=0;
  86. }
  87. Usart1ReadCnt ++; //数据个数
  88. }
  89. if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)//空闲中断
  90. {
  91. //用读SR和DR的方法清除IDLE
  92. rxes = USART1->SR;
  93. rxes = USART1->DR;
  94. if(Usart1ReadCnt>5)
  95. {
  96. if(strstr(Usart1ReadBuff,"open")!=NULL)
  97. {
  98. jr_flag=1;
  99. }else if(strstr(Usart1ReadBuff,"open")!=NULL)
  100. {
  101. jr_flag=0;
  102. }
  103. else if(strstr(Usart1ReadBuff,"mode1")!=NULL)
  104. {
  105. mode_flag=1;
  106. }else if(strstr(Usart1ReadBuff,"mode0")!=NULL)
  107. {
  108. mode_flag=0;
  109. }
  110. }
  111. Usart1ReadCnt=0;
  112. memset(Usart1ReadBuff,0,Usart1ReadLen-1);
  113. }
  114. }