ds1302显示问题 小时、分、秒个位都显示5 其他正常 这是怎么回事?
#include<reg51.h>#defineuintunsignedint#defineucharunsignedchar//寄存器宏定义#defineWRITE_S...
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
//寄存器宏定义
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define WRITE_PROTECT 0x8E
#define disp_wei P1
#define disp_duan P2
//位寻址寄存器定义
sbit ACC_7 = ACC^7;
//管脚定义
sbit SCLK = P3^6; // DS1302时钟信号7脚
sbit DIO= P3^4; // DS1302数据信号6脚
sbit CE = P3^5; // DS1302片选5脚 RST
uchar duan[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0x02};
uchar wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar _disp[]={1,2,3,4,5,6,7,8};
uchar time1;
uint time2;
void Write1302 ( unsigned char addr,dat ); // 地址、数据发送子程序
uchar Read1302 ( unsigned char addr ); //数据读取子程序
void Initial(void); //初始化DS1302
void time_pros(void); //进制转换
void display(); //显示
void main(void)
{
Initial();
while(1)
{
time_pros();
display();
}
}
//地址、数据发送子程序
void Write1302 ( unsigned char addr,dat )
{
unsigned char i,temp;
CE=0; //CE引脚为低,数据传送中止
SCLK=0; //清零时钟总线
CE = 1; //CE引脚为高,逻辑控制有效
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO = temp&0x01; //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//发送数据
for ( i=8; i>0; i-- )
{
SCLK = 0;
temp = dat;
DIO = temp&0x01;
dat >>= 1;
SCLK = 1;
}
CE = 0;
}
//数据读取子程序
unsigned char Read1302 ( unsigned char addr )
{
unsigned char i,temp,dat1,dat2;
CE=0;
SCLK=0;
CE = 1;
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO =temp&0x01; //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//读取数据
for ( i=8; i>0; i-- )
{
ACC_7=DIO;
SCLK = 1;
ACC>>=1;
SCLK = 0;
}
CE=0;
dat1=ACC;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return (dat1);
}
//初始化DS1302
void Initial(void)
{
Write1302 (WRITE_PROTECT,0X00); //禁止写保护
Write1302 (WRITE_SECOND,0x59); //秒位初始化
Write1302 (WRITE_MINUTE,0x59); //分钟初始化
Write1302 (WRITE_HOUR,0x12); //小时初始化
Write1302 (WRITE_PROTECT,0x80); //允许写保护
}
void time_pros(void)
{
_disp[0]=Read1302(0x85)/10; //时十位
_disp[1]=Read1302(0x85)%10; //时个位
_disp[2]=10; // -
_disp[3]=Read1302(0x83)/10; //分十位
_disp[4]=Read1302(0x83)%10; //分个位
_disp[5]=10; // -
_disp[6]=Read1302(0x81)/10; //秒十位
_disp[7]=Read1302(0x81)%16; //秒个位
}
void display(void)
{
uchar aa1=0;
for(aa1=0;aa1<8;aa1++)
{
disp_wei=wei[aa1];
disp_duan=duan[_disp[aa1]];
}
} 展开
#define uint unsigned int
#define uchar unsigned char
//寄存器宏定义
#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84
#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85
#define WRITE_PROTECT 0x8E
#define disp_wei P1
#define disp_duan P2
//位寻址寄存器定义
sbit ACC_7 = ACC^7;
//管脚定义
sbit SCLK = P3^6; // DS1302时钟信号7脚
sbit DIO= P3^4; // DS1302数据信号6脚
sbit CE = P3^5; // DS1302片选5脚 RST
uchar duan[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,0x02};
uchar wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar _disp[]={1,2,3,4,5,6,7,8};
uchar time1;
uint time2;
void Write1302 ( unsigned char addr,dat ); // 地址、数据发送子程序
uchar Read1302 ( unsigned char addr ); //数据读取子程序
void Initial(void); //初始化DS1302
void time_pros(void); //进制转换
void display(); //显示
void main(void)
{
Initial();
while(1)
{
time_pros();
display();
}
}
//地址、数据发送子程序
void Write1302 ( unsigned char addr,dat )
{
unsigned char i,temp;
CE=0; //CE引脚为低,数据传送中止
SCLK=0; //清零时钟总线
CE = 1; //CE引脚为高,逻辑控制有效
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO = temp&0x01; //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//发送数据
for ( i=8; i>0; i-- )
{
SCLK = 0;
temp = dat;
DIO = temp&0x01;
dat >>= 1;
SCLK = 1;
}
CE = 0;
}
//数据读取子程序
unsigned char Read1302 ( unsigned char addr )
{
unsigned char i,temp,dat1,dat2;
CE=0;
SCLK=0;
CE = 1;
//发送地址
for ( i=8; i>0; i-- ) //循环8次移位
{
SCLK = 0;
temp = addr;
DIO =temp&0x01; //每次传输低字节
addr >>= 1; //右移一位
SCLK = 1;
}
//读取数据
for ( i=8; i>0; i-- )
{
ACC_7=DIO;
SCLK = 1;
ACC>>=1;
SCLK = 0;
}
CE=0;
dat1=ACC;
dat2=dat1/16; //数据进制转换
dat1=dat1%16; //十六进制转十进制
dat1=dat1+dat2*10;
return (dat1);
}
//初始化DS1302
void Initial(void)
{
Write1302 (WRITE_PROTECT,0X00); //禁止写保护
Write1302 (WRITE_SECOND,0x59); //秒位初始化
Write1302 (WRITE_MINUTE,0x59); //分钟初始化
Write1302 (WRITE_HOUR,0x12); //小时初始化
Write1302 (WRITE_PROTECT,0x80); //允许写保护
}
void time_pros(void)
{
_disp[0]=Read1302(0x85)/10; //时十位
_disp[1]=Read1302(0x85)%10; //时个位
_disp[2]=10; // -
_disp[3]=Read1302(0x83)/10; //分十位
_disp[4]=Read1302(0x83)%10; //分个位
_disp[5]=10; // -
_disp[6]=Read1302(0x81)/10; //秒十位
_disp[7]=Read1302(0x81)%16; //秒个位
}
void display(void)
{
uchar aa1=0;
for(aa1=0;aa1<8;aa1++)
{
disp_wei=wei[aa1];
disp_duan=duan[_disp[aa1]];
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询