C51单片机制作数码管显示数字时钟————可调C源程序

合集下载

8位数码管显示电子时钟c51单片机程序

8位数码管显示电子时钟c51单片机程序

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]填充相应的代码。

51单片机数码管显示时钟(C语言)

51单片机数码管显示时钟(C语言)

* 输出
:无
*******************************************************************************
/
void main() {
Ds1302Init(); Timer0Configuration(); while(1) {
Ds1302ReadTime(); disp[7] = DIG_CODE[TIME[0]&0x0f]; disp[6] = DIG_CODE[TIME[0]>>4]; disp[5] = 0X40; disp[4] = DIG_CODE[TIME[1]&0x0f]; disp[3] = DIG_CODE[TIME[1]>>4]; disp[2] = 0X40; disp[1] = DIG_CODE[TIME[2]&0x0f]; disp[0] = DIG_CODE[TIME[2]>>4]; }
/
void DigDisplay() interrupt 1 { //定时器在工作方式二会自动重装初,所以不用在赋值。 // TH0=0X9c;//给定时器赋初值,定时 1ms // TL0=0X00;
DIG=0; //消隐 switch(Num) //位选,选择点亮的数码管, {
case(7): LSA=0;LSB=0;LSC=0; break;
uchar n;
EA = 0; RST = 0; _nop_();
SCLK = 0;//先将 SCLK 置低电平。 _nop_(); RST = 1; //然后将 RST(CE)置高电平。 _nop_();
for (n=0; n<8; n++)//开始传送八位地址命令 {

51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)

51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)

51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)效果图:程序如下://51单片机c语言电子钟(已加入调时、闹铃、整点报时功能)#include<reg51.h>#include<absacc.h>#define uchar unsigned char#define uint unsigned int/*七段共阴管显示定义*///此表为LED 的字模,共阴数码管0-9 -uchar code dispcode[] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //段码控制/*定义并初始化变量*/uchar seconde=0;//秒uchar minite=0;//分uchar hour=12; //时uchar shi=0;//闹铃功能uchar fen=0;uchar bjcs;//报警次数sbit P1_0=P1^0; //second 调整定义sbit P1_1=P1^1; //minite调整定义sbit P1_2=P1^2; //hour调整定义sbit P1_5=P1^5; //整点报时sbit P1_3=P1^3; //闹铃功能,调整时间sbit P1_6=P1^6; //调整时sbit P1_7=P1^7; //调整分sbit P1_4=P1^4; //关闭闹铃/*函数声明*/void delay(uint k ); //延时子程序void time_pro( ); //时间处理子程序void display( ); //显示子程序void keyscan( ); //键盘扫描子程序/*xx子程序*/void delay (uint k){uchar j;while((k--)!=0){for(j=0;j<125;j++){;}}}/*时间处理子程序*/void time_pro(void){if(seconde==60){seconde=0;minite++;if(minite==60){minite=0;hour++;if(hour==24){hour=0;}}}}/*显示子程序*/void display(void){if(P1_3==1){P2=0XFE; P0=dispcode[seconde%10];//秒个位delay(1);P2=0XFD;P0=dispcode[seconde/10];//秒十位delay(1);P2=0XFB;P0=dispcode[10];//间隔符-delay(1);P2=0XF7;P0=dispcode[minite%10];//分个位delay(1);P2=0XEF;P0=dispcode[minite/10];//分十位delay(1);P2=0XDF;P0=dispcode[10];//间隔符-delay(1);P2=0XBF;P0=dispcode[hour%10];//时个位delay(1);P2=0X7F;P0=dispcode[hour/10];//时十位delay(1);}}/*键盘扫描子程序*/void keyscan(void){if(P1_0==0)//秒位的调整{delay (30);if(P1_0==0){seconde++;if(seconde==60){seconde=0;}}delay(250);}if(P1_1==0)//分位的调整{delay(30);if(P1_1==0){minite++;if(minite==60){minite=0;}}delay(250);}if(P1_2==0)//时位的调整{delay(30);if(P1_2==0){hour++;if(hour==24){hour=0;}}delay(250);}}/*整点报警*/void zhengdian (void){if((seconde==0)&(minite==0))//整点报时{P1_5=0;delay(1000);P1_5=1;}}/*定时闹钟*/void dingshi(void){if(P1_3==0)//按住P1_3BU不松,显示闹铃设置界面,分别按P1_6、P1_7设置闹铃时间。

基于c51功能最全的电子钟程序(lcd1602)

基于c51功能最全的电子钟程序(lcd1602)

功能最全的电子钟【单片机】c51数字时钟(带年月日显示)摘要:本设计以单片机为核心,lcd1602显示。

采用独立键盘输入能任意修改当前时间日期和设定闹钟时间。

