这是我初学单片机,并用Proreus软件自己绘制自己编写程序实现的一个交通红绿灯的系统。这是硬件连接图和程序,以便今后复习。左上右下中控制部分,上面一个开关按下时,南北方向绿灯,东西方向红灯黄灯闪烁3秒绿灯设置15秒,红灯设置18秒。绿灯走完15秒后黄灯闪烁3秒变为红灯,红灯走完18秒后变成绿灯,以此循环。源程序:COUNTEQU30H;T0中断计数SNLEDDATAEQU31H;存储南北数码管显示的数据SNCOLORFLAGEQU32H;=1时南北绿灯亮,=2时南北黄灯亮,=3时南北红灯亮SNLEDDATAINITEQU33H;存储南北各颜色灯的初始时间EWLEDDATAEQU34H;存储东西数码管显示的数据EWCOLORFLAGEQU35H;=1时东西红灯亮,=2时东西绿灯亮,=3时东西黄灯亮EWLEDDATAINITEQU36H;存储东西各颜色灯的初始时间org0000hlJMPMAINorg0003hljmpexint0ORG000BHlJMPINTT0org0013hljmpexint1org0100h;主函数**************************************MAIN:CALLINITSTART:MOVA,COUNTCJNEA,#200,NOT1S;没到1秒跳转MOVCOUNT,#00;1秒时间到DECSNLEDDATAMOVA,SNLEDDATAJNZcheckewleddataMOVSNLEDDATA,SNLEDDATAINITINCSNCOLORFLAGMOVA,SNCOLORFLAGCJNEA,#04,checkewleddataMOVSNCOLORFLAG,#01checkewleddata:DECEWLEDDATAMOVA,EWLEDDATAJNZtimenotoverMOVEWLEDDATA,EWLEDDATAINITINCEWCOLORFLAGMOVA,EWCOLORFLAGCJNEA,#04,timenotoverMOVEWCOLORFLAG,#01TIMENOTOVER:NOT1S:callewcolorcheckCALLsnCOLORCHECKCALLDISPLAYJMPSTART;*********************************************;初始化函数***********************************INIT:MOVSP,#60HMOVTMOD,#01HMOVTH0,#0eCHMOVTL0,#078HMOVCOUNT,#00HMOVSNLEDDATAINIT,#15MOVEWLEDDATAINIT,#18MOVSNLEDDATA,SNLEDDATAINITMOVEWLEDDATA,EWLEDDATAINITSETBTR0;开定时器0SETBET0;允许定时器0中断setbex0;允许外部0中断setbex1;允许外部1中断setbit0;外部中断0下降沿触发setbit1;外部中断1下降沿触发SETBEA;开总中断MOVP2,#0feHMOVSNCOLORFLAG,#01MOVEWCOLORFLAG,#01RET;*****************************************;外部中断0***********************************exint0:pushaccpushpswmova,p0pushaccmova,p1pushaccmova,p2pushaccclrex1clrp2.0clrp2.1clrp2.2clrp2.3setbp1.4clrp1.1clrp1.2setbp1.5clrp1.3clrp1.4jnbp3.2,$setbex1popaccmovp2,apopaccmovp1,apopaccmovp0,apoppswpopaccreti;外部中断1***********************************exint1:pushaccpushpswmova,p0pushaccmova,p1pushaccmova,p2pushaccclrex0clrp2.0clrp2.1clrp2.2clrp2.3setbp1.4setbp1.2clrp1.1clrp1.0setbp1.3clrp1.5clrp1.4jnbp3.3,$setbex0popaccmovp2,apopaccmovp1,apopaccmovp0,apoppswpopaccreti;T0中断函数*******************************INTT0:PUSHACCPUSHPSWMOVTH0,#0ecH;#0echMOVTL0,#078H;#078hMOVA,P2RLACJNEA,#11101111B,NOT5MOVA,#0feH;p2.0=0,南北数码管十位亮,p2.1=0,南北数码管个位亮NOT5:MOVP2,A;p2.2=0,东西数码管十位亮,p2.3=0,东西数码管个位亮INCCOUNTPOPPSWPOPACCRETI;****************************************;南北红绿黄灯控制**************************snCOLORCHECK:PUSHACCPUSHBPUSHPSWsngreen:MOVA,SNCOLORFLAGCJNEA,#01,snYELLOWMOVSNLEDDATAINIT,#03;设置南北黄灯闪烁时间CLRP1.2CLRP1.1SETBP1.0;南北绿灯亮JMPsnCOLORCHECKOUTsnYELLOW:;MOVA,SNCOLORFLAGCJNEA,#02,snredMOVSNLEDDATAINIT,#18;设置南北红灯亮时间MOVA,COUNTMOVB,#100DIVABMOVA,BCJNEA,#00H,snNOTFLASHCPLP1.1;南北黄灯闪烁CLRP1.0CLRP1.2snNOTFLASH:JMPsnCOLORCHECKOUTsnred:MOVSNLEDDATAINIT,#15;设置南北绿灯亮时间CLRP1.0CLRP1.1SETBP1.2;南北红灯亮snCOLORCHECKOUT:POPPSWPOPBPOPACCRET;******************************************************;东西红绿黄灯控制**************************************ewCOLORCHECK:PUSHACCPUSHBPUSHPSWewred:MOVA,ewCOLORFLAGCJNEA,#01,ewyellowMOVewLEDDATAINIT,#15;设置东西绿灯亮时间CLRP1.3CLRP1.4SETBP1.5;东西红灯亮JMPewCOLORCHECKOUTewyellow:;MOVA,ewCOLORFLAGCJNEA,#03,ewgreenMOVewLEDDATAINIT,#18;设置东西红灯亮时间MOVA,COUNTMOVB,#100DIVABMOVA,BCJNEA,#00H,ewNOTFLASHCPLP1.4;东西黄灯闪烁CLRP1.3CLRP1.5ewNOTFLASH:JMPewCOLORCHECKOUTewgreen:MOVewLEDDATAINIT,#03;设置东西黄灯闪烁时间CLRP1.4CLRP1.5SETBP1.3;东西绿灯亮ewCOLORCHECKOUT:POPPSWPOPBPOPACCRET;************************************************************;数码管显示**************************************************DISPLAY:PUSHACCPUSHPSWPUSHBMOVA,SNLEDDATAMOVB,#10DIVABMOVR0,BMOVR1,AMOVA,EWLEDDATAMOVB,#10DIVABMOVR2,BMOVR3,AMOVDPTR,#TABLEMOVA,P2DISPSNH:CJNEA,#0feH,DISPSNLMOVA,R1MOVCA,@A+DPTRMOVP0,AJMPDISPOUTDISPSNL:CJNEA,#0fdH,DISPEWHMOVA,R0MOVCA,@A+DPTRMOVP0,AJMPDISPOUTDISPEWH:CJNEA,#0fbH,DISPEWLMOVA,R3MOVCA,@A+DPTRMOVP0,AJMPDISPOUTDISPEWL:CJNEA,#0f7H,DISPOUTMOVA,R2MOVCA,@A+DPTRMOVP0,ADISPOUT:POPBPOPPSWPOPACCRET;**************************************************TABLE:DB3FH,06H,5BH,4FH,66H,6DH,7DH,07H,7FH,6FH;0123456789END