1、实验名称:SUMA & BUZZER 实验描述:一个带有闹钟的数码时钟,加三个键,一个调小时键 一个调分钟键,一个设置闹钟时间键 实验方法:TIMER0中断用来计时,控制数码时钟的时间显示 还可设置半秒或四分之一秒,用来控制音调 TIMER1用来控制音普,timer0用MODE2自动加载模式 */# include sbit speaker=P23 ; sbit AA=P22 ; /调时用 sbit BB=P21 ; /调分用 sbit CC=P20 ; / 设置闹钟用 sbit P1_7=P17; /小数点 / int code seven_reg=0xC0,0xF9,0xA4,0xB0,0
2、x99,0x92,0x82,0xF8,0x80,0x90; /0123456789 int code seven_reg=0x40,0x79,0x24,0x30,0x19,0x12,0x2,0x78,0x00,0x10; / int code scan=0x1F,0x2F,0x4F,0x8F;/1110,1101,1011,0111 int code scan=0x1,0x2,0x4,0x8;/0001,0010,0100,1000 unsigned int timer0_times; unsigned int timer1_times; unsigned int timer0_times_A
3、A; /按纽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
4、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 song222= 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,
5、0,4 ; /*/ void timer1_isr() interrupt TF1_VECTOR using 2 TR1=0; TL1=(65536-tone(songj0)%256; TH1=(65536-tone(songj0)/256; TR1=1; speaker=speaker; /*/ void timer0_isr() interrupt TF0_VECTOR using 1 /*/if(CC!=0) timer0_times_EE=0;elsetimer0_times_EE+; if(CC=0&timer0_times_EE=4000) /按1S进入设置闹钟模式 mode+;
6、/500 0.125s if(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_h
7、our=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
8、.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=10
9、00) 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.
10、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_regnow.minute%10;if(now.half_second%2=0)P1_7=1; /*实现让它0.5秒闪一次*/break; case 1: P1=seven_regnow.minute/10;/小数点不亮同,P1_7=1 if(
11、now.half_second%2=0)P1_7=1; /*为什么不能放在上一句前面昵?*/ break; case 2: P1=seven_regnow.hour %10; break; case 3: P1=seven_regnow.hour /10; break; break;case 1: switch(i) case 0: P1=seven_regnow.alarm_minute%10; break; case 1: P1=seven_regnow.alarm_minute/10; break; case 2: P1=seven_regnow.alarm_hour %10;break
12、; case 3: P1=seven_regnow.alarm_hour /10; break; break;P3=scani;i+; if (i=4) i=0; if(now.quarter_second%2=0) k+; if(k=(songj1*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=1s TH0=(256-250); ET0=1; TR0=1; EA=1; void timer1_initi
13、alize() TR1=0; TL1=(65536-tonesongj0)%256; TH1=(65536-tonesongj0)/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) i
14、f(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; #includesbit P10=P10; /第0位数码管sbit P11=P11;sbit P12=P12;sbit P13=P13;#define THCO 0xee#define THLO 0x00unsigned char miao=0,fen=0,shi=0;unsigned char code duan=0x3F, 0x06,
15、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(c200) miao+; if(miao=60)miao=0;fen+; if(fen=60)shi+;fen=0; c=0; if(k3)k=0; k+; switch(k-1) case(0):P10=0;P0=duanshi/10;break; case(1):P11=0;P0=duanshi%10;break; case(2):P12=0;P0=duanfen/10;break; case(3):P13=0;P0=duanfen%10;break; 这是时钟程序,可以运行,无小数点,显示时分,小数点断码是0x80,其余的只有靠你自己了。有问题的话,qq :1499648096