You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

223 lines
6.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. /**
  2. * @file lcd.c
  3. * @author 1722451300@qq.com
  4. * @brief
  5. * @version 0.1
  6. * @date 2021-12-15
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. #include "lcd.h"
  12. volatile char lcd_rxbuff[50] = {0};
  13. volatile int32_t lcd_count = 0;
  14. //volatile int32_t recv_lcd_data_state=0;//1就判断是不是接收完成,0不判断
  15. //volatile int32_t from_lcd_recv_state = 0;
  16. //volatile int32_t lcd_t8_state = 0;
  17. extern volatile int32_t first_set_motor_speed_dsp;
  18. volatile int32_t lcd_issue_motor_speed=0;
  19. volatile int16_t judge_now_foreward_or_reversal_state=0;//正转0,反转1
  20. volatile int32_t first_set_speed_state=0;
  21. extern volatile int32_t speed_dsp_now;//当前电机的速度单位dsp
  22. extern volatile int32_t motor_jerk_state;//1电机处于急停状态
  23. //-----------------------------------------
  24. //-----------------------------------------
  25. void lcd_uart_config(void)
  26. {
  27. /**
  28. * @brief LCD的芯片与上位机的通信 Pro2303LCD段
  29. *
  30. */
  31. uart1_config();
  32. }
  33. void send_cmd_to_lcd(volatile char buff[20], unsigned int num)
  34. {
  35. /**
  36. * @brief LCD发送11命令
  37. *
  38. */
  39. while (num--)
  40. {
  41. while (!TRMT1)
  42. ;
  43. TX1B = *buff;
  44. buff++;
  45. }
  46. }
  47. //---------------------------------------
  48. //---------------------------------------
  49. uint8_t g_lcd_rxbuf_isready = 0;
  50. uint32_t g_process_recv_lcd_data_time = 0;
  51. void recv_from_lcd_data(unsigned char data)
  52. {
  53. /**
  54. * @brief lcd的数据
  55. *
  56. */
  57. g_lcd_rxbuf_isready=true;//进入中断就让他为真
  58. lcd_rxbuff[lcd_count++] = data;
  59. g_process_recv_lcd_data_time= hal_get_irqticket();//记录最后的时间
  60. }
  61. uint32_t judge_recv_lcd_data_finish(uint32_t time)
  62. {
  63. /**
  64. * @brief LCD的数据完成
  65. */
  66. static uint32_t recv_lcd_data_time = 0;
  67. recv_lcd_data_time = time;
  68. if (hal_has_passedms(recv_lcd_data_time) > 10) {
  69. g_lcd_rxbuf_isready=false;
  70. recv_lcd_data_time = hal_get_ticket();
  71. send_cmd_to_lcd(lcd_rxbuff,lcd_count);//接收完成将数据发给电脑
  72. lcd_count=0;
  73. return 0;
  74. }
  75. }
  76. /**
  77. * @brief lcd的指令进行分析
  78. * @param buff num
  79. *
  80. */
  81. uint8_t lcd_data_analyse(volatile char buff[20], unsigned int num)
  82. {
  83. volatile char str[2]={0};
  84. volatile int32_t speed=0;
  85. if(buff[0]!=0x5A || buff[1]!=0xA5)
  86. {
  87. return -1;
  88. }
  89. if(buff[3]==0x83)//如果是就代表着指令要发送给电机控制电机的状态
  90. {
  91. if(buff[4]==0x50&&buff[5]==0x20)//速度指令(包括+-设置停止四种方法)
  92. {
  93. lcd_issue_motor_speed = buff[7]<<8|buff[8];
  94. lcd_issue_motor_speed = lcd_issue_motor_speed*6;//这个是最终lcd下发的电机速度单位dsp
  95. if(first_set_speed_state==0)//第一次是进行速度的设定不需要向电机下发
  96. {
  97. first_set_motor_speed_dsp = lcd_issue_motor_speed;
  98. first_set_speed_state=1;
  99. lcd_issue_motor_speed=0;
  100. }
  101. else
  102. {
  103. lcd_issue_motor_speed = judge_foreward_or_reversal(lcd_issue_motor_speed,judge_now_foreward_or_reversal_state);//判断正转还是反转
  104. }
  105. }
  106. else if(buff[4]==0x50&&buff[5]==0x00)//正转命令
  107. {
  108. judge_now_foreward_or_reversal_state=0;
  109. lcd_issue_motor_speed = judge_foreward_or_reversal(first_set_motor_speed_dsp,0);//判断正转还是反转lcd_issue_motor_speed单位dsp
  110. }
  111. else if(buff[4]==0x50&&buff[5]==0x10)//反转命令
  112. {
  113. judge_now_foreward_or_reversal_state=1;
  114. lcd_issue_motor_speed = judge_foreward_or_reversal(first_set_motor_speed_dsp,1);//判断正转还是反转
  115. //set_motor_speed(lcd_issue_motor_speed);
  116. }
  117. }
  118. else return -1;
  119. }
  120. void lcd_switch_anomaly_page(void)
  121. {
  122. /**
  123. * @brief ,
  124. * 5A A5 07 82 00 84 5A 01 00 01
  125. */
  126. unsigned char anomaly_page_send_lcd_cmd[10]={0};
  127. anomaly_page_send_lcd_cmd[0]=0x5A;
  128. anomaly_page_send_lcd_cmd[1]=0xA5;
  129. anomaly_page_send_lcd_cmd[2]=0x07;
  130. anomaly_page_send_lcd_cmd[3]=0x82;
  131. anomaly_page_send_lcd_cmd[4]=0x00;
  132. anomaly_page_send_lcd_cmd[5]=0x84;
  133. anomaly_page_send_lcd_cmd[6]=0x5A;
  134. anomaly_page_send_lcd_cmd[7]=0x01;
  135. anomaly_page_send_lcd_cmd[8]=0x00;
  136. anomaly_page_send_lcd_cmd[9]=0x08;
  137. send_cmd_to_lcd(anomaly_page_send_lcd_cmd,10);
  138. }
  139. void lcd_switch_anomaly_page_key(void)
  140. {
  141. /**
  142. * @brief ,
  143. * 5A A5 07 82 00 84 5A 01 00 01
  144. */
  145. unsigned char anomaly_page_send_lcd_cmd[10]={0};
  146. anomaly_page_send_lcd_cmd[0]=0x5A;
  147. anomaly_page_send_lcd_cmd[1]=0xA5;
  148. anomaly_page_send_lcd_cmd[2]=0x07;
  149. anomaly_page_send_lcd_cmd[3]=0x82;
  150. anomaly_page_send_lcd_cmd[4]=0x00;
  151. anomaly_page_send_lcd_cmd[5]=0x84;
  152. anomaly_page_send_lcd_cmd[6]=0x5A;
  153. anomaly_page_send_lcd_cmd[7]=0x01;
  154. anomaly_page_send_lcd_cmd[8]=0x00;
  155. anomaly_page_send_lcd_cmd[9]=0x07;
  156. send_cmd_to_lcd(anomaly_page_send_lcd_cmd,10);
  157. }
  158. void lcd_actual_speed_set(int32_t now_speed)
  159. {
  160. /**
  161. * @brief
  162. * 5A A5 05 82 50 90 00 64
  163. */
  164. volatile unsigned char lcd_show_now_motor_speed[10]={0};
  165. now_speed = now_speed/6;
  166. lcd_show_now_motor_speed[0]=0x5A;
  167. lcd_show_now_motor_speed[1]=0xA5;
  168. lcd_show_now_motor_speed[2]=0x05;
  169. lcd_show_now_motor_speed[3]=0x82;
  170. lcd_show_now_motor_speed[4]=0x50;
  171. lcd_show_now_motor_speed[5]=0x90;
  172. //速度具体值
  173. lcd_show_now_motor_speed[6]=(now_speed>>8)&0xff;//速度高字节
  174. lcd_show_now_motor_speed[7]=now_speed&0xff;//速度低字节
  175. send_cmd_to_lcd(lcd_show_now_motor_speed,8);
  176. }
  177. void lcd_speed_set_val(int32_t now_speed)
  178. {
  179. /**
  180. * @brief
  181. * 5A A5 05 82 50 20 00 00
  182. */
  183. unsigned char lcd_show_now_motor_speed[10]={0};
  184. lcd_show_now_motor_speed[0]=0x5A;
  185. lcd_show_now_motor_speed[1]=0xA5;
  186. lcd_show_now_motor_speed[2]=0x05;
  187. lcd_show_now_motor_speed[3]=0x82;
  188. lcd_show_now_motor_speed[4]=0x50;
  189. lcd_show_now_motor_speed[5]=0x20;
  190. //速度具体值
  191. lcd_show_now_motor_speed[6]=0x00;//速度高字节
  192. lcd_show_now_motor_speed[7]=0x00;//速度低字节
  193. send_cmd_to_lcd(lcd_show_now_motor_speed,8);
  194. }
  195. void clear_zero(void)
  196. {
  197. /**
  198. * @brief
  199. *
  200. */
  201. lcd_actual_speed_set(0);
  202. lcd_speed_set_val(0);
  203. judge_now_foreward_or_reversal_state=0;
  204. first_set_motor_speed_dsp=0;
  205. lcd_issue_motor_speed=0;
  206. }