一、题目内容分析
- LCD高亮显示
- 模拟电压输入(ADC)
- 按键输入(单击;未要求长短按)
- LED闪烁指示
二、LCD高亮显示实现
设置了高亮选择变量->hightlight_sel
LCD_SetBackColor(Yellow); //设置高亮显示背景色
sprintf(text," Upper:LD%d ",upper_led+1);
LCD_DisplayStringLine(Line6,(u8*)text); //字符串显示
LCD_SetBackColor(Black); //设置背景颜色
if(hightlight_sel == 1){
LCD_SetBackColor(Yellow);
sprintf(text," Min Volt:%.1f ",V_min);
LCD_DisplayStringLine(Line4,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," Min Volt:%.1f ",V_min);
LCD_DisplayStringLine(Line4,(u8*)text);
}
if(hightlight_sel == 2){
LCD_SetBackColor(Yellow);
sprintf(text," Upper:LD%d ",upper_led+1);
LCD_DisplayStringLine(Line6,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," Upper:LD%d ",upper_led+1);
LCD_DisplayStringLine(Line6,(u8*)text);
}
if(hightlight_sel == 3){
LCD_SetBackColor(Yellow);
sprintf(text," Lower:LD%d ",lower_led + 1);
LCD_DisplayStringLine(Line8,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," Lower:LD%d ",lower_led + 1);
LCD_DisplayStringLine(Line8,(u8*)text);
}
三、ADC\按键见14届文章
四、LED灯闪烁
五、Usr.c
#include "usr.h"
#include "math.h"
struct keys key[4]={0,0,0};
uint8_t menu = 0;
uint8_t status = 0;
float V;
float V_max = 2.4;
float V_min = 1.2;
uint8_t hightlight_sel = 0;
uint8_t upper_led = LD1;
uint8_t lower_led = LD2;
void get_ADCvalue(void)
{
HAL_ADC_Start(&hadc2);
V = HAL_ADC_GetValue(&hadc2)*3.3/(4096.0-1.0);
}
void key_proc(void)
{
if(key[0].key_single_flag == 1){
key[0].key_single_flag = 0;
menu++;
menu%=2;
}else if(key[1].key_single_flag == 1){
key[1].key_single_flag = 0;
if(menu == MENU_SET){
hightlight_sel ++;
hightlight_sel %= 4;
}
}else if(key[2].key_single_flag == 1){
key[2].key_single_flag = 0;
if(menu == MENU_SET){
if(hightlight_sel == 0){
V_max += 0.3;
}else if(hightlight_sel == 1){
V_min += 0.3;
}else if(hightlight_sel == 2){
upper_led ++;
upper_led %= 8;
if(upper_led == lower_led){
upper_led ++;
}
}else if(hightlight_sel == 3){
lower_led ++;
lower_led %= 8;
if(lower_led == upper_led)
{
lower_led++;
}
}
}
}else if(key[3].key_single_flag == 1){
key[3].key_single_flag = 0;
if(menu == MENU_SET){
if(hightlight_sel == 0){
V_max -= 0.3;
}else if(hightlight_sel == 1){
V_min -= 0.3;
}else if(hightlight_sel == 2){
if(upper_led == 0){upper_led = LD8;}
else{upper_led --;}
if(lower_led == upper_led)
{
upper_led --;
}
if(upper_led == 255){upper_led = LD8;}
}else if(hightlight_sel == 3){
//lower_led --;
if(lower_led == 0){lower_led = LD8;}
else{lower_led --;}
if(lower_led == upper_led)
{
lower_led--;
}
if(lower_led == 255){lower_led = LD8;}
}
}
}
if(V_max >= 3.3){
V_max = 3.3;
}else if(V_max <= 0){
V_max = 0;
}
if(V_min <= 0){
V_min = 0;
}else if(V_min >= 3.3){
V_min = 3.3;
}
}
void led_set(uint8_t led_dis){
HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOC,led_dis<<8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
}
//void led_toggle(uint8_t led_dis){
// HAL_GPIO_TogglePin(GPIOC,(uint8_t)pow(2,led_dis));
// HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_SET);
// HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,GPIO_PIN_RESET);
//}
void display_init(void)
{
LCD_Init();
LCD_Clear(Black);
LCD_SetBackColor(Black);
LCD_SetTextColor(White);
led_set(0x00);
}
void menu_display(void)
{
char text[25];
get_ADCvalue();
if((V >= V_min) && (V <= V_max)){
status = STATUS_Normal;
led_set(0x00);
}else if(V > V_max){
status = STATUS_Upper;
// led_set((uint8_t)pow(2,upper_led));
}else if(V < V_min){
status = STATUS_Lower;
// led_set((uint8_t)pow(2,lower_led));
}
if(menu == MENU_MAIN){
sprintf(text," Main ");
LCD_DisplayStringLine(Line0,(u8*)text);
sprintf(text," Volt:%.1f ",V);
LCD_DisplayStringLine(Line2,(u8*)text);
if(status == STATUS_Upper){
sprintf(text," Status:Upper ");
}else if(status == STATUS_Normal){
sprintf(text," Status:Normal ");
}else if(status == STATUS_Lower){
sprintf(text," Status:Lower ");
}
LCD_DisplayStringLine(Line4,(u8*)text);
LCD_ClearLine(Line6);
LCD_ClearLine(Line8);
}else if(menu == MENU_SET){
sprintf(text," Setting ");
LCD_DisplayStringLine(Line0,(u8*)text);
if(hightlight_sel == 0){
LCD_SetBackColor(Yellow);
sprintf(text," MAX Volt:%.1f ",V_max);
LCD_DisplayStringLine(Line2,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," MAX Volt:%.1f ",V_max);
LCD_DisplayStringLine(Line2,(u8*)text);
}
if(hightlight_sel == 1){
LCD_SetBackColor(Yellow);
sprintf(text," Min Volt:%.1f ",V_min);
LCD_DisplayStringLine(Line4,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," Min Volt:%.1f ",V_min);
LCD_DisplayStringLine(Line4,(u8*)text);
}
if(hightlight_sel == 2){
LCD_SetBackColor(Yellow);
sprintf(text," Upper:LD%d ",upper_led+1);
LCD_DisplayStringLine(Line6,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," Upper:LD%d ",upper_led+1);
LCD_DisplayStringLine(Line6,(u8*)text);
}
if(hightlight_sel == 3){
LCD_SetBackColor(Yellow);
sprintf(text," Lower:LD%d ",lower_led + 1);
LCD_DisplayStringLine(Line8,(u8*)text);
LCD_SetBackColor(Black);
}else{
sprintf(text," Lower:LD%d ",lower_led + 1);
LCD_DisplayStringLine(Line8,(u8*)text);
}
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM6){
key[0].key_sta = HAL_GPIO_ReadPin(B1_GPIO_Port,B1_Pin);
key[1].key_sta = HAL_GPIO_ReadPin(B2_GPIO_Port,B2_Pin);
key[2].key_sta = HAL_GPIO_ReadPin(B3_GPIO_Port,B3_Pin);
key[3].key_sta = HAL_GPIO_ReadPin(B4_GPIO_Port,B4_Pin);
for(uint8_t i = 0;i < 4;i++){
switch(key[i].key_judge_sta){
case 0:{
if(key[i].key_sta == 0){
key[i].key_judge_sta = 1;
}else{
key[i].key_judge_sta = 0;
}
}break;
case 1:{
if(key[i].key_sta == 0){
key[i].key_judge_sta = 2;
}else{
key[i].key_judge_sta = 0;
}
}break;
case 2:{
if(key[i].key_sta == 1){
key[i].key_single_flag = 1;
key[i].key_judge_sta = 0;
}
}break;
}
}
}
if(htim->Instance == TIM7){
static uint16_t time = 0;
static char light_flag = 0;
if(status == STATUS_Upper){
time ++;
time %= 19;
if(time == 9){
light_flag = 1;
}else if(time >9){
light_flag = 0;
}
if(time == 9){
led_set((uint8_t)pow(2,upper_led));
}else{
led_set(0x00);
}
}else if(status == STATUS_Lower){
time ++;
time %= 19;
if(time == 9){
light_flag = 1;
}else if(time >9){
light_flag = 0;
}
if(time == 9){
led_set((uint8_t)pow(2,lower_led));
}else{
led_set(0x00);
}
}else{
time = 0;
}
}
}
六、Usr.h
#ifndef __USR_H
#define __USR_H
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stdio.h"
#include "lcd.h"
#include "adc.h"
#include "tim.h"
struct keys{
uint8_t key_sta;
uint8_t key_judge_sta;
uint8_t key_single_flag;
};
/*define----------------------------------------------------------------------*/
#define B4_Pin GPIO_PIN_0
#define B4_GPIO_Port GPIOA
#define B1_Pin GPIO_PIN_0
#define B1_GPIO_Port GPIOB
#define B2_Pin GPIO_PIN_1
#define B2_GPIO_Port GPIOB
#define B3_Pin GPIO_PIN_2
#define B3_GPIO_Port GPIOB
#define MENU_MAIN 0
#define MENU_SET 1
#define STATUS_Upper 0
#define STATUS_Lower 1
#define STATUS_Normal 2
#define LD1 0 //1
#define LD2 1 //2
#define LD3 2 //4
#define LD4 3 //8
#define LD5 4 //16
#define LD6 5 //36
#define LD7 6 //64
#define LD8 7 //128
void key_proc(void);
void menu_display(void);
void display_init(void);
#endif