完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
本帖最后由 zoudiy16888 于 2015-7-20 10:58 编辑
现在做的一个项目要增加新功能。想请教大家能不能加上去。增加的功能是在搜索模式里加上温度控制,在进入搜索模式里当PCB板温度达到65摄氏度时,搜索模式会闪灯报警一次,然后返回之前的模式。如果是PCB板温度到了65摄氏度,这时要进入搜索模式时会闪灯报警一次,后返回之前模式。想请教大家有没有好的方法改过来?谢谢! 源程序如下: #include "includes.h" #define MODE_M1M1 0 #define MODE_M1M2 1 #define MODE_M2M1 2 #define MODE_M2M2 3 #define MODE_M3M1 4 #define MODE_M3M2 5 #define MODE_SEARCH 6 #define MODE_S1 7 #define MODE_S2 8 #define MODE_CHECK 9 #define MODE_HOME 10 #define MODE_IDLE 11 #define MODE_CHECK1 21 u8 MODE_DUTY[ MODE_CHECK + 1][ 3] = { {70, 30, 60}, //MODE_M1M1 {30, 15, 30}, //MODE_M1M2 {40, 35, 75}, //MODE_M2M1 {20, 20, 30}, //MODE_M2M2 {30, 65, 50}, //MODE_M3M1 {15, 20, 20}, //MODE_M3M2 {85, 70, 70}, //MODE_SEARCH {50, 15, 30}, //MODE_S1 {15, 0, 15}, //MODE_S2 { 0, 0, 0}, //¹Ø»ú 22UF */ }; #define SHADE_tiME 300 //500ms #define SHADE_TIME1 300 //10000ms #define V1 (6500 - 200) #define V2 (5900 - 200) #define V3 7250 #define V4 6850 static u8 mode_duty[ MODE_CHECK + 1][ 3]; static u8 mode = MODE_CHECK; static u8 mode_buf = MODE_CHECK; static u8 mode_last = MODE_IDLE; static u32 voltage = 0; static u8 voltage_check_flag = 1; static u32 m6_count = 0; static u8 temp_process_flag = 1; static u8 home_flag = 0; static u8 startflag = 0; /* *********************************************** *º¯ÊýÃû£º get_voltage_task *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void get_voltage_task(void) { u32 v1 = adc_value[ ADC_CHANNEL_3V]; u32 v2 = adc_value[ ADC_CHANNEL_BV]; voltage = v2 * 3000UL / v1; voltage = 258UL * voltage / 60UL; } /* *********************************************** *º¯ÊýÃû£º mode_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void set_mode(u8 m) { mode = m; if( mode <= MODE_CHECK ) { mode_duty[ mode][ LED_DEV_LED1] = MODE_DUTY[ mode][ LED_DEV_LED1]; mode_duty[ mode][ LED_DEV_LED2] = MODE_DUTY[ mode][ LED_DEV_LED2]; mode_duty[ mode][ LED_DEV_LED3] = MODE_DUTY[ mode][ LED_DEV_LED3]; if( mode == MODE_S1 || mode == MODE_S2 ) { led_set_status( LED_DEV_LED1,LED_MODE1, mode_duty[ mode][ LED_DEV_LED1], 0 ); led_set_status( LED_DEV_LED2,LED_MODE1, mode_duty[ mode][ LED_DEV_LED2], 0 ); led_set_status( LED_DEV_LED3,LED_MODE1, mode_duty[ mode][ LED_DEV_LED3], 0 ); } else { led_set_status_shade(LED_DEV_LED1,mode_duty[ mode][ LED_DEV_LED1], SHADE_TIME); led_set_status_shade(LED_DEV_LED2,mode_duty[ mode][ LED_DEV_LED2], SHADE_TIME); led_set_status_shade(LED_DEV_LED3,mode_duty[ mode][ LED_DEV_LED3], SHADE_TIME); } } } /* *********************************************** *º¯ÊýÃû£º voltage_check *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void voltage_check(void) { if( mode <= MODE_M3M2 || MODE_S1 == mode || mode == MODE_S2) { if( voltage <= V2 && home_flag == 0 && startflag ) { led_set_status( LED_DEV_LED1, LED_MODE2, mode_duty[ mode][ LED_DEV_LED1], 1 ); led_set_status( LED_DEV_LED2, LED_MODE2, mode_duty[ mode][ LED_DEV_LED2], 1 ); led_set_status( LED_DEV_LED3, LED_MODE2, mode_duty[ mode][ LED_DEV_LED3], 1 ); led_set_wait_mode2(LED_DEV_LED1); led_set_wait_mode2(LED_DEV_LED2); led_set_wait_mode2(LED_DEV_LED3); if( mode == MODE_M3M2) { led_set_status( LED_DEV_LED1, LED_MODE_KEEP, 0, 0 ); led_set_status( LED_DEV_LED2, LED_MODE_KEEP, 28, 100 ); led_set_status( LED_DEV_LED3, LED_MODE_KEEP, 28, 100 ); } else { led_set_status_shade( LED_DEV_LED1, 0, 300 ); led_set_status_shade( LED_DEV_LED2, 28, 300 ); led_set_status_shade( LED_DEV_LED3, 28, 300 ); } mode = MODE_HOME; home_flag = 1; temp_process_flag = 1; return; } if( voltage <= V1 && voltage_check_flag) { voltage_check_flag = 0; led_set_status( LED_DEV_LED1, LED_MODE2, mode_duty[ mode][ LED_DEV_LED1], 2 ); led_set_status( LED_DEV_LED2, LED_MODE2, mode_duty[ mode][ LED_DEV_LED2], 2 ); led_set_status( LED_DEV_LED3, LED_MODE2, mode_duty[ mode][ LED_DEV_LED3], 2 ); led_set_wait_mode2(LED_DEV_LED1); led_set_wait_mode2(LED_DEV_LED2); led_set_wait_mode2(LED_DEV_LED3); set_mode(mode); temp_process_flag = 1; } } } /* *********************************************** *º¯ÊýÃû£º temp_check_count *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void temp_check_count(void) { static u32 count = 0; if( mode != mode_last ) { count++; if( count >= 10000UL ) { count = 0; temp_process_flag = 1; mode_last = mode; } } else { count = 0; } } /* *********************************************** *º¯ÊýÃû£º mode_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_idle_processor(void) { switch(key_value.value) { case KEY_CLICK: if( key_value.num == KEY_NUM1 ) { set_mode( MODE_M1M1); } else if( key_value.num == KEY_NUM12 ) { set_mode( MODE_M1M1); } break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: break; default: break; } } /* *********************************************** *º¯ÊýÃû£º key_m1_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_m1_processor(void) { switch(key_value.value) { case KEY_CLICK: if( key_value.num == KEY_NUM1 ) { temp_process_flag = 1; if( mode == MODE_M1M1) { mode = MODE_M1M2; } else { mode = MODE_M1M1; } set_mode( mode); } if( key_value.num == KEY_NUM12 ) { if( mode == MODE_M1M1 ) { set_mode( MODE_M2M1); } else { set_mode( MODE_M2M2); } } break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: if( key_value.num == KEY_NUM1 ) { mode_buf = mode; led_duty_cycle_save(LED_DEV_LED1,0); led_duty_cycle_save(LED_DEV_LED2,0); led_duty_cycle_save(LED_DEV_LED3,0); set_mode(MODE_SEARCH); } if( key_value.num == KEY_NUM12 ) { set_mode(MODE_CHECK); } break; default: break; } } /* *********************************************** *º¯ÊýÃû£º key_m2_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_m2_processor(void) { switch(key_value.value) { case KEY_CLICK: if( key_value.num == KEY_NUM1 ) { temp_process_flag = 1; if( mode == MODE_M2M1) { mode = MODE_M2M2; } else { mode = MODE_M2M1; } set_mode( mode); } if( key_value.num == KEY_NUM12 ) { if( mode == MODE_M2M1 ) { set_mode( MODE_M3M1); } else { set_mode( MODE_M3M2); } } break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: if( key_value.num == KEY_NUM1 ) { mode_buf = mode; led_duty_cycle_save(LED_DEV_LED1,0); led_duty_cycle_save(LED_DEV_LED2,0); led_duty_cycle_save(LED_DEV_LED3,0); set_mode(MODE_SEARCH); } if( key_value.num == KEY_NUM12 ) { set_mode(MODE_CHECK); } break; default: break; } } /* *********************************************** *º¯ÊýÃû£º key_m3_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_m3_processor(void) { switch(key_value.value) { case KEY_CLICK: if( key_value.num == KEY_NUM1 ) { temp_process_flag = 1; if( mode == MODE_M3M1) { mode = MODE_M3M2; } else { mode = MODE_M3M1; } set_mode( mode); } if( key_value.num == KEY_NUM12 ) { if( mode == MODE_M3M1 ) { set_mode( MODE_S1); } else { set_mode( MODE_S2); } } break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: if( key_value.num == KEY_NUM1 ) { mode_buf = mode; led_duty_cycle_save(LED_DEV_LED1,0); led_duty_cycle_save(LED_DEV_LED2,0); led_duty_cycle_save(LED_DEV_LED3,0); set_mode(MODE_SEARCH); } if( key_value.num == KEY_NUM12 ) { set_mode(MODE_CHECK); } break; default: break; } } /* *********************************************** *º¯ÊýÃû£º key_search_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_search_processor(void) { switch(key_value.value) { case KEY_CLICK: break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: break; default: break; } } /* *********************************************** *º¯ÊýÃû£º search_run *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void search_run(void) { m6_count++; if( m6_count < 10000) { return; } { if( mode_buf == MODE_S1 || mode_buf == MODE_S2 ) { led_set_status( LED_DEV_LED1,LED_MODE1, mode_duty[ mode_buf][ LED_DEV_LED1], 0 ); led_set_status( LED_DEV_LED2,LED_MODE1, mode_duty[ mode_buf][ LED_DEV_LED2], 0 ); led_set_status( LED_DEV_LED3,LED_MODE1, mode_duty[ mode_buf][ LED_DEV_LED3], 0 ); } else { led_duty_cycle_read(LED_DEV_LED1); led_duty_cycle_read(LED_DEV_LED2); led_duty_cycle_read(LED_DEV_LED3); } mode = mode_buf; } } /* *********************************************** *º¯ÊýÃû£º key_s_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_s1s2_processor(void) { switch(key_value.value) { case KEY_CLICK: if( home_flag) { mode = MODE_HOME; led_set_status_shade( LED_DEV_LED1, 0, 50 ); led_set_status_shade( LED_DEV_LED2, 28, 300 ); led_set_status_shade( LED_DEV_LED3, 28, 300 ); temp_process_flag = 1; } else { if( key_value.num == KEY_NUM1 ) { if( mode == MODE_S1) { mode = MODE_S2; } else { mode = MODE_S1; } set_mode( mode); } if( key_value.num == KEY_NUM12 ) { if( mode == MODE_S1 ) { set_mode( MODE_M1M1); } else { set_mode( MODE_M1M2); } } } break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: if( key_value.num == KEY_NUM1 ) { mode_buf = mode; led_duty_cycle_save(LED_DEV_LED1,0); led_duty_cycle_save(LED_DEV_LED2,0); led_duty_cycle_save(LED_DEV_LED3,0); set_mode(MODE_SEARCH); } if( key_value.num == KEY_NUM12 ) { set_mode(MODE_CHECK); } break; default: break; } } /* *********************************************** *º¯ÊýÃû£º key_home_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_home_processor(void) { switch(key_value.value) { case KEY_CLICK: if( key_value.num == KEY_NUM1 ) { set_mode( MODE_S2); } break; case KEY_DOUBLE_CLICK: break; case KEY_LONG_CLICK: if( key_value.num == KEY_NUM12 ) { set_mode(MODE_CHECK); } break; default: break; } } /* *********************************************** *º¯ÊýÃû£º key_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void key_processor(void) { if( key_value.value ) { switch(mode) { case MODE_CHECK: break; case MODE_IDLE: key_idle_processor(); break; case MODE_M1M1: case MODE_M1M2: key_m1_processor(); break; case MODE_M2M1: case MODE_M2M2: key_m2_processor(); break; case MODE_M3M1: case MODE_M3M2: key_m3_processor(); break; case MODE_SEARCH: key_search_processor(); break; case MODE_S1: case MODE_S2: key_s1s2_processor(); break; case MODE_HOME: key_home_processor(); break; default: break; } key_set(0, KEY_NONE); } } /* *********************************************** *º¯ÊýÃû£º mode_processor *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ static u16 count = 0; static u8 flag = 0; void mode_processor(void) { if( mode == MODE_CHECK) { if( flag == 0 ) { if(voltage > V3) { led_set_status( LED_DEV_GREED, LED_MODE_KEEP, 80, 0); flag =1; } else { led_set_status( LED_DEV_RED, LED_MODE_KEEP, 80, 0); flag =1; } count = 0; } if( flag == 1 ) { delay_ms(150); count++; if( count >= 5 ) { flag = 3; } if( key_value.value == KEY_NUM1) { flag = 3; } } if( flag == 3 ) { led_set_status( LED_DEV_GREED, LED_MODE_KEEP, 0, 0); led_set_status( LED_DEV_RED, LED_MODE_KEEP, 0, 0); flag = 0; temp_process_flag = 1; voltage_check_flag = 1; set_mode(MODE_IDLE); home_flag = 0; startflag = 1; } } } /* *********************************************** *º¯ÊýÃû£º temp_check *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void high_temp_check(void) { if(ntc_temp > 80 && ntc_temp < 150 && MODE_IDLE != mode) { set_mode(MODE_CHECK); } } /* *********************************************** *º¯ÊýÃû£º temp_check *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ void temp_process(void) { if( mode > MODE_M3M2 ) { return; } if( ntc1_temp > 70 && temp_process_flag) { led_set_status_shade(LED_DEV_LED1,25, 1500);//ÿMSÃë±ä»¯0.1 led_set_status_shade(LED_DEV_LED2,20, 1500);//ÿMSÃë±ä»¯0.1 led_set_status_shade(LED_DEV_LED3,20, 1500);//ÿMSÃë±ä»¯0.1 temp_process_flag = 0; } if( ntc1_temp < 50 && (!temp_process_flag)) { led_set_status_shade(LED_DEV_LED1,mode_duty[ mode][ LED_DEV_LED1], SHADE_TIME1); led_set_status_shade(LED_DEV_LED2,mode_duty[ mode][ LED_DEV_LED2], SHADE_TIME1); led_set_status_shade(LED_DEV_LED3,mode_duty[ mode][ LED_DEV_LED3], SHADE_TIME1); temp_process_flag = 1; } } /* *********************************************** *º¯ÊýÃû£º delay_ms *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ volatile u32 time_counter = 0; void delay_ms(u32 time) { time_counter = time; while(time_counter) { IWDG_ReloadCounter(); } } /* *********************************************** *º¯ÊýÃû£º smartsys_device_timer4_init *¹¦ÄÜ £º *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *********************************************** */ static void smartsys_device_timer4_init(void) { TIM4_TimeBaseInit(TIM4_PRESCALER_128, 125);//1ms TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE); TIM4_Cmd(ENABLE); } void main( void ) { led_init(); pwm_init(); key_init(); CLK_ClockSwitchConfig(CLK_SWITCHMODE_MANUAL, CLK_SOURCE_HSI, ENABLE, CLK_CURRENTCLOCKSTATE_ENABLE); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); enableInterrupts(); smartsys_device_timer4_init(); delay_ms(500); IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); IWDG_SetPrescaler(IWDG_Prescaler_128); IWDG_SetReload(0xFF); IWDG_Enable(); while(1) { IWDG_ReloadCounter(); adc_task(); get_voltage_task(); mode_processor(); temp_task(); high_temp_check(); temp_process(); key_processor(); voltage_check(); } } /* *************************************************** *º¯ÊýÃû£º SmartSys_Device_Timer4_InterruptService *¹¦ÄÜ £º¶¨Ê±Æ÷4ÖжϷþÎñº¯Êý *ÊäÈë : ÎÞ *·µ»Ø : ÎÞ *************************************************** */ #pragma vector = 25 __interrupt void SmartSys_Device_Timer4_InterruptService(void) { TIM4_ClearFlag(TIM4_FLAG_UPDATE); if(time_counter) { time_counter--; } key_task(); led_task(); temp_check_count(); if(mode == MODE_SEARCH) { search_run(); } else { m6_count = 0; } } 硬件有二个按键S1和S2。短按S1就在模式1、模式1.2转换;S1和S2同时短按下去就从模式1转为模式2,一直到模式4;长按S1就会进入搜索模式下亮10秒后返回之前的模式。 现在想在搜索模式里加个温度检测,在进入搜索模式时或进入搜索模式后温度达到65度时需要闪灯报警后返之前模式。 |
|
相关推荐
2个回答
|
|
|
|
|
|
学习学习,谢谢分享!
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2183 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
2011 浏览 3 评论
4612 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2154 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2687 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-25 12:51 , Processed in 0.643233 second(s), Total 75, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号