功能介绍:
0.本系统采用STC89C52作为单片机
1.LCD1602液晶实时显示当前检测的充电电压
2.当电压超过设定阈值时,继电器断开,停止充电,同时蜂鸣器报警,提醒充电完成
3.按键可切换界面,并设置充电饱和阈值
4.采用DC002作为电源接口可直接输入5V给整个系统供电
原理图:
PCB:
主程序:
#include "main.h"bit refreshFlag = 1;bit chargeSwitch = 1; //充电开关enum _MODE_DF_ dispMode;float f_volt = 0.0;float f_voltMax = 4.2;unsigned char buzzerDelay = 0; //蜂鸣器延时unsigned char setIndex = 0;unsigned char i = 0;unsigned char cnt = 0;char dis0[16]; //定义显示区域临时存储数组void main(void){Timer0_Init();LCD_Init(); //初始化液晶LCD_DispStr(0, 0, " Welcome! "); //充电DelayS(1); //延时有助于稳定LCD_Clear(); //清屏while (1) //主循环{if (refreshFlag == 1) //100ms获取数据并发送{refreshFlag = 0;if (cnt < 5) //累加{f_volt = f_volt + (float)ReadADC(AIN0_GND) * 5 / 255;cnt++;}else //求平均{cnt = 0;f_volt = f_volt / 5;f_volt = f_volt * 2 ;//液晶显示if (dispMode == NORMAL){DispNormal(&f_volt);}if (chargeSwitch == 1){RELAY = CLOSE;if (f_volt > f_voltMax) //电池充满{chargeSwitch = 0; //切断充电buzzerDelay = 10; //充满电响铃一段时间}}else{RELAY = OPEN;}if (buzzerDelay > 0) //响铃一段时间{buzzerDelay--;BUZZER = ~BUZZER;}else{BUZZER = OFF;}f_volt = 0;}}KeyProcess();}}void DispNormal(float *volt){sprintf(dis0, "Voltage: %3.1fV ", *volt); //打印电压值LCD_DispStr(0, 0, dis0);if (chargeSwitch == 1){LCD_DispStr(0, 1, "Status : Charge "); //充电}else{LCD_DispStr(0, 1, "Status : Idle "); //空闲}}void DispSetVoltage(unsigned char setIndex){LCD_DispStr(0, 0, " Set Voltage ");sprintf(dis0, " Max: %3.1fV ", f_voltMax); //打印电压阈值LCD_DispStr(0, 1, dis0);switch (setIndex){case 1: LCD_SetCursor(11, 1, 1); break;default:;} }/*------------------------------------------------定时器初始化子程序------------------------------------------------*/void Timer0_Init(void){TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响TL0 = 0x00;//设置定时初值TH0 = 0x4C;//设置定时初值 50msEA = 1; //总中断打开ET0 = 1; //定时器中断打开TR0 = 1; //定时器开关打开}/*------------------------------------------------定时器中断子程序------------------------------------------------*/void Timer0_Interrupt(void) interrupt 1{static unsigned char time50ms = 0;TL0 = 0x00;//设置定时初值TH0 = 0x4C;//设置定时初值 50mstime50ms++;if (time50ms >= 2) // 0.1s{time50ms = 0;refreshFlag = 1;}}
仿真演示视频:
/video/BV1nS4y1z79g/
实物演示视频:
/video/BV12G411E7uY/