#include<reg51.h>
#define unit unsigned int
#define uchar unsigned char
uchar Second,Minute,Hour,Date,Month,Year,week
#define one 0x80
#define two 0xc0
sbit RS_1602=P2^0; //error C141:syntax error near 'void' error C247: non_addires/-constant initializer
sbit E_1602=P2^1;
sbit IO_1302=P2^4;
sbit CLK_1302=P2^3;
sbit RST_1302=P2^2;
sbit key_mode=P1^0;
sbit key_up=P1^1;
sbit key_down=P1^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
uchar code tab1[]={"20 - - week"};
uchar code tab2[]={" : : "};
void init();
uchar BCD_Decimal(uchar bcd);
uchar Dec_BCD(uchar Dec);
void delay(uint xms); //error C141:syntax error near 'xms',expected')'
write_1602com(uchar com);
write_1602dat(uchar dat);
void write_hms(uchar add,uchar dat);
void write_ymd(uchar add,uchar dat);
void write_week(uchar week);
lcd_init();
void write_byte(uchar dat);
uchar read_byte();
void write_1302(uchar add,uchar dat);
uchar read_1302(uchar add);
void ds1302_init();
void keyscan();
void main()
{
lcd_init();
ds1302_init();
init();
while(1)
keyscan();
}
write_1602com(uchar com)
{
RS_1602=0;
P2=com;
delay(1);
E_1602=1;
delay(1);
E_1602=0;
}
write_1602dat(uchar dat)
{
RS_1602=1;
P2=dat;
delay(1);
E_1602=1;
delay(1);
E_1602=0;
}
void write_hms(uchar add,uchar dat)
{
uchar gw,sw;
gw=dat%10;
sw=dat/10;
write_1602com(two+add);
write_1602dat(0x30+sw);
write_1602dat(0x30+gw);
}
void write_ymd(uchar add,uchar dat)
{
uchar gw,sw;
gw=dat%10;
sw=dat/10;
write_1602com(one+add);
write_1602dat(0x30+sw);
write_1602dat(0x30+gw);
}
void write_week(uchar week)
{
write_1602com(one+0x0f);
write_1602dat(0x30+week);
}
lcd_init()
{
uchar kk;
write_1062com(0x38);
write_1062com(0x0c);
write_1062com(0x06);
write_1062com(0x01);
write_1062com(one);
for(kk=0;kk<6;kk++)
write_1062dat(tab1[kk]);
write_1062com(two+4);
for(kk=0;kk<8;kk++)
write_1062dat(tab2[kk]);
}
void write_byte(uchar dat)
{
uchar kk;
ACC=dat;
RST_1302=1;
for(kk=8;kk>0;kk--)
{
IO_1302=ACC0;
CLK_1302=0;
CLK_1302=1;
ACC=ACC>>1;
}
}
uchar read_byte()
{
uchar kk;
RST_1302=1;
for(kk=8;kk>0;kk--)
{
ACC7=IO_1302;
CLK_1302=1;
CLK_1302=0;
ACC=ACC>>1;
}
return(ACC);
}
void write_1302(uchar add,uchar dat)
{
RST_1302=0;
CLK_1302=0;
RST_1302=1;
write_byte(add);
write_byte(dat);
CLK_1302=1;
RST_1302=O;
}
uchar read_1302(uchar add)
{
uchar temp;
RST_1302=0;
CLK_1302=0;
RST_1302=1;
write_byte(add);
temp=read_byte();
CLK_1302=1;
RST_1302=0;
return (temp);
}
void ds1302_init()
{
RST_1302=0;
CLK_1302=0;
write_1302(0x80,Second|0x00);
}
void keyscan()
{
uchar key1n;
if(key_mode==0)
{
delay(5);
if(key_mode==0)
{
while(!key_mode);
key1n++;
switch(key1n)
{
case 1:
TR0=0;
write_1602com(two+0x0b);
write_1602com(0x0f);
write_1302(0x8e,0x00);
write_1302(0x80,0x80|Dec_BCD(Second));
break;
case 2:write_1602com(two+8);break;
case 3: write_1602com(two+5);break;
case 4:write_1602com(one+0x0f);break;
case 5:write_1602com(one+0x09);break;
case 6:write_1602com(one+0x06);break;
case 7:write_1602com(one+0x03);break;
case 8:
write_1602com(0x0c);
write_1302(0x80,0x00|Dec_BCD(Second));
write_1302(0x8e,0x80);
TR0=1;
break;
}
}
}
if(key1n!=0)
{
if(key_up==0)
{
delay(5);
if(key_up==0)
{
while(!key_up);
switch(key1n)
{
case 1:Second++;
if(Second==60)
Second=0;
write_hms(0x0a,Second);
write_1302(0x80,0x80|Dec_BCD(Second));
write_1602com(two+0x0b);
break;
case 2: Minute++;
if(Minute==60)
Minute=0;
write_hms(7,Minute);
write_1302(0x82,Dec_BCD(Minute));
write_1602com(two+8);
break;
case 3:Hour++;
if(Hour==24)
Hour=0;
write_hms(4,Hour);
write_1302(0x84,Dec_BCD(Hour));
write_1602com(two+5);
break;
case 4:week++;
if(week==8)
week=1;
write_week(week);
write_1302(0x8a,week);
write_1602com(one+0x0f);
break;
case 5:Date++;
if(Date==32)
Date=1;
write_ymd(8,Date);
write_1302(0x86,Dec_BCD(Date));
write_1602com(one+9);
break;
case 6: Month++;
if(Month==13)
Month=1;
write_ymd(5,Month);
write_1302(0x88,Dec_BCD(Month));
write_1602com(one+6);
break;
case 7:Year++;
if(Year==100)
Year=0;
write_ymd(2,Year);
write_1302(0x8c,Dec_BCD(Year));
write_1602com(one+3);
break;
}
}
}
if(key_down==0)
{
delay(5);
if(key_down==0)
{
while(!key_down);
switch(key1n)
{
case 1:Second--;
if(Second==-1)
Second=59;
write_hms(0x0a,Second);
write_1302(0x80,0x80|Dec_BCD(Second));
write_1602com(two+0x0b);
break;
case 2: Minute--;
if(Minute==-1)
Minute=59;
write_hms(7,Minute);
write_1302(0x82,Dec_BCD(Minute));
write_1602com(two+8);
break;
case 3:Hour--;
if(Hour==-1)
Hour=23;
write_hms(4,Hour);
write_1302(0x84,Dec_BCD(Hour));
write_1602com(two+5);
break;
case 4:week--;
if(week==0)
week=7;
write_week(week);
write_1302(0x8a,week);
write_1602com(one+0x0f);
break;
case 5:Date--;
if(Date==255)
Date=31;
write_ymd(8,Date);
write_1302(0x86,Dec_BCD(Date));
write_1602com(one+9);
break;
case 6:Month--;
if(Month==255)
Month=12;
write_ymd(5,Month);
write_1302(0x88,Dec_BCD(Month));
write_1602com(one+6);
break;
case 7:Year--;
if(Year==-1)
Year=99;
write_ymd(2,Year);
write_1302(0x8c,Dec_BCD(Year));
write_1602com(one+3);
break;
}
}
}
}
}
void init()
{
TMOD=0x01;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{
Second=BCD_Decimal(read_1302(0x81));
Minute=BCD_Decimal(read_1302(0x83));
Hour=BCD_Decimal(read_1302(0x85));
Date=BCD_Decimal(read_1302(0x87));
Month=BCD_Decimal(read_1302(0x89));
week=BCD_Decimal(read_1302(0x8b));
Year=BCD_Decimal(read_1302(0x8d));
write_hms(10,Second);
write_hms(7,Minute);
write_hms(4,Hour);
write_ymd(8,Date);
write_ymd(5,Month);
write_ymd(2,Year);
write_week(week);
}
uchar BCD_Decimal(uchar bcd)
{
uchar Decimal;
Decimal=bcd>>4;
return (Decimal=Decimal*10+(bcd&=0x0F));
}
uchar Dec_BCD(uchar Dec)
{
uchar BCD;
BCD=(Dec)/10*16+(Dec)%10;
return(BCD);
}
void delay(unit xms)
{
unit i,j;
for(i=xms;i>0;i--)
for(j=100;j>0;j--);
}
此帖出自
小平头技术问答
#define uint unsigned int
#define uchar unsigned char
uchar Second,Minute,Hour,Date,Month,Year,week;
#define one 0x80
#define two 0xc0
sbit RS_1602=P2^0; //error C141:syntax error near 'void' error C247: non_addires/-constant initializer
sbit E_1602=P2^1;
sbit IO_1302=P2^4;
sbit CLK_1302=P2^3;
sbit RST_1302=P2^2;
sbit key_mode=P1^0;
sbit key_up=P1^1;
sbit key_down=P1^2;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
uchar code tab1[]={"20 - - week"};
uchar code tab2[]={" : : "};
void init();
uchar BCD_Decimal(uchar bcd);
uchar Dec_BCD(uchar Dec);
void delay(uint xms); //error C141:syntax error near 'xms',expected')'
write_1602com(uchar com);
write_1602dat(uchar dat);
void write_hms(uchar add,uchar dat);
void write_ymd(uchar add,uchar dat);
void write_week(uchar week);
lcd_init();
void write_byte(uchar dat);
uchar read_byte();
void write_1302(uchar add,uchar dat);
uchar read_1302(uchar add);
void ds1302_init();
void keyscan();
void main()
{
lcd_init();
ds1302_init();
init();
while(1)
keyscan();
}
write_1602com(uchar com)
{
RS_1602=0;
P2=com;
delay(1);
E_1602=1;
delay(1);
E_1602=0;
}
write_1602dat(uchar dat)
{
RS_1602=1;
P2=dat;
delay(1);
E_1602=1;
delay(1);
E_1602=0;
}
void write_hms(uchar add,uchar dat)
{
uchar gw,sw;
gw=dat%10;
sw=dat/10;
write_1602com(two+add);
write_1602dat(0x30+sw);
write_1602dat(0x30+gw);
}
void write_ymd(uchar add,uchar dat)
{
uchar gw,sw;
gw=dat%10;
sw=dat/10;
write_1602com(one+add);
write_1602dat(0x30+sw);
write_1602dat(0x30+gw);
}
void write_week(uchar week)
{
write_1602com(one+0x0f);
write_1602dat(0x30+week);
}
lcd_init()
{
uchar kk;
write_1602com(0x38);
write_1602com(0x0c);
write_1602com(0x06);
write_1602com(0x01);
write_1602com(one);
for(kk=0;kk<6;kk++)
write_1602dat(tab1[kk]);
write_1602com(two+4);
for(kk=0;kk<8;kk++)
write_1602dat(tab2[kk]);
}
void write_byte(uchar dat)
{
uchar kk;
ACC=dat;
RST_1302=1;
for(kk=8;kk>0;kk--)
{
IO_1302=ACC0;
CLK_1302=0;
CLK_1302=1;
ACC=ACC>>1;
}
}
uchar read_byte()
{
uchar kk;
RST_1302=1;
for(kk=8;kk>0;kk--)
{
ACC7=IO_1302;
CLK_1302=1;
CLK_1302=0;
ACC=ACC>>1;
}
return(ACC);
}
void write_1302(uchar add,uchar dat)
{
RST_1302=0;
CLK_1302=0;
RST_1302=1;
write_byte(add);
write_byte(dat);
CLK_1302=1;
RST_1302=0;
}
uchar read_1302(uchar add)
{
uchar temp;
RST_1302=0;
CLK_1302=0;
RST_1302=1;
write_byte(add);
temp=read_byte();
CLK_1302=1;
RST_1302=0;
return (temp);
}
void ds1302_init()
{
RST_1302=0;
CLK_1302=0;
write_1302(0x80,Second|0x00);
}
void keyscan()
{
uchar key1n;
if(key_mode==0)
{
delay(5);
if(key_mode==0)
{
while(!key_mode);
key1n++;
switch(key1n)
{
case 1:
TR0=0;
write_1602com(two+0x0b);
write_1602com(0x0f);
write_1302(0x8e,0x00);
write_1302(0x80,0x80|Dec_BCD(Second));
break;
case 2:write_1602com(two+8);break;
case 3: write_1602com(two+5);break;
case 4:write_1602com(one+0x0f);break;
case 5:write_1602com(one+0x09);break;
case 6:write_1602com(one+0x06);break;
case 7:write_1602com(one+0x03);break;
case 8:
write_1602com(0x0c);
write_1302(0x80,0x00|Dec_BCD(Second));
write_1302(0x8e,0x80);
TR0=1;
break;
}
}
}
if(key1n!=0)
{
if(key_up==0)
{
delay(5);
if(key_up==0)
{
while(!key_up);
switch(key1n)
{
case 1:Second++;
if(Second==60)
Second=0;
write_hms(0x0a,Second);
write_1302(0x80,0x80|Dec_BCD(Second));
write_1602com(two+0x0b);
break;
case 2: Minute++;
if(Minute==60)
Minute=0;
write_hms(7,Minute);
write_1302(0x82,Dec_BCD(Minute));
write_1602com(two+8);
break;
case 3:Hour++;
if(Hour==24)
Hour=0;
write_hms(4,Hour);
write_1302(0x84,Dec_BCD(Hour));
write_1602com(two+5);
break;
case 4:week++;
if(week==8)
week=1;
write_week(week);
write_1302(0x8a,week);
write_1602com(one+0x0f);
break;
case 5:Date++;
if(Date==32)
Date=1;
write_ymd(8,Date);
write_1302(0x86,Dec_BCD(Date));
write_1602com(one+9);
break;
case 6: Month++;
if(Month==13)
Month=1;
write_ymd(5,Month);
write_1302(0x88,Dec_BCD(Month));
write_1602com(one+6);
break;
case 7:Year++;
if(Year==100)
Year=0;
write_ymd(2,Year);
write_1302(0x8c,Dec_BCD(Year));
write_1602com(one+3);
break;
}
}
}
if(key_down==0)
{
delay(5);
if(key_down==0)
{
while(!key_down);
switch(key1n)
{
case 1:Second--;
if(Second==-1)
Second=59;
write_hms(0x0a,Second);
write_1302(0x80,0x80|Dec_BCD(Second));
write_1602com(two+0x0b);
break;
case 2: Minute--;
if(Minute==-1)
Minute=59;
write_hms(7,Minute);
write_1302(0x82,Dec_BCD(Minute));
write_1602com(two+8);
break;
case 3:Hour--;
if(Hour==-1)
Hour=23;
write_hms(4,Hour);
write_1302(0x84,Dec_BCD(Hour));
write_1602com(two+5);
break;
case 4:week--;
if(week==0)
week=7;
write_week(week);
write_1302(0x8a,week);
write_1602com(one+0x0f);
break;
case 5:Date--;
if(Date==255)
Date=31;
write_ymd(8,Date);
write_1302(0x86,Dec_BCD(Date));
write_1602com(one+9);
break;
case 6:Month--;
if(Month==255)
Month=12;
write_ymd(5,Month);
write_1302(0x88,Dec_BCD(Month));
write_1602com(one+6);
break;
case 7:Year--;
if(Year==-1)
Year=99;
write_ymd(2,Year);
write_1302(0x8c,Dec_BCD(Year));
write_1602com(one+3);
break;
}
}
}
}
}
void init()
{
TMOD=0x01;
TH0=0;
TL0=0;
EA=1;
ET0=1;
TR0=1;
}
void timer0() interrupt 1
{
Second=BCD_Decimal(read_1302(0x81));
Minute=BCD_Decimal(read_1302(0x83));
Hour=BCD_Decimal(read_1302(0x85));
Date=BCD_Decimal(read_1302(0x87));
Month=BCD_Decimal(read_1302(0x89));
week=BCD_Decimal(read_1302(0x8b));
Year=BCD_Decimal(read_1302(0x8d));
write_hms(10,Second);
write_hms(7,Minute);
write_hms(4,Hour);
write_ymd(8,Date);
write_ymd(5,Month);
write_ymd(2,Year);
write_week(week);
}
uchar BCD_Decimal(uchar bcd)
{
uchar Decimal;
Decimal=bcd>>4;
return (Decimal=Decimal*10+(bcd&=0x0F));
}
uchar Dec_BCD(uchar Dec)
{
uchar BCD;
BCD=(Dec)/10*16+(Dec)%10;
return(BCD);
}
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=100;j>0;j--);
}
一周热门 更多>