51单片机流水灯、数码管、定时器、秒表、计算器程序.doc

上传人:飞****2 文档编号:61353431 上传时间:2022-11-21 格式:DOC 页数:17 大小:45.50KB
返回 下载 相关 举报
51单片机流水灯、数码管、定时器、秒表、计算器程序.doc_第1页
第1页 / 共17页
51单片机流水灯、数码管、定时器、秒表、计算器程序.doc_第2页
第2页 / 共17页
点击查看更多>>
资源描述

《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;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 教案示例

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