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.

219 lines
6.5 KiB

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