1、三、系统设计方案按照系统设计的功能的要求,初步确定设计系统由单片机主控模块、八位数码管显示模块、键扫描接口电路共三个主要模块组成。主控芯片使用AT89C52单片机,键盘电路采用4*4矩阵键盘电路,并附有三位功能选择键模块,显示模块采用八位一体共阴极数码管。51系列单片机定时器的结构及工作原理:组成:两个16位的定时器T0和T1,以及他们的工作方式寄存器TMOD和控制寄存器TCON等组成。内部通过总线与CPU相连。定时器T0和T1各由两个8位特殊功能寄存器TH0、TL0、TH1、TL1构成。工作方式寄存器TMOD:用于设置定时器的工作模式和工作方式;控制寄存器TCON:用于启动和停止定时器的计数
2、,并控制定时器的状态;单片机复位时,两个寄存器的所有位都被清0。四、软件仿真单片机最小系统电路图如图五、程序#include#define uchar unsigned char#define uint unsigned short intsbit p14=P14;sbit p15=P15;sbit p16=P16;sbit p17=P17;uchar code table1=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0,0x40;/定义显示段码uchar num,temp;s
3、hort int x1=0,x2=0;uchar operateFlag=0,a=1,b=1,c=1,d=1,e=1,f=1,g=1,h=1;bit result=0,keyDown=0,affirm=0;uchar time=0,seconds=0,minutes=0,hours=0,choice=0;void delay(uchar k)/定义延时函数 uchar i,j; for(i=k;i0;i-) for(j=124;jj-);void TIME0_INT() /初始化定时器0 TMOD=0x01; TH0=(65536-10000)/256; TL0=(65536-10000)%2
4、56; a=b=d=e=g=h=0; c=f=16; EA=1; ET0=1; TR0=1;void timer0() interrupt 1/定时器中断0服务函数 time+;void AllInitial()/每种功能的初始化 if(choice=0|affirm=0) a=h=16; b=c=g=f=16; d=0; e=choice; EA=0; else if(choice=1&affirm=1) h=g=f=e=0; a=b=c=d=16; time=seconds=minutes=hours=0; else if(choice=2& c=f=17; EA=1; else if(c
5、hoice=3& a=b=c=d=e=f=g=16; h=num=0; char keyscan() keyDown=0; P3=0xfe; temp=P3; temp=temp&0xf0; while(temp!=0xf0) delay(5); switch(temp) case 0xee: num=1; break; case 0xde: num=2; case 0xbe: num=3; case 0x7e: operateFlag=0x01;=0xf0) /判断按键是否松开 keyDown=1; P3=0xfd; case 0xed: num=4; case 0xdd: num=5; c
6、ase 0xbd: num=6; case 0x7d: operateFlag=0x02; P3=0xfb; case 0xeb: num=7; case 0xdb: num=8; case 0xbb: num=9; case 0x7b: P3=0xf7; case 0xe7: num=0; case 0xd7: case 0xb7: case 0x77: return num;void smg(uchar a, uchar b, uchar c, uchar d, uchar e,uchar f, uchar g, uchar h) P0=0; P2=0xfe; P0=table1a; de
7、lay(1); P2=0xff; P2=0xfd; P0=table1b; P2=0xfb; P0=table1c; P2=0xf7; P0=table1d; P2=0xef; P0=table1e; P2=0xdf; P0=table1f; P2=0xbf; P0=table1g; P2=0x7f; P0=table1h;void jishu(uchar num1) if(h=0&g=16) h=num1; else if(g=16) g=h; h=num1; else if(f=16) f=g; else if(e=16) e=f; else if(d=16) d=e; else if(c
8、=16) c=d; else if(b=16) b=c; else if(a=16) a=b;b=c;c=d;d=e;e=f;f=g;g=h;h=num1; elsevoid calculator() num=keyscan(); if(keyDown=1) jishu(num); num=0; smg(a,b,c,d,e,f,g,h);/*秒表程序*void mbcs() if(operateFlag=0x01) EA=!EA; if(operateFlag=0x02) time=seconds=minutes=0; if(time=100) time=0; +seconds; if(sec
9、onds=60) seconds=0; +minutes; if(minutes=60) minutes=0; h=time%10; g=time/10; f=seconds%10; e=seconds/10; a=b=c=d=16;/*时钟程序*void szcs() if(num=1) +hours; else if(num=4) -hours; else if(num=2) else if(num=5) -minutes; else if(num=3) +seconds; else if(num=6) -seconds; if(time=100) seconds+; /增长溢出 if(s
10、econds=60) minutes+; seconds=0; if(minutes=60) minutes=0; if(hours=24) hours=minutes=seconds=0; if(minutes=60) +hours; minutes=0; if(hours hours=minutes=seconds=0; if(hours/消减溢出 if(seconds=255) seconds=59;-minutes; if(minutes=255) minutes=59;-hours; if(hours=255) hours=minutes=seconds=0; a=hours/10;
11、 b=hours%10; d=minutes/10; e=minutes%10; g=seconds/10; h=seconds%10;void singal(int wei,int duan,int timeSpan) P2=wei; P0=duan; delay(timeSpan);void mainKey()/扫描按键的功能变化 if(p14=0) delay(5); if(p14=0) choice=1; e=choice; while(!p14); else if(p15=0) if(p15=0) choice=2;p15); else if(p16=0) if(p16=0) cho
12、ice=3;p16); if(p17=0) if(p17=0&choice!=0) affirm=!affirm; AllInitial();p17); void main() TIME0_INT(); EA=0; AllInitial(); while(1) /主循环 mainKey(); if(affirm=1) switch(choice) case 1:mbcs();break;case 2:szcs();case 3:calculator();软件设计好后,在KEIL上面进行编译,把编译得到的hex文件加载到Proteus里面进行仿真,得到Proteus仿真电路图如图。时钟秒表清零键盘选择六 实验心得通过本次单片机最小系统的设计、应用实验的学习,收获颇丰,对单片机最小系统有了一定的认识和了解。基本掌握了利用Protus软件仿真电路的方法。并为电路编写程序,实现一定的功能。巩固了单片机课上所学的知识,而且也学到了很多在书本上所没有学到过的知识,使我懂得了理论与实际相结合是很重要的,在今后的学习实践中,要加强知识与实践相结合的运用。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1