大神帮忙看看这个ds1302程序的错误在哪好吗?
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include <intrins.h>
#include "lcd1602.h"
sbit rst = P3^5;
sbit sclk = P3^6;
sbit dsio = P3^4;
unsigned char table_time[] = {0, 0, 35, 11, 8, 1, 14};
unsigned char table_rtc[8];
void write_byte(unsigned char byte)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
sclk = 0;
dsio = byte & 0x01;
byte = byte >> 1;
sclk = 1;
_nop_();
}
}
void write_ds1302(unsigned char add, unsigned char dat)
{
rst = 0;
_nop_();
sclk = 0;
_nop_();
rst = 1;
_nop_();
write_byte(add);
write_byte(dat);
rst = 0;
_nop_();
sclk = 1;
_nop_();
dsio = 1;
_nop_();
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char i, temp;
rst = 0;
_nop_();
sclk = 0;
_nop_();
rst = 1;
_nop_();
write_byte(add);
for(i = 0; i < 8; i++)
{
temp = temp >> 1;
sclk = 0;
if(dsio)
temp = temp | 0x80;
sclk = 1;
}
rst = 0;
_nop_();
sclk = 1;
_nop_();
dsio = 1;
return temp;
}
void set_time()
{
unsigned char i, add;
write_ds1302(0x8e, 0x00);
for(i = 0, add = 0x80; i < 7; i++)
{
table_time = table_time / 10 * 16 + table_time % 10;
write_ds1302(add, table_time);
add = add + 2;
}
write_ds1302(0x8e, 0x80);
}
void read_time()
{
unsigned char i, add;
for(i = 0, add = 0x81; i < 7; i++)
{
table_time = read_ds1302(add);
add = add + 2;
}
table_rtc[7] = table_time[0] % 16;
table_rtc[6] = table_time[0] / 16;
table_rtc[5] = ':';
table_rtc[4] = table_time[1] % 16;
table_rtc[3] = table_time[1] / 16;
table_rtc[2] = ':';
table_rtc[1] = table_time[2] % 16;
table_rtc[0] = table_time[2] / 16;
}
void display()
{
unsigned char i;
write_1602com(0x80);
for(i = 0; i < 8; i++)
{
write_1602data(table_rtc);
}
}
void main()
{
init1602();
set_time();
while(1)
{
read_time();
display();
}
}
我没发现我哪里出错了,但是看结果应该是bcd转换那里有问题,但是我都是按照教程写的程序,而且我都写了两遍也没有什么问题,但是1602显示出来的就是乱码,就是那两个冒号能够显示出来,1602是我写完然后贴过来的,绝对没有问题,我就不写出来了
求大神帮忙看看
#include <intrins.h>
#include "lcd1602.h"
sbit rst = P3^5;
sbit sclk = P3^6;
sbit dsio = P3^4;
unsigned char table_time[] = {0, 0, 35, 11, 8, 1, 14};
unsigned char table_rtc[8];
void write_byte(unsigned char byte)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
sclk = 0;
dsio = byte & 0x01;
byte = byte >> 1;
sclk = 1;
_nop_();
}
}
void write_ds1302(unsigned char add, unsigned char dat)
{
rst = 0;
_nop_();
sclk = 0;
_nop_();
rst = 1;
_nop_();
write_byte(add);
write_byte(dat);
rst = 0;
_nop_();
sclk = 1;
_nop_();
dsio = 1;
_nop_();
}
unsigned char read_ds1302(unsigned char add)
{
unsigned char i, temp;
rst = 0;
_nop_();
sclk = 0;
_nop_();
rst = 1;
_nop_();
write_byte(add);
for(i = 0; i < 8; i++)
{
temp = temp >> 1;
sclk = 0;
if(dsio)
temp = temp | 0x80;
sclk = 1;
}
rst = 0;
_nop_();
sclk = 1;
_nop_();
dsio = 1;
return temp;
}
void set_time()
{
unsigned char i, add;
write_ds1302(0x8e, 0x00);
for(i = 0, add = 0x80; i < 7; i++)
{
table_time = table_time / 10 * 16 + table_time % 10;
write_ds1302(add, table_time);
add = add + 2;
}
write_ds1302(0x8e, 0x80);
}
void read_time()
{
unsigned char i, add;
for(i = 0, add = 0x81; i < 7; i++)
{
table_time = read_ds1302(add);
add = add + 2;
}
table_rtc[7] = table_time[0] % 16;
table_rtc[6] = table_time[0] / 16;
table_rtc[5] = ':';
table_rtc[4] = table_time[1] % 16;
table_rtc[3] = table_time[1] / 16;
table_rtc[2] = ':';
table_rtc[1] = table_time[2] % 16;
table_rtc[0] = table_time[2] / 16;
}
void display()
{
unsigned char i;
write_1602com(0x80);
for(i = 0; i < 8; i++)
{
write_1602data(table_rtc);
}
}
void main()
{
init1602();
set_time();
while(1)
{
read_time();
display();
}
}
我没发现我哪里出错了,但是看结果应该是bcd转换那里有问题,但是我都是按照教程写的程序,而且我都写了两遍也没有什么问题,但是1602显示出来的就是乱码,就是那两个冒号能够显示出来,1602是我写完然后贴过来的,绝对没有问题,我就不写出来了
求大神帮忙看看