新手做时钟求助
时间:10-02
整理:3721RD
点击:
我想做一个时钟,先做个简单点的,直接给他设置一个初试时间,用1602显示出来,但是好像有错误,求大神解救!
代码在这
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6;
sbit rw=P2^5;
sbit en=P2^7;
sbit sclk=P3^6;
sbit io=P3^4;
sbit rst=P3^5;
uchar timew[7]={14,3,7,2,21,48,44};
uchar timer[7];
uchar biaow[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar biaor[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
//1602子程序
//void write_code(uchar cod)
//void write_date(uchar dat)
//void chushi(void)
//void display1602()
//void delay_50us(uint t)
//void delay_50ms(uint t)
//1302子程序
//void write1302s(uchar dat);
//void write1302(uchar add,uchar dat);
//void read1302(uchar add);
//void set(void);
//void read(void);
//
//
// 1602
//
//
void delay_50us(uint t)
{
uint j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6245;j>0;j--);
}
void write_code(uchar cod)
{
en=0;
rs=0;
rw=0;
P0=cod;
delay_50us(10);
en=1;
delay_50us(20);
en=0;
}
void write_date(uchar dat)
{
en=0;
rs=1;
rw=0;
P0=dat;
delay_50us(10);
en=1;
delay_50us(20);
en=0;
}
void chushi(void)
{
delay_50us(300);
write_code(0x38);
delay_50us(100);
write_code(0x38);
delay_50us(100);
write_code(0x38);
write_code(0x08);
write_code(0x01);
write_code(0x06);
write_code(0x0c);
}
void display1602()
{
uchar j;
write_code(0x80);
for(j=0;j<16;j++)
{
write_date(timer[j]);
delay_50us(10);
}
while(1);
}
//
// 1302
//
//
void write1302s(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
sclk=0;
io=dat&0x01;
dat=dat>>1;
sclk=1;
}
}
void write1302(uchar add,uchar dat)
{
rst=0;
_nop_();
sclk=0;_nop_();
rst=1;
_nop_();
write1302s(add);
write1302s(dat);
rst=0;
_nop_();
io=1;
sclk=1;
}
uchar read1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write1302s(add);
for(i=0;i<8;i++)
{
value=value>>1;
sclk=0;
if(io)
value=value|0x80;
sclk=1;
}
rst=0;
_nop_();
sclk=0;
_nop_();
sclk=1;
io=1;
return value;
}
void set(void)
{
uchar i,j;
for(i=0;i<7;i++)
{
j=timew[i]/10;
timew[i]=timew[i]%10;
timew[i]=timew[i]+j*16;
}
write1302(0x8e,0x80);
for(i=0;i<7;i++)
{
write1302(biaow[i],timew[i]);
}
write1302(0x8e,0x80);
}
void read_rtc(void)
{
uchar i;
for(i=0;i<7;i++)
{
timer[i]=read1302(biaor[i]);
}
}
void main(void)
{
set();
chushi();
while(1)
{
read_rtc();
diplay1602();
}
}
代码在这
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit rs=P2^6;
sbit rw=P2^5;
sbit en=P2^7;
sbit sclk=P3^6;
sbit io=P3^4;
sbit rst=P3^5;
uchar timew[7]={14,3,7,2,21,48,44};
uchar timer[7];
uchar biaow[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
uchar biaor[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
//1602子程序
//void write_code(uchar cod)
//void write_date(uchar dat)
//void chushi(void)
//void display1602()
//void delay_50us(uint t)
//void delay_50ms(uint t)
//1302子程序
//void write1302s(uchar dat);
//void write1302(uchar add,uchar dat);
//void read1302(uchar add);
//void set(void);
//void read(void);
//
//
// 1602
//
//
void delay_50us(uint t)
{
uint j;
for(;t>0;t--)
for(j=19;j>0;j--);
}
void delay_50ms(uint t)
{
uint j;
for(;t>0;t--)
for(j=6245;j>0;j--);
}
void write_code(uchar cod)
{
en=0;
rs=0;
rw=0;
P0=cod;
delay_50us(10);
en=1;
delay_50us(20);
en=0;
}
void write_date(uchar dat)
{
en=0;
rs=1;
rw=0;
P0=dat;
delay_50us(10);
en=1;
delay_50us(20);
en=0;
}
void chushi(void)
{
delay_50us(300);
write_code(0x38);
delay_50us(100);
write_code(0x38);
delay_50us(100);
write_code(0x38);
write_code(0x08);
write_code(0x01);
write_code(0x06);
write_code(0x0c);
}
void display1602()
{
uchar j;
write_code(0x80);
for(j=0;j<16;j++)
{
write_date(timer[j]);
delay_50us(10);
}
while(1);
}
//
// 1302
//
//
void write1302s(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
sclk=0;
io=dat&0x01;
dat=dat>>1;
sclk=1;
}
}
void write1302(uchar add,uchar dat)
{
rst=0;
_nop_();
sclk=0;_nop_();
rst=1;
_nop_();
write1302s(add);
write1302s(dat);
rst=0;
_nop_();
io=1;
sclk=1;
}
uchar read1302(uchar add)
{
uchar i,value;
rst=0;
_nop_();
sclk=0;
_nop_();
rst=1;
_nop_();
write1302s(add);
for(i=0;i<8;i++)
{
value=value>>1;
sclk=0;
if(io)
value=value|0x80;
sclk=1;
}
rst=0;
_nop_();
sclk=0;
_nop_();
sclk=1;
io=1;
return value;
}
void set(void)
{
uchar i,j;
for(i=0;i<7;i++)
{
j=timew[i]/10;
timew[i]=timew[i]%10;
timew[i]=timew[i]+j*16;
}
write1302(0x8e,0x80);
for(i=0;i<7;i++)
{
write1302(biaow[i],timew[i]);
}
write1302(0x8e,0x80);
}
void read_rtc(void)
{
uchar i;
for(i=0;i<7;i++)
{
timer[i]=read1302(biaor[i]);
}
}
void main(void)
{
set();
chushi();
while(1)
{
read_rtc();
diplay1602();
}
}
1602的程序我单独测试过好像没错的。
有没有整个文件包含仿真图
这倒是没有,我实在普中科技的板子上做的
读出来的1302时间数据要做处理再送到液晶显示!
读出来的是8位二进制数对吧,是不是要变为BCD码?
是的,处理了就行了
非常感谢分享~下下来学习一下~非常有用谢谢