一个完整的嵌入式系统,包括任务执行部分和人机交互部分。在前四节中,我们已经讲解了LED、LCD和按键,用这三者就能够实现一个人机交互系统,也即搭建整个嵌入式系统的框架。在后续,只要将各个功能加入到这个交互系统中,即可完成整个嵌入式系统的设计。因此,尽管你什么都不会,只会使用LED、LCD和按键,也至少能获得省三或省二的奖项。下面我们就以第十四届省赛题为例,看看如何实现一个完善的交互系统。
首先我们来看第一个要求:按下B1切换界面:
结合第二节:LCD显示的内容,只需要将切换界面处的判断条件更换为key[0].single_flag == 1即可:
if (key[0].single_flag == 1)
{LCD_Clear(Black); //每切换一次界面清一次屏view++;if (view > 2){view = 0;}key[0].single_flag = 0; //标志位清零(重要!)
}
再来看第二个要求:
B2的要求相对于B1来说,只是增加了不同界面的不同功能,其本质是一样的。注意,第二个小要求需要切换选择参数,这就需要一个标志变量choose。每次重新进入参数界面时,默认可调整的参数为R,只需要在切换界面时把choose重新赋值为'R'即可。
char choose = 'R';
if (key[1].single_flag == 1)
{if (view == 0) //数据界面{/* 5s切换模式 */if (M == 'H') M = 'L';else M = 'H';N++; //统计次数}else if (view == 1) //参数界面{if (choose == 'R') choose = 'K';else choose = 'R';}key[1].single_flag = 0; //标志位清零(重要!)
}
要求3:
结合要求2,当choose=='R'时,对R进行++;当choose=='K'时,对K进行++:
if (key[2].single_flag == 1 && view == 1) //参数界面
{if (choose == 'R'){R++;if (R > 10) R = 1;}else{K++;if (K > 10) K = 1;}key[2].single_flag = 0; //标志位清零(重要!)
}
要求4:
要求4的第1个要求与要求3类似,不再赘述。第2个要求要标志是否处于锁定状态,同样需要一个标志变量lock。
if (key[3].single_flag==1 && view==1) //参数界面
{if (choose == 'R'){R--;if (R < 1) R = 10;}else{K--;if (K < 1) K = 10;}key[3].single_flag = 0; //标志位清零(重要!)
}bool lock = 0;
if (view==0 && lock==0) //数据界面,解锁状态
{if (key[3].long_flag == 1) lock = 1;key[3].long_flag = 0; //标志位清零(重要!)
}
else if (view==0 && lock==1) //数据界面,锁定状态
{if (key[3].single_flag == 1) lock = 0;key[3].single_flag = 0; //标志位清零(重要!)
}
将所有有关LED、LCD与按键的操作封装成函数,在主循环中调用,即可实现一个交互系统。
/* task.c */#include "task.h"uint8_t view = 0;
char choose = 'R';
bool lock = 0;
char M = 'L';
int P;
double V, MH, ML;
int R = 1;
int K = 1;
int N = 0;
char text[20];void key_pro(void)
{//B1if (key[0].single_flag == 1){LCD_Clear(Black);view++;choose = 'R';if (view > 2){view = 0;}key[0].single_flag = 0;}//B2if (key[1].single_flag == 1){if (view == 0){/***************5s切换模式LED_Toggle(LD2);HAL_Delay(100);***************/if (M == 'H') M = 'L';else M = 'H';N++;}else if (view == 1){if (choose == 'R') choose = 'K';else choose = 'R';}key[1].single_flag = 0;}//B3if (key[2].single_flag == 1 && view == 1){if (choose == 'R'){R++;if (R > 10) R = 1;}else{K++;if (K > 10) K = 1;}key[2].single_flag = 0;}//B4if (key[3].single_flag==1 && view==1){if (choose == 'R'){R--;if (R < 1) R = 10;}else{K--;if (K < 1) K = 10;}key[3].single_flag = 0;}if (view==0 && lock==0){if (key[3].long_flag == 1) lock = 1;key[3].long_flag = 0;}else if (view==0 && lock==1){if (key[3].single_flag == 1) lock = 0;key[3].single_flag = 0;}
}void display(void)
{if (view == 0){LCD_DisplayStringLine(Line1, (uint8_t *)" DATA ");sprintf(text, " M=%c ", M);LCD_DisplayStringLine(Line3, (uint8_t *)text);sprintf(text, " P=%d%% ", P);LCD_DisplayStringLine(Line4, (uint8_t *)text);sprintf(text, " V=%.1f ", V);LCD_DisplayStringLine(Line5, (uint8_t *)text);LED_On(LD1);}if (view == 1){LCD_DisplayStringLine(Line1, (uint8_t *)" PARA ");sprintf(text, " R=%d ", R);LCD_DisplayStringLine(Line3, (uint8_t *)text);sprintf(text, " K=%d ", K);LCD_DisplayStringLine(Line4, (uint8_t *)text);LED_Off(LD1);}if (view == 2){LCD_DisplayStringLine(Line1, (uint8_t *)" RECD ");sprintf(text, " N=%d ", N);LCD_DisplayStringLine(Line3, (uint8_t *)text);sprintf(text, " MH=%.1f ", MH);LCD_DisplayStringLine(Line4, (uint8_t *)text);sprintf(text, " ML=%.1f ", V);LCD_DisplayStringLine(Line5, (uint8_t *)text);LED_Off(LD1);}if (lock == 1) LED_On(LD3);else LED_Off(LD3);
}
/* task.h */#ifndef __TASK_H
#define __TASK_H#include "main.h"
#include <stdio.h>
#include "led.h"
#include "lcd.h"
#include "fonts.h"
#include "key.h"void key_pro(void);
void display(void);#endif /* __TASK_H */