具有显示年月日(区分闰年和二月),闹钟报警和整点报时功能主程序:/********************************************************************************************************************************************************************************* ****************************************** lcd1602电子钟********************************************************************************************************************************** *************************************************************************************************** ********************/# include <reg52.h># include "lcd16024.h"sbit key1 = P2^0; //调整sbit key2 = P2^1; //加1sbit key3 = P2^2; //减1sbit speaker = P2^3; //蜂鸣器sbit key4 = P2^4; //闹钟设计bit cal_year = 1; //进入判断闰年标志位bit leap_year; //闰年标志位bit calculate = 0; //日加一标记bit run = 0; //闹钟标志bit beep = 0; //整点报时标志//uint8 num = 0; //调整是给的脉冲uint8 code str1[] = "D: ";uint8 code str2[] = "T: ";uint8 code str3[] = "Wek";uint8 daystr[]="2013-07-29 "; //年月日格式uint8 timestr[]="21:30:59 N"; //时分秒格式uint8 daystr1[]="2013-07-29 "; //闹钟年月日格式uint8 timestr1[]="21:30:59 N"; //闹钟时分秒格式uint8 numweek = 0; //星期加1标记char week = 1; //星期char sec = 53; //秒char min = 50; //分char hour = 23; //时uint8 day = 30; // 日uint8 month = 9; //月uint16 year = 2013; //年char week1 = 1; //闹钟星期char sec1 = 58; //闹钟秒char min1 = 50; //闹钟分char hour1 = 23; //闹钟时uint8 day1 = 30; //闹钟日uint8 month1 = 9; //闹钟月uint16 year1 = 2013; //闹钟年uint8 WeekData1; //闹钟星期标记uint8 number = 0; //定时uint8 WeekData; //星期标记uint8 speaker_num; //整点报时次数uint8 scan_key(void); //函数声名/****************************************************************************** ***************************** 更新LCD时间分离读取******************************************************************************************************************* *******/void TimeChange(){//时分秒timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';timestr[4] = min%10+'0';timestr[3] = min/10+'0';timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';//年月日daystr[9] = day%10+'0';daystr[8] = day/10+'0';daystr[6] = month%10+'0';daystr[5] = month/10+'0';daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';//星期WeekData = week+'0';}/****************************************************************************** ***************************** 闹钟更新LCD时间分离读取******************************************************************************************************************* *******/void TimeChange1(){//时分秒timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';//年月日daystr1[9] = day1%10+'0';daystr1[8] = day1/10+'0';daystr1[6] = month1%10+'0';daystr1[5] = month1/10+'0';daystr1[3] = year1%10+'0';daystr1[2] = year1/10%10+'0';daystr1[1] = year1/100%10+'0';daystr1[0] = year1/1000+'0';//星期WeekData1 = week1+'0';}/****************************************************************************** ***************************** 初始化系统定时器0 ******************************************************************************************************************* *******/void systimer0_init(void){TMOD |=0x01;//设置为1时用或(|)TMOD &=0xfd;//设置为0时用与(&)TH0 = 0xDC; // 定时10msTL0 = 0x00;EA = 1;ET0=1;TR0=1;EX0 = 1;}/****************************************************************************** ***************************** 闹钟时间设置*************************************************************************************************************** *******/void naozhong(){uint8 number = 1;uint8 a = 0;uint8 b = 0;if(0 == key4){Delay1Ms(5);if(0 == key4){a = 1;LCD_write_command(0xc0+9);LCD_write_command(0x0f);}while(!key4);}while(a){if(0 == key4){Delay1Ms(5);if(0 == key4){a = 0;run = ~run;while(!key4);}}if(run){timestr[9] = 'Y';}else{timestr[9] = 'N';}if(0 == key1){Delay1Ms(5);if(0 == key1){b = 1;}while(!key1);}while(b){if(0 == key1){Delay1Ms(5);if(0 == key1){number++;if(4 == number)b = 0;}while(!key1);if(number == 1){LCD_write_command(0xc0+9);LCD_write_command(0x0f);}if(number == 2){LCD_write_command(0xc0+6);LCD_write_command(0x0f);}if(number == 3){LCD_write_command(0xc0+3);LCD_write_command(0x0f);}}}switch(number){case 1:if(0 == key2) //闹钟秒加1的设置{Delay1Ms(5);if(0 == key2){sec1++;if(60 == sec1){sec1 = 0;}while(!key2);timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';LCD_write_char(8,1,timestr1[6]);LCD_write_char(9,1,timestr1[7]);LCD_write_command(0xc0+9);}}if(0 == key3) //闹钟秒减1的设置Delay1Ms(5);if(0 == key3){sec1--;if(sec1 < 0){sec1 = 59;}while(!key3);timestr1[7] = sec1%10+'0';timestr1[6] = sec1/10+'0';LCD_write_char(8,1,timestr1[6]);LCD_write_char(9,1,timestr1[7]);LCD_write_command(0xc0+9);}}break;case 2:if(0 == key2) //闹钟分加1的设置{Delay1Ms(5);if(0 == key2){min1++;if(60 == min1){min1 = 0;}while(!key2);timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';;LCD_write_char(5,1,timestr1[3]);LCD_write_char(6,1,timestr1[4]);LCD_write_command(0xc0+6);}}if(0 == key3) //闹钟分减1的设置{Delay1Ms(5);if(0 == key3){min1--;if(min1 < 0){min1 = 59;}while(!key3);timestr1[4] = min1%10+'0';timestr1[3] = min1/10+'0';;LCD_write_char(5,1,timestr1[3]);LCD_write_char(6,1,timestr1[4]);LCD_write_command(0xc0+6);}}break;case 3:if(0 == key2) //闹钟时加1的设置{Delay1Ms(5);if(0 == key2){hour1++;if(24 == hour1){hour1 = 0;}while(!key2);timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';LCD_write_char(2,1,timestr1[0]);LCD_write_char(3,1,timestr1[1]);LCD_write_command(0xc0+3);}}if(0 == key3) //闹钟时减1的设置{Delay1Ms(5);if(0 == key3){hour1--;if(hour1 < 0){hour1 = 23;}while(!key3);timestr1[1] = hour1%10+'0';timestr1[0] = hour1/10+'0';LCD_write_char(2,1,timestr1[0]);LCD_write_char(3,1,timestr1[1]);LCD_write_command(0xc0+3);}}break;case 4:b = 0;LCD_write_command(0x0c);break;}}}LCD_write_command(0x0c);while(!key4);}/****************************************************************************** ***************************** 判断按键进入时间调整*************************************************************************************************************** *******/uint8 scan_key(void){uint8 number = 1;uint8 a = 0;if(0 == key1){Delay1Ms(5);if(0 == key1){while(!key1);a = 1;LCD_write_command(0xc0+9);LCD_write_command(0x0f);}}while(a){if(0 == key1){Delay1Ms(5);if(0 == key1){number++;while(!key1);TR0 = 0;if(number == 2){LCD_write_command(0xc0+6);LCD_write_command(0x0f);}if(number == 3){LCD_write_command(0xc0+3);LCD_write_command(0x0f);}if(number == 4){LCD_write_command(0x80+11);LCD_write_command(0x0f);}if(number == 5){LCD_write_command(0x80+8);LCD_write_command(0x0f);}if(number == 6){LCD_write_command(0x80+5);LCD_write_command(0x0f);}if(number == 7){LCD_write_command(0xc0+14);LCD_write_command(0x0f);}if(8 == number){LCD_write_command(0x0c);a = 0;number = 0;}}}switch(number){case 1:if(0 == key2) //秒加1的设置{Delay1Ms(5);if(0 == key2){sec++;if(60 == sec){sec = 0;}timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';LCD_write_char(8,1,timestr[6]);LCD_write_char(9,1,timestr[7]);LCD_write_command(0xc0+9);while(!key2);}}if(0 == key3) //秒减1的设置{Delay1Ms(5);if(0 == key3){sec--;if(sec < 0){sec = 59;}timestr[7] = sec%10+'0';timestr[6] = sec/10+'0';LCD_write_char(8,1,timestr[6]);LCD_write_char(9,1,timestr[7]);LCD_write_command(0xc0+9);while(!key3);}}break;case 2:if(0 == key2) //分加1的设置{Delay1Ms(5);if(0 == key2){min++;if(60 == min){min = 0;}timestr[4] = min%10+'0';timestr[3] = min/10+'0';;LCD_write_char(5,1,timestr[3]);LCD_write_char(6,1,timestr[4]);LCD_write_command(0xc0+6);while(!key2);}}if(0 == key3) //分减1的设置{Delay1Ms(5);if(0 == key3){min--;if(min < 0){min = 59;}timestr[4] = min%10+'0';timestr[3] = min/10+'0';LCD_write_char(5,1,timestr[3]);LCD_write_char(6,1,timestr[4]);LCD_write_command(0xc0+6);while(!key3);}}break;case 3:if(0 == key2) //时加1的设置{Delay1Ms(5);if(0 == key2){hour++;while(!key2);if(24 == hour){hour = 0;}timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';LCD_write_char(2,1,timestr[0]);LCD_write_char(3,1,timestr[1]);LCD_write_command(0xc0+3);}}if(0 == key3) //时减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);hour--;if(hour < 0){hour = 23;}timestr[1] = hour%10+'0';timestr[0] = hour/10+'0';LCD_write_char(2,1,timestr[0]);LCD_write_char(3,1,timestr[1]);LCD_write_command(0xc0+3);}}break;case 4:if(0 == key2) //日加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);calculate = 1;if(calculate == 1){if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day++;if(day > 31){day=1;}}if(month==4|month==6|month==9|month==11){day++;if(day > 30){day=1;}}if(month == 2){cal_year = 1;while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day++;if(day > 30){day=1;}}else{day++;if(day > 29){day=1;}}}calculate = 0;}daystr[9] = day%10+'0';daystr[8] = day/10+'0';LCD_write_char(10,0,daystr[8]);LCD_write_char(11,0,daystr[9]);LCD_write_command(0x80+11);}}if(0 == key3) //日减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);calculate = 1;if(calculate == 1){if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day--;if(day == 0){day=31;}}if(month==4|month==6|month==9|month==11){day--;if(day == 0){day=30;}}if(month == 2){cal_year = 1;while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day--;if(day == 0){day=30;}}else{day--;if(day == 0){day=29;}}}calculate = 0;}daystr[9] = day%10+'0';daystr[8] = day/10+'0';LCD_write_char(10,0,daystr[8]);LCD_write_char(11,0,daystr[9]);LCD_write_command(0x80+11);}}break;case 5:if(0 == key2) //月加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);month++;if(13 == month){month = 1;}daystr[6] = month%10+'0';daystr[5] = month/10+'0';LCD_write_char(7,0,daystr[5]);LCD_write_char(8,0,daystr[6]);LCD_write_command(0x80+8);}}if(0 == key3) //月减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);month--;if(month == 0){month = 12;}daystr[6] = month%10+'0';daystr[5] = month/10+'0';LCD_write_char(7,0,daystr[5]);LCD_write_char(8,0,daystr[6]);LCD_write_command(0x80+8);}}break;case 6:if(0 == key2) //年加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);year++;}daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';LCD_write_char(2,0,daystr[0]);LCD_write_char(3,0,daystr[1]);LCD_write_char(4,0,daystr[2]);LCD_write_char(5,0,daystr[3]);LCD_write_command(0x80+5);}if (0 == key3) //年减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);year--;if(year == 0){year = 2020;}daystr[3] = year%10+'0';daystr[2] = year/10%10+'0';daystr[1] = year/100%10+'0';daystr[0] = year/1000+'0';LCD_write_char(2,0,daystr[0]);LCD_write_char(3,0,daystr[1]);LCD_write_char(4,0,daystr[2]);LCD_write_char(5,0,daystr[3]);LCD_write_command(0x80+5);}}break;case 7:if(0 == key2) //星期加1的设置{Delay1Ms(5);if(0 == key2){while(!key2);week++;if(7 == week){week = 0;}LCD_write_char(14,1,week+'0');LCD_write_command(0xc0+14);}}if (0 == key3) //星期减1的设置{Delay1Ms(5);if(0 == key3){while(!key3);week--;if(week < 0){week = 6;}LCD_write_char(14,1,week+'0');LCD_write_command(0xc0+14);}}break;case 8:TR0 = 1;break;}}}/****************************************************************************** ***************************** 主函数******************************************************************************************************************* *******/void main(void){systimer0_init();LCD_init();LCD_write_str(0,0,str1);LCD_write_str(0,1,str2);LCD_write_str(13,0,str3);speaker = 0;while (1){TimeChange();scan_key();naozhong();LCD_write_str(2,0,daystr);LCD_write_str(2,1,timestr);LCD_write_char(14,1,WeekData);if(1 == run){if(sec==sec1 & month==month1 & hour==hour){speaker_num =30;beep = 1;}}else{speaker_num =0;beep = 0;}}}/****************************************************************************** ***************************** 定时中断0 ******************************************************************************************************************* *******/void time_0() interrupt 1{TH0 = 0xDC; // 定时10msTL0 = 0x00;number++;if(number ==100){sec++;if(beep){speaker=!speaker;speaker_num--;if(speaker_num == 0){beep=0;speaker = 0;}}if(sec == 60){sec = 0;min++;if(min == 60){min = 0;hour++; //小时加1speaker_num = hour%12; //蜂鸣器响的次数beep = 1;if(hour == 24){hour = 0;calculate = 1;if(calculate == 1) //判断这个月有多少天{if(month==1|month==3|month==5|month==7|month==8|month==10|month==12){day++;if(month==7|month==12){week++;if(7 == week){week = 0;}}else{if(day <= 31){week++;if(7 == week){week = 0;}}}if(day > 31){if(month==7|month==12){day = 1;}else{day=0;}month++;if(month > 12){month=1;year++;cal_year=1;}}}if(month==4|month==6|month==9|month==11) {day++;week++;if(7 == week){week = 0;}if(day > 30){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}if(month == 2){while(cal_year == 1){leap_year = ((year % 4 == 0 && year % 100 != 0)||(year % 400 == 0));cal_year = 0;}if(leap_year==1){day++;if(day <= 30){week++;if(7 == week){week = 0;}}if(day > 30){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}else{day++;week++;if(7 == week){week = 0;}if(day > 29){day=1;month++;if(month > 12){month=1;year++;cal_year=1;}}}}}calculate=0;}}}}}////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Lcd1602.c子程序:////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /****************************************************************************** ***************************** lcd1602低层函数********************************************************************************************************************* *******/# include <reg52.h># include <intrins.h># include "lcd16024.h"# define LCD_DATA P0 //LCD1602的数据口定义sbit LCD_RS = P0^0; //LCD1602控制线的定义,4位控制方式sbit LCD_RW = P0^1;sbit LCD_EN = P0^2;/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void Delay1Us(uint16 n){for(;n>0;n--){_nop_();}}/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void Delay1Ms(uint16 n){while(n--){Delay1Us(1000);}}/****************************************************************************** ***************************** 延时1MS********************************************************************************************************************* *******/void LCD_en_write(void){LCD_EN=0;LCD_EN=1;Delay1Us(1);LCD_EN=0;}/****************************************************************************** ************************ LCD写一个字节命令函数************************************************************************************************************ *******/void LCD_write_command(uint8 command){Delay1Us(16);LCD_RS=0;LCD_RW=0;LCD_DATA&=0x0f;LCD_DATA|=command&0xf0;LCD_en_write();command=command<<4;LCD_DATA&=0x0f;LCD_DATA|=command&0xf0;LCD_en_write();}/****************************************************************************** ********************** LCD写一个字节数据函数*************************************************************************************************************** *******/void LCD_write_data(uint8 Data){Delay1Us(16);LCD_RS=1;LCD_RW=0;LCD_DATA&=0x0f;LCD_DATA|=Data&0xf0;LCD_en_write();Data=Data<<4;LCD_DATA&=0x0f;LCD_DATA|=Data&0xf0;LCD_en_write();}/****************************************************************************** ***************************** LCD1602光标定位函数************************************************ x--列0~15;y--行0~1********************************************************************************************************* *******/void LCD_set_xy(uint8 x,uint8 y){uint8 address;if(y==0)address=0x80+x;else address=0xc0+x;LCD_write_command(address);}/***************************************************************************************************** LCD1602 初始化函数,四位显示方式******************************************************************************************************* *******/void LCD_init(){LCD_write_command(0x28);// Delay1Us(40);LCD_write_command(0x28);LCD_write_command(0x0c);LCD_write_command(0x01);LCD_write_command(0x06);Delay1Ms(2);}/****************************************************************************** ***************************** LCD写字符串函数******************************************************* x--列0~15;y--行0~1******************************************************* s指向字符串数组**************************************************************************************************************** *******/void LCD_write_str(uint8 x,uint8 y,uint8 *s){LCD_set_xy(x,y);while(*s){LCD_write_data(*s);s++;}}/****************************************************************************** ***************************** LCD写一个字符函数******************************************************* x--列0~15;y--行0~1******************************************************* d--字符的ASCII码**************************************************************************************************************** *******/void LCD_write_char(uint8 x,uint8 y,uint8 d){LCD_set_xy(x,y);LCD_write_data(d);}/*////////////////////////////////////////////////////////////////// 等待繁忙标志/////////////////////////////////////////////////////////////////void LCD_wait(void){P0 = 0xFF;do{LCD_RS = 0;LCD_RW = 1;LCD_EN = 0;LCD_EN = 1;}while (BUSY == 1);LCD_EN = 0;}*//****************************************************************************** ***************************** LCD1602左移********************************************************************************************************************* *******void LCD_youyi(uint8 y,uint8 *s){LCD_write_str(17,y,s);for(a=0;a<16;a++){LCD_write_command(0x1c); //左移LCD_write_command(0x1c); 为右移Delay1Ms(6);}}*///LCD_write_command(0x0d);//光标闪烁//LCD_write_command(0x0e);//光标显示不闪烁//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// Lcd1602.h头文件////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// #ifndef _LCD16024_H_#define _LCD16024_H_typedef unsigned char uint8;typedef unsigned int uint16;typedef unsigned long uint32;void Delay1Us(uint16 n);void Delay1Ms(uint16 n);void LCD_write_data(uint8 dat);void LCD_write_command(uint8 com); //BuysC为0时忽略忙检测void LCD_set_xy(uint8 x,uint8 y);void LCD_en_write(void);void LCD_write_char(uint8 x, uint8 y, uint8 Data1);void LCD_write_str(uint8 x, uint8 y,uint8 *s);void LCD_init();//void LCD_wait(void);//void LCD_youyi(uint8 y,uint8 *s);#endif。

