STC10F(1T单片机) 操作DS1302 数码管显示都是0
调了3天不知道为什么数码管全部显示0代码如下:#include"REG52.H"#include"INTRINS.H"typedefunsignedcharuint8;t...
调了3天 不知道为什么 数码管全部显示0 代码如下:
#include "REG52.H"
#include "INTRINS.H"
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit SCK = P2^2;
sbit SDA = P2^1;
sbit RST = P2^3;
sbit EN138 = P2^7;
sbit ADDRA = P2^6;
sbit ADDRB = P2^5;
sbit ADDRC = P2^4;
#define DS1302_W_ADDR 0xBE
#define DS1302_R_ADDR 0xBF
uint8 time[7]={10,25,23,20,11,4,12}; //秒 分 时 日 月 周 年
uint8 num[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数码管0-9
void display();
void delay()
{
_nop_();
_nop_();
}
/*** 写一个字节*/
void write_ds1302_byte(uint8 dat)
{
uint8 i;
for (i=0;i<8;i++)
{
SCK = 0; //时钟线拉低
delay();
dat >>= 1;
SDA = dat & 0x01;
SCK = 1;
delay();
}
}
/*** 读一个字节*/
uint8 read_ds1302_byte(void)
{
uint8 i, dat=0;
for (i=0;i<8;i++)
{
SCK = 0; //时钟线拉低
delay();
dat >>= 1;
if (SDA)
dat |= 0x80;
SCK = 1;
delay();
}
return dat;
}
void reset_ds1302(void)
{
RST = 0;
SCK = 0;
RST = 1;
}
/** * 清除写保护*/
void clear_ds1302_WP(void)
{
reset_ds1302();
RST = 1;
write_ds1302_byte(0x8E);
write_ds1302_byte(0);
SDA = 0;
RST = 0;
}
/** * 设置写保护*/
void set_ds1302_WP(void)
{
reset_ds1302();
RST = 1;
write_ds1302_byte(0x8E);
write_ds1302_byte(0x80);
SDA = 0;
delay();
RST = 0;
}
/**
* 设定时钟数据
*/
void set_time(uint8 *timedata)
{
uint8 i, tmp;
for (i=0; i<7; i++) // 转化为BCD格式
{
tmp = timedata[i] / 10;
timedata[i] = timedata[i] % 10;
timedata[i] = timedata[i] + tmp*16;
}
clear_ds1302_WP();
reset_ds1302(); //
RST = 1;
delay(); //
write_ds1302_byte(DS1302_W_ADDR);
delay();
for (i=0; i<7; i++)
{
write_ds1302_byte(timedata[i]);
delay();
}
write_ds1302_byte(0);
SDA = 0;
delay();
RST = 0;
delay();
set_ds1302_WP();
}
/*读时钟数据
*/
void read_time(uint8 *timedata)
{
uint8 i, tmp;
clear_ds1302_WP();
reset_ds1302();
RST = 1;
write_ds1302_byte(DS1302_R_ADDR);
for (i=0; i<7; i++)
{
timedata[i] = read_ds1302_byte();
delay();
}
SDA = 0;
delay();
RST = 0;
delay();
set_ds1302_WP();
for (i=0; i<7; i++) // 转化为正常格式
{
tmp = timedata[i] / 16;
timedata[i] = timedata[i] % 16;
timedata[i] = timedata[i] + tmp*10;
}
}
main()
{
EN138 = 1;
TMOD = 0X01;
TH0 = 0XFC; //用于数码管动态扫描
TL0 = 0X66;
ET0 = 1;
EA = 1;
TR0 = 1;
set_time(time);
while(1)
{
read_time(time); //秒分时日月周年
}
} 展开
#include "REG52.H"
#include "INTRINS.H"
typedef unsigned char uint8;
typedef unsigned int uint16;
sbit SCK = P2^2;
sbit SDA = P2^1;
sbit RST = P2^3;
sbit EN138 = P2^7;
sbit ADDRA = P2^6;
sbit ADDRB = P2^5;
sbit ADDRC = P2^4;
#define DS1302_W_ADDR 0xBE
#define DS1302_R_ADDR 0xBF
uint8 time[7]={10,25,23,20,11,4,12}; //秒 分 时 日 月 周 年
uint8 num[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数码管0-9
void display();
void delay()
{
_nop_();
_nop_();
}
/*** 写一个字节*/
void write_ds1302_byte(uint8 dat)
{
uint8 i;
for (i=0;i<8;i++)
{
SCK = 0; //时钟线拉低
delay();
dat >>= 1;
SDA = dat & 0x01;
SCK = 1;
delay();
}
}
/*** 读一个字节*/
uint8 read_ds1302_byte(void)
{
uint8 i, dat=0;
for (i=0;i<8;i++)
{
SCK = 0; //时钟线拉低
delay();
dat >>= 1;
if (SDA)
dat |= 0x80;
SCK = 1;
delay();
}
return dat;
}
void reset_ds1302(void)
{
RST = 0;
SCK = 0;
RST = 1;
}
/** * 清除写保护*/
void clear_ds1302_WP(void)
{
reset_ds1302();
RST = 1;
write_ds1302_byte(0x8E);
write_ds1302_byte(0);
SDA = 0;
RST = 0;
}
/** * 设置写保护*/
void set_ds1302_WP(void)
{
reset_ds1302();
RST = 1;
write_ds1302_byte(0x8E);
write_ds1302_byte(0x80);
SDA = 0;
delay();
RST = 0;
}
/**
* 设定时钟数据
*/
void set_time(uint8 *timedata)
{
uint8 i, tmp;
for (i=0; i<7; i++) // 转化为BCD格式
{
tmp = timedata[i] / 10;
timedata[i] = timedata[i] % 10;
timedata[i] = timedata[i] + tmp*16;
}
clear_ds1302_WP();
reset_ds1302(); //
RST = 1;
delay(); //
write_ds1302_byte(DS1302_W_ADDR);
delay();
for (i=0; i<7; i++)
{
write_ds1302_byte(timedata[i]);
delay();
}
write_ds1302_byte(0);
SDA = 0;
delay();
RST = 0;
delay();
set_ds1302_WP();
}
/*读时钟数据
*/
void read_time(uint8 *timedata)
{
uint8 i, tmp;
clear_ds1302_WP();
reset_ds1302();
RST = 1;
write_ds1302_byte(DS1302_R_ADDR);
for (i=0; i<7; i++)
{
timedata[i] = read_ds1302_byte();
delay();
}
SDA = 0;
delay();
RST = 0;
delay();
set_ds1302_WP();
for (i=0; i<7; i++) // 转化为正常格式
{
tmp = timedata[i] / 16;
timedata[i] = timedata[i] % 16;
timedata[i] = timedata[i] + tmp*10;
}
}
main()
{
EN138 = 1;
TMOD = 0X01;
TH0 = 0XFC; //用于数码管动态扫描
TL0 = 0X66;
ET0 = 1;
EA = 1;
TR0 = 1;
set_time(time);
while(1)
{
read_time(time); //秒分时日月周年
}
} 展开
展开全部
都没有看到你的显示函数,上面看了一下没有什么问题!
建议把完整的贴出来。
建议把完整的贴出来。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询