电子时钟程序代码
基于定时器的四位数码管时钟程序代码

实验名称:SUMA & BUZZER实验描述:一个带有闹钟的数码时钟,加三个键,一个调小时键一个调分钟键,一个设置闹钟时间键实验方法:TIMER0中断用来计时,控制数码时钟的时间显示还可设置半秒或四分之一秒,用来控制音调TIMER1用来控制音普,,timer0用MODE2自动加载模式*/# include<reg52.h>sbit speaker=P2^3 ;sbit AA=P2^2 ; //调时用sbit BB=P2^1 ; //调分用sbit CC=P2^0 ; // 设置闹钟用sbit P1_7=P1^7; //小数点// int code seven_reg[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//0123456789int code seven_reg[]={0x40,0x79,0x24,0x30,0x19,0x12,0x2,0x78,0x00,0x10};// int code scan[]={0x1F,0x2F,0x4F,0x8F}; //1110,1101,1011,0111int code scan[]={0x1,0x2,0x4,0x8}; //0001,0010,0100,1000unsigned int timer0_times;unsigned int timer1_times;unsigned int timer0_times_AA; //按纽AA用unsigned int timer0_times_BB; //按纽BB用unsigned int timer0_times_CC; // 半秒计时用unsigned int timer0_times_DD; //四分之一秒用unsigned int timer0_times_EE; // 闹钟用typedef struct{unsigned char second;unsigned char minute;unsigned char hour;unsigned char half_second;unsigned char alarm_hour;unsigned char alarm_minute;unsigned char quarter_second ;} time; //此处是固定格式,不能改time now;char mode=0;int i=0,j=0,k=0;int code tone[]={1012,956,852,759,716,638,568,506,478,426,379};//7()低音)1234567(中音)123(高音)int code song[22][2]={ {6,2},{6,2},{7,4},{6,2},{6,2}, {7,4},{6,2},{7,2},{8,2},{7,2},{6,2},{7,1},{6,1},{4,4},{3,2},{1,2},{3,2},{4,2},{3,2},{3,1},{1,1},{0,4}} ;/********************************************************************/void timer1_isr() interrupt TF1_VECTOR using 2{ TR1=0;TL1=(65536-tone[(song[j][0])])%256;TH1=(65536-tone[(song[j][0])])/256;TR1=1;speaker=~speaker;}/*******************************************************************/void timer0_isr() interrupt TF0_VECTOR using 1{/***************************************************/if(CC!=0) timer0_times_EE=0;else{ timer0_times_EE++;if(CC==0&&timer0_times_EE==4000) //按1S进入设置闹钟模式{mode++;//500 ~0.125sif(mode==2) mode=0;}}/**=调时键设置=*/if(AA!=0) timer0_times_AA=0;else{ timer0_times_AA++;if(AA==0&&timer0_times_AA==500&&mode==0)//500*0.25ms=0.125s{ now.hour++;// timer0_times_AA=0;if (now.hour==24) now.hour=0;//后边代码不会达到此效果}if(AA==0&timer0_times_AA==500&&mode==1) //设置闹钟时间HOUR{ now.alarm_hour++;if(now.alarm_hour==24) now.alarm_hour=0;}}/***=调分键设置=***/if (BB!=0) timer0_times_BB=0;else{ timer0_times_BB++;if(BB==0&&timer0_times_BB==500&&mode==0) //0.125s{ //timer0_times_BB=0;now.minute++;if(now.minute==60) now.minute=0;}if(BB==0&&mode==0){ if(timer0_times_BB==2000) //0.5s{ now.minute++;timer0_times_BB=1000;if(now.minute==60) now.minute=0;}}if(BB==0&&timer0_times_BB==500&&mode==1) //设置闹钟时间MINUTE { now.alarm_minute++;if(now.alarm_minute==60) now.alarm_minute=0;}if(BB==0&&mode==1){ if(timer0_times_BB==2000) //0.5s{ now.alarm_minute++;timer0_times_BB=1000;if(now.minute==60) now.minute=0;}}}/*=自然时间设置=*/timer0_times_DD++; //四分之一秒if(timer0_times_DD==1000){ now.quarter_second++;timer0_times_DD=0;if(now.quarter_second==60) now.quarter_second=0;} //二分之timer0_times_CC++;if(timer0_times_CC==2000){now.half_second++;timer0_times_CC=0;if(now.half_second==60) now.half_second=0;}timer0_times++; //一秒一分一时if (timer0_times==4000){ now.second++;timer0_times=0;if(now.second==60){ now.minute++;now.second=0;if(now.minute==60){ now.hour++;now.minute=0;if(now.hour==24) now.hour=0;} } }/******************扫描显示******************************/switch(mode){case 0 :{switch(i){ /*0.005秒选一次*/ case 0:P1=seven_reg[now.minute%10] ;if(now.half_second%2==0)P1_7=1; /*实现让它0.5秒闪一次*/break;case 1:P1=seven_reg[now.minute/10];//小数点不亮同,P1_7=1if(now.half_second%2==0)P1_7=1; /*为什么不能放在上一句前面昵????*/ break;case 2:P1=seven_reg[now.hour %10];break;case 3:P1=seven_reg[now.hour /10];break;}} break;case 1:{switch(i){case 0:P1=seven_reg[now.alarm_minute%10] ;break;case 1:P1=seven_reg[now.alarm_minute/10];break;case 2: P1=seven_reg[now.alarm_hour %10];break;case 3: P1=seven_reg[now.alarm_hour /10];break; }} break;}P3=scan[i];i++;if (i==4) i=0;if(now.quarter_second%2==0){ k++;if(k==(song[j][1]*4)){ j++;k=0;if(j==22) j=0;} } }/****************************************************************/void timer0_initialize(){ EA=0;TR0=0;TMOD=0X12;TL0=(256-250); //0.025ms 自动加载模式0.025*4000=1sTH0=(256-250);ET0=1;TR0=1;EA=1;}void timer1_initialize(){TR1=0;TL1=(65536-tone[song[j][0]])%256;TH1=(65536-tone[song[j][0]])/256;TMOD=0X12;ET1=1;}main(){ unsigned char m1=0;speaker=0;now.alarm_minute=1;timer0_initialize();timer1_initialize();while(1){if(now.alarm_minute!=0)//将闹钟设置为0时,相当于取消闹钟,不会响{ if(now.hour==now.alarm_hour&&now.minute==now.alarm_minute) { if(CC==0&&timer0_times_EE==500) m1=1;switch (m1){ case 0:TR1=1 ;break;case 1 :TR1=0;speaker=0;break;}}else{TR1=0;speaker=0;m1=0;} } } }#include<reg52.h>sbit P10=P1^0; //第0位数码管sbit P11=P1^1;sbit P12=P1^2;sbit P13=P1^3;#define THCO 0xee#define THLO 0x00unsigned char miao=0,fen=0,shi=0;unsigned char code duan[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; main(void){TMOD=0x11;TH0=THCO;TL0=THLO;EA=1;ET0=1;TR0=1;while(1);}void timw0() interrupt 1{static unsigned char c=0,k=0;TH0=THCO;TL0=THLO;P1|=0xff;c++;if(c>200) {miao++;if(miao>=60){miao=0;fen++;}if(fen>=60){shi++;fen=0;}c=0;}if(k>3){k=0;}k++;switch(k-1){case(0):P10=0;P0=duan[shi/10];break;case(1):P11=0;P0=duan[shi%10];break;case(2):P12=0;P0=duan[fen/10];break;case(3):P13=0;P0=duan[fen%10];break;}}这是时钟程序,可以运行,无小数点,显示时分,小数点断码是0x80,其余的只有靠你自己了。
单片机时钟程序设计代码