基于C51单片机的数字时钟课程设计(C语言带闹钟)

基于C51单片机的数字时钟课程设计(C语言带闹钟)

单片机技术课程设计数字电子钟学院:班级:姓名:学号:教师:摘要电子钟在生活中应用非常广泛,而一种简单方便的数字电子钟则更能受到人们的欢迎。

所以设计一个简易数字电子钟很有必要。

本电子钟采用AT89C52单片机为核心,使用12MHz 晶振与单片机AT89C52 相连接,通过软件编程的方法实现以24小时为一个周期,同时8位7段LED数码管(两个四位一体数码管)显示小时、分钟和秒的要求,并在计时过程中具有定时功能,当时间到达提前定好的时间进行蜂鸣报时。

该电子钟设有四个按键KEY1、KEY2、KEY3、KEY4和KEY5键,进行相应的操作就可实现校时、定时、复位功能。

具有时间显示、整点报时、校正等功能。

走时准确、显示直观、运行稳定等优点。

具有极高的推广应用价值。

关键词:电子钟 AT89C52 硬件设计软件设计目录NO TABLE OF CONTENTS ENTRIES FOUND.一、数字电子钟设计任务、功能要求说明及方案介绍1.1 设计课题设计任务设计一个具有特定功能的电子钟。

具有时间显示,并有时间设定,时间调整功能。

