这是用单片机仿真软件做的一个电子时钟程序 电路图在下面:
#include<reg52.h>
#include<intrins.h>
#include<stdlib.h>
#define uint unsigned int
#define uchar unsigned char
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
sbit BF=P0^7;
sbit d=P2^3;
unsigned char code string[]={"Test Result"};
unsigned char code digit[]={"0123456789"};
void delay1ms()
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<33;j++);
}
void delay(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
unsigned char busy(void)//判忙函数
{
bit result;
RS=0;
RW=1;
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
result=BF;
E=0;
return result;
}
void writeinstruction(uchar dictate)//写指令
{
while(busy()==1);
RS=0;
RW=0;
E=0;
_nop_();
_nop_();
P0=dictate;
_nop_();
_nop_();
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
E=0;
}
void writeaddress(uchar x)//写地址
{
writeinstruction(x|0x80);
}
void wretedata(uchar y)//写数据
{
while(busy()==1);
RS=1;
RW=0;
E=0;
P0=y;
_nop_();
_nop_();
_nop_();
_nop_();
E=1;
_nop_();
_nop_();
_nop_();
_nop_();
E=0;
}
void init(void)//初始化
{
d=0;
delay(15);
writeinstruction(0x38);//工作方式:8位总线,2行,5*7点阵
delay(5);
writeinstruction(0x38);//工作方式:8位总线,2行,5*7点阵
delay(5);
writeinstruction(0x38);//工作方式:8位总线,2行,5*7点阵
delay(5);
writeinstruction(0x0f);//显示状态:显示开,有光标,光标闪烁
delay(5);
writeinstruction(0x06);//光标移动:光标右移,字符不移
delay(5);
writeinstruction(0x01);
delay(5);
}
void main(void)
{
unsigned char D1,D2,D3,D4,D5; //定义变量i指向字符串数组元素
unsigned int x; //定义变量,储存检测结果
unsigned char i;
init();
delay(10);
writeaddress(0x02);
i=0;
while(string[i]!=0)
{
wretedata(string[i]);
i++;
}
while(1)
{
x=rand();
D1=x%10;
D2=x%10000%1000%100/10;
D3=x%10000%1000/100;
D4=x%10000/1000;
D5=x/10000;
writeaddress(0x45);
wretedata(digit[D5]);
wretedata(digit[D4]);
wretedata(digit[D3]);
wretedata(digit[D2]);
wretedata('.');
wretedata(digit[D1]);
for(i=0;i<4;i++)
delay(250);
}
}