为什么我读的DS1302数据隔一个数就出现一个乱码 比如显示01完是个乱码 然后是03 然后就是乱码 然后05。乱码 07 乱码 。。这样一直下去 有知道为什么的吗
程序如下
#include <reg52.h>
#include <DS1302.H>
sbit DS1302_data=P3^7;
sbit DS1302_cs=P3^5;
sbit DS1302_clk=P3^6;
sbit wei=P2^7;
sbit duan=P2^6;
xdata table1[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
xdata table2[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
table3[]={0,0,6,0,6,0};
//#include "intrins.h"
unsigned char year,month,date,hour,minute,second,week,a,b,c,i,j;
//unsigned char display_buffer[6];
void delay()
{
for(i=0;i<50;i++)
{for(j=0;j<10;j++);
}
}
void short_ds1302_delay();//短延时
void long_ds1302_delay();//长延时
void DS1302_write(unsigned char DS1302_temp); //写入一个字节
unsigned char DS1302_read(void); //读一字节
void DS1302_init();//ds1302初始化
void DS1302_write_time(int command,int dat);//写入second or minute or hour...... or 写入一个命令
unsigned char DS1302_read_time(int command);//读取second or minute or hour......
void short_ds1302_delay()
{
unsigned char i;
for(i=0;i<5;i++);
}
void long_ds1302_delay()
{
unsigned char i;
for(i=0;i<25;i++);
}
void DS1302_write(unsigned char DS1302_temp) //写入一个字节
{
unsigned char i,temp;
long_ds1302_delay();
temp=DS1302_temp;
for(i=0;i<8;i++)
{
DS1302_data=temp&0x01;
short_ds1302_delay();
DS1302_clk=1;
short_ds1302_delay();
DS1302_clk=0;
temp=temp>>1;
}
}
unsigned char DS1302_read(void) //读一字节
{
unsigned char i,temp;
long_ds1302_delay();
temp=0;
for(i=0;i<8;i++)
{
temp=temp>>1;
if(DS1302_data==1)
temp=temp+0x80;
DS1302_clk=1;
short_ds1302_delay();
DS1302_clk=0;
short_ds1302_delay();
}
return temp;
}
/*
/////////写操作设置,当a=0x80时禁止写操作,a=0x00时允许////////////////
void DS1302_write_able(unsigned char a)
{
DS1302_cs=0;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x8e);
DS1302_data=1;
DS1302_write(a);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
}
/////////读操作设置,当a=0x80时禁止读操作,a=0x00时允许////////////////
void DS1302_read_able(unsigned char a)
{
DS1302_cs=0;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x8f);
DS1302_data=1;
DS1302_write(a);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
}
*/
////////////////////ds1302初始化////////////////////////
void DS1302_init()
{
unsigned char i;
DS1302_write_time(0x8e,0x00); //允许写操作
DS1302_cs=0;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x81);
DS1302_data=1;
i=DS1302_read();
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
second=((i>>4)&0x07)*10+(i&0x0f);
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x80);
DS1302_data=1;
DS1302_write(0x7f&i);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
DS1302_clk=0; //充电设置
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(0x90);
DS1302_data=1;
// DS1302_write(0xa6); //允许充电,一个二极管 4k电阻
DS1302_write(0x00); //禁止充电
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
DS1302_write_time(0x8e,0x80); //禁止写操作
}
unsigned char DS1302_read_time(int command)//读取second or minute or hour......
{
unsigned char i;
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(command);
DS1302_data=1;
i=DS1302_read();
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
return i;
}
void DS1302_readdate()
{
unsigned char i;
DS1302_cs=0;
//读取second
i=DS1302_read_time(0x81);
second=((i>>4)&0x07)*10+(i&0x0f);
//读取分minute
i=DS1302_read_time(0x83);
minute=((i>>4)&0x07)*10+(i&0x0f);
//读取时hour
i=DS1302_read_time(0x85);
hour=((i>>4)&0x03)*10+(i&0x0f);
//读取日date
i=DS1302_read_time(0x87);
date=((i>>4)&0x03)*10+(i&0x0f);
//读取月份month
i=DS1302_read_time(0x89);
month=((i>>4)&0x01)*10+(i&0x0f);
//读取周week
i=DS1302_read_time(0x8b);
week=i&0x07;
//读取年份year
i=DS1302_read_time(0x8d);
year=((i>>4)&0x0f)*10+(i&0x0f);
}
void DS1302_write_time(int command,int dat)//写入second or minute or hour...... or 写入一个命令
{
DS1302_clk=0;
short_ds1302_delay();
DS1302_cs=1;
DS1302_write(command);
DS1302_data=1;
DS1302_write(dat);
DS1302_cs=0;
short_ds1302_delay();
DS1302_clk=1;
}
void DS1302_writedate()
{
DS1302_write_time(0x8e,0x00); //允许写操作
DS1302_cs=0;
//写second
DS1302_write_time(0x80,0x7f&(((second/10)<<4)+(second%10)));
//minute
DS1302_write_time(0x82,0x7f&(((minute/10)<<4)+(minute%10)));
//hour
DS1302_write_time(0x84,0x3f&(((hour/10)<<4)+(hour%10)));
//date
DS1302_write_time(0x86,0x3f&(((date/10)<<4)+(date%10)));
//month
DS1302_write_time(0x88,0x1f&(((month/10)<<4)+(month%10)));
//week
DS1302_write_time(0x8a,0x07&week);
//year
DS1302_write_time(0x8c,((year/10)<<4)+(year%10));
DS1302_write_time(0x8e,0x80); //禁止写操作
}
main()
{
DS1302_init();
DS1302_write_time(0x8e,0x00); //允许写操作
DS1302_cs=0;
//写second
DS1302_write_time(0x80,0x7f&(((second/10)<<4)|(second%10)));
DS1302_write_time(0x8e,0x80); //禁止写操作
while(1){ a=DS1302_read_time(0x81);
//second=((a&0x07)>>4)*10+(a&0x0f);
b=(a&0x70)>>4;
c=a&0x0f;
wei=1;
P0=table1[0];
wei=0;
duan=1;
// t=table3;
P0=table2;
duan=0;
delay();
wei=1;
P0=table1[1];
wei=0;
duan=1;
// t=table3;
P0=table2[c];
duan=0;
delay();
}}
或者
IO接口没有上拉。。。。。。。。。。。。。。。。。。。。。。
一周热门 更多>