1、单片机实训单片机最小系统v单片机以高性能、高速度、体积小、价格低廉、稳定可靠而得到广泛应用,是工业控制、设计智能化仪器仪表的首选微控制器,也是测控、监控的重要组成部分。v单片机最小系统主要包括时钟电路和复位电路。v用流水灯的程序验证单片机最小系统流水灯的程序v#include vvoid delay0_2s()vvunsigned char i;vfor(i=0;i10;i+)/设置10次循环次数vvTH1=0 x3c;/定时器初值vTL1=0 xb0;vTR1=1;/启动T1 vwhile(!TF1);/查询计数是否溢出vTF1=0;/定时时间到vvvvoid main()/主函数vvwhi
2、le(1)vunsigned char i,w;vTMOD=0 x10;/设置T1工作方式1vw=0 x01;/显示码初值vfor(i=0;i8;i+)vP1=w;/w取反后送P1口,点亮相应的流水灯 vw=1;/点亮灯的位置移动左移vdelay0_2s();vvw=0 x80;/显示码vfor(i=0;i=1;/右移vdelay0_2s();/延时vvv流水灯的原理图XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P
3、1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C51C110uC210uR110K6MHZCRYSTAL12345678161514131211109RN13301234567820191817161514139101211U2LED-BARG
4、RAPH-GRNC410uF秒表v设计两位LED数码管显示00-99,计满清零秒表的程序v#include vunsigned char msec,sec;/定义msec为50ms计数变量,sec为秒变量vvoid delay(unsigned char i)/延时函数vunsigned char j,k;vfor(k=0;ki;k+)vfor(j=0;j255;j+);vvoid T0_INT(void)interrupt 1 /定时器0中断类型号为1vTH0=0 x3c;/50ms定时初值vTL0=0 xb0;vmsec+;/中断次数增1vif(msec=20)/中断次数到20次吗?vms
5、ec=0;/是,1s计时到,50ms计数变量清零vsec+;/秒变量增1vif(sec=100)/到100s吗?vsec=0;/是,秒变量清零vvoid main()/主函数vunsigned char led=0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90;/定义数字09字型显示码vunsigned char temp;vTMOD=0 x01;/定时器0工作方式1vTH0=0 x3C;/定时初值50msvTL0=0 xb0;vEA=1;vTR0=1;/开总中断vET0=1;/开定时器0中断vP3=0 xff;/p3口做输
6、入vwhile(1)v vP2=0 x01;/选p2.0控制的数码管vP1=ledsec/10;/显示秒个位vdelay(10);vP2=0 x02;/选p2.1控制的数码管vP1=ledsec%10;/显示秒十位vdelay(10);vtemp=P3;/读入p3口引脚状态并取反vtemp=temp&0 x2c;/屏蔽掉无关位,保留三位按键状态vif(temp!=0)/判断有无按键按下vif(temp=0 x04)/按键暂停键vTR0=0;/停止计数vif(temp=0 x08)/按下启动键vTR0=1;/开始计数vif(temp=0 x20)/按下清零键vTR0=0;sec=0;msec=0
7、;秒表的原理图XTAL218XTAL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5
8、/A1326P2.6/A1427U1AT89C5112345678161514131211109RN1300R11kR21kR31kA02B018A13B117A24B216A35B315A46B414A57B513A68B612A79B711CE19AB/BA1U374LS245串口秒表v用串行口显示00-99,每隔一秒发送一次,循环反复;v程序采用查询方式编写串口秒表的程序v#include vunsigned char led=0 xc0,0 xf9,0 xa4,0 xb0,0 x99,0 x92,0 x82,0 xf8,0 x80,0 x90;vunsigned char msec,s
9、ec;vvoid delay();vvoid time0()interrupt 1v v TH0=0 x3c;vTL0=0 xb0;vmsec+;vif(msec=20)vv msec=0;v sec+;v if(sec=100)sec=0;vvvvoid main()vv TMOD=0 x01;vTH0=0 x3c;vTL0=0 xb0;vEA=1;vET0=1;vvSCON=0;/可不写vTR0=1;vwhile(1)vv SBUF=ledsec%10;v while(TI=0);v TI=0;v SBUF=ledsec/10;v while(TI=0);v TI=0;v delay();
10、vvvvoid delay()vv unsigned char j,k;v for(j=0;j50;j+)v for(k=0;k&1D1324561081112913U274LS164SRG8RC1/-&1D1324561081112913U374LS164交通信号灯顺序控制v设计一个十字路口交通信号灯的顺序控制器v绿灯亮,放行20sv黄灯亮,警告5sv红灯亮,禁止25svS0键按下时,A路放行,B路禁行vS1键按下时,A路禁行,B路放行交通灯的程序v#includevunsigned char t0,t1;/定义全局变量,用来保存延时时间循环次数vvoid delay0_5s1()/延时v
11、for(t0=0;t00 x0a;t0+)/采用全局变量t0作为循环控制变量v TH1=0 x3c;/设置定时初值vTL1=0 xb0;TR1=1;/启动T1vwhile(!TF1);/查询计数是否溢出vTF1=0;/定时时间到v void delay_t1(unsigned char t)v for(t1=0;t1t;t1+)/采用t1作为循环控制变量v delay0_5s1();v void int_0()interrupt 0/紧急情况中断 unsigned char i,j,k,l,m;v i=P1;/保护现象v j=t0;v k=t1;v l=TH1;v m=TL0;v P1=0 x
12、db;/两个方向都是红灯v delay_t1(20);/延时10sv P1=i;/恢复现象v t0=j;v t1=k;v TH1=l;v TL1=m;vvoid int_1()interrupt 2/特殊情况中断v unsigned char i,j,k,l,m;v EA=0;/关中断v i=P1;/保护现象v j=t0;v k=t1;v l=TH1;v m=TL1;v EA=1;/开中断v P1=0 xf3;/A路放行v delay_t1(10);v EA=0;/关中断v vP1=i;/恢复现象v t0=j;v t1=k;v TH1=l;v TL1=m;v EA=1;/开中断vv void
13、main()/主函数v unsigned char k;vTMOD=0 x10;/T1在工作方式1vEA=1;/开总中断允许位vEX0=1;/开外部中断0中断允许位vIT0=1;/设置外部中断0为下降沿触发vEX1=1;/开外部中断1中断允许位vIT1=1;/设置外部中断1为下降沿触发vwhile(1)vP1=0 xf3;/A绿灯,B红灯,延时55svdelay_t1(10);vfor(k=0;k3;k+)/A绿灯闪烁3次vP1=0 xf3;vdelay0_5s1();/延时0.5s vP1=0 xfb;vdelay0_5s1();vP1=0 xeb;/A黄灯,B红灯,延时2Svdelay_t
14、1(4);vP1=0 xde;/A红灯,B绿灯,延时55Svdelay_t1(10);vfor(k=0;k3;k+)/B绿灯闪烁3次vP1=0 xde;vdelay0_5s1();/延时0.5SvP1=0 xdf;vdelay0_5s1();/延时0.5SvP1=0 xdd;/A红灯,B黄灯,延时2Svdelay_t1(4);交通灯的原理图A-redA-redB-redB-redA-greenA-greenA-yellowA-yellowB-yellowB-redA-greenA-yellowA-redB-greenB-greenB-greenB-yellowB-yellowXTAL218XT
15、AL119ALE30EA31PSEN29RST9P0.0/AD039P0.1/AD138P0.2/AD237P0.3/AD336P0.4/AD435P0.5/AD534P0.6/AD633P0.7/AD732P1.01P1.12P1.23P1.34P1.45P1.56P1.67P1.78P3.0/RXD10P3.1/TXD11P3.2/INT012P3.3/INT113P3.4/T014P3.7/RD17P3.6/WR16P3.5/T115P2.7/A1528P2.0/A821P2.1/A922P2.2/A1023P2.3/A1124P2.4/A1225P2.5/A1326P2.6/A1427U1AT89C51R1300R2300R3300R4300R5300R6300R7300R8300R9300R10300R11300R12300R13300R14300D7LED-REDD8LED-REDD9LED-REDD10LED-REDD2LED-GREEND3LED-GREEND4LED-GREEND5LED-YELLOWD6LED-YELLOWD12LED-YELLOWD1LED-GREEND11LED-YELLOW实训总结