led.c文件
#include "led.h"
void Led(uint16_t addr,uint16_t enable)
{
static uint16_t temp = 0x0000;
static uint16_t temp_old = 0xffff;
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_All, GPIO_PIN_SET);
if(enable)
temp |= 0x0100 << addr;
else
temp &= ~(0x0100 << addr);
if(temp != temp_old)
{
HAL_GPIO_WritePin(GPIOC, temp, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_2, GPIO_PIN_RESET);
temp_old = temp;
}
}
key.c文件
#include "key.h"
extern uint16_t flag_10ms;
uint16_t Key_Scan(void)
{
static uint16_t cnt1 = 0;
static uint16_t cnt2 = 0;
static uint16_t cnt3 = 0;
static uint16_t cnt4 = 0;
uint16_t key_temp = 0;;
if(flag_10ms == 1)
{
if(B1 == 0) cnt1++;
if(B1 == 1)
{
if(cnt1 > 2)
{
key_temp = 1;
}
cnt1 = 0;
}
if(B2 == 0) cnt2++;
if(B2 == 1)
{
if(cnt2 > 2)
{
key_temp = 2;
}
cnt2 = 0;
}
if(B3 == 0) cnt3++;
if(B3 == 1)
{
if(cnt3 > 2)
{
key_temp = 3;
}
cnt3 = 0;
}
if(B4 == 0) cnt4++;
if(B4 == 1)
{
if(cnt4 > 2)
{
key_temp = 4;
}
cnt4 = 0;
}
flag_10ms = 0;
}
return key_temp;
}
key.h文件
#ifndef __key_h
#define __key_h
#include "main.h"
#define B1 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0)
#define B2 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_1)
#define B3 HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_2)
#define B4 HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)
uint16_t Key_Scan(void);
#endif
interrupt.c文件
#include "interrupt.h"
uint16_t flag_10ms,flag_200ms;
uint16_t t;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim -> Instance == TIM2)
{
flag_10ms = 1;
if(++t > 20)
{
flag_200ms = 1 - flag_200ms;
t = 0;
}
}
}
dadc.c文件
#include "dadc.h"
double Get_ADC(ADC_HandleTypeDef *pin)
{
uint16_t adc;
HAL_ADC_Start(pin);
adc = HAL_ADC_GetValue(pin);
return adc * 3.3 / 4096;
}
main.c文件
注:LCD部分同资源包底层代码
头文件部分
#include "led.h"
#include "key.h"
#include "interrupt.h"
#include "dadc.h"
#include "lcd.h"
#include "stdio.h"
定义变量部分
Status:0~3分别对应Max_Volt,Min_Volt,Upper,Lower.
Mode:0—数据界面 1—参数界面 Setting_Mode同理
double Volt,Max_Volt = 2.4,Min_Volt = 1.2;
int16_t Upper = 1,Lower = 2;
uint16_t Status;
char text_volt[20],text_Max_Volt[20],text_Min_Volt[20],text_Upper[20],text_Lower[20];
uint16_t Mode;
uint16_t Setting_Mode;
extern uint16_t flag_200ms;
函数部分
按键逻辑函数
切屏的时候需要调用LCD切屏函数 LCD_Clear(Black); 这里是在切换的时候将屏幕切换为黑色。
Upper,Lower这种按键加减的变量在定义的时候必须定义为有符号的类型。
处理方法,以加法为例(减法同理):
if(Upper == Lower) Upper++;
if(Lower == Upper) Lower++;
void Key_Pro(void)
{
switch(Key_Scan())
{
case 1:
LCD_Clear(Black);
Mode = 1 - Mode;
break;
case 2:
if(++Setting_Mode > 3) Setting_Mode = 0;
break;
case 3:
switch(Setting_Mode)
{
case 0:
Max_Volt += 0.3;
if(Max_Volt >= 3.3) Max_Volt = 0;
break;
case 1:
Min_Volt += 0.3;
if(Min_Volt >= 3.3) Min_Volt = 0;
break;
case 2:
Upper++;
if(Upper == Lower) Upper++;
if(Upper > 8) Upper = 0;
break;
case 3:
Lower++;
if(Lower == Upper) Lower++;
if(Lower > 8) Lower = 0;
break;
}
break;
case 4:
switch(Setting_Mode)
{
case 0:
Max_Volt -= 0.3;
if(Max_Volt <= 0) Max_Volt = 3.3;
break;
case 1:
Min_Volt -= 0.3;
if(Min_Volt <= 0) Min_Volt = 3.3;
break;
case 2:
Upper--;
if(Upper == Lower) Upper--;
if(Upper < 1) Upper = 8;
break;
case 3:
Lower--;
if(Lower == Upper) Lower--;
if(Lower < 1) Lower = 8;
break;
}
break;
}
}
Lcd显示函数
第一个函数为数据显示界面,第二个函数为参数设置界面。
解决方法: 根据变量Status的状态(0—Max_Volt 1—Min_Volt 2—Upper 3—Lower)进行选择。如:此时选择的参数为Min_Volt,就在显示Min_Volt这一行的前面加上LCD_SetBackColor(Green); 这一行的后面加上 LCD_SetBackColor(Black); 然后有四个状态,用一个Switch即可。
void Display_Data(void)
{
LCD_DisplayStringLine(Line1, (u8*)" Main ");
sprintf(text_volt," Volt:%.2f ",Get_ADC(&hadc2 ));
LCD_DisplayStringLine(Line4, (u8*)text_volt);
switch(Status)
{
case 0:
LCD_DisplayStringLine(Line6, (u8*)" Status:Upper ");
break;
case 1:
LCD_DisplayStringLine(Line6, (u8*)" Status:Lower ");
break;
case 2:
LCD_DisplayStringLine(Line6, (u8*)" Status:Normal ");
break;
}
}
void Display_Parm(void)
{
LCD_DisplayStringLine(Line1, (u8*)" Setting ");
sprintf(text_Max_Volt," Max Volt:%.1fV",Max_Volt);
sprintf(text_Min_Volt," Min Volt:%.1fV",Min_Volt);
sprintf(text_Upper," Upper:LD%d",Upper);
sprintf(text_Lower," Lower:LD%d",Lower);
switch(Setting_Mode)
{
case 0:
LCD_SetBackColor(Green);//¶ÔMax_Volt¸ßÁÁÏÔʾ£¬ÏÂÃæͬÀí
LCD_DisplayStringLine(Line2, (u8*)text_Max_Volt);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line3, (u8*)text_Min_Volt);
LCD_DisplayStringLine(Line4, (u8*)text_Upper);
LCD_DisplayStringLine(Line5, (u8*)text_Lower);
break;
case 1:
LCD_DisplayStringLine(Line2, (u8*)text_Max_Volt);
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line3, (u8*)text_Min_Volt);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line4, (u8*)text_Upper);
LCD_DisplayStringLine(Line5, (u8*)text_Lower);
break;
case 2:
LCD_DisplayStringLine(Line2, (u8*)text_Max_Volt);
LCD_DisplayStringLine(Line3, (u8*)text_Min_Volt);
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line4, (u8*)text_Upper);
LCD_SetBackColor(Black);
LCD_DisplayStringLine(Line5, (u8*)text_Lower);
break;
case 3:
LCD_DisplayStringLine(Line2, (u8*)text_Max_Volt);
LCD_DisplayStringLine(Line3, (u8*)text_Min_Volt);
LCD_DisplayStringLine(Line4, (u8*)text_Upper);
LCD_SetBackColor(Green);
LCD_DisplayStringLine(Line5, (u8*)text_Lower);
LCD_SetBackColor(Black);
break;
}
}
Led逻辑函数
在定时器回调函数里定时200ms,当电压满足条件时,对应的Led进行闪烁,再遍历除这个闪烁灯外的其他Led,对这些Led熄灭
void Led_Pro(void)
{
Volt = Get_ADC(&hadc2 );
if(Volt > Max_Volt)
{
Status = 0;
if(flag_200ms)
{
Led(Upper - 1,1);
}
else
{
Led(Upper - 1,0);
}
for(uint16_t i = 0; i < 8;i++ )
{
if(i != Upper - 1)
Led(i,0);
}
}
else if(Volt < Min_Volt)
{
Status = 1;
if(flag_200ms)
{
Led(Lower - 1,1);
}
else
{
Led(Lower - 1,0);
}
for(uint16_t i = 0; i < 8;i++ )
{
if(i != Lower - 1)
Led(i,0);
}
}
else
{
Status = 2;
for(uint16_t i = 0; i < 8;i++ )
{
Led(i,0);
}
}
}
主函数部分
初始化
HAL_TIM_Base_Start_IT(&htim2);
LCD_Init();
LCD_Clear(Black);
LCD_SetTextColor(White );
LCD_SetBackColor(Black);
死循环
Key_Pro();
if(!Mode) Display_Data();
else Display_Parm();
Led_Pro();