代码我明天发,现在电脑没电了
STC89C52 实现LCD1602万年历(就是王云51单片机C语言程序设计教程中的代码,我换了一个头文件,以及引脚定义,但我不知道为什么全是无效基地址,求解,急用)
#ifndef__I2C_H__
#define__I2C_H__
#include <STC89C5xRC.H>
#define uchar unsigned char
#define uint unsigned int
sbit sda=P20;
sbit scl=P21;
void c02_delay();
void start();
void stop();
void respons();
void c_init();
void c02_write_byte(uchar date);
uchar c02_read_byte();
void c02_write_add(uchar address,uchar date);
uchar c02_read_add(uchar address);
#endif
#include "i2c.h"
void c02_delay()
{ ;; }
void start()
{
sda=1;
c02_delay();
scl=1;
c02_delay();
sda=0;
c02_delay();
}
void stop()
{
sda=0;
c02_delay();
scl=1;
c02_delay();
sda=1;
c02_delay();
}
void respons()
{
uchar i;
scl=1;
c02_delay();
while((sda==1)&&(i<250))i++;
scl=0;
c02_delay();
}
void c_init()
{
sda=1£»
c02_delay();
scl=1;
c02_delay();
}
void c02_write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
c02_delay();
sda=CY;
c02_delay();
scl=1;
c02_delay();
}
scl=0;
c02_delay();
sda=1;
c02_delay();
}
uchar c02_read_byte()
{
uchar i,k;
scl=0;
c02_delay();
sda=1;
c02_delay();
for(i=0;i<8;i++)
{
scl=1;
c02_delay();
k=(k<<1)|sda;
scl=0;
c02_delay();
}
return k;
}
void c02_write_add(uchar address,uchar date)
{
start();
c02_while_byte(0xa0);
respons();
c02_write_byte(address);
respons();
c02_write_byte(date);
respons();
stop();
}
uchar c02_read_add(uchar address)
{
uchar date;
start();
c02_write_byte(0xa0);
respons();
c02_write_byte(address);
respons();
start();
c02_write_byte(0xa1);
respons();
date=c02_read_byte();
stop();
return date;
}
#include <STC89C5xRC.H>
#include "intrins.h"
#include "i2c.h"
#define uchar unsigned char
#define uint unsigned int
bit ds18b20_flag;
uchar code table1[]=" : : ";
uchar code table2[]="Alarm set:";
uchar miao,fen,shi,wendu_shi,wendu_ge,wendu_shu,variate,alarm_shi,alarm_fen,alarm_miao;
uchar flag=1,count=0,flag_up=0,flag_down=0,flag_alarm=0,alarm_on,alarm_count=0,done=0;
uint temp,wendu;
sbit ds1302_sclk=P36;
sbit ds1302_io=P34;
sbit ds1302_ce=P35;
sbit ds=P37;
sbit fm=P15;
sbit lcd1602_rs=P26£»
sbit lcd1602_rw=P25;
sbit lcd1602_en=P27;
sbit set=P31;
sbit up=P30;
sbit down=P32;
sbit nao=P33;
void delay(uchar x)
{
uchar y,z;
for(z=x;z>0;z--)
for(y=110;y>0;y--);
}
void delayus(uint t)
{
while(t--);
}
void ds18b20_reset()
{
ds=1;
delayus(5);
ds=0;
delayus(80);
ds=1;
delayus(14);
if(ds==0)
ds18b20_flag=1;
else
ds18b20_flag=0;
delayus(20);
}
bit ds_read_bit()
{
bit dat;
ds=0;
_nop_();
_nop_();
ds=1;
_nop_();
dat=ds;
delayus(10);
return dat;
}
void ds_write_ds1302_byte(uchar,dat)
{
uchar i;
for(i=0;i<8;i++)
{
ds=0;
_nop_();
ds=dat&0x01;
delayus(6);
ds=1£»
dat=dat>>1;
}
delayus(6);
}
uchar ds_read_byte()
{
uchar i,j,k;
for(i=0;i<8;i++)
{
j=ds_read_bit();
k=(j<<7)|(k>>1);
}
return k;
}
uint read_temperature()
{
uchar a,b;
ds18b20_reset();
ds_write_ds1302_byte(0xcc);
ds_write_ds1302_byte(0x44);
delayus(150);
ds18b20_reset();
ds_write_ds1302_byte(0xcc);
ds_write_ds1302_byte(0xbe);
delayus(150);
a=ds_read_byte();
b=ds_read_byte();
temp=b;
temp=temp<<8;
temp=temp|a;
temp=temp*0.0625*10+0.5;
return temp;
}
void write_ds1302_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
ds1302_sclk=0;
ds1302_io=dat&0x01;
dat=dat>>1;
ds1302_sclk=1;
}
}
void ds1302_write(uchar add,uchar date)
{
ds1302_ce=0;
ds1302_sclk=0;
ds1302_ce=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(date);
ds1302_ce=0;
_nop_();
ds1302_io=1;
ds1302_sclk=1;
}
uchar ds1302_read(uchar add)
{
uchar i;value;
ds1302_ce=0;
ds1302_sclk=0;
ds1302_ce=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
value=value>>1;
ds1302_sclk=0;
if(ds1302_io)
{
value=value|0x80;
}
ds1302_sclk=1;
}
ds1302_ce=0;
_nop_();
ds1302_sclk=1;
_nop_();
ds1302_sclk=1;
ds1302_io=1;
return value;
}
void set_rtc()
{
ds1302_ce=0;
ds1302_sclk=1;
ds1302_write(0x8e,0x00);
ds1302_write(0x80,0x58);
ds1302_write(0x82,0x56);
ds1302_write(0x84,0x23);
ds1302_write(0x90,0xa5);
ds1302_write(0x8e,0x80);
}
void lcd_write_com(uchar com)
{
lcd1602_rs=0;
lcd1602_rw=0;
lcd1602_en=0;
P0=com;
lcd1602_en=1;
delay(5);
lcd1602_en=0;
}
void lcd_write_date(uchar date)
{
lcd1602_rs=1;
lcd1602_rw=0;
lcd1602_en=0;
P0=date;
lcd1602_en=1;
delay(5);
lcd1602_en=0;
}
void lcd1602_init()
{
uchar lcdnum;
lcd_write_com(0x38);
lcd_write_com(0x0c);
lcd_write_com(0x06);
lcd_write_com(0x01);
lcd_write_com(0x80+0x40);
for(lcdnum=0;lcdnum<13;lcdnum++)
{
lcd_write_date(table1[lcdnum]);
delay(2);
}
}
void write_time1(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
lcd_write_com(0x80+0x40+add);
lcd_write_date(0x30+shi);
lcd_write_date(0x30+ge);
}
ucahr bcd_decimal(uchar bcd)
{
uchar decimal;
decimal=bcd>>4;
decimal=decimal*10+(bcd&=0x0f);
return decimal;
}
void wendu_decimal(uint dat)
{
wendu_shi=dat/100;
wendu_ge=dat%100/10;
wendu_shu=dat%100/10;
lcd_write_com(0x80+0x40+10);
lcd_write_date(0x30+wendu_shi);
lcd_write_date(0x30+wendu_ge);
lcd_write_date(0x2e);
lcd_write_date(0x30+wendu_shu);
lcd_write_date(0xdf);
lcd_write_date(0x43);
}
uchar key_bcd(uchar key_decimal)
{
uchar temp;
temp=(((key_decimal/10)&0x0f)<<4)|(key_decimal%10);
return temp;
}
void key_up_down()
{
if(up==0)
{
delay(50);
flag_up=1;
while(!up);
switch(count)
{
case 1:
miao++;
if(miao>59)
miao=0;
break;
case 2:
fen++;
if(fen>59)
fen=0;
break;
case 3:
shi++;
if(shi>23)
shi=0;
break;
}
}
if(down==0)
{
delay(50);
flag_down=1;
while(!down);
switch(count)
{
case 1:
miao--;
if(miao==255)
miao=59;
break;
case 2:
fen--;
if(fen==255)
fen=59;
break;
case 3:
shi--;
if(miao==255)
shi=23;
break;
}
}
}
void alarm_ring()
{
if (alarm_on==1)
{
if (shi==alarm_shi && fen==alarm_fen && miao==alarm_miao)
flag_alarm=1;
}
if(flag_alarm==1)
{
uchar i,j,t
t=30;
for(i=0;i<200;i++)
{
fm=~fm;
for(j=0;j<t;j++)
}
if (set==0 || up==0 || down==0 || nao==0)
{
delay(50);
if(set==0 || up==0 || down==0 || nao==0)
{
while(!set);
while(!up);
while(!down);
while(!nao);
flag_alarm=0;
fm=1;
}
}
}
}
void c02_init()
{
c_init();
alarm_shi=c02_read_add(1);
delay(200);
alarm_fen=c02_read_add(2);
delay(200);
alarm_miao=c02_read_add(3);
delay(200);
alarm_on=c02_read_add(4);
delay(200);
}
void alarm_huan()
{
uchar num;
lcd_write_com(0x01);
lcd_write_com(0x80);
for (num=0;num<10;num++)
{
lcd_write_date(table2[num]);
dalay(2);
}
lcd_write_com(0x0F);
write_time1(4,alarm_shi);
lcd_write_date(0x3a);
write_time1(7,alarm_fen);
lcd_write_date(0x3a);
write_time1(10,alarm_miao);
if (alarm_on==0)
{
lcd_write_com(0x80+0x40+13);
lcd_write_date('O');
lcd_write_date('F');
lcd_write_date('F');
}
if (alarm_on==1)
{
lcd_write_com(0x80+0x40+13);
lcd_write_date(' ');
lcd_write_date('O');
lcd_write_date('N');
}
}
void key_set_alarm()
{
if(set==0 && alarm_count==0 && flag_alarm==0)
{
delay(50);
if(set==0)
{
while(!set);
count++;
lcd_write_com(0x0f);
if(flag==1)
{
done=1;
flag=0;
ds1302_write(0x8e,0x00);
ds1302_write(0x80,key_bcd(miao)|0x80);
ds1302_write(0x8e,0x80);
}
}
}
if(nao==0 && count==0 && flag_alarm==0)
{
delay(50);
if(nao==0)
{
while(!nao);
alarm_count++£»
if(flag==1)
{
done=1;
flag=0;
alarm_huan();
}
}
}
}
void keyjpress()
{
key_set_alarm();
if(count!=0)
{
switch(count)
{
case 1:do
{
lcd_write_com(0x80+0x40+7);
key_up_down();
if(flag_up || flag_down)
{
flag_up=0;
flag_down=0;
ds1302_write(0x8e,0x00);
ds1302_write(0x80,key_bcd(miao)|0x80);
ds1302_write(0x8e,0x80);
write_time1(6,miao);
lcd_write_com(0x80+0x40+7);
}
}
while(count==2);
break;
case 2:do
{
lcd_write_com(0x80+0x40+4);
key_up_down();
if(flag_up || flag_down)
{
flag_up=0;
flag_down=0;
ds1302_write(0x8e,0x00);
ds1302_write(0x82,key_bcd(fen));
ds1302_write(0x8e,0x80);
write_time1(3,fen);
lcd_write_com(0x80+0x40+4);
}
}
while(count==3);
break;
case 3:do
{
lcd_write_com(0x80+0x40+1);
key_up_down();
if(flag_up || flag_down)
{
flag_up=0;
flag_down=0;
ds1302_write(0x8e,0x00);
ds1302_write(0x84,key_bcd(shi));
ds1302_write(0x8e,0x80);
write_time1(0,shi);
lcd_write_com(0x80+0x40+1);
}
}
while(count==4);
break;
case 4:
lcd_write_com(0x0c);
flag=1;
done=0;
count=0;
ds1302_write(0x8e,0x00);
ds1302_write(0x80,key_bcd(miao)&0x7f);
ds1302_write(0x8e,0x80);
break;
default:break;
}
}
if (alarm_count!=0)
{
switch(alarm_count)
{
case 1:
lcd_write_com(0x80+0x40+15);
if(up==0)
{
delay(50);
if(up==0)
{
while(!up);
alarm_on=1;
lcd_write_com(0x80+0x40+13);
lcd_write_date(' ');
lcd_write_date('O');
lcd_write_date('N');
c02_write_add(4,alarm_on);
delay(200);
lcd_write_com(0x80+0x40+15);
}
}
if (down==0)
{
if(down==0);
{
while(!down);
alarm_on=0;
lcd_write_com(0x80+0x40+13);
lcd_write_date('O');
lcd_write_date('F');
lcd_write_date('F');
c02_write_add(4,alarm_on);
delay(200);
lcd_write_com(0x80+0x40+15);
}
}
break;
case 2:
lcd_write_com(0x80+0x40+11);
if(up==0 || down==0)
{
delay(50);
if(up==0)
{
while(!up);
alarm_miao++;
if(alarm_miao>59)
alarm_miao=0;
}
if(down==0)
{
while(!down);
alarm_miao--;
if(alarm_miao==255)
alarm_miao=59;
}
write_time1(10,alarm_miao);
lcd_write_com(0x80+0x40+11);
c02_write_add(3,alarm_miao);
delay(200);
}
break;
case 3:
lcd_write_com(0x80+0x40+8);
if(up==0 || down==0)
{
delay(50);
if(up==0)
{
while(!up);
alarm_fen++;
if(alarm_fen>59)
alarm_fen=0;
}
if(down==0)
{
while(!down);
alarm_fen--;
if(alarm_fen==255)
alarm_fen=59;
}
write_time1(7,alarm_fen);
lcd_write_com(0x80+0x40+8);
c02_write_add(2,alarm_fen);
delay(200);
}
break;
case 4:
lcd_write_com(0x80+0x40+5);
if(up==0 || down==0)
{
delay(50);
if(up==0)
{
while(!up);
alarm_shi++;
if(alarm_shi>23)
alarm_shi=0;
}
if(down==0)
{
while(!down);
alarm_shi--;
if(alarm_shi==255)
alarm_shi=23;
}
write_time1(4,alarm_shi);
lcd_write_com(0x80+0x40+5);
c02_write_add(1,alarm_shi);
delay(200);
}
break;
case 5:
alarm_count=0;
lcd1602_init();
flag=1;
done=0;
break;
}
}
}
void xianshi()
{
miao=bcd_decimal(de1302_read(0x81));
fen=bcd_decimal(de1302_read(0x83));
shi=bcd_decimal(de1302_read(0x85));
write_time1(6,miao);
write_time1(3,fen);
write_time1(0,shi);
wendu=read_temperature();
wendu_decimal(wendu);
}
main ()
{
uchar ch;
lcd1602_en=0;
P0=0XFF;
lcd1602_init();
ds18b20_reset();
c02_init();
ch=ds1302_read(0x81)>>7;
if(ch)
{
set_rtc();
}
while(1)
{
if(done==1)
{
keyjpress();
}
if(done==0)
{
xianshi();
key_set_alarm();
alarm_ring();
}
}
}