ucharseg[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0X82,0XF8,
0X80,0X90,0XFF};//数码管显示值ucharn;//led的移位状态uinttim;//定时器计时的值uintzhong;//重量的值ucharflag_s=0;//继电器开关标志ucharfangxiang=0;//灯的方向ucharyi,er,san,si;//数码管显示位voidallinit();
voidDelayms(uintms);
voiddisplay(ucharyi,er,san,si);
voidkey();
voidled_zheng();
voidled_fan();
voidcontrol();
voidTimer0Init(void);
voidTimer1Init(void);
voidmain()
{
allinit();
Timer0Init();
Timer1Init();
eeprom_write(0x00,0);
while(1)
{
display(yi,er,san,si);
}
}
voidallinit()
{
P2=0xa0;P0=0x00;//关闭蜂鸣器继电器P2=0x80;P0=0xff;//关闭ledP2=0xe0;P0=0xff;//段选P2=0xc0;P0=0xff;//位选}
voiddisplay(ucharyi,er,san,si)
{
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[yi];//段选P2=0xc0;P0=0x01;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[er];//段选P2=0xc0;P0=0x02;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[san];//段选P2=0xc0;P0=0x04;//位选Delayms(1);
P2=0xe0;P0=0xff;
P2=0xe0;P0=seg[si];//段选P2=0xc0;P0=0x08;//位选Delayms(1);
/*P2=0xe0;P0=seg[wu];//段选P2=0xc0;P0=0x10;//位选Delayms(2);
P2=0xe0;P0=seg[liu];//段选P2=0xc0;P0=0x20;//位选Delayms(2);
P2=0xe0;P0=seg[qi];//段选P2=0xc0;P0=0x40;//位选Delayms(2);
P2=0xe0;P0=seg[ba];//段选P2=0xc0;P0=0x80;//位选Delayms(2);
*/}
voidkey()
{
if(P30==0)
{
Delayms(5);
if(P30==0)
{
flag_s=0;
}
while(!P30)
{
display(yi,er,san,si);
};
}
if(P31==0)
{
Delayms(5);
if(P31==0)
{
fangxiang=2;
}
while(!P31)
{
display(yi,er,san,si);
};
}
if(P32==0)
{
Delayms(5);
if(P32==0)
{
fangxiang=1;
}
while(!P32)
{
display(yi,er,san,si);
};
}
if(P33==0)
{
Delayms(5);
if(P33==0)
{
flag_s=1;
}
while(!P33)
{
display(yi,er,san,si);
};
}
}
voidled_zheng()
{
P2=0x80;
P0=~(1<<n);
n++;
if(n==8)n=0;
}
voidled_fan()
{
P2=0x80;
P0=~(0x80>>(8-n));
n--;
if(n==0)n=8;
}
voidcontrol()
{
P2=0xa0;P0=0x00;//关闭蜂鸣器继电器P2=0x80;P0=0xff;//关闭ledP2=0xe0;P0=0xff;//段选P2=0xc0;P0=0xff;//位选if(zhong>750)
{
P2=0xa0;P0=0x40;//关闭蜂鸣器继电器P2=0x80;P0=0xff;//关闭led }
elseif(flag_s==1)
{
P2=0xa0;P0=0x10;
if(fangxiang==1)
led_zheng();
elseif(fangxiang==2)
led_fan();
else {
P2=0xa0;P0=0x10;
P2=0x80;P0=0xff;
}
}
else {
fangxiang=0;
n=0;
P2=0xa0;P0=0x00;//关闭蜂鸣器继电器P2=0x80;P0=0xff;//关闭led }
}
voidDelayms(uintms) //@11.0592MHz{
unsignedchari, j,k;
for(k=ms;k>0;k--)
{
_nop_();
_nop_();
_nop_();
i=11;
j=190;
do {
while (--j);
} while (--i);
}
}
voidTimer0Init(void) //1毫秒@11.0592MHz{
AUXR|=0x80; //定时器时钟1T模式TMOD&=0xF0; //设置定时器模式TL0=0xCD; //设置定时初值TH0=0xD4; //设置定时初值TF0=0; //清除TF0标志TR0=1; //定时器0开始计时EA=1;
ET0=1;
}
voidTimer1Init(void) //1微秒@11.0592MHz{
AUXR|=0x40; //定时器时钟1T模式TMOD&=0x0F; //设置定时器模式TL1=0x9A; //设置定时初值TH1=0xA9; //设置定时初值TF1=0; //清除TF1标志TR1=1; //定时器1开始计时EA=1;
ET1=1;
}
voidtimer1(void) interrupt3{
ucharadd=0x00;
uchartemp;
if(zhong>750)
{
temp=eeprom_read(add);
if(zhong!=temp)
{
eeprom_write(add,zhong);
add++;
}
}
}
voidtimer0(void) interrupt1{
tim++;
key();
zhong=(uint)(ad_read(0x03)/255.0*1000);
yi=zhong/1000;
er=zhong/100%10;
san=zhong/10%10;
si=zhong%10;
if(tim==200)
{
tim=0;
control();
}
}