关于一个51单片机的简单问题,用下面程序读DS1302时钟芯片的时钟数据,在液晶1602显示时速度快了近十倍~!
#include<reg52.h>#defineucharunsignedchar#defineuintunsignedintsbiten=P2^4;//使能sbitrs...
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit en=P2^4;//使能
sbit rs=P2^2;//数据命令
sbit rw=P2^3;//读写
sbit DS_RST=P3^2;
sbit DS_IO=P3^3;
sbit DS_CLK=P1^7;
uchar shu[]={'0','1','2','3','4','5','6','7','8','9','-','D','a','y',':',' '};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//LCD1602初始化
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_datae(uchar datae)
{
rs=1;
P0=datae;
delay(5);
en=1;
delay(5);
en=0;
}
void init_LCD()//初始化LCD
{
en=0;
rw=0;
write_com(0x38);//显示模式控制
write_com(0x0c);//显示开关及光标控制
write_com(0x06);//数据指针设置
write_com(0x01);//清零
write_com(0x80);
}
uchar read_DS(uchar command)
{
uint i=0;
uchar zhi=0;
DS_RST=0;
DS_CLK=0;
DS_RST=1;
for(i=0;i<8;i++)
{
DS_IO=command&0x01;
DS_CLK=0;
DS_CLK=1;
command>>=1;
}
for(i=0;i<8;i++)
{
DS_CLK=1;
DS_CLK=0;
if(DS_IO)
zhi|=0x01<<i; //0x01<<i;zhi=zhi|0x01;<< 优先级
}
DS_RST=0;
zhi=(zhi/16*10)+zhi%16;
return zhi;
}
void write_DS(uchar command,uchar zhi)
{
uint j=0;
DS_RST=0;
DS_CLK=0;
DS_RST=1;
for(j=0;j<8;j++)
{
DS_IO=command&0x01;
DS_CLK=0;
DS_CLK=1;
command>>=1;
}
for(j=0;j<8;j++)
{
DS_IO=zhi&0x01;
DS_CLK=0;
DS_CLK=1;
zhi>>=1;
}
}
void init_DS()
{
write_DS(0x8E,0x00);//打开寄存器写入
write_DS(0x84,0x80);//设置24小时制 , 时间初始化
write_DS(0x8C,0x11);
write_DS(0x88,0x05);
write_DS(0x86,0x12);
write_DS(0x8A,0x04);
write_DS(0x84,0x21);
write_DS(0x82,0x33);
write_DS(0x80,0x00);//启动时钟
write_DS(0x8E,0x80);//关闭寄存器操作;
}
void main()
{
init_LCD();
init_DS();
while(1)
{
write_datae(shu[2]);
write_datae(shu[0]);
write_datae(shu[read_DS(0x8D)/10]);
write_datae(shu[read_DS(0x8D)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x89)/10]);
write_datae(shu[read_DS(0x89)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x87)/10]);
write_datae(shu[read_DS(0x87)%10]);
write_datae(shu[10]);
write_datae(shu[11]);
write_datae(shu[12]);
write_datae(shu[13]);
write_datae(shu[read_DS(0x8B)%10]);
write_com(0x80+0x40+0x03);
write_datae(shu[read_DS(0x85)/10]);
write_datae(shu[read_DS(0x85)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x83)/10]);
write_datae(shu[read_DS(0x83)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x81)/10]);
write_datae(shu[read_DS(0x81)%10]);
write_datae(shu[15]);
write_datae(shu[15]);
write_datae(shu[15]);
write_datae(shu[15]);
write_datae(shu[15]);
}
} 展开
#define uchar unsigned char
#define uint unsigned int
sbit en=P2^4;//使能
sbit rs=P2^2;//数据命令
sbit rw=P2^3;//读写
sbit DS_RST=P3^2;
sbit DS_IO=P3^3;
sbit DS_CLK=P1^7;
uchar shu[]={'0','1','2','3','4','5','6','7','8','9','-','D','a','y',':',' '};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//LCD1602初始化
void write_com(uchar com)
{
rs=0;
P0=com;
delay(5);
en=1;
delay(5);
en=0;
}
void write_datae(uchar datae)
{
rs=1;
P0=datae;
delay(5);
en=1;
delay(5);
en=0;
}
void init_LCD()//初始化LCD
{
en=0;
rw=0;
write_com(0x38);//显示模式控制
write_com(0x0c);//显示开关及光标控制
write_com(0x06);//数据指针设置
write_com(0x01);//清零
write_com(0x80);
}
uchar read_DS(uchar command)
{
uint i=0;
uchar zhi=0;
DS_RST=0;
DS_CLK=0;
DS_RST=1;
for(i=0;i<8;i++)
{
DS_IO=command&0x01;
DS_CLK=0;
DS_CLK=1;
command>>=1;
}
for(i=0;i<8;i++)
{
DS_CLK=1;
DS_CLK=0;
if(DS_IO)
zhi|=0x01<<i; //0x01<<i;zhi=zhi|0x01;<< 优先级
}
DS_RST=0;
zhi=(zhi/16*10)+zhi%16;
return zhi;
}
void write_DS(uchar command,uchar zhi)
{
uint j=0;
DS_RST=0;
DS_CLK=0;
DS_RST=1;
for(j=0;j<8;j++)
{
DS_IO=command&0x01;
DS_CLK=0;
DS_CLK=1;
command>>=1;
}
for(j=0;j<8;j++)
{
DS_IO=zhi&0x01;
DS_CLK=0;
DS_CLK=1;
zhi>>=1;
}
}
void init_DS()
{
write_DS(0x8E,0x00);//打开寄存器写入
write_DS(0x84,0x80);//设置24小时制 , 时间初始化
write_DS(0x8C,0x11);
write_DS(0x88,0x05);
write_DS(0x86,0x12);
write_DS(0x8A,0x04);
write_DS(0x84,0x21);
write_DS(0x82,0x33);
write_DS(0x80,0x00);//启动时钟
write_DS(0x8E,0x80);//关闭寄存器操作;
}
void main()
{
init_LCD();
init_DS();
while(1)
{
write_datae(shu[2]);
write_datae(shu[0]);
write_datae(shu[read_DS(0x8D)/10]);
write_datae(shu[read_DS(0x8D)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x89)/10]);
write_datae(shu[read_DS(0x89)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x87)/10]);
write_datae(shu[read_DS(0x87)%10]);
write_datae(shu[10]);
write_datae(shu[11]);
write_datae(shu[12]);
write_datae(shu[13]);
write_datae(shu[read_DS(0x8B)%10]);
write_com(0x80+0x40+0x03);
write_datae(shu[read_DS(0x85)/10]);
write_datae(shu[read_DS(0x85)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x83)/10]);
write_datae(shu[read_DS(0x83)%10]);
write_datae(shu[10]);
write_datae(shu[read_DS(0x81)/10]);
write_datae(shu[read_DS(0x81)%10]);
write_datae(shu[15]);
write_datae(shu[15]);
write_datae(shu[15]);
write_datae(shu[15]);
write_datae(shu[15]);
}
} 展开
3个回答
展开全部
有好程序可以到 中国电子开发网(www.ourdev.cn) 去晒一晒 我一直在那里潜水
我最近也在研究这个程 所以 我简单的看了一下你的程序 你是把DS1302的数据读出来直接就送了显示 像这样
write_datae(shu[read_DS(0x89)/10]);
write_datae(shu[read_DS(0x89)%10]);
我也习惯这样写程序 很好 但是冷不丁的看起来有点复杂 不过效果还是很好的
我最近也在研究这个程 所以 我简单的看了一下你的程序 你是把DS1302的数据读出来直接就送了显示 像这样
write_datae(shu[read_DS(0x89)/10]);
write_datae(shu[read_DS(0x89)%10]);
我也习惯这样写程序 很好 但是冷不丁的看起来有点复杂 不过效果还是很好的
展开全部
请教你以前怎么做,现在怎么做,使得显示速度提高了近十倍?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
{'0','1','2','3','4','5','6','7','8','9','-','D','a','y',':',' '};
追问
???????
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询