#include<reg52.h>
#include <intrins.h> //库函数头文件,代码中引用了_nop_()函数
#define uchar unsigned char
#define uint unsigned int
sbit PWM= P3^2;//蓄电池开关
sbit FuZai=P3^3;//负载开关
sbit LED=P2^3;//充电指示灯
sbit LED1=P2^4;//充电指示灯
sbit LED2=P2^5;//欠压指示灯
sbit beep=P2^6;//蜂鸣器控制位
uchar count,battery_v;
//AD转换
sbit csad=P2^7; //定义AD的片选位
sbit adwr=P3^6; //定义AD的WR端口
sbit adrd=P3^7; //定义AD的RD端口
unsigned char V1,V2,V3,adval,ad_vo;
//1602液晶显示
sbit RS=P2^0; //P2.0
sbit RW=P2^1; //P2.1
sbit EN=P2^2; //P2.2
unsigned char code table1[]=" TYNCFDKZQXTSJ";
unsigned char code table2[]=" D:12.0 N:";
unsigned char code lcdd[]="0123456789";
uchar xs_vo,num;
//AT24c02保存数据
bit write=0; //写24C02的标志;
sbit scl=P2^4;
sbit sda=P2^5;
uchar p=2,a1;
//RS232串口通信
//sbit RXD=P3^0;
//sbit TXD=P3^1;
/*声明调用函数*/
void init();//初始化主函数
void delay(unsigned int t); //可控延时函数
void delay1(); //软件实现延时函数,5个机器周期
void buzzer();
unsigned char get_ad(); // AD程序
//1602液晶显示部分
void write_com(uchar com);
void write_data(uchar date);
void init1602();
void yj1602();
//AT24c02保存数据
void init24c02();
void start(); //启动
void stop(); //停止
void respons(); //应答
void write_byte(uchar date);
//uchar read_byte();
void write_add(uchar address,uchar date);
//uchar read_add(uchar address);
void AT24c02();
//void gar_ad();
void main()
{
init();
init1602();
init24c02();
battery_v=get_ad();
yj1602();
if(battery_v<108)PWM=0;
else PWM=1;
while(1)
{
delay(5000);
battery_v=gar_ad();
yj1602();
if(battery_v<0)
{
PWM=1;FuZai=1;buzzer();
}
if(battery_v>=108)
{
if(145<=battery_v)
{
LED1=0;//启动过充电压指示灯
LED=1;
LED2=1;
PWM=1;//停止充电
buzzer();//蜂鸣器报警
AT24c02();
}
else if(120<=battery_v<145) //蓄电池电压大于12V而且小于13.5V
{
LED=0;
LED1=1;LED2=1;
TR0=1; //启动固定PWM充电
if(count==4)
{
PWM=0;
}
if(count==12)
{
count=0;
PWM=1;
}
}
else if(battery_v<120)
{
LED=0;LED1=1;LED2=1;
TR0=0; //关闭固定PWM充电
PWM=0;
}
}
else
{
LED=1;//启动欠压指示灯
LED1=1;LED2=0;
FuZai=1;//关闭负载
PWM=0;
buzzer();
write=1;
AT24c02();
}
}
}
/*****定时中断函数*****/
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;//定时时间为50ms,每50ms中断一次
count++;
}
void init()//初始化主函数
{
TMOD=0x01;
TH0=(65536-50)/256;
TL0=(65536-50)%256;//定时时间为50ms,每50ms中断一次
EA=1;
ET0=1;
PWM=1;//初始化时先关闭充电
LED=0;//开启正常工作指示灯
LED1=1;//关闭异常指示灯
LED2=1;
beep=1;
write=0;
}
/*延时函数*/
void delay(unsigned int t)
{
unsigned int j,i;
for(i=0;i<t;i++)
for(j=0;j<110;j++);
}
/*延时函数1*/
void delay1()
{
_nop_(); _nop_();
_nop_(); _nop_();
_nop_();
}
//蜂鸣器报警函数
void buzzer()
{
beep=0; delay(10);
beep=1;
}
//ad转换程序
unsigned char get_ad() // AD程序
{
csad=0; //置CSAD为0,
adwr=1;
_nop_();
adwr=0; //启动AD转换
_nop_();
adwr=1;
delay(2); //AD转换时间
P1=0xff; //读取P1口之前先给其写全1
adrd=1; //选通ADCS
_nop_();
adrd=0; //AD读使能
_nop_();
adval=P1; //AD数据读取赋给P1口,得到ad转换的结果
adrd=1;
ad_vo=(float)adval*150.0/256.0;//得到蓄电池的电压
return(ad_vo);
}
/*LCD1602显示部分子函数*/
void init1602() //LCD初始化函数
{
EN=0;
write_com(0x38);//设置16X2显示,5X7点阵,8位数据接口
write_com(0x0c);//设置开显示,不显示光标
write_com(0x06);//写一个字符后地址指针加1
write_com(0x01);//显示清零,数据指针清零
}
void write_com(uchar com)//1602写命令函数
{
RW=0; delay1();
RS=0; // RW=0,RS=0,写LCD命令字
delay1();
EN=1; //E端时序
delay1();
P0=com; //将com中的命令字写入LCD数据口
delay1();
EN=0; delay1();
delay(5);
}
void write_data(uchar date)//1602写数据函数
{
RW=0; delay1();
RS=1; // RW=1,RS=0,写LCD数据
delay1();
EN=1; // E端时序
delay1();
P0=date; // 将dat中的显示数据写入LCD数据口
delay1();
EN=0; delay1();
delay(5);
}
void yj1602()
{ uchar xs_vo;
init1602();
xs_vo=battery_v;
V1=xs_vo/100;
V2=xs_vo%100/10;
V3=xs_vo%10;
write_com(0x80+0x10);
for(num=0;num<14;num++)
{
write_data(table1[num]);
delay(5);
}
write_com(0x80+0x50);
write_data(' ');
delay(5);
write_data('S');
delay(5);
write_data(':');
write_data(' ');
delay(5);
write_data(lcdd[1]);
delay(5);
write_data(lcdd[2]);
delay(5);
write_data('.');
delay(5);
write_data(lcdd[0]);
delay(5);
write_data(' ');
delay(5);
write_data(' ');
delay(5);
write_data('N');
delay(5);
write_data(':');
delay(5);
write_data(lcdd[V1]);
delay(5);
write_data(lcdd[V2]);
delay(5);
write_data('.');
delay(5);
write_data(lcdd[V3]);
delay(5);
for(num=0;num<16;num++)//移屏显示
{
write_com(0x1c);
delay(1000);
}
}
//AT24C02.h //数据存储模块
//at24c02部分子函数
void init24c02()//24c02初始化函数
{
sda=1; delay1();
scl=1; delay1();
}
void start() //开始信号
{
sda=1; delay1();
scl=1; delay1();
sda=0; delay1();
}
void stop() //停止
{
sda=0; delay1();
scl=1; delay1();
sda=1; delay1();
}
void respons() //应答
{
uchar i;
scl=1; delay1();
while((sda==1)&&(i<250)) i++;
scl=0; delay1();
}
void write_byte(uchar date)//串行发送字节函数
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0; delay1();
sda=CY; delay1();
scl=1; delay1();
}
scl=0; delay1();
sda=1; delay1();
}
uchar read_byte()//串行读字节函数
{
uchar i,k;
scl=0;
delay1();
sda=1;
delay1();
for(i=0;i<8;i++)
{
scl=1;
delay1();
k=(k<<1)|sda;
scl=0;
delay1();
}
return k;
}
void write_add(uchar address,uchar date)//24c02字节写入函数
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)//24c02字节读操作函数
{
uchar date;
start();
write_byte(0xa0); respons();
write_byte(address); respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void AT24c02()
{
init24c02();
a1=battery_v; //保存蓄电池过电压数据
if(write==1) //判断计时器是否计时一秒
{
write=0; //清零
write_add(p,a1); //在24c02的地址p中写入数据a1
p++;
}
}
Build target 'Target 1'
assembling STARTUP.A51...
compiling wgibgbv.c...
WGIBGBV.C(70): warning C206: 'gar_ad': missing function-prototype
linking...
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
SEGMENT: ?PR?_READ_ADD?WGIBGBV
*** WARNING L1: UNRESOLVED EXTERNAL SYMBOL
SYMBOL: GAR_AD
MODULE: wgibgbv.obj (WGIBGBV)
*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
SYMBOL: GAR_AD
MODULE: wgibgbv.obj (WGIBGBV)
ADDRESS: 02A7H
Program Size: data=21.1 xdata=0 code=1618
"denghwewo" - 0 Error(s), 4 Warning(s)
|