8位数码管显示电子时钟c51单片机程序时间:2012-09-10 13:52:26 来源:作者:/*8位数码管显示时间格式 05—50—00 标示05点50分00秒S1 用于小时加1操作S2 用于小时减1操作S3 用于分钟加1操作S4 用于分钟减1操作*/#include<reg52.h>sbit KEY1=P3^0; //定义端口参数sbit KEY2=P3^1;sbit KEY3=P3^2;sbit KEY4=P3^3;sbit LED=P1^2; //定义指示灯参数code unsigned char tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0—9unsigned char StrTab[8]; //定义缓冲区unsigned char minute=19,hour=23,second; //定义并初始化为 12:30:00void delay(unsigned int cnt){while(--cnt);}/******************************************************************//* 显示处理函数*//******************************************************************/void Displaypro(void){StrTab[0]=tab[hour/10]; //显示小时StrTab[1]=tab[hour%10];StrTab[2]=0x40; //显示"-"StrTab[3]=tab[minute/10]; //显示分钟StrTab[4]=tab[minute%10];StrTab[5]=0x40; //显示"-"StrTab[6]=tab[second/10]; //显示秒StrTab[7]=tab[second%10];}main(){TMOD |=0x01; //定时器0 10ms inM crystal 用于计时TH0=0xd8; //初值TL0=0xf0;ET0=1;TR0=1;TMOD |=0x10; //定时器1用于动态扫描TH1=0xF8; //初值TL1=0xf0;ET1=1;TR1=1;EA =1;Displaypro(); //调用显示处理函数while(1){if(!KEY1) //按键1去抖以及动作{delay(10000);if(!KEY1){hour++;if(hour==24)hour=0; //正常时间小时加1 Displaypro();}}if(!KEY2) //按键2去抖以及动作{delay(10000);if(!KEY2){hour--;if(hour==255)hour=23; //正常时间小时减1 Displaypro();}}if(!KEY3) //按键去抖以及动作{delay(10000);if(!KEY3){minute++;if(minute==60)minute=0; //分加1Displaypro();}}if(!KEY4) //按键去抖以及动作{delay(10000);if(!KEY4){minute--;if(minute==255)minute=59; //分减1Displaypro();}}}}/******************************************************************//* 定时器1中断 */ /******************************************************************/void time1_isr(void) interrupt 3 using 0 //定时器1用来动态扫描{static unsigned char num;TH1=0xF8; //重入初值TL1=0xf0;switch (num){case 0:P2=0;P0=StrTab[num];break; //分别调用缓冲区的值进行扫描 case 1:P2=1;P0=StrTab[num];break;case 2:P2=2;P0=StrTab[num];break;case 3:P2=3;P0=StrTab[num];break;case 4:P2=4;P0=StrTab[num];break;case 5:P2=5;P0=StrTab[num];break;case 6:P2=6;P0=StrTab[num];break;case 7:P2=7;P0=StrTab[num];break;default:break;}num++; //扫描8次,使用8个数码管if(num==8)num=0;}/******************************************************************//* 定时器0中断 */ /******************************************************************/void tim(void) interrupt 1 using 1{static unsigned char count; //定义内部局部变量TH0=0xd8; //重新赋值TL0=0xf0;count++;switch (count){case 0:case 20:case 40:case 60:case 80:Displaypro();break; //隔一定时间调用显示处理case 50:P1=~P1;break; //半秒 LED 闪烁default:break;}if (count==100){count=0;second++; //秒加1if(second==60){second=0;minute++; //分加1if(minute==60){minute=0;hour++; //时加1if(hour==24)hour=0;}}}}基于单片机的LCD1602控制总线模式时间:2012-09-10 13:50:39 来源:作者:第一行显示"Welcome";第二行显示="Happy day";若要显示其他字符,请直接往数组LCMLineOne[16]和LCMLineTwo[16]填充相应的代码。
直接上图,仿真图如下:源程序如下,可以对比时序方式,理解总线的操作方法。
#include<reg51.h>//#include<absacc.h>#define uchar unsigned char#define uint unsigned int#define busy 0x80uchar xdata LCMWriteCOM _at_ 0x80ff; //写指令寄存器uchar xdata LCMReadCOM _at_ 0xa0ff ; //读指令寄存器uchar xdata LCMWriteData _at_ 0xc0ff ; //写数据寄存器uchar xdata LCMReadData _at_ 0xe0ff ; //读数据寄存器uchar data LCMLineOne[16]="Welcome"; //第一行显示的数据uchar data LCMLineTwo[16]="Happy day"; //第二行显示的数据void Delayms(uchar ms){uchar i,j;for(i=0;i<ms;i++)for(j=0;j<57;j++);}//写指令寄存器void LCMWriteC(uchar COMData){ uchar LCMStatus;do{LCMStatus=(LCMReadCOM&busy);}while(LCMStatus!=0);LCMWriteCOM=COMData;}//读指令寄存器uchar LCMReadC(){uchar LCMStatus;do{LCMStatus=(LCMReadCOM&busy);}while(LCMStatus!=0);LCMStatus=LCMReadCOM;return(LCMStatus);}//读数据寄存器uchar LCMReadD(uchar addr){uchar LCMStatus;do{LCMStatus=(LCMReadCOM&busy);}while(LCMStatus!=0);LCMWriteC(0x80+addr);LCMStatus=LCMReadData;return(LCMStatus);}//写数据寄存器带地址void LCMWriteDAdd(uchar addr,uchar LCMData) {uchar LCMStatus;do{LCMStatus=(LCMReadCOM&busy);}while(LCMStatus!=0);LCMWriteC(0x80+addr);LCMWriteData=LCMData;}//写数据寄存器无地址void LCMWriteD(uchar LCMData){uchar LCMStatus;do{LCMStatus=(LCMReadCOM&busy);}while(LCMStatus!=0);LCMWriteData=LCMData;}//初始化void LCMInit(void){ Delayms(15);LCMWriteCOM=0x38;Delayms(5);LCMWriteCOM=0x38;Delayms(5);LCMWriteCOM=0x38;Delayms(5);LCMWriteC(0x38);LCMWriteC(0x08);LCMWriteC(0x01);LCMWriteC(0x06);LCMWriteC(0x0c);}main(){ uchar i;LCMInit();while(1){ LCMWriteC(0x80); //第一行开始地址 for(i=0;i<16;i++)LCMWriteD(LCMLineOne[i]);LCMWriteC(0x80+0x40); //第二行开始地址 for(i=0;i<16;i++)LCMWriteD(LCMLineTwo[i]);} }。