如何把从DS1302中读出的数据用51单片机用动态扫描方式显示在数码管上

2024-12-31 05:56:12
推荐回答(2个)
回答1:

建议先将动态扫描测试通过,从18B20读出的数据通过数学运算将每个位分离出来,然后依次将数据放入显示缓存中即可。

回答2:

楼上,人家是ds1302的,呵呵,
给你个参考:
#include

sbit clock_clk=P2^7;
sbit clock_dat=P2^6;
sbit clock_rst=P2^5;

sbit wei1=P2^0;
sbit wei2=P2^1;
sbit wei3=P2^2;
sbit wei4=P2^3;
sbit wei5=P2^4;
sbit wei6=P3^3;

sbit a0 = ACC ^ 0;//为了编程方便定义的位变量
sbit a1 = ACC ^ 1;
sbit a2 = ACC ^ 2;
sbit a3 = ACC ^ 3;
sbit a4 = ACC ^ 4;
sbit a5 = ACC ^ 5;
sbit a6 = ACC ^ 6;
sbit a7 = ACC ^ 7;

unsigned char Tab_Num[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90};//0-9数字

void delay(int num)//延时函数
{
while(num--) ;
}

void clock_write(unsigned char dd) //写入一个字节,DS1302时序
{
ACC=dd;
clock_clk=0;clock_dat=a0; clock_clk=1;
clock_clk=0;clock_dat=a1; clock_clk=1;
clock_clk=0;clock_dat=a2; clock_clk=1;
clock_clk=0;clock_dat=a3; clock_clk=1;
clock_clk=0;clock_dat=a4; clock_clk=1;
clock_clk=0;clock_dat=a5; clock_clk=1;
clock_clk=0;clock_dat=a6; clock_clk=1;
clock_clk=0;clock_dat=a7; clock_clk=1;
}

unsigned char clock_read(void) //读出一个字节,时序
{

clock_clk=1; clock_clk=0; a0=clock_dat;
clock_clk=1; clock_clk=0; a1=clock_dat;
clock_clk=1; clock_clk=0; a2=clock_dat;
clock_clk=1; clock_clk=0; a3=clock_dat;
clock_clk=1; clock_clk=0; a4=clock_dat;
clock_clk=1; clock_clk=0; a5=clock_dat;
clock_clk=1; clock_clk=0; a6=clock_dat;
clock_clk=1; clock_clk=0; a7=clock_dat;
return(ACC);
}

unsigned char read_clock(unsigned char reg1)//从reg1寄存器读数据
{
unsigned char dd=0;
clock_clk=0;
clock_rst=0;
clock_rst=1;
clock_write(reg1);
dd=clock_read();
clock_rst=0;
return(dd);
}

void write_clock(unsigned char reg2, unsigned char date)//向reg2写入date数据
{
clock_clk=0;
clock_rst=0;
clock_rst=1;
clock_write(reg2);
clock_write(date);
clock_rst=0;
}

void show(void)//显示和闪烁函数
{
unsigned char d, e;
unsigned int i;
for(i=0;i<1000;i++)
{
d=read_clock(0x81);//读秒
e=d;
d=d>>4;
e=e&0x0f;

P0 = Tab_Num[ d ];//秒十位
wei2=1;
delay(250);
wei2=0;
P0 = Tab_Num[ e ];//秒各位
wei1=1;
delay(250);
wei1=0;

d=read_clock(0x83);//读分钟
e=d;
d=d>>4;
e=e&0x0f;

P0 = Tab_Num[ d ];//分十位
wei4=1;
delay(250);
wei4=0;
P0 = Tab_Num[ e ];//分各位
wei3=1;
delay(250);
wei3=0;

d=read_clock(0x85);//读小时
e=d;
d=d>>4;
e=e&0x0f;

P0 = Tab_Num[ d ];//小时十位
wei6=1;
delay(250);
wei6=0;
P0 = Tab_Num[ e ];//小时各位
wei5=1;
delay(250);
wei5=0;
}

for(i=0;i<400;i++) //间隔显示
{
d=read_clock(0x87);//读日
e=d;
d=d>>4;
e=e&0x0f;
P0 = Tab_Num[ d ];//日十位
wei2=1;
delay(250);
wei2=0;
P0 = Tab_Num[ e ];//日各位
wei1=1;
delay(250);
wei1=0;

d=read_clock(0x89);//读月
e=d;
d=d>>4;
e=e&0x0f;
P0 = Tab_Num[ d ];//月十位
wei4=1;
delay(250);
wei4=0;
P0 = Tab_Num[ e ];//月各位
wei3=1;
delay(250);
wei3=0;

d=read_clock(0x8d);//读年
e=d;
d=d>>4;
e=e&0xf;
P0 = Tab_Num[ d ];//年十位
wei6=1;
delay(250);
wei6=0;
P0 = Tab_Num[ e ];//年各位
wei5=1;
delay(250);
wei5=0;
}
for(i=0;i<200;i++) //间隔显示
{
d=read_clock(0x8b);//读年
e=d;
d=d>>4;
e=e&0xf;
P0 = Tab_Num[ d ];//年十位
wei2=1;
delay(250);
wei2=0;
P0 = Tab_Num[ e ];//年各位
wei1=1;
delay(250);
wei1=0;
}
}

/*--------------------时钟的初始化---------------------------*/
void Init_1302(void)//如果不满意现在的表,可以初始化调整,写入程序之后,再把初始化屏蔽掉
{
write_clock(0x8e,0x00);//close_protect
delay(250);
write_clock(0x8c,0x08);//year 08年
delay(250);
write_clock(0x88,0x6);//month 06
delay(250);
write_clock(0x86,0x25);//date 25
delay(250);
write_clock(0x84,0x8);//hour 8
delay(250);
write_clock(0x82,0x28);//minute 28
delay(250);
write_clock(0x80,0x29);//second 40
delay(250);
write_clock(0x8a,0x03);//week_day 3
delay(250);
write_clock(0x90,0xa0);//supply_power
delay(250);

write_clock(0x8e,0x80);//protect
delay(250);
}
void main(void)
{
int i;
for(i=0;i<2;i++)
delay(2000);
Init_1302();//DS1302寄存器初始化

P2=0x00;

while(1)
{
show();
}
}