自己编了个简单的时钟程序,用数码官显示的,可是显示结果错误了,数码官的E脚老是亮着,小弟不知道是什么原因请大虾们帮忙看看解决下啊~~在这谢谢了~~~~
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code Disp_Tab[] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90,0xbf};
uchar code dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F};
uchar code write_add[]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar code red_add[]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
uchar time_dat[]={10,5,12,25,0,0,0};
uchar disp_dat[8];
sbit sck=P3^4;
sbit io=P3^5;
sbit rst=P3^6;
uchar red_byte();
uchar red_ds1302(uchar add);
void write_byte(uchar dat);
void write_ds1302(uchar add,uchar dat);
void set_time();
void red_time();
void pros();
void disply();
void delay(unsigned int i);
void write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
sck=0;
_nop_();
io=dat&0x01;
dat=dat>>1;
sck=1;
_nop_();
}
}
void write_ds1302(uchar add,uchar dat)
{
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_byte(add);
write_byte(dat);
sck=1;
_nop_();
io=1;
rst=0;
_nop_();
}
unsigned char red_byte()
{
uchar i,temp;
for(i=0;i<8;i++)
{
sck=0;
_nop_();
temp=temp>>1;
if(io)
temp=temp|0x80;
sck=1;
_nop_();
}
return (temp);
}
unsigned char red_ds1302(uchar add)
{
uchar temp;
rst=0;
_nop_();
sck=0;
_nop_();
rst=1;
_nop_();
write_byte(add);
temp=red_byte();
sck=1;
_nop_();
io=1;
rst=0;
_nop_();
return(temp);
}
void set_time()
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_dat[i]/10;
time_dat[i]=time_dat[i]%10;
time_dat[i]=time_dat[i]+j*16;
}
write_ds1302(0x8e,0x00);
for(i=0;i<7;i++)
{
write_ds1302(write_add[i],time_dat[i]);
}
write_ds1302(0x8e,0x80);
}
void red_time()
{
uchar i;
for(i=0;i<7;i++)
{
time_dat[i]=red_ds1302(red_add[i]);
}
}
void pros()
{
disp_dat[0]=time_dat[6]%16;
disp_dat[1]=time_dat[6]/16;
disp_dat[2]=10;
disp_dat[3]=time_dat[5]%16;
disp_dat[4]=time_dat[5]/16;
disp_dat[5]=10;
disp_dat[6]=time_dat[4]%16;
disp_dat[7]=time_dat[4]/16;
}
void disply()
{
uchar i;
P0=0xff;
for(i=0;i<8;i++)
{
P0=Disp_Tab[disp_dat[i]];
P1=dispbit[i];
delay(10);
}
}
void delay(unsigned int i)
{
uint j;
for(i; i > 0; i--)
for(j = 100; j > 0; j--);
}
void main()
{
set_time();
while(1)
{
red_time();
pros();
disply();
}
} |