为什么ds1302在51单片机中,使用时,读出的分/秒显示数据为什么要显5A呀?
程序中还有以下:voidtime_pros(void){disp[0]=time_data[6]%16;disp[1]=time_data[6]/16;disp[2]=1...
程序中还有以下:
void time_pros(void)
{
disp[0]=time_data[6]%16;
disp[1]=time_data[6]/16;
disp[2]=17;
disp[3]=time_data[5]%16;
disp[4]=time_data[5]/16;
disp[5]=17;
disp[6]=time_data[4]%16;
disp[7]=time_data[4]/16;
}
void display(void)
{
uchar i;
for(i=0;i<8;i++)
{
P2=smg_du[disp[i]];
P1=smg_we[i];
delay_50us(20);
}
} 展开
void time_pros(void)
{
disp[0]=time_data[6]%16;
disp[1]=time_data[6]/16;
disp[2]=17;
disp[3]=time_data[5]%16;
disp[4]=time_data[5]/16;
disp[5]=17;
disp[6]=time_data[4]%16;
disp[7]=time_data[4]/16;
}
void display(void)
{
uchar i;
for(i=0;i<8;i++)
{
P2=smg_du[disp[i]];
P1=smg_we[i];
delay_50us(20);
}
} 展开
3个回答
展开全部
楼上,人家是ds1302的,呵呵,
给你个参考:
#include<reg52.h>
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[] = ;//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();
}
}
给你个参考:
#include<reg52.h>
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[] = ;//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();
}
}
展开全部
你用什么显示的?如果是1602的话需要再转成ASCII码才能显示。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ds1302存储数据是16进制的,你显示前要转成10进制
追问
程序中有以下两段:
void set_rtc(void)
{
uchar i,j;
for(i=0;i<7;i++)
{
j=time_data[i]/10;
time_data[i]=time_data[i]%10;
time_data[i]=time_data[i]+j*16;
}
应没问题吧?帮看看,按理说,DS1302中的数据不应超过59吧?!
追答
你的数据在显示秒的地方会跳动吗?如果会跳动就是DS1302已经初始化好了,只是在数据处理上有问题啦;那就是数据转换有问题啦
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询