1.2 设计课题的功能要求说明设计一个具有特定功能的电子钟。

该电子钟上电或按键复位后能自动显示系统提示符“d.1004-22”,进入时钟准备状态;第一次按电子钟启动/调整键,电子钟从12时59分0秒开始运行,进入时钟运行状态;按电子钟S5键,则电子钟进入时钟调整状态,此时可利用各调整键调整时间,调整结束后可按S5键再次进入时钟运行状态。

1.3 设计课的设计总体方案介绍及工作原理说明本电子钟主要由单片机、键盘、显示接口电路和复位电路构成,设计课题的总体方案如图1所示:图1-1总体设计方案图本电子钟的所有的软件、参数均存放在AT89C52的Flash ROM和内部RAM 中,减少了芯片的使用数量简化了整体电路也降低了整机的工作电流。

键盘采用动态扫描方式。

利用单片机定时器及计数器产生定时效果通过编程形成数字钟效果,再利用数码管动态扫描显示单片机内部处理的数据,同时通过端口读入当前外部控制状态来改变程序的不同状态,实现不同功能。

数字钟51单片机程序c语言

数字钟51单片机程序c语言
F0:if(P3_4==0)
goto F0; //*按键未释放,在此等候*//
}
//*延时子函数*/
unsigned char i,j;
for(i=5;i>0;i--)
for(j=248;j>0;j--);
}
//*5mS定时中断服务 子函数*//
void zd(void) interrupt 1
{
TH0=-(4800//256);
TL0=-(4800%256);
deda++;
}
//*调整走时时间*//
{
t_dirve();
}
if(P3_7==0)
delay(1);
if(P3_7==0)
t_min++;
if(t_min==60)
{
if(deda<=100)
d_05s=0;
else d_05s=1;
if(deda>=200)
{sec++;deda=0;}
if(sec==60)
{min++;sec=0;}
if(min==60)
{hour++;min=0;}
if(hour==24)
{hour=0;}
p_out(); //*判别闹时到 子函数*// 否
}
//*定时器 T0 5mS初始化*//
void init_timer()
{
TMOD=0x01;
TH0=-(4800//256);
TL0=-(4800%256);
IE=0x82;

8位数码管显示电子时钟c51单片机程序之欧阳法创编

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;sbitKEY4=P3^3;sbit LED=P1^2; //定义指示灯参数code unsigned chartab[]={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 10msinM 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; //正常时间小时加1Displaypro(); } }if(!KEY2) //按键2去抖以及动作{ delay(10000); if(!KEY2) { hour--;if(hour==255)hour=23; //正常时间小时减1Displaypro(); } }if(!KEY3) //按键去抖以及动作{ delay(10000); if(!KEY3) { minute++;if(minute==60)minute=0; //分加1 Displaypro(); } }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;//分别调用缓冲区的值进行扫描 case1:P2=1;P0=StrTab[num];break; case2:P2=2;P0=StrTab[num];break; case3:P2=3;P0=StrTab[num];break; case4:P2=4;P0=StrTab[num];break; case5:P2=5;P0=StrTab[num];break; case6:P2=6;P0=StrTab[num];break; case7:P2=7;P0=StrTab[num];break;default:break; } num++; //扫描8次,使用8个数码管 if(num==8) num=0;}/******************************************************************//* 定时器0中断*//******************************************************************/void tim(void) interrupt 1 using1{ static unsigned char count; //定义内部局部变量TH0=0xd8; //重新赋值 TL0=0xf0;count++; switch (count) { case 0:case 20: case 40: case 60: case80:Displaypro();break; //隔一定时间调用显示处理case 50:P1=~P1;break; //半秒 LED 闪烁default:break; }if (count==100) { count=0;second++; //秒加1 if(second==60){ second=0; minute++;//分加1 if(minute==60){ minute=0;hour++; //时加1 if(hour==24) hour=0; } } } }基于单片机的LCD1602控制总线模式时间:2012-09-10 13:50:39 来源:作者:第一行显示"Welcome";第二行显示="Happy day";若要显示其他字符,请直接往数组 LCMLineOne[16]和LCMLineTwo[16]填充相应的代码。

STC90C51单片机数码管时钟程序

char DelayCNT;
//此表为LED的字模,共阴数码管0-9 -
unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};
unsigned int i,xiaoshi=0,fenzhong=0,miao=0 ;
void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 120; j > 0; j--);
}
void timer() interrupt 1
{
TH0=(65535-50000)%256;
TH1=(65535-50000)/256;
if(++DelayCNT>=50)
/*******************************************************************************
*标题:试验数码管显示数字时钟*
**
*连接方法:用短路帽将J15和J16连起来
JP10(P06的A、B、C*
{
xiaoshi=0;
}
}
LedOut[4]=Disp_Tab[fenzhong%10];
LedOut[5]=Disp_Tab[10];
LedOut[6]=Disp_Tab[miao%100/10];//秒显示
LedOut[7]=Disp_Tab[miao%10];
for( i=0; i<8; i++) //实现8位动态扫描循环
ET0=1;

51单片机C语言可调时钟(2)(1)

/*这是一个真正有意义的时钟key1功能键选择可调位,短按,每按一下有一位闪烁长按闪烁不断向下一位推移key2 加键短按相应闪烁的位加1,长按连续加1;key3 减键短按相应闪烁的位减1,长按连续减一;key4 确定键按下退出调时,正常显示;*/#include<reg52.h>#define uint unsigned int#define uchar unsigned char#define LED P0#define KEY_1 0x0e#define KEY_2 0x0d#define KEY_3 0x0b#define KEY_4 0x07#define KEY_NULL 0x0f#define KEY_PRESS 0x80#define KEY_LONG 0x40#define KEY_STATE_INIT 0#define KEY_STATE_PRESS 1#define KEY_STATE_LONG 2#define KEY_STATE_UP 3#define KEY_LONG_PERIOD 20#define KEY_CONTINUE_PERIOD 10bit set;bit dao1S=0;bit dao2MS=0;bit dao10MS;sbit dula=P2^6;sbit wela=P2^7;sbit key1=P3^4;sbit key2=P3^5;sbit key3=P3^6;sbit key4=P3^7;int main_flag,exit_flag,up_flag,down_flag;int tab[]={0,0,0,0,0,0};uchar weitable[]={0x01,0x02,0x04,0x08,0x10,0x20};uchar tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; void nint(){ TMOD=0X01;TH0=0XF8;TL0=0XCC;TR0=1;ET0=1;}uchar KeyScan(){if(key1==0) return KEY_1;if(key2==0) return KEY_2;if(key3==0) return KEY_3;if(key4==0) return KEY_4;return KEY_NULL;}uchar GetKey(){uchar keyRetu=0,keyTemp=KEY_NULL;static uchar s_keyState=KEY_STATE_INIT,keyTime=0,keyLast=KEY_NULL;keyTemp=KeyScan();switch (s_keyState){case KEY_STA TE_INIT:if(keyTemp!=KEY_NULL){s_keyState=KEY_STA TE_PRESS;}break;case KEY_STA TE_PRESS:if(keyTemp!=KEY_NULL){s_keyState=KEY_STA TE_LONG;keyTime=0;keyLast=keyTemp;}else{s_keyState=KEY_STA TE_INIT;}break;case KEY_STA TE_LONG:if(keyTemp==KEY_NULL){s_keyState=KEY_STA TE_INIT;keyRetu=(keyLast|KEY_PRESS);}else{if(++keyTime>=KEY_LONG_PERIOD) //按下时间>1s{s_keyState=KEY_STATE_UP;keyTime=0;}}break;case KEY_STA TE_UP:if(keyTemp==KEY_NULL){s_keyState=KEY_STA TE_INIT;}else{if(++keyTime>=KEY_CONTINUE_PERIOD) //按下时间>0.5s {keyTime=0;keyRetu=(keyLast|KEY_LONG);}}break;}return keyRetu;}void updatetime(){if(dao1S){dao1S=0;if(++tab[5]==10){ tab[5]=0;if(++tab[4]==6){tab[4]=0;if(++tab[3]==10){ tab[3]=0;if(++tab[2]==6){ tab[2]=0;if(tab[0]<2){if(++tab[1]==10){ tab[1]=0;tab[0]++;}}else{ if(tab[1]==4){ tab[1]=0;tab[0]=0;}} }}}}}}void display(){ static uchar k=0;dula=1;LED=tab1[tab[k]];if(set&&((k==main_flag-1))){LED=0XFF;}dula=0;LED=0Xff;wela=1;LED=weitable[k];wela=0;if(++k>5) k=0;}void sittime(uchar hour,uchar minute,uchar second ) { uchar a1,a2,b1,b2,c1,c2;a1=hour/10;a2=hour%10;b1=minute/10;b2=minute%10;c1=second/10;c2=second%10;tab[0]=a1;tab[1]=a2;tab[2]=b1;tab[3]=b2;tab[4]=c1;tab[5]=c2;}void main(){ nint();EA=1;sittime(15,20,15);while(1){updatetime();if(dao2MS){dao2MS=0;display();}if(dao10MS){dao10MS=0;switch (GetKey()){case (KEY_1|KEY_PRESS):if(++main_flag>=7)main_flag=0;break;case (KEY_1|KEY_LONG):if(++main_flag>=7)main_flag=0;break;case (KEY_2|KEY_PRESS):switch(main_flag){case 1:{if(++tab[0]>=3)tab[0]=0;}break;case 2:{if(++tab[1]>4)tab[1]=0;}break;case 3:{if(++tab[2]>5)tab[2]=0;}break;case 4:{if(++tab[3]>9)tab[3]=0;}break;case 5:{if(++tab[4]>5)tab[4]=0;}break;case 6:if(++tab[5]>9)tab[5]=0;}break;case (KEY_2|KEY_LONG): switch(main_flag) {case 1:{if(++tab[0]>=3)tab[0]=0;}break;case 2:{if(++tab[1]>4)tab[1]=0;}break;case 3:{if(++tab[2]>5)tab[2]=0;}break;case 4:{if(++tab[3]>9)tab[3]=0;}break;case 5:{if(++tab[4]>5)tab[4]=0;}break;case 6:if(++tab[5]>9)tab[5]=0;}break;case (KEY_3|KEY_PRESS):switch(main_flag){case 1:{if(--tab[0]<0)tab[0]=2;}break;case 2:{if(--tab[1]<0)tab[1]=4;}break;case 3:{if(--tab[2]<0)tab[2]=5;}break;case 4:{if(--tab[3]<0)tab[3]=9;}break;case 5:{if(--tab[4]<0)tab[4]=5;}break;case 6:if(--tab[5]<0)tab[5]=9;}break;case (KEY_3|KEY_LONG): switch(main_flag){case 1:{if(--tab[0]<0)tab[0]=2;}break;case 2:{if(--tab[1]<0)tab[1]=4;}break;case 3:{if(--tab[2]<0)tab[2]=5;}break;case 4:{if(--tab[3]<0)tab[3]=9;}break;case 5:{if(--tab[4]<0)tab[4]=5;}break;case 6:if(--tab[5]<0)tab[5]=9;}break;case (KEY_4|KEY_PRESS):main_flag=0;display();}}}}void timer() interrupt 1{ static count=0;static count1=0;TH0=0XF8;TL0=0XCC;dao2MS=1;count++;if(++count1==10){ c ount1=0;dao10MS=1;}if(!(count%25)) set = !set;if(count==500) {count=0;dao1S=1; }}。

51单片机带字库液晶12864ds1302数字时钟c源程序(无按键修改功能)

51单片机+带字库液晶12864+DS1302数字时钟C 源程序(无按键修改功 能)过两天的搜索与调试,在别人程序的基础上,不断修改,终于调试成功了 这个程序。

目前还不能修改时间与日期,只是以预定时间以始。

适用于开发板:51单片机(AT89S52 +带字库液晶12864(ST7920)+DS1302实时时钟)实现功能:简单,数字时钟 +日期(以后会不断完美)。

C 语言源程序如下:#include <reg52.h>#include <intrins.h> #define uchar unsigned char #define uint unsigned int /*DS1302 端 口设置 */sbit SCK=P3A 6;sbit SDA=P3A4;sbit RST = P3A5;bitReadRTC_Flag;/* 12864 端口定义 */ #define LCD_data P0 sbit LCD_RS = P2A4;sbit LCD_RW = P2A5; sbit LCD_EN = P2A 6;//液晶使能控制sbit PSB 二卩2八1; //并口控制 //DS1302 时钟 //DS1302 IO // DS1302 复位 //读DS1302全局变量//带字库液晶 12864数据口// 寄存器选择输入// 液晶读/写控制sbit RES=P2A3;uchar code dis1[] = {"电子设计天地"};// 液晶显示的汉字uchar code dis2[] = {"有志者,事竟成!"};uchar code dis4[] = {'0','1','2','3','4','5','6','7','8','9'};unsigned char temp;#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};void lcd_pos(uchar X,uchar Y); //确定显示位置unsigned char l_tmpdate[7]={0,7,16,19,10,1,9};〃秒分时日月周年09-10-19 16:07:00code unsigned charwrite_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //秒分时日月周年最低位读写位code unsigned charread_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};void Write_Ds1302_byte(unsigned char temp);void Write_Ds1302( unsigned char address,unsigned char dat );unsigned char Read_Ds1302 ( unsigned char address );void Read_RTC(void);//read RTCvoid Set_RTC(void); //set RTCvoid InitTIMER0(void);//inital timer0/*******************************************************************//**//* 延时函数 *//* *//******************************************************************* /void delay(unsigned int m) // 延时程序 {unsigned int i,j;for(i=0;i<m;i++)**************************************************//*检查LCD 忙状态*//*lcd_busy 为1时,忙,等待。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档