单片机时钟程序设计代码好的,由于你没有给出具体的单片机型号,下面是一个基于 51 单片机的时钟程序设计示例:```c#include <reg52.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>// 定义变量char str[20];int i=0;int j=0;int k=0;int time=0;int flag=0;int h=0,m=0,s=0;void delay(int xms){int i,j;for(i=xms;i>0;i--)for(j=110;j>0;j--);}void time0() interrupt 1{TH0=(65536-50000)/256;TL0=(65536-50000)%256;time++;if(time==20){time=0;s++;if(s==60){s=0;m++;if(m==60){m=0;h++;}}}// 将时间数据转换为字符串sprintf(str," %02d:%02d:%02d",h,m,s);// 输出时间字符串for(i=0;i<16;i++){P0=str[i];delay(1000);}}void main(){// 初始化定时器 0 为 16 位定时器模式,计数初值为 50000 TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;while(1);}```该程序利用 51 单片机的定时器 0 来产生定时中断,每 20 次中断为 1 秒,通过对秒、分、时的累计来实现时钟功能。
在中断服务函数中,将当前时间转换为字符串,并通过循环输出到 P0 口。
基于51单片机电子时钟程序

// 本程序实现功能:显示小时和分钟,并以最后一位的小数点闪烁一次表示一秒。
按下INT0键后显示日期。
并在所设定的时间蜂铃器响5次以此为闹钟;// 第二:按下INT1键后,可对时间,日期,闹钟进行设置,再次按下INT1推出设置//// 显示说明:前两位显示小时和月份,后两位显示分钟和日期//#include <reg52.h>/*==========================================宏定义uchar和uint===========================================*/#define uchar unsigned char#define uint unsigned intsbit alarm=P1^4;/*==============================================变量的定义==============================================*/int year=2010;/*初始年份为2010年*/uchar alarm_hour=0,alarm_min=0; /*初始闹钟时间为00:00*/uchar qian=0,bai=0,shi=0,ge=0,key_flag=0,Key=0,num=0,Flag=0; /*定义输出函数变量和按键号*/uchar x,dis_flag=0; /*显示变换标志位*/uchar Key_control=0; /*按键被按下的标志位*/uchar mounth=7,day=25; /*初始日期设为7月25号*/uchar hour=0,t=0,min=0,sec=0,ring=0;/*初始时间为00:00:00*//*=============================================子函数的定义=============================================*/void Init(); /*此函数用于初始化所有需要使用的中断*/void delay(uint z); /*用于数码管显示*/uchar Key_num(void); /*此函数为确定按下的按键输出编号*/void Led_display();void display(uchar cc, uchar dd); /*显示时间的函数,中间的点表示:*/void display_nian(uchar cc, uchar dd); /*显示年份的显示函数,即没有中间的点*/void display_date(uchar cc, uchar dd); /*显示日期的函数,即四个小数点全亮*/void display_alarm(uchar cc, uchar dd); /*显示闹钟的函数,第二个和第四个点*/void Leap_Nonleap(int aa); /*判断是闰年还是平年,并将二月的最大天数赋给Mounth_array[1]*/void Judge_Setting(uchar Key_set); /*所得出的按键号进行对应的设置*//*==========================================所使用数组的定义============================================*/uchar Mounth_array[12]={31,29,31,30,31,30,31,31,30,31,30,31}; /*每个月的最大天数数组*/uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,/*数码管显示编码*/};uchar code table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};/*百位及其小数点的段码*//*==============================================主函数部分==============================================*/void main(){Init();while(1){Leap_Nonleap(year); /*进入大循环后首先对年份进行判断*/while(Key_control)/*当P3.3被按下后Key_control=1进入函数进行设置,直到第二次Key_control=0推出循环*/{Flag=Key_num(); /*将按键函数里面是否有按键被按下的标志位赋给Flag*/if(Flag) /*当有按键被按下时,进入设置函数*/{Judge_Setting(num); /*将num值传入函数,并进行设置*/}Led_display(); /*保证在设置的循环时有显示*/}Led_display();/*循环式动态显示*/}}/*===========================================系统初始化函数=============================================*/void Init() /*初始化系统,启动计时器0,1,外部中断0,1*/{TMOD=0x01; /*将计时器0定位工作方式1,将计时器1定为工作方式2*/TH0=(65536-50000)/256;TL0=(65536-50000)%256;ET0=1;TR0=1;// ET1=1;// TR1=1;IE0=1;EX0=1; /*使用外部中断0和1,分别作为显示变换,设置的前戏*/IT0=1;/*为下降沿突发*/IE1=1;EX1=1;IT1=1;/*为下降沿突发*/EA=1;}void Display_flag() interrupt 0 /*使用外部中断0,进行显示时间和日期的转换P3.2口切换显示*/{dis_flag++;if(dis_flag==4) /*当dis_flag=0时,输出时间,当dis_flag=1时,输出日期,当dis_flag=2时,输出闹钟*/dis_flag=0; /*当dis_flag=3时,输出年份。
LED数字显示电子时钟源程序代码

