#include <AT89X51.H>
#include <intrins.h> //_nop_();
#include "lcd.h"
/*宏定义*/
#define uchar unsigned char
#define uint unsigned int
//#define N 5
sbit CLK=P2^4;
sbit OE=P2^5;
sbit EOC=P2^6;
sbit ST=P2^7;
sbit p20=P2^0;
sbit cong1=P2^2;
sbit feng1=P2^1;
bit flag;
bit flag1;
bit flag2;
/*全局变量*/
//uchar led_bit;
//uchar dat;
uchar dat1[2];
//uchar d[5];
uchar a[6];
//uchar count1;
uchar get;
uchar i;
uchar v[6];
/*全局变量*/
uchar counter;
uchar second;
uchar min,min1;
uchar hour,hour1;
uchar code table1[]={"0123456789"};
uchar code table2[]={"TIME:"};
uchar code table3[]={"VOLT:"};
uchar table4[7];
uchar code table5[]={"NO"};
/*标准LED共阴字形代码*/
//unsigned char code numb0[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0xbf,0xff}; //0xaf为‘-’的代码
/*标准LED共阳字形代码*/
//unsigned char code numb1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x00};
/*延时程序*/
delay1(uint m)
{
for(m;m>0;m--);
}
void time_init(void)
{
ST=0;
OE=0;
TMOD=0x21; //16位定时器
EA=1;
ET0=1;
ET1=1;
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
TH1=216;
TL1=216;
TR0=1;
TR1=1;
}
/*
void pais(uchar a[])
{uchar i,j,min0;
uchar tem0;
for(i=0;i<N;i++)*/
/*sort ten num*/
/* for(i=0;i<N-1;i++)
{
min0=i;
for(j=i+1;j<N;j++)
if(a[min0]>a[j])
min0=j;
tem0=a[i];
a[i]=a[min0];
a[min0]=tem0;
}
}
*/
/*定时器0中断程序*/
void timer0(void) interrupt 1 using 2
{TL0=(65535-50000)%256;
TH0=(65535-50000)/256;
counter++;
if(counter==20)
{counter=0;
second++;
}
}
void t1(void) interrupt 3 using 0
{
CLK=~CLK;
}
/*时间处理*/
void proccess(void)
{if(second==60)
{second=0;
min++;
}
if(min==60)
{min=0;
if(hour<23)
hour++;
else
hour=0;
}
}
void show(void)
{
a[0]=table1[hour];
a[1]=':';
a[2]=table1[(min/10)];
a[3]=table1[(min%10)];
a[4]=' ';
a[5]=0x00;
}
/*
void fun(void)
{
d[count1]=dat;
if(count1>4)
{
count1=0;
pais(d);
get=dat;
}
get=dat;
}
*/
void just(void)
{uchar real;
if(EOC==1)
{i=0;
OE=1;
_nop_();
_nop_();
_nop_();
_nop_();
get=P0;
_nop_();
_nop_();
_nop_();
_nop_();
OE=0;
// get=170; ///
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ST=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
ST=0;
}
i++;
real=get;
real=real*0.97;
v[0]=real/100+0x30;
v[1]='.';
v[2]=(real%100)/10+0x30;
v[3]=real%10+0x30;
v[4]='V';
v[5]=0x00;
if(get<100)
{table4[0]=0x20;
table4[1]=0x20;
table4[2]=0x20;
table4[3]=0x20;
table4[4]=0x20;
table4[5]=0x20;
table4[6]=0x00;
}
else if (get<110)
{table4[0]=0xff;
table4[1]=0x20;
table4[2]=0x20;
table4[3]=0x20;
table4[4]=0x20;
table4[5]=0x20;
table4[6]=0x00; }
else if(get<125)
{table4[0]=0xff;
table4[1]=0xFF;
table4[2]=0x20;
table4[3]=0x20;
table4[4]=0x20;
table4[5]=0x20;
table4[6]=0x00;}
else if (get<140)
{table4[0]=0xff;
table4[1]=0xFF;
table4[2]=0xFF;
table4[3]=0x20;
table4[4]=0x20;
table4[5]=0x20;
table4[6]=0x00; }
else if(get<150)
{table4[0]=0xff;
table4[1]=0xFF;
table4[2]=0xFF;
table4[3]=0xFF;
table4[4]=0x20;
table4[5]=0x20;
table4[6]=0x00;
}
else
{table4[0]=0xff;
table4[1]=0xFF;
table4[2]=0xFF;
table4[3]=0xFF;
table4[4]=0xff;
table4[5]=0x20;
table4[6]=0x00;
}
//get=170;
if((get>145)&&flag2)
lcd_dis(table5,0xcc);
if((get<=145)&&(second>3))
flag2=0;
}
void cong(void)
{
cong1=0;
feng1=0;
}
void feng(void)
{
cong1=1;
feng1=1;//feng
}
void stop(void)
{
cong1=1;
feng1=0;
}
main()
{//uchar j;
time_init();
lcdinit();
lcd_dis(table2,0x80);
lcd_dis(table3,0xc0);
flag=1;
flag1=1;
flag2=1;
ST=1;
ST=0;
cong1=1;
feng1=0;
delay1(60000);
// dat=0xff;
while(1)
{
just();
if(p20==1) //为镍氢电池
{
// flag=1;
if(get<179) //<1.75
{
cong();//cong
proccess();
show();
}
else
stop();//stop
}
/****************/
if(p20==0)
{
if((get>82)&flag)
feng();//feng
else
{
flag=0;
if(get<154) //<1.5
{
cong();//cong
proccess();
show();
}
else
stop();//stop
}
}
lcd_dis(a,0x85);
lcd_dis(v,0xc5);
lcd_dis(table4,0x8a);
delay1(255);
delay1(255);
delay1(255);
delay1(255);
delay1(255);
delay1(255);
delay1(255);
delay1(255);
}
}