#include <reg52.h>
#include <intrins.h>
sbit rst=P3^4;
sbit sck=P3^5;
sbit sda=P3^6;
sbit LS138A=P2^2;
sbit LS138B=P2^3;
sbit LS138C=P2^4;
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年 最低位读写位
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code unsigned char set_time_rtc[7]={50,59,12,31,1,10,10};//读入时间
code unsigned Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};//数码管显示
unsigned char set_time_rtc_bcd[];
unsigned char read_time_rtc[];
unsigned char read_time_rtc_bcd[];
void write_ds1302_byte(unsigned char dat);
void write_ds1302(unsigned char add,unsigned char dat);
unsigned char read_ds1302(unsigned char add);
void set_rtc();
void read_rtc();
void main()
{
unsigned char i;
set_rtc();
while(1)
{
read_rtc();
for(i=0;i<8;i++)
{
P0=Disp_Tab[read_time_rtc_bcd]; //查表法得到要显示数字的数码段
switch(i)
{
case 0lS138A=0; LS138B=0; LS138C=0; break;
case 1 LS138A=1; LS138B=0; LS138C=0; break;
case 2 LS138A=0; LS138B=1; LS138C=0; break;
case 3 LS138A=1; LS138B=1; LS138C=0; break;
case 4 LS138A=0; LS138B=0; LS138C=1; break;
case 5 LS138A=1; LS138B=0; LS138C=1; break;
case 6 LS138A=0; LS138B=1; LS138C=1; break;
case 7 LS138A=1; LS138B=1; LS138C=1; break;
}
i++;
}
}
}
void write_ds1302_byte(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
sck=0;
sda=dat&0x01;
sck=1;
dat>>=1;
}
}
void write_ds1302(unsigned char add,unsigned char dat)
{
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
write_ds1302_byte(dat);
_nop_();
rst=0;
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char i,tmp=0x00;
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_ds1302_byte(add);
for(i=0;i<8;i++)
{
tmp>>=1;
sck=0;
if(sda)
tmp=sda|0x80;
_nop_();
sck=1;
_nop_();
}
rst=0;
_nop_();
sck=0;
_nop_();
sda=1;
_nop_();
return tmp;
}
void set_rtc()
{
unsigned char i,tmp;
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
tmp=set_time_rtc/10;
set_time_rtc_bcd=set_time_rtc%10+tmp*16;
write_ds1302(write_rtc_address,set_time_rtc_bcd);
}
write_ds1302(0x8e,0x80);
}
void read_rtc()
{
read_time_rtc[0]=read_ds1302(0x80);
read_time_rtc[1]=read_ds1302(0x82);
read_time_rtc[2]=read_ds1302(0x83);
read_time_rtc_bcd[0]=read_time_rtc[0]/16;
read_time_rtc_bcd[1]=read_time_rtc[0]%16;
read_time_rtc_bcd[2]=0x00;
read_time_rtc_bcd[4]=read_time_rtc[1]%16;
read_time_rtc_bcd[5]=read_time_rtc[1]/16;
read_time_rtc_bcd[6]=0x00;
read_time_rtc_bcd[7]=read_time_rtc[2]/16;
read_time_rtc_bcd[8]=read_time_rtc[2]%16;
}
***************************************************************
大家帮忙看看,程序执行,只有数码管最后一位显示个8,其他都没显示
实在找不出哪里有问题了,请大家帮帮忙
还有就是编译的时候会出现
*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
这样的错误,不过HEX文件还是可以生成的,大家帮忙啊,万分感谢 |