LED数字显示电子时钟源程序代码程序:(注已完全经过调试,达到预期目的)#include<reg51.h>#define uchar unsigned char#define uint unsigned intuchar count=0;sbit LED=P1^0;uchar tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //显示数码管0,1,3,4,5,6,7,8,9 uchar miao=0,fen=0,hour=0;void delay(uint i) //延迟函数{uint j;for(;i>0;i--)for(j=124;j>0;j--);}void init(){ET0=1;TMOD=0x51; //选定定时器0,1和中断0,1TH0=(65536-25000)/256;TL0=(65536-25000)%256; //设定时器0时间为250ms一次ET1=1;TH1=0xff;TL1=0xff; //定时器1为中断EX0=1;IT0=1; //中断0开EX1=1;IT1=1; //中断1开TR0=1; //定时器0开TR1=1; //定时器1开EA=1; //总中断开}void display() //时钟显示函数{P0=tab[hour/10%10];P2=0xfe;delay(1);P0=tab[hour%10];P2=0xfd;delay(1);P0=tab[fen/10%10];P2=0xfb;delay(1);P0=tab[fen%10];P2=0xf7;delay(1);P0=tab[miao/10%10];P2=0xef;delay(1);P0=tab[miao%10];P2=0xdf;delay(1);}void main() //主函数{init();LED=0;while(1){display();}}void T0_int() interrupt 1 //定时器0函数{TH0=(65536-25000)/256;TL0=(65536-25000)%256;count++;if(count==20){count=0;miao= miao +1;LED=~LED;if(sec==60){miao=0;fen= fen +1;if(fen ==60){fen =0;hour=hour+1;if(hour==24)hour=0;}}}}void T1_int() interrupt 3 //定时器1函数控制秒针加1 {TH1=0xff;TL1=0xff;miao=miao+1;if(miao ==60)miao =0;}void I1_int() interrupt 2 //中断1函数控制按键分针加1 {EX0=0;fen= fen +1;if(fen ==60)fen =0;EX0=1;}void I0_int() interrupt 0 //中断函数0控制按键时针加1 {EX1=0;hour=hour+1;if(hour==24)hour=0;EX1=1;}。
VHDL语言写的电子钟程序(XTS江科大)

