《51单片机流水灯、数码管、定时器、秒表、计算器程序.doc》由会员分享,可在线阅读,更多相关《51单片机流水灯、数码管、定时器、秒表、计算器程序.doc(17页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、流水灯#includechar data_group=0x7e,0xbd,0xdb,0xe7,0xdb,0xbd,0x7e;unsigned int a;void delay(unsigned int time)int i,j;for(i=time;i0;i-)for(j=100;j0;j-);void move_mid_side(void) int m;for(m=0;m=0;a-)temp=temp;P1=temp;delay(500);temp=temp;temp=1;temp=0x80;delay(1000);void move_left_only(void)int a,temp;de
2、lay(500);temp=0x01;for(a=7;a=0;a-)temp=temp;P1=temp;delay(500);temp=temp;temp=0;a-)P1=temp;delay(500);temp=temp1;temp=temp;delay(1000);void move_left_hold(void)int a,temp;temp=0xfe;for(a=8;a=0;a-)P1=temp;delay(500);temp=1;temp=temp;delay(1000);void main()while(1)move_left_hold();move_mid_side();move
3、_right_hlod();move_mid_side();move_left_only();move_right_only();数码管#include char data_duan=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/09char data_wei=0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00; /0xff全关,之后为第一个数码管到第八个数码管/char data_wei_add=0x7f,0x3f,0x1f,0x0f,0x07,0x03,0x01,0x00;void de
4、lay(unsigned int time) int i,j;for(i=time;i0;i-) for(j=0;j100;j+);void show_number_only(void)int i;P2=data_wei1; /P2为位选for(i=0;i10;i+)P0=data_duani;/P0为段选delay(500);/*数码管动态显示子程序*/void move_show1(void)int i;for(i=1;i2;i+)P2=data_weii;P0=data_duani;delay(1);void move_show2(void)int i;for(i=1;i3;i+)P2=
5、data_weii;P0=data_duani;delay(1);void move_show3(void)int i;for(i=1;i4;i+)P2=data_weii;P0=data_duani;delay(1);void move_show4(void)int i;for(i=0;i5;i+)P2=data_weii;P0=data_duani;delay(1);void move_show5(void)int i;for(i=0;i6;i+)P2=data_weii;P0=data_duani;delay(1);void move_show6(void)int i;for(i=0;i
6、7;i+)P2=data_weii;P0=data_duani;delay(1);void move_show7(void)int i;for(i=0;i8;i+)P2=data_weii;P0=data_duani;delay(1);void move_show8(void)int i;for(i=0;i0;i-) move_show1();temp=600;for(i=temp;i0;i-)move_show2();temp=400;for(i=temp;i0;i-)move_show3();temp=200;for(i=temp;i0;i-)move_show4();temp=200;f
7、or(i=temp;i0;i-)move_show5();temp=200;for(i=temp;i0;i-)move_show6();temp=100;for(i=temp;i0;i-)move_show7();temp=100;for(i=temp;i0;i-)move_show8(); void show_all(void) /将所有的位选端口打开,变化段选,显示整体变化19int i;P2=data_wei9;for(i=0;i10;i+)P0=data_duani;delay(500); int main(void)while(1)/show_number_only();move_s
8、how8();/move_show_hold();/show_all();独立按键#include sbit key1=P30;sbit d2=P10;int temp,number=0;void delay(unsigned int time)int i,j;for(i=time;i0;i-)for(j=100;j0;j-) ;int main(void)temp=0xfe;while(1)d2=1;key1=1;P1=temp;if(key1=0)delay(100);if(key1=0) number+; if(number=7) temp=temp; temp=temp1; temp=
9、temp; P1=temp;elsetemp=0xfe;number=0;矩阵按键#includechar data_duan=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,;/09char data_wei=0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00; int number=0;void delay(unsigned int time) int i,j;for(i=time;i0;i-) for(j=0;j100;j+);/
10、void move_show1(void)/int i;/for(i=1;i2;i+)/P2=data_weii;/P0=data_duani;/delay(1);/void move_show2(void)/int i;/for(i=1;i3;i+)/P2=data_weii;/P0=data_duani;/delay(1);/void scan_key(void)int temp;P1=0xfe;temp=P1;temp=temp&0xf0;while(temp!=0xf0) delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0) switch(
11、temp)case 0xe0:number=12;break;case 0xd0:number=13;break;case 0xb0:number=14;break; case 0x70:number=15;break;while(temp!=0xf0) temp=P1; temp=temp&0xf0;P1=0xfd;temp=P1;temp=temp&0xf0;while(temp!=0xf0) delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0)switch(temp)case 0xe0:number=8;break;case 0xd0:num
12、ber=9;break;case 0xb0:number=10;break; case 0x70:number=11;break;while(temp!=0xf0) temp=P1; temp=temp&0xf0;P1=0xfb;temp=P1;temp=temp&0xf0;while(temp!=0xf0) delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0)switch(temp)case 0xe0:number=4;break;case 0xd0:number=5;break;case 0xb0:number=6;break; case 0x
13、70:number=7;break;while(temp!=0xf0) temp=P1; temp=temp&0xf0;P1=0xf7;temp=P1;temp=temp&0xf0;while(temp!=0xf0) delay(1);temp=P1;temp=temp&0xf0;while(temp!=0xf0)switch(temp)case 0xe0:number=0;break;case 0xd0:number=1;break;case 0xb0:number=2;break; case 0x70:number=3;break;while(temp!=0xf0) temp=P1; te
14、mp=temp&0xf0;/return number;int main(void)/int num=0;while(1)/num=scan_key(num);scan_key(); P2=data_wei1; P0=data_duannumber;定时器中断#include char data_duan=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;/09char data_wei=0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00; /0xff全关,之后为第一个数码管到第八个数码管int
15、number=0,time=0;int main(void)/TMOD=0x01;/设置工作方式定时器0/TH0=(65536-50000)/256; /给计数器装初值/TL0=(65535-50000)%256;/EA=1; /开总中断/ET0=1; /定时器T0中断允许/TR0=1; /启动定时器0TMOD=0x10;/设置工作方式定时器1TH1=(65536-50000)/256; /给计数器装初值TL1=(65535-50000)%256;EA=1; /开总中断ET1=1; /定时器T1中断允许TR1=1; /启动定时器1while(1)if(number=20) P2=data_we
16、i2;P0=data_duan4; if(time=40) P2=data_wei0;number=0;time=0; void exter3() interrupt 3TH1=(65536-50000)/256; /给计数器装初值TL1=(65535-50000)%256;time+;number+;/void exter1() interrupt 1/TH0=(65536-50000)/256; /给计数器装初值/TL0=(65535-50000)%256;/time+;/number+;/时钟#include char data_duan=0xc0,0xf9,0xa4,0xb0,0x99
17、,0x92,0x82,0xf8,0x80,0x90;/09char data_wei=0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x00; unsigned long int second=0,minute=0,hour=0,point=0,tip=0;unsigned long int time=0,sign=0,number=0,key=0,variable=0;unsigned long int S1=0,S2=0,M1=0,M2=0,H1=0;H2=0;void delay(unsigned int time) int i,j;for(i
18、=time;i0;i-) for(j=0;jvariable) ; void alarm_show_s(void)int sign2;for(sign2=1;sign22;sign2+) /数码管动态显示 S1=second%10; P2=data_weisign2; P0=data_duanS1; delay(1); for(sign2=2;sign23;sign2+) S2=second/10; P2=data_weisign2; P0=data_duanS2; delay(1); for(sign2=3;sign24;sign2+) P2=data_weisign2; P0=0xbf;
19、delay(1); for(sign2=4;sign25;sign2+) M1=minute%10; P2=data_weisign2; P0=data_duanM1; delay(1); for(sign2=5;sign26;sign2+) M2=minute/10; P2=data_weisign2; P0=data_duanM2; delay(1); for(sign2=6;sign27;sign2+) P2=data_weisign2; P0=0xbf; delay(1); for(sign2=7;sign28;sign2+) H1=hour%10; P2=data_weisign2;
20、 P0=data_duanH1; delay(1); for(sign2=8;sign29;sign2+) H2=hour/10; P2=data_weisign2; P0=data_duanH2; delay(1); void record_set_alarm(void)/设定闹钟所改变的值second=variable%60;minute=variable/60%60;hour=variable/3600%24; if(hour=24)variable=0;void record_time(void)second=time%60;minute=time/60%60;hour=time/36
21、00%24; if(hour=24)time=0;int main(void)TMOD=0x01;TH0=(65536-50000)/256;TL0=(65536-50000)%256;EA=1;ET0=1;TR0=1;while(1)record_time();/record_set_alarm();alarm_show_s();key_scan();adjust_alarm();void extern1() interrupt 1 /定时器中断0 TH0=(65536-50000)/256; /重装值 TL0=(65536-50000)%256; sign+; if(sign=20) time+;sign=0;