數碼管顯示實時時鐘的資料正常,但是在1602顯示的時候只用了秒位,顯示的資料就出錯。
時鐘函式:
#include<reg52.h>
#include<intrins.h>
#include "menukey.h"
#include "yanshi.h"
#include "led.h"
#include "lcd.h"
#include"clock.h"
sbit ce=P2^5;
sbit sclk=P2^3;
sbit io=P2^4;
sbit we=P2^7;
void write(unsigned char dz,unsigned char sj)
{
unsigned char i;
ce=0;
sclk=0;
ce=1;
for(i=0;i<8;i++)
{
sclk=0;
io=dz&0x01;
sclk=1;
dz>>=1;
}
for(i=0;i<8;i++)
{
sclk=0;
io=sj&0x01;
sclk=1;
sj>>=1;
}
}
char read(unsigned char dz)
{
unsigned char i,sj;
ce=0;
sclk=0;
ce=1;
for(i=0;i<8;i++)
{
sclk=0;
io=dz&0x01;
sclk=1;
dz>>=1;
}
for(i=0;i<8;i++)
{
sclk=0;
sj>>=1;
if(io) sj|=0x80;
sclk=1;
}
return sj;
}
unsigned char bcdz(unsigned char dat)
{
unsigned char dat1,dat2;
dat1=dat/16;
dat2=dat%16;
dat2=dat2+dat1*10;
return dat2;
}
unsigned char zbcd(unsigned char dat)
{
unsigned char dat1,dat2;
dat1=dat/10;
dat2=dat%10;
dat2=dat2+dat1*16;
return dat2;
}
void clock()
{
unsigned char i;
static char x,y,z;
unsigned char a1,a2,b1,b2,c1,c2;
write(0x8e,0);
write(0x80,zbcd(12));
write(0x82,zbcd(00));
write(0x84,zbcd(00));
write(0x8e,0x80);
while(1)
{
write(0x8e,0);
x=bcdz(read(0x81));
y=bcdz(read(0x83));
z=bcdz(read(0x85));
write(0x8e,0x80);
c1=x/10;
c2=x%10;
b1=y/10;
b2=y%10;
a1=z/10;
a2=z%10;
for(i=0;i<50;i++);
lcd(c1,c2,a1,a2,b1,b2);
}
}
1602函式:
#include<reg52.h>
#include "menukey.h"
#include "yanshi.h"
#include "lcd.h"
sbit rs=P3^5;
sbit rw=P3^6;
sbit en=P3^4;
sbit we=P2^7;
void busy()
{
unsigned char Busy;
P0=0xff;
rs=0;
rw=1;
do
{
en=1;
Busy=P0;
en=0;
}while(Busy&0x80);
}
void write_zl(unsigned zl)
{
busy();
rs=0;
rw=0;
P0=zl;
en=1;
en=0;
}
void write_sj(unsigned sj)
{
busy();
rs=1;
rw=0;
P0=sj;
en=1;
en=0;
}
void lcd(unsigned char a1,unsigned char a2,unsigned char b1,unsigned char b2,unsigned char c1,unsigned char c2)
{
// unsigned char a[9]={1,2,3,4,5,6,7,8,9};
unsigned char i;
we=1;
P0=0xff;
we=0;
while(1)
{
we=1;
P0=0xff;
we=0;
write_zl(0x38);
write_zl(0x0c);
write_zl(0x01);
write_zl(0x06);
write_zl(0x80 | 0x00);
write_sj(116);
write_sj(105);
write_sj(109);
write_sj(101);
write_sj(58);
write_sj(a1+'0');
write_sj(a2+'0');
delay(3000);
i++;
if(i==10)
i=0;
}
write_zl(0x01);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/89122.html
標籤:單片機/工控