实验目的:实现时钟显示,1——12——1——11——0——1循环,时钟调时,闹铃,闹铃定时等功能。
程序和注释:<code1.vhd>LIBRARY ieee;USE ieee.std_logic_1164.ALL;USE ieee.std_logic_unsigned.ALL;entity code1 isport (clk : in std_logic;reset : in std_logic;shi_key : in std_logic;fen_key : in std_logic;set_mode_key:in std_logic;en:out std_logic_vector(7 downto 0);y :out std_logic_vector(7 downto 0);beep_s:out std_logic);end code1 ;ARCHITECTURE code2 of code1 issignal a:integer:=0; --ji shisignal d:integer:=0; --xian shisignal b,b0,b1,b3,b4,b5,b6:std_logic:='1';signal e:std_logic:='0';--xian shisignal c:integer:=9; --miao geweisignal ch:integer:=5; --miao shiweisignal eight,eight1:integer:=0; --8 ge shu ma guansignal fg:integer:=9; --fen ge weisignal fs:integer:=5; --fen shi weisignal sg:integer:=1; --shi ge weisignal ss:integer:=1; --shi shi weisignal p:std_logic:='0';--zantingsignal set_mode :std_logic_vector(1 downto 0);signal beep_m1,beep_m2,beep_h1,beep_h2:integer:=1;signal AM_PM:std_logic:='0';signal beep_count:integer:=0;signal beep_clk: std_logic:='1';signal c1,c2,c3,c4,c5,c6:integer:=0;component set0port ( set_clk:in std_logic;m_key:in std_logic;h_key:in std_logic;mode_key:in std_logic;mode:out std_logic_vector(1 downto 0);set_out_m:out std_logic;set_out_h:out std_logic;beep_key_m:out std_logic;beep_key_h:out std_logic);end component;beginprocess(clk)beginif clk'event and clk='1' thend<=d+1;beep_count<=beep_count+1;if set_mode="01" thenb0<=b3;b1<=b4;elsea<=a+1;if a=30000000 then b<=not b;b1<=not b1;b0<=not b0;a<=0;end if;end if;if beep_count=15000000 thenbeep_clk<=not beep_clk;beep_count<=0;-- if beep_count=35000000 then beep_clk<=not beep_clk;beep_count<=0;end if;end if;if d=5000 then e<=not e;d<=0;end if;end if;end process;process(e)--variable c1,c2,c3,c4,c5,c6:integer:=0;beginif e'event and e='1' thenif set_mode="00"or set_mode="01" thenc1<=c;c2<=ch;c3<=fg;c4<=fs;c5<=sg;c6<=ss;end if;if set_mode="10" thenc1<=0;c2<=0;c3<=beep_m1;c4<=beep_m2;c5<=beep_h1;c6<=beep_h2;--c3<=3;--c4<=2;--c5<=5;--c6<=0;end if;case eight iswhen 0 =>en<="11111110";case c1 iswhen 0 => y<="11000000"; when 1 => y<="11111001"; when 2 => y<="10100100"; when 3 => y<="10110000"; when 4 => y<="10011001"; when 5 => y<="10010010"; when 6 => y<="10000010"; when 7 => y<="11111000"; when 8 => y<="10000000"; when 9 => y<="10010000"; when others =>y<="11000000"; end case;eight<=1;when 1 =>en<="11111101";case c2 iswhen 0 => y<="11000000"; when 1 => y<="11111001"; when 2 => y<="10100100";when 3 => y<="10110000"; when 4 => y<="10011001"; when 5 => y<="10010010"; when others =>y<="11000000"; end case;eight<=2;when 2 =>en<="11111011";y<="10111111";eight<=3;when 3 =>en<="11110111";case c3 iswhen 0 => y<="11000000"; when 1 => y<="11111001"; when 2 => y<="10100100"; when 3 => y<="10110000"; when 4 => y<="10011001"; when 5 => y<="10010010"; when 6 => y<="10000010"; when 7 => y<="11111000"; when 8 => y<="10000000"; when 9 => y<="10010000"; when others =>y<="11000000"; end case;eight<=4;when 4 =>en<="11101111";case c4 iswhen 0 => y<="11000000"; when 1 => y<="11111001"; when 2 => y<="10100100"; when 3 => y<="10110000"; when 4 => y<="10011001"; when 5 => y<="10010010"; when others =>y<="11000000"; end case;eight<=5;when 5 =>en<="11011111";y<="10111111";eight<=6;when 6 =>en<="10111111";case c5 iswhen 0 => y<="11000000"; when 1 => y<="11111001"; when 2 => y<="10100100"; when 3 => y<="10110000"; when 4 => y<="10011001"; when 5 => y<="10010010"; when 6 => y<="10000010"; when 7 => y<="11111000"; when 8 => y<="10000000"; when 9 => y<="10010000"; when others =>y<="11000000"; end case;eight<=7;when 7 =>en<="01111111";case c6 iswhen 0 => y<="11000000"; when 1 => y<="11111001"; when others =>y<="11000000"; end case;eight<=0;when others => eight<=0;end case;end if ;end process;miao:process(b,reset)beginif reset='0' thenc<=0;ch<=0;elseif b'event and b='0' thenif c=9 thenc<=0;elsec<=c+1;end if;if ch=5 and c=9 thench<=0;elseif c=9 thench<=ch+1;end if;end if;end if;end if;end process;fen:process(b0,reset,ch,c,b3)beginif reset='0' thenfg<=0;fs<=0;elseif b0'event and b0='0' thenif (((ch=5)and(c=9)) or (set_mode="01" and b3='0'))then if fg=9 thenfg<=0;elsefg<=fg+1;end if;if fs=5 and fg=9 thenfs<=0;elseif fg=9 thenfs<=fs+1;end if;end if;end if;end if;end if;end process;shi:process(b1,reset,fg,fs,ch,c,b4)beginif reset='0' thensg<=0;ss<=0;elseif b1'event and b1='0' thenif ((c=9) and (ch=5) and (fg=9) and (fs=5))or (set_mode="01" and b4='0') then--if ((ss=1 and sg=1) or sg=9) then-- sg<=0;--else-- sg<=sg+1;--end if;if sg=9 thensg<=0;elsesg<=sg+1;end if;if (ss=1 and sg=1 and AM_PM='0')thenss<=0;sg<=0;AM_PM<=not AM_PM;elseif sg=9 thenss<=ss+1;end if;end if;if (ss=1 and sg=2 and AM_PM='1')thenss<=0;sg<=1;AM_PM<=not AM_PM;elseif sg=9 thenss<=ss+1;end if;end if;end if;end if;end if;end process;set_time:set0 port map( set_clk=>clk,m_key=>fen_key,h_key=>shi_key,mode_key=>set_mode_key,mode=>set_mode,set_out_m=>b3,set_out_h=>b4,beep_key_m=>b5,beep_key_h=>b6);beep_set_fen:process(set_mode_key,b5)beginif b5'event and b5='0' thenif set_mode="10" thenif b5='0' thenif beep_m1=9 thenbeep_m1<=0;elsebeep_m1<=beep_m1+1;end if;if beep_m2=5 and beep_m1=9 thenbeep_m2<=0;elseif beep_m1=9 thenbeep_m2<=beep_m2+1;end if;end if;end if;end if;end if;end process;beep_set_shi:process(set_mode_key,b6)beginif b6'event and b6='0' thenif set_mode="10" thenif b6='0' thenif ((beep_h2=1 and beep_h1=1) or beep_h1=9) thenbeep_h1<=0;elsebeep_h1<=beep_h1+1;end if;if beep_h2=1 and beep_h1=1 thenbeep_h2<=0;elseif beep_h1=9 thenbeep_h2<=beep_h2+1;end if;end if;end if;end if;end if;end process;beep:process(e)beginif e'event and e='1' thenif beep_m1=fg thenif beep_m2=fs thenif beep_h1=sg thenif beep_h2=ss thenbeep_s<=beep_clk;end if;end if;end if;elsebeep_s<='1';end if;end if;end process;end code2;<set0.vhd>LIBRARY ieee;USE ieee.std_logic_1164.ALL;USE ieee.std_logic_unsigned.ALL;entity set0 isport ( set_clk:in std_logic;m_key:in std_logic;h_key:in std_logic;mode_key:in std_logic;mode:out std_logic_vector(1 downto 0);set_out_m:out std_logic;set_out_h:out std_logic;beep_key_m:out std_logic;beep_key_h:out std_logic);end set0;ARCHITECTURE set1 of set0 issignal s1:std_logic_vector (1 downto 0);beginprocess(mode_key) --模式设定beginif mode_key'event and mode_key='1' thens1<=s1+'1';mode<=s1;if s1="10" thens1<="00";end if;end if;end process;process(m_key,set_clk) --传递调时按键信号beginif set_clk'event and set_clk='1' then-- if s1="01" thenset_out_m<=m_key;-- else-- set_out_m<='1';-- end if;-- if s1="10" thenbeep_key_m<=m_key;-- else-- set_out_m<='1';-- end if;end if;end process;process(h_key,set_clk) --传递调时按键信号beginif set_clk'event and set_clk='1' then-- if s1="01" thenset_out_h<=h_key;-- else-- set_out_h<='1';-- end if;-- if s1="10" thenbeep_key_h<=h_key; -- else-- beep_key_h<='1';-- end if;end if;end process;end set1;引脚:clk input PIN_31en[7] output PIN_226 en[6] output PIN_230 en[5] output PIN_231 en[4] output PIN_232 en[3] output PIN_221 en[2] output PIN_222 en[1] output PIN_223 en[0] output PIN_224y[7] output PIN_233 y[6] output PIN_234 y[5] output PIN_235 y[4] output PIN_236 y[3] output PIN_237 y[2] output PIN_238 y[1] output PIN_239 y[0] output PIN_240实验中的注意的问题:1、数码管动态显示时,要对时钟信号进行分频,而且,分频计数最好在5000个时钟信号以上,否则数码管显示的可能是模糊的数字8;2、时间计数进程中的if语句要进行嵌套,否则,计数的更新会有一秒的滞后。
经典时钟C语言程序代码

学号:37
指导老师:王欣欣
设计思想及目的:用C语言编写时钟代码,然后下载到51单片机上,用1602液晶屏实现年、月、日、星期、时、分、秒的可调简易时钟。
//***********************************************************************************************************************************
{
yue--;
if(yue==0)
yue=12;
write1(0x80+6);
nyr(6,yue);
}
if(keynum==6)
{
nian--;
if(nian==-1)
week++;
if(week==21)
week=0;
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(keynum==1)
if(keynum==1)
{
TR0=0;
write1(0x80+0x40+9);
write1(0x0f);
}
if(keynum==2)
write1(0x80+0x40+6);
if(keynum==3)
write1(0x80+0x40+3);
}
nyr(6,yue);
}
nyr(9,ri);
write1(0x80+12);
单片机C语言_电子时钟程序

#include<AT89X52.H>#define uint unsigned int#define uchar unsigned charunsigned char key2;bit ding=1;unsigned char Getkey(void);uchar a,n=0,shi,fen,miao;void delay01s(void);uchar LED[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};uchar LED1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//有小数点的void init(); //函数声明void Delay(unsigned char z);//函数声明void display(); //函数声明//函数声明void main() //函数声明{P1=0xfe;//对P1口赋初值init(); //函数调用while(1){//函数调用key2=Getkey();switch(key2){case 1:shi++;if(shi==24){shi=0;}break;case 2:fen++;if(fen==60){fen=0;}break;case 3:if(fen!=0)fen--;if(fen==24){fen=0;}break;case 4:ding=~ding;default:break;}display(); //函数调用}}void init(){TMOD=0x01; //定时器工作方式选择和赋初值TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1; //开总中断ET0=1; //开定时器中断TR0=1; //启动定时器}void timer0() interrupt 1 //中断服务程序{if(ding==1){TH0=(65536-50000)/256;//中断时间50msTL0=(65536-50000)%256; //定时器重新赋初值a++;if(a==10){n=~n;}if(a==20){n=~n;a=0;P1=P1<<1|P1>>7;miao++;if(miao==60){miao=0;fen++;if(fen==60){fen=0;shi++;if(shi==24){shi=0;}}}}}}void display()//显示程序{P0=LED[shi/10];P2=((P2&0x0f)|0x70); Delay(4);if(n==0){P0=LED[shi%10];}else{P0=LED1[shi%10];}P2=((P2&0x0f)|0xb0);Delay(4);P0=LED[fen/10];P2=((P2&0x0f)|0xd0);Delay(4);P0=LED[fen%10];P2=((P2&0x0f)|0xe0);Delay(4);}/**********获得键值子程序**********************/ unsigned char bool;//bool 是否松键的标志unsigned char Getkey(void){unsigned char temp,key=0;P2=(P2&0xff)|0x0f;if((P2&0xff)!=((P2&0xff)|0x0f)) // 有键按下{//delay01s();if(((P2&0xff)!=((P2&0xff)|0x0f)) &&(bool==0)) // 有键按下{temp=~(P2|0xf0);if(temp==1) key=1;else if(temp==2) key=2;else if(temp==4) key=3;else if(temp==8) key=4;bool=1;}}if(((P2&0xff)==((P2&0xff)|0x0f)) &&(bool==1)){bool=0;}return key; //返回1~16键值}/********延时程序******/void delay01s(void){unsigned char j,k;for(j=5;j>0;j--) //198{for(k=15;k>0;k--)//248{;}}}void Delay(unsigned char z){unsigned char i,j,k; //定义变量for(i=z;i>0;i--)for(j=25;j>0;j--)for(k=20;k>0;k--);}#include<reg52.h>//头文件#define uchar unsigned char//宏定义#define uint unsigned intsbit P31=P3^1;//位声明sbit P32=P3^2;sbit P33=P3^3;uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//共阴段码表uint hou1,hou2,min1,min2,sec1,sec2,numhou,nummin,numsec,yue1,yue2,ri1,ri2,numyue,numri; uint num,m,n;void delayms(uint xms)//延时函数{uint i,j;for(i=xms;i>0;i--)for(j=110;j>0;j--);}void p31xd()//按键P31消抖{ delayms(2);while(P31!=1);delayms(2);}void p32xd()//按键P32消抖{ delayms(1);while(P32!=1);delayms(1);}void p33xd()//按键P33消抖{ delayms(1);while(P33!=1);delayms(1);}void displaysj()//显示时间{P2=0x00;P0=table[hou1];//显示时的第一位P2=0X20;delayms(1);P2=0xff;P0=table[hou2]&0x7f;//显示时的第二位与小数点P2=0X10;delayms(1);P2=0xff;P0=table[min1];//显示分的第一位P2=0X08;delayms(1);P2=0xff;P0=table[min2]&0x7f;//显示时的第二位与小数点P2=0X04;delayms(1);P2=0xff;P0=table[sec1];//显示秒的第一位P2=0X02;delayms(1);P2=0xff;P0=table[sec2];//显示秒的第二位P2=0X01;delayms(1);}void displayrq()//显示日期{ P2=0x00;P0=table[yue1];//显示月的第一位P2=0X20;delayms(1);P2=0x00;P0=table[yue2]&0x7f;//显示月的第二位P2=0X10;delayms(1);P2=0x00;P0=table[ri1];//显示日的第一位P2=0X08;delayms(1);P2=0x00;P0=table[ri2];//显示日的第二位P2=0X04;delayms(1);}void houqh()//时针切换函数{hou1=numhou/10;hou2=numhou%10;}void minqh()//分针切换函数{min1=nummin/10;min2=nummin%10;}void secqh()//秒针切换函数{sec1=numsec/10;sec2=numsec%10;}void yueqh()//月切换函数{yue1=numyue/10;yue2=numyue%10;}void riqh()//日切换函数{ri1=numri/10;ri2=numri%10;}void start()//初始化函数{num=0;TMOD=0x01;TH0=(65532-45872)/256;TL0=(65532-45872)%256;EA=1;IT0=0;//电平触发(低电平有效)EX0=1;ET0=1;TR0=1;yueqh(),riqh();//初始化日月切换}void ritiao()//日期的调节函数{ if(P32==0)//日期加一调节键{p32xd();if((numyue==1)||(numyue==3)||(numyue==5)||(numyue==7)||(numyue==8)||(numyue==10)||( numyue==12)){numri+=1;if(numri>=31)//如果是大月,日期有31号,等于符号是防止日期大于31号而乱码{numri=1;}}if((numyue==4)||(numyue==6)||(numyue==9)||(numyue==11)){numri+=1;if(numri>=30)//如果是小月,日期有30号{numri=1;}}if(numyue==2){numri+=1;if(numri>=28)//如果是小月,日期有28号{numri=1;}}riqh();//日期调节后切换一下}if(P33==0)//日期减一调节键{p33xd();if((numyue==1)||(numyue==3)||(numyue==5)||(numyue==7)||(numyue==8)||(numyue==10)||( numyue==12)){numri-=1;if(numri==0){numri=31;}}if((numyue==4)||(numyue==6)||(numyue==9)||(numyue==11)){numri-=1;if(numri==0){numri=30;}}if(numyue==2){numri-=1;if(numri==0){numri=28;}}riqh();}}void yuetiao()//月份的调节函数{if(P32==0){p32xd();numyue+=1;if(numyue==13)//月份为12时再加一马上为一月{numyue=1;}yueqh();}if(P33==0){p33xd();numyue-=1;if(numyue==0){numyue=12;}yueqh();}}void int0() interrupt 0{p31xd();while(P31!=0)//第一次按下p31时,进行秒调时{for(m=0;m<40;m++)//此for循环不显示秒,为的是使秒闪烁{P2=0x00;P0=table[hou1];P2=0X20;delayms(1);P2=0x00;P0=table[hou2]&0x7f;P2=0X10;delayms(1);P2=0x00;P0=table[min1];P2=0X08;delayms(1);P2=0x00;P0=table[min2]&0x7f;P2=0X04;delayms(1);if(P32==0)//如果按一下P32,则秒加一{p32xd();numsec+=1;if(numsec==60){numsec=0;}secqh();}if(P33==0)//如果按一下P33,则秒减一{p33xd();numsec-=1;if(numsec==-1){numsec=59;}secqh();}};for(n=0;n<40;n++)//此循环时分秒全显示,也为的是使秒闪烁{ displaysj();if(P32==0){p32xd();numsec+=1;if(numsec==60){numsec=0;}secqh();}if(P33==0){p33xd();numsec-=1;if(numsec==-1){numsec=59;}secqh();}}};p31xd();while(P31!=0)//第二次按下p31时,进行分调时{for(m=0;m<40;m++){P2=0x00;P0=table[hou1];P2=0X20;delayms(1);P2=0x00;P0=table[hou2]&0x7f;P2=0X10;delayms(1);P2=0x00;P0=table[sec1];P2=0X02;delayms(1);P2=0x00;P0=table[sec2];P2=0X01;delayms(1);if(P32==0){p32xd();nummin+=1;if(nummin==60){nummin=0;}minqh();}if(P33==0){p33xd();nummin-=1;if(nummin==-1){nummin=59;}minqh();}}for(n=0;n<40;n++){ displaysj();if(P32==0){p32xd();nummin+=1;if(nummin==60){nummin=0;}minqh();}if(P33==0){nummin-=1;if(nummin==-1){nummin=59;}minqh();}}};p31xd();while(P31!=0)//第三次按下p31时,进行时调时{for(m=0;m<40;m++){P2=0x00;P0=table[min1];P2=0X08;delayms(1);P2=0x00;P0=table[min2]&0x7f;P2=0X04;delayms(1);P2=0x00;P0=table[sec1];P2=0X02;delayms(1);P2=0x00;P0=table[sec2];P2=0X01;delayms(1);if(P32==0){p32xd();numhou+=1;if(numhou==24){numhou=0;}}if(P33==0){p33xd();numhou-=1;if(numhou==-1){numhou=23;}houqh();}}for(n=0;n<40;n++){ displaysj();if(P32==0){p32xd();numhou+=1;if(numhou==24){numhou=0;}houqh();}if(P33==0){p33xd();numhou-=1;if(numhou==-1){numhou=23;}houqh();}}}p31xd();while(P31!=0)//第四次按下p31时,进行日期调节{for(m=0;m<50;m++){ P2=0x00;P0=table[yue1];P2=0x20;delayms(1);P2=0x00;P0=table[yue2]&0x7f;P2=0x10;delayms(1);ritiao();}for(n=0;n<50;n++){ displayrq();ritiao();}}p31xd();while(P31!=0)//第五次按下p31时,进行月调节{for(m=0;m<50;m++){P2=0xff;P0=table[ri1];P2=0xf7;delayms(1);P2=0xff;P0=table[ri2];P2=0xfb;delayms(1);yuetiao();}for(n=0;n<50;n++){ displayrq();yuetiao();}}p31xd();}void yuejia(){ numri=1;numyue+=1;if(numyue==13){numyue=1;}}void timer0() interrupt 1{TH0=(65532-46100)/256;TL0=(65532-46100)%256;num++;if(num==20){num=0;TH0=(65532-46100)/256;TL0=(65532-46100)%256;numsec=numsec+1;if(numsec==60){numsec=0;nummin=nummin+1;if(nummin==60){nummin=0;numhou=numhou+1;if(numhou==24){numhou=0;numri+=1;if((numyue==1)||(numyue==3)||(numyue==5)||(numyue==7)||(numyue==8)||(numyue==10)||( numyue==12)){numri+=1;if(numri>=31){numri=1;numyue+=1;if(numyue==13){numyue=1;}}}if((numyue==4)||(numyue==6)||(numyue==9)||(numyue==11)){numri+=1;if(numri>=31){numri=1;numyue+=1;if(numyue==13){numyue=1;}}}if(numyue==2){numri+=1;if(numri>=29){numri=1;numyue+=1;if(numyue==13){numyue=1;}}}}}}}}void main(){numhou=12;//初始化时间设为12点,日期设为1月1日nummin=0;numsec=0;numyue=1;numri=1;start();while(1){if(P32==1)//默认(没有按下p32时)显示时间{houqh(),minqh(),secqh();displaysj();}if(P32==0)//当按下p32键时显示日期{yueqh(),riqh();displayrq();}}}。
AVR——Mega16制作的电子时钟(仿真图+源程序)

{ fen=0; shi++; if(shi==24)
{ shi=0; } }
}
TCNT1H=0X8f; TCNT1L=0X80; }
绥化学院电子协会 2010 级耿国辉
QQ:835751934
PORTA=0X00; PORTC=0X00; PORTB=0X07; }
/*************定时器初始化*************/ void timer1_init(void)
{ TCCR1B=0X04; //256 分频 TCNT1H=0X8f; TCNT1L=0X80; TIMSK|=BIT(2); //定时器中断使能位 SREG|=BIT(7); //设置总中断 } /*************按键扫描*****************/ void key_scan() {
PORTA=0X00;
PORTA=table[fen/10];
//5
PORTC=tab[3];
delay(1);
PORTA=0X00;
PORTA=0x40;
பைடு நூலகம்
//6
PORTC=tab[2];
delay(1);
PORTA=0X00;
PORTA=table[shi%10];
//7
PORTC=tab[1];
delay(1);
PORTA=0X00;
PORTA=table[shi/10];
//8
PORTC=tab[0];
delay(1);
PORTA=0X00;
} /************主函数*****************/ void main(void) { init(); //初始化 timer1_init(); while(1)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code weixuan[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //位选,控制哪个数码管亮。
(从右到左)
uchar
code
duanxuan[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf, 0xff}; //0-9,'-','灭'
uchar data timedata[3]={0x00,0x00,0x00}; //时间缓冲区,分别为:秒、时、分。
uchar data datetime[8]={0xc0,0xc0,0xbf,0xc0,0xc0,0xbf,0xc0,0xc0}; //时间缓冲区,初始化显示00-00-00。
uchar tt1,tt2,tt,tt0,num=0;
uchar flag,flag1,flag2,flash;
sbit key1=P1^3;
sbit key2=P1^5;
sbit key3=P1^7;
//**************延时函数*********************************
void delay(uint del)
{
uchar i, j;
for(i=0; i<del; i++)
for(j=0; j<=148; j++);
}
//********************调用显示************************
void display()
{
datetime[0]=timedata[0]%10; datetime[1]=timedata[0]/10; //秒
datetime[3]=timedata[1]%10; datetime[4]=timedata[1]/10; //分 datetime[6]=timedata[2]%10; datetime[7]=timedata[2]/10; //时 if(!flag)
{
P2=0X80; //秒显示
P0=duanxuan[datetime[0]];
delay(2);
P2=0X40;
P0=duanxuan[datetime[1]];
delay(2);
}
else
{
P2=0X80; //秒显示
P0=duanxuan[datetime[0]]|flash;
delay(2);
P2=0X40;
P0=duanxuan[datetime[1]]|flash;
delay(2);
}
P2=0X20; //显示'-'
P0=duanxuan[10];
delay(2);
if(!flag1)
{
P2=0X10; //分显示
P0=duanxuan[datetime[3]];
delay(2);
P2=0X08;
P0=duanxuan[datetime[4]];
delay(2);
}
else
{
P2=0X10; //分显示
P0=duanxuan[datetime[3]]|flash;
delay(2);
P2=0X08;
P0=duanxuan[datetime[4]]|flash;
delay(2);
}
P2=0X04; //显示'-' P0=duanxuan[10];
delay(2);
if(!flag2)
{
P2=0X02; //小时
P0=duanxuan[datetime[6]];
delay(2);
P2=0X01;
P0=duanxuan[datetime[7]];
delay(2);
}
else
{
P2=0X02; //小时
P0=duanxuan[datetime[6]]|flash;
delay(2);
P2=0X01;
P0=duanxuan[datetime[7]]|flash;
delay(2);
}
}
/******************按键调节时间*****************************/ void keyscan()
{
if(key1==0)
{
delay(10);
if(key1==0)
{
num++;
while(!key1);
while(1)
{
if(num==1)
{
flag=1;
flag1=0;
flag2=0;
if(key2==0)
{
delay(10);
if(key2==0)
{
timedata[0]--;
if(timedata[0]==-1)
timedata[0]=60;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
timedata[0]++;
if(timedata[0]==60)
timedata[0]=0;
while(!key3);
}
}
}
if(key1==0)
{
delay(10);
if(key1==0)
num++;
while(!key1);
}
if(num==2)
{
flag=0;
flag1=1;
flag2=0;
if(key2==0)
{
delay(10);
if(key2==0)
{
timedata[1]--;
if(timedata[1]==-1)
timedata[1]=60;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
timedata[1]++;
if(timedata[1]==60)
timedata[1]=0;
while(!key3);
}
}
if(num==3)
{
flag=0;
flag1=0;
flag2=1;
if(key2==0)
{
delay(10);
if(key2==0)
{
timedata[2]--;
if(timedata[2]==-1)
timedata[2]=24;
while(!key2);
}
}
if(key3==0)
{
delay(10);
if(key3==0)
{
timedata[2]++;
if(timedata[2]==25)
timedata[2]=0;
while(!key3);
}
}
if(num==4)
{
num=0;
flag=0;
flag1=0;
flag2=0;
break;
}
}
}
}
}
//主函数
void main()
{
TMOD=0x01;
ET0=1;
TR0=1;
TH0=0x40;
TL0=0x00;
EA=1;
while(1)
{
keyscan();
}
}
//***************定时器函数*************************** void timer1() interrupt 1
{
TH0=0x40; //50ms自加一次。
TL0=0x00;
P3=100;
display();
if((flag||flag1||flag2))
{
tt0++;
if(tt0==10)
{
flash=~flash;
tt0=0;
}
}
//P3=11
else
{
tt++;
if(tt==20)
{
tt=0;
timedata[0]++; //秒加1
if(timedata[0]==60)
{
timedata[0]=0;
timedata[1]++; //分加1
if(timedata[1]==60)
{
timedata[1]=0;
timedata[2]++; //时加1
if(timedata[2]==24)
{
timedata[2]=0 ;
}
}
}
}
}
}。