From fa8b98f748447e13fa25f09b31873a8495ccd1a0 Mon Sep 17 00:00:00 2001 From: zhuang <1722451300@qq.com> Date: Tue, 28 Dec 2021 12:30:51 +0800 Subject: [PATCH] =?UTF-8?q?main=E4=B8=AD=E8=B0=83=E8=AF=95urat1=E9=80=9A?= =?UTF-8?q?=E4=BF=A1=E6=AD=A3=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++ app.c | 167 ++++++++++++-------------------------------------- basic_type.h | 2 +- es7p_project_test.suo | Bin 17408 -> 17408 bytes lcd.c | 49 ++++++--------- lcd.h | 2 +- led.c | 18 ++++++ led.h | 6 +- system_init.c | 16 +++-- t21_t8.c | 110 +++++++++++++++------------------ t21_t8.h | 1 + uart.c | 48 ++++++++++----- 12 files changed, 186 insertions(+), 240 deletions(-) diff --git a/README.md b/README.md index a92e3d7..17c4c6c 100644 --- a/README.md +++ b/README.md @@ -31,3 +31,10 @@ [建议阅读避免踩坑](./doc/问题积累.md) +``` +第一步:接收屏幕串口数据正常,打断点的形式,拿到buff里面的命令数据, + +第一步:测试两个串口通信正常, +第二步: +``` + diff --git a/app.c b/app.c index f4f97fc..df792c7 100644 --- a/app.c +++ b/app.c @@ -9,131 +9,6 @@ * */ #include "app.h" -extern volatile int32_t lcd_issue_motor_speed;//lcd下发的速度单位dsp -extern volatile int32_t speed_dsp_now;//当前电机的速度单位dsp -extern int16_t motor_off_state;//电机关闭的状态 -extern volatile int16_t judge_now_foreward_or_reversal_state;//正转反转的状态 -extern int32_t wait_send_to_motor_time1s;//等待发送的时间 -extern int32_t first_set_speed_state; //设置速度的状态 -extern volatile int32_t recv_lcd_data_state=0;//1就判断是不是接收完成,0不判断 -extern volatile int32_t recv_motor_data_state=0;//1就判断是不是接收完成,0不判断 -void func(void) -{ - volatile unsigned char str[] = "speed-1000"; - - send_cmd_to_motor(str,10); //发送一串字符测试 -} -void global_state_inquire(void) -{ -/** - * @brief 对状态进行查询 - * - */ - motor_jerk_status();//判断电机是否被按下 - //inquire_motor_error_state();//每1.5秒去读取电机的速度 - if(motor_off_state)//判断电机是否停止的状态 - { - motor_off_state=0; - judge_now_foreward_or_reversal_state=0;//判断正转还是反转的标志位清零 - first_set_speed_state=0;//停止以后清零,下次第一次设置速度不执行 - } - if(recv_lcd_data_state) - { - data_from_lcd_recv_Ok(); //判断接收来自lcd的数据是否完成 - } - if(recv_motor_data_state) - { - data_from_motor_recv_Ok(); //判断接收来自电机的数据是否完成 - } - -} -/** -void main() -{ - peripheral_config(); - //start_up(); //开机提示 - //func(); - int32_t nowexpectspeed_main=0; - int32_t setspeedthistime_main=0; - while (1) - { - global_state_inquire(); - if(lcd_issue_motor_speed!=speed_dsp_now) - { - if(lcd_issue_motor_speed>speed_dsp_now&&wait_send_to_motor_time1s>1100)//目标800,现在0 - //if(lcd_issue_motor_speed>speed_dsp_now) - { - wait_send_to_motor_time1s=0;//开始等待 - nowexpectspeed_main = speed_dsp_now + 100; - if (nowexpectspeed_main > lcd_issue_motor_speed) { - nowexpectspeed_main = lcd_issue_motor_speed; - } - set_moter_speed_internal(nowexpectspeed_main);// 去设置的是10 - //sleep_ms(500); - } - else if(lcd_issue_motor_speed1100)//目标0 //现在550 差值600 - //else if(lcd_issue_motor_speed 10) { + g_lcd_rxbuf_isready=false; + recv_lcd_data_time = hal_get_ticket(); + send_cmd_to_lcd(lcd_rxbuff,lcd_count);//接收完成将数据发给电脑 + lcd_count=0; + } +} + +void main() +{ + peripheral_config();//各种初始化包括ram清零 + uint8_t record_last_time=0; + while (1) + { + debug_light_ctrl_process_in_main_loop();//闪灯效果 + if(g_lcd_rxbuf_isready)//接收开始 + { + record_last_time = judge_recv_lcd_data_finish_main(record_last_time); + } + } +} + diff --git a/basic_type.h b/basic_type.h index a6aba4a..9c08a35 100644 --- a/basic_type.h +++ b/basic_type.h @@ -28,7 +28,7 @@ typedef int int16_t; typedef unsigned int uint16_t; typedef long int32_t; typedef unsigned long uint32_t; -//typedef unsigned char bool; + #define false 0 #define true 1 #define NULL 0 diff --git a/es7p_project_test.suo b/es7p_project_test.suo index 3d1513c52b9003988c1ca5e097720313c0963d37..2f93a6d0bcadd23991752e9a96647d7271c85f00 100644 GIT binary patch delta 326 zcmZqZU~K4M+>pbc#Q*;P|6c;6OM$oy#Ge=_z4;6K zd1hV)CLos^h=D5pPrk^aviS@51>wmq8U~xc$oq5hGAaS(*pcN+G<`T27fn8>r9XLz zLe^w1CoYx$%k5-L#0*$2E%S`l|9r8B6RI9$CJan2pb^*v~WbGD{egs8kfCoh9AP?!l>afzl6hhSn}x^8Y_aY<@XOo@?Ue2K;6k4{<|H9&PJK!Sl0 zs820) - { - send_cmd_to_lcd(lcd_rxbuff,lcd_count); - send_cmd_to_motor(lcd_rxbuff, lcd_count); - if(motor_jerk_state==1&&lcd_rxbuff[4]==0x50&&lcd_rxbuff[5]==0x20)//说明现在还是急停状态 - { - send_cmd_to_lcd(lcd_rxbuff,lcd_count); - lcd_switch_anomaly_page();//急停状态的时候显示异常界面 - sleep_ms(3000); - lcd_switch_anomaly_page_key(); - lcd_count = 0; - recv_lcd_data_state=0; - } - else - { - //send_cmd_to_motor(lcd_rxbuff, lcd_count); - lcd_data_analyse(lcd_rxbuff, lcd_count); - //send_cmd_to_lcd(lcd_rxbuff,lcd_count);//调试2 - //send_cmd_to_motor(motor_rxbuff, motor_Count); //调试1将数据发回给串口助手 - lcd_count = 0; - recv_lcd_data_state=0; - } - + static uint32_t recv_lcd_data_time = 0; + if (hal_has_passedms(recv_lcd_data_time) > 10) { + g_lcd_rxbuf_isready=false; + recv_lcd_data_time = hal_get_ticket(); + send_cmd_to_lcd(lcd_rxbuff,lcd_count);//接收完成将数据发给电脑 } } diff --git a/lcd.h b/lcd.h index fdfceca..8f8e458 100644 --- a/lcd.h +++ b/lcd.h @@ -10,7 +10,7 @@ void lcd_uart_config(void); void lcd_send_commands(void); void send_cmd_to_lcd(volatile char buff[20],unsigned int num); -void data_from_lcd_recv_Ok(void); +void judge_recv_lcd_data_finish(void); uint8_t lcd_data_analyse(volatile char buff[20], unsigned int num); void recv_from_lcd_data(unsigned char data); void lcd_switch_anomaly_page(void); diff --git a/led.c b/led.c index 7b1eacf..14df3b1 100644 --- a/led.c +++ b/led.c @@ -29,3 +29,21 @@ void beep_config(void) beep_port &= ~(0x01 << 0); //低电平 } + + +void debug_light_ctrl_process_in_main_loop() { +/** + * @brief 蓝色灯的闪烁300ms + * + */ + static uint32_t last_ctrl_light_ticket = 0; + + uint16_t flicker_period = configDebugLightFlipPeriodMS; + if (hal_has_passedms(last_ctrl_light_ticket) > flicker_period) { + static uint8_t debuglight_state = false; + DEBUG_LIGHT_IO() = debuglight_state; + debuglight_state = !debuglight_state; + last_ctrl_light_ticket = hal_get_ticket(); + } +} + diff --git a/led.h b/led.h index 2f9a7cd..29996d2 100644 --- a/led.h +++ b/led.h @@ -5,6 +5,10 @@ #include "hic.h" #include "basic_type.h" #include "io_config.h" +#include "t21_t8.h" +#define configDebugLightFlipPeriodMS 300 +#define DEBUG_LIGHT_IO() PB4 + #define led_on debug_led_port &=~(0x01<<4) #define led_off debug_led_port |=(0x01<<4) @@ -15,5 +19,5 @@ void led_config(void); void beep_config(void); void jerk_key_config(void); -void start_up(void); +void debug_light_ctrl_process_in_main_loop(); #endif \ No newline at end of file diff --git a/system_init.c b/system_init.c index 037aa68..04539a5 100644 --- a/system_init.c +++ b/system_init.c @@ -18,8 +18,6 @@ extern volatile int32_t speed_dsp_now;//当前电机的速度单位dsp extern volatile int32_t motor_jerk_state; extern volatile char lcd_rxbuff[50]; extern volatile int32_t lcd_count; -extern volatile int32_t from_lcd_recv_state; -extern volatile int32_t lcd_t8_state; extern volatile int16_t judge_now_foreward_or_reversal_state; void ram_clear(void) { @@ -62,14 +60,22 @@ void clear_global_variable(void) motor_jerk_state= 0; lcd_rxbuff[50]= 0; lcd_count= 0; - from_lcd_recv_state= 0; - lcd_t8_state= 0; judge_now_foreward_or_reversal_state=0; } +void set_gie(uint8_t state) { + /** + * @brief 参考:ES7P169C_Datasheet_C%20V1.3.pdf:7.5.1 + */ + while (GIE != state) { + GIE = state; + } +} + void sys_init(void) { ram_clear(); - clear_global_variable(); + //clear_global_variable(); gpio_Init(); + set_gie(1); } \ No newline at end of file diff --git a/t21_t8.c b/t21_t8.c index 07f6fbf..c099c44 100644 --- a/t21_t8.c +++ b/t21_t8.c @@ -2,27 +2,40 @@ #include "led.h" volatile unsigned char timer_cnt; extern volatile int motor_jerk_state; -volatile uint32_t g_ticket100us = 0; -void t21_config(void) -{ - /** - * @brief 初始化T21 翻转LED的状态 - * - */ - T21CL = 0x00; //定时器模式 - T21CM = 0x0F; //预分频1:16 - T21CH = 0x79; //后分频次数121 - T21PH = 0x0F; //周期值高4位 - T21PL = 0xFF; //周期值低8位 - T21VIE = 1; //打开T21溢出中断 - T21VIF = 0; //清标志位 - GIE = 1; //开全局中断 - T21EN = 1; //使能T21 -} int32_t wait_send_to_motor_time1s=0; int32_t recv_motor_data_time=0; int32_t recv_lcd_data_time=0; int32_t inquire_error_time=0; +volatile uint32_t g_ticket100us = 0;//一直再记录过去的时间 +/** + * @brief 32M的情况下初始化时钟1作为ticket源 + * + */ +void init_timer1_as_ticketustimer_in32M() { + // T11M + /** + * @brief + * 设定时钟为1ms一次中断 + * T11CM = 0x0f 16分频 = 2MHZ + * T11CH = 0 后分频0 + * T11PH T11PL = 200 //100us + * T11PH = 0x07 + * T11PL = 0xD0 + */ + + T11CL = 0x00; //定时器模式 + T11CM = 0x0F; //预分频1:16 //最大1:16 + T11CH = 0x00; //后分频次数0+1 + static_assert(configTicketUs <= 10000 && configTicketUs >= 100); + uint16_t count = 2000 / 1000 * configTicketUs; + T11PH = count >> 8; //周期值高8位 + T11PL = count & 0xFF; // 200 + T11VIE = 1; //打开T11溢出中断 + T11VIF = 0; //清标志位 + T11EN = 1; //使能T11 +} + + int32_t clear_time() { if(wait_send_to_motor_time1s>=UINT32_MAX) @@ -46,6 +59,25 @@ int32_t clear_time() inquire_error_time=0; } } + +uint32_t hal_has_passed_ticket(uint32_t lastticket) { + uint32_t now = g_ticket100us; + if (now >= lastticket) { + return now - lastticket; + } else { + return UINT32_MAX - lastticket + now; + } +} + +uint32_t hal_has_passedms(uint32_t lastticket) { + /** + * 根据ticket的数值进行修改. + */ + static_assert(configTicketUs == 1000); + return hal_has_passed_ticket(lastticket); +} + + int32_t hal_get_ticket() { return g_ticket100us; @@ -71,34 +103,7 @@ void hal_ticket_inc() recv_lcd_data_time++; inquire_error_time++; } -volatile uint32_t g_ticket100us = 0; -/** - * @brief 32M的情况下初始化时钟1作为ticket源 - * - */ -void init_timer1_as_ticketustimer_in32M() { - // T11M - /** - * @brief - * 设定时钟为1ms一次中断 - * T11CM = 0x0f 16分频 = 2MHZ - * T11CH = 0 后分频0 - * T11PH T11PL = 200 //100us - * T11PH = 0x07 - * T11PL = 0xD0 - */ - T11CL = 0x00; //定时器模式 - T11CM = 0x0F; //预分频1:16 //最大1:16 - T11CH = 0x00; //后分频次数0+1 - static_assert(configTicketUs <= 10000 && configTicketUs >= 100); - uint16_t count = 2000 / 1000 * configTicketUs; - T11PH = count >> 8; //周期值高8位 - T11PL = count & 0xFF; // 200 - T11VIE = 1; //打开T11溢出中断 - T11VIF = 0; //清标志位 - T11EN = 1; //使能T11 -} void system_module_process_interrupt() { /** @@ -111,20 +116,3 @@ void system_module_process_interrupt() { } } - -void t21_irq_interrupt(void) -{ - /** - * @brief T21的0.5s的中断服务函数 - * - */ - if (T21VIE == 1 && T21VIF == 1) //定时器溢出中断 - { - T21VIF = 0; //清标志位 - if (motor_jerk_state == 1) - { - PB0 = ~PB0; - } - PB4 = ~PB4; - } -} diff --git a/t21_t8.h b/t21_t8.h index 67b5801..d00c7a6 100644 --- a/t21_t8.h +++ b/t21_t8.h @@ -16,5 +16,6 @@ void system_module_process_interrupt(); int32_t sleep_ms(int32_t time); void init_timer1_as_ticketustimer_in32M(); int32_t hal_get_ticket(); +uint32_t hal_has_passedms(uint32_t lastticket); #endif diff --git a/uart.c b/uart.c index 7276eb9..8c73ce7 100644 --- a/uart.c +++ b/uart.c @@ -16,14 +16,11 @@ #include "lcd.h" #if 0 #endif -unsigned char rxbuf; -unsigned char rx485buf; #define IT_SR //中断发送接收,注释此宏定义则使用查询方式发送接收 #define CLRWDT() \ { \ __Asm CWDT; \ } //宏定义清狗指令 -extern unsigned char rxbuf; void uart0_config(void) { @@ -141,10 +138,22 @@ void uart1_irq_interrupt(void) * @brief 串口1的中断服务函数 * */ - if (RX1IE == 1 && RX1IF == 1) + if (RX1IE == 1) { - rxbuf = RX1B; - recv_from_lcd_data(rxbuf); + //帧格式出错 + if (FERR1 == 1) { + } + //接收溢出 + if (OERR1 == 1) { + //重启串口 + RX1EN = 0; + RX1EN = 1; + } + if ( RX1IF == 1) + { + uint8_t rx232data = RX1B; + recv_from_lcd_data(rx232data); + } } } @@ -152,18 +161,29 @@ void uart1_irq_interrupt(void) void isr(void) interrupt { /** - * @brief 串口0的中断服务函数 + * @brief 中断服务函数 * */ - //uint16_t count = 1200; - if (RX0IE == 1 && RX0IF == 1) + if (RX0IE == 1) + { + //帧格式出错 + if (FERR0 == 1) { + } + //接收溢出 + if (OERR0 == 1) { + //重启串口 + RX0EN = 0; + RX0EN = 1; + } + + if ( RX0IF == 1)//串口0(电机数据接收) { - rx485buf = RX0B; - recv_from_motor_data(rx485buf); + uint8_t rx485data = RX0B; + recv_from_motor_data(rx485data); } - t21_irq_interrupt(); - uart1_irq_interrupt(); - system_module_process_interrupt(); + } + uart1_irq_interrupt();//屏幕数据接收 + system_module_process_interrupt();//定时器中断 } #endif