当前位置:   article > 正文

STM32F103—有关陶晶驰串口屏的串口使用代码_淘晶驰串口屏和stm32

淘晶驰串口屏和stm32

串口HMI的基本指令集(官网链接) [USART HMI 资料中心] 

注:设备接收指令结束符为”0XFF 0XFF 0XFF”三个字节(HEX数据,不是字符串数据)。

适用于STM32F103系列的串口1,编写串口协议。

usart1.c

  1. #include "usart1.h"
  2. float everything =0;
  3. //接收缓存
  4. u8 reccmd_temp1[command_length1] ;
  5. //接收标志
  6. u8 rec_flag1 = 0;
  7. u8 flag_temp=0;
  8. u8 flag_first=0;
  9. int fputc(int ch, FILE *f)
  10. {
  11. USART_SendData(USART1, (unsigned char) ch);
  12. while (!(USART1->SR & USART_FLAG_TXE));
  13. return (ch);
  14. }
  15. void USART1_Int(void)
  16. {
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. USART_InitTypeDef USART_InitStructure;
  19. NVIC_InitTypeDef NVIC_InitStructure;
  20. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
  21. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
  22. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1 TX;
  23. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出;
  24. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  25. GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;
  26. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART1 RX;
  27. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入;
  28. GPIO_Init(GPIOA, &GPIO_InitStructure); //端口A;
  29. USART_InitStructure.USART_BaudRate = 115200; //波特率;
  30. USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位;
  31. USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位1位;
  32. USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位;
  33. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控;
  34. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式;
  35. USART_Init(USART1, &USART_InitStructure);//配置串口参数;
  36. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //中断号;
  37. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级;
  38. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级;
  39. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  40. NVIC_Init(&NVIC_InitStructure);
  41. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  42. USART_Cmd(USART1, ENABLE); //使能串口
  43. }
  44. void USART1_IRQHandler(void)
  45. {
  46. u8 rec_tem = 0;//--接收缓存
  47. static u8 rec_count = 0;//--接收计数
  48. static u8 Start_rec_flag1 = 0;//接受标志位
  49. if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
  50. {
  51. rec_tem = USART_ReceiveData(USART1);
  52. if(rec_tem == open_first)//判断接受的数据的指令头是否正确
  53. {
  54. rec_count = 0;
  55. Start_rec_flag1 = 1;
  56. }
  57. if(Start_rec_flag1)//开始接收
  58. {
  59. reccmd_temp1[rec_count ++] = rec_tem;//将接收到的一个字节数据存入 接收缓存
  60. if(rec_count == command_length1)//接收完成
  61. {
  62. Start_rec_flag1 = 0;
  63. rec_count = 0;
  64. rec_flag1 = 1;
  65. }
  66. }
  67. USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除标志位;
  68. }
  69. }
  70. //--协议解析 AA 01 00 00 00 00 00 00
  71. void protocol_analysis(void)
  72. {
  73. if(reccmd_temp1[0] == 0xAA ) //判断是起始位和名称
  74. {
  75. reccmd_temp1[0] = 0;
  76. switch(reccmd_temp1[1])//判断类型
  77. {
  78. case 0x01:
  79. led_on; //开灯
  80. everything+=1;
  81. flag_first = 0;
  82. break;
  83. case 0x02:
  84. led_off; //关灯
  85. everything-=1;
  86. flag_first = 1;
  87. break;
  88. }
  89. }
  90. }

usart1.h

  1. #ifndef _usart1_H
  2. #define _usart1_H
  3. #include "stm32f10x.h"
  4. #include "stdbool.h"
  5. #include "stdio.h"
  6. #include "led.h"
  7. extern float everything;
  8. //--指令长度
  9. #define command_length1 8
  10. //--接收缓存
  11. extern u8 reccmd_temp1[command_length1];
  12. //--接收标志
  13. extern u8 rec_flag1;
  14. extern u8 dis_tence;
  15. extern u8 flag_first;
  16. //--指令头
  17. #define open_first 0xAA
  18. //串口1初始化
  19. void USART1_Int(void);
  20. // 串口1 接收中断函数
  21. void USART1_IRQHandler(void);
  22. //串口协议
  23. void protocol_analysis(void);
  24. #endif

main.c

  1. #include "stm32f10x.h"
  2. #include "systick.h"
  3. #include "led.h"
  4. #include "usart1.h"
  5. int main()
  6. {
  7. led_int();
  8. delay_init();
  9. USART1_Int();
  10. while(1)
  11. {
  12. while(flag_first ==0)
  13. {
  14. protocol_analysis(); //串口协议解析
  15. printf("t1.txt=\" %.3lf\r\n\"\xff\xff\xff", everything);//串口屏实时显示
  16. }
  17. }
  18. }

在上述的串口屏实时显示的时候,使用串口屏软件中t1.txt的模块。即:everthing在t1.txt的模块中显示。

在串口屏软件中写的协议,如下所示:

 其中printh用于十六进制

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号