单片机原理及应用(双语版·新形态版) 课件 第6章定时器计数器与串行口操作_第1页
单片机原理及应用(双语版·新形态版) 课件 第6章定时器计数器与串行口操作_第2页
单片机原理及应用(双语版·新形态版) 课件 第6章定时器计数器与串行口操作_第3页
单片机原理及应用(双语版·新形态版) 课件 第6章定时器计数器与串行口操作_第4页
单片机原理及应用(双语版·新形态版) 课件 第6章定时器计数器与串行口操作_第5页
已阅读5页,还剩114页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

IntroductoryMicrocontrollerTheoryandApplicationsChapter6Timer/CounterandSerialPortOperation定时COUNTER/TIMEROPERATIONSTART: MOV R0,#0FFhLOOP1: MOV R1,#0C3h DJNZ R1,$ DJNZ R0,LOOP1 RETBusyWaiting计数WhydoweneedaCounter/Timer?HardwarecircuitOscillatorInterruptCPUROMRAMTimer1Timer0I/OportsSerialExternalinterruptsCounterinputsP0P1P2P3TXDRXD8051MicrocontrollerWhatcanwedowithaCounter/Timer?LotsofthingsMeasurethetimeofaneventProduceatimedelayCountitemsWhatisthedifferencebetweenaCounterandTimer?COUNTER/TIMEROPERATIONOSCILLATOR12:101TimerCounterC/Tbit(inTMOD)PinT0COUNTER/TIMEROPERATIONCPUCrystalMachinecyclesClockcyclesT1(16bits)TH1TL1GTFTRC/TTMODTCONINT10=OpenT1Timer1Osc/12C/TP3.5P3.3Counter01TimerStartOverflow0111=Closed40239133843753663573483393210311130122913281427152616251724182319222021P1.0P1.1P1.2P1.3P1.4P1.5P1.6P1.7P2.7P2.6P2.5P2.4P2.3P2.2P2.1P2.0Vcc+5vP0.0P0.1P0.2P0.3P0.4P0.5P0.6VssGNDXTAL2P3.0RXDRSTP3.1TXDP3.2INT0P3.3INT1P3.4T0P3.5T1P3.6WRP3.7RDXTAL1P0.7Vpp/EAPROG/ALEPSEN8051P1P0P2P3DoublefunctionpinsT0TH0TL0T1TH1TL1COUNTER/TIMERSTF1TR1TF0TR0IE1IT1IE0IT0TimerFlag(overflow溢出)TimerRun(Start)COUNTER/TIMERCONTROLTCONregisterTimerFlag(overflow溢出)TimerRun(Start)GATEC/TM1M0GATEC/TM1M0CounterorTimer?TimermodeCOUNTER/TIMERMODESTMODregisterM1M0Mode000011102113Timer1Timer0Notbit-addressableTimerMode0T0TF0flag01FFFh0Interruptsdisabled13-bittimerTR0bit01SETBTR0MOVTH0,#1DhMOVTL0,#57hTimerMode0T0TF0flag011FFFh0Interruptsdisabled13-bittimerTR0bit1TimerMode1T0TF0flag0FFFFh0Interruptsdisabled16-bittimer1SETBTR0MOVTH0,#7DhMOVTL0,#57hTR0bit01TimerMode1T0TF0flag01FFFFh0Interruptsdisabled16-bittimerTR0bit1TL0TimerMode2TH0TF0flag0FFhFFhInterruptsdisabled8-bittimerTR0bitSETBTR0MOVTH0,#43HPreloadvalue01TL0TimerMode2TH0TF0flag01FFhFFhInterruptsdisabled8-bittimerTR0bitPreloadvalue18-bittimerTL0TimerMode2TH0TF0flag01FFhFFhCLRTF0InterruptsdisabledTR0bitPreloadvalue18-bittimerTL0TimerMode2TH0TF0flag01FFhFFhCLRTF0InterruptsdisabledTR0bitPreloadvalue1TH0TimerMode3TL0TF0flag0FFhFFhTwo8-bittimersTR1bit1TR0bit0TF1flag0SETBTR1MOVTL0,#C3hMOVTH0,#63h0TH0TimerMode3TL0TF0flag0FFhFFhTwo8-bittimersTR1bit1TR0bit1TF1flag0SETBTR00TH0TimerMode3TL0TF0flag0FFhFFhTwo8-bittimersTR1bit1TR0bit1TF1flag011IFTimerInterruptisenabledTHEN TF0/TF1flagisclearedautomatically atstartoftheISRIFTimerInterruptisdisabledTHEN TF0/TF1flagmustbeclearedbythe program1.LoadcounterwithpreloadvalueT0TimerMode1(16-bittimer)00TR0TF0Interruptenabled预先加料1.Loadcounterwithpreloadvalue01111111111111110T0FFFBTimerMode1(16-bittimer)00TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR001111111111111110T0FFFBTimerMode1(16-bittimer)10TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR001111111111111111T0Timingpulse1FFFB10TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR001111111111111110T0Timingpulse2FFFC10TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR011111111111111101T0Timingpulse3FFFD10TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR011111111111111110T0Timingpulse4FFFE10TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR011111111111111111T0Timingpulse5FFFF10TR0TF0Interruptenabled1.Loadcounterwithpreloadvalue2.StartcounterbysettingTR03.Onoverflow:TF0isset(inTCON) Interruptgenerated ISRcalled.TFcleared

0000000000000000T0Timingpulse6000011TR0TF0Interruptenabled1.LoadTH0withpreloadvalue(FCh)TH0TimerMode2(8-bitauto-reload)00TR0TF0TL00111111000000000LoadTH0withpreloadvalueStartcounterbysettingTR0TH0TimerMode2(8-bitauto-reload)10TR0TF0TL00111111000000000LoadTH0withpreloadvalueStartcounterbysettingTR0TH0valueisputintoTL0.Starts TL0as8-bitcounterTH0TimerMode2(8-bitauto-reload)10TR0TF0TL01111111001111100LoadTH0withpreloadvalueStartcounterbysettingTR0TH0valueisputintoTL0.Starts TL0as8-bitcounterTH0TimerMode2(8-bitauto-reload)10TR0TF0TL01111111001111101LoadTH0withpreloadvalueStartcounterbysettingTR0TH0valueisputintoTL0.Starts TL0as8-bitcounterTH0TimerMode2(8-bitauto-reload)10TR0TF0TL01111111001111110LoadTH0withpreloadvalueStartcounterbysettingTR0TH0valueisputintoTL0.Starts TL0as8-bitcounterTH0TimerMode2(8-bitauto-reload)10TR0TF0TL01111111001111111LoadTH0withpreloadvalueStartcounterbysettingTR0TH0valueisputintoTL0.Starts TL0as8-bitcounter 4.SetsTF0.CallsISRTH0TimerMode2(8-bitauto-reload)11TR0TF0TL00111111000000000LoadTH0withpreloadvalueStartcounterbysettingTR0TH0valueisputintoTL0.Starts TL0as8-bitcounter 4.SetsTF0.CallsISR 5.Reloads.StartscountingTH0TimerMode2(8-bitauto-reload)10TR0TF0TL01111111001111100TimerInitialisationStepsSettheTimermodeTMODCalculatethepreloadvalue--T0/T1EnabletheinterruptsIEStartthetimerrunningTR0/TR1

6.1.7CalculationofPreloadValuePreloadvalue= Maximumvalue+1–CountvalueModeMaximumPreloadEquation01FFFh(8191)Preloadvalue=8191+1–Countvalue1FFFFh(65535)Preloadvalue=65535+1–Countvalue2FFh(255)Preloadvalue=255+1–Countvalue3FFh(255)Preloadvalue=255+1–CountvalueExample6.1.10.1 AnelectronicpeoplecounterReset8051P1.0LED+5VP0.7T0Counter0125000TimerInitialisationStepsSettheTimermodeTMOD Counter0,Mode1,TMOD=05H2.Calculatethepreloadvalue--T0/T1

3.EnabletheinterruptsIE

4.StartthetimerrunningTR0/TR1

TimerInitialisationStepsSettheTimermodeTMOD Counter0,Mode1,TMOD=05H2.Calculatethepreloadvalue--T0 FE0Ch3.EnabletheinterruptsIE

4.StartthetimerrunningTR0/TR1

PreloadValue=Maximumvalue+1-CountForCount=500Preloadvalue=65535+1–500=FE0ChInitialisationStepsSettheTimermodeTMOD Counter0,Mode1,TMOD=05H2.Calculatethepreloadvalue--T0/T1 FE0Ch3.EnabletheinterruptsIE EA,ET04.StartthetimerrunningTR0/TR1

TR0YNStartEnableinterruptISRReturnReset?InitialisetimerStopcounterLOOP:TurnontheLEDStartcounterTurnofftheLEDReloadthecounter

ORG 0000h SJMP MAIN ;JumpoverISR ORG 000BhAJMP TIMERINTMAIN:MOV P0,#0FFh ;MakeP0input SETB P1.0 ;LEDoff MOV TMOD,#05h ;Counter0,Mode1 SETB EA SETB ET0 ;EnableinterruptsLOOP:MOV TH0,#0FEh MOV TL0,#0Ch ;Loadcounter

SETB TR0 ;StartcounterJNB P0.7,$ ;WaitforReset CLR TR0 ;StopcounterSETB P1.0 ;TurnoffLEDSJMP LOOPTIMERINT: CLR P1.0;TurnonLED MOV TH0,#0FEh MOV TL0,#0Ch;Reloadcounter

RETI

Pleasetrythefollowing.. Question6.3.1 Question6.3.2 Question6.3.17

(Pages132-4)Question6.3.1 PurposeofC/TbitToselectCounter(1)orTimer(0)Question6.3.2WhenisTF0/TF1set?Whencounteroverflows(Maxvalue+1) ORG 0000h AJMP _____ ORG _____ ;Timer0ISR AJMP COUNTMAIN: MOV TMOD,_____ ;Cntr0,Mode1 SETB P1.0 SETB ____ ;Enableints SETB ____ ;EnableCntr0 MOV TH0,_____;Preload MOV TL0,_____ SETB ____ ;StartCountLOOP: NOP ;Mainprog SJMP LOOP COUNT: _____ P1.0 ;ResetP1.0 _____ ;ReturnMAIN000Bh#01hEAET0#0FCh#18hTR0CLRRETIExample6.1.10.2ElectroniccamerashuttercontrolP2.0P3.0ButtonShutter80511=Pressed0=Normal0=Closed1=OpenExample6.1.10.2ElectroniccamerashuttercontrolP2.0P3.0ButtonShutter80510=Normal0=Closed12MHz:1machinecycle=1ms50ms=50000machinecyclesTimerInitialisationStepsSettheTimermodeTMOD Timer1,Mode1,TMOD=10h2.Calculatethepreloadvalue--T1

3.EnabletheinterruptsIE

4.StartthetimerrunningTR0/TR1

C/TM1M0GATEC/TM1M0Timer1Timer0TMODregisterGATE00010000TimerInitialisationStepsSettheTimermodeTMOD Timer1,Mode1,TMOD=10h2.Calculatethepreloadvalue--T0/T1 3CB0h3.EnabletheinterruptsIE

4.StartthetimerrunningTR0/TR1

1machinecycle=1ms50msrequires50000machinecycles

PreloadValue=Maximumvalue+1-CountForCount=50000Preloadvalue=65535+1–50000=3CB0hTimerInitialisationStepsSettheTimermodeTMOD Timer1,Mode1,TMOD=10h2.Calculatethepreloadvalue--T0/T1 3CB0h3.DisabletheinterruptIE ET1=04.StartthetimerrunningTR0/TR1

TR1

BUTTON BIT P2.0 SHUTTER BIT P3.0 ORG 0000H MAIN: SETB BUTTON MOV TMOD,#10h;Timer1,Mode1

CLR TF1REPEAT: MOV TH1,#3Ch MOV TL1,#0B0h;Loadcounter JNB BUTTON,$;Waitforbutton SETB SHUTTER;Openshutter SETB TR1;Starttimer

JNB TF1,$ ;Waitforoverflow CLR TF1 CLR SHUTTER;Closeshutter

JB BUTTON,$;WaitforNormal SJMP REPEATExample6.1.10.3SquarewaveonP3.0.Freq=1kHz;Timer0Mode1;Clock=12MHzP3.080511ms.5msInitialisationStepsSettheTimermodeTMOD Timer0,Mode1,TMOD=01h2.Calculatethepreloadvalue--T0

3.DisabletheinterruptIE

4.StartthetimerrunningTR0

C/TM1M0GATEC/TM1M0Timer1Timer0TMODregisterGATE00010000InitialisationStepsSettheTimermodeTMOD Timer0,Mode1,TMOD=01h2.Calculatethepreloadvalue--T0 FE0Ch3.DisabletheinterruptIE Disableinterrupts4.StartthetimerrunningTR0

SetTR0 1machinecycle=1ms .5msrequires500machinecycles

ForCount=500Preloadvalue=65535+1–500=FE0Ch

WAVE BITP3.0 ORG 0000h MAIN: MOV TMOD,#01h;Timer0,Mode1 CLR TF0;ClearTFflagNEXT: MOV TL0,#0Ch MOV TH0,#0FEh;Preloadtimer

SETB TR0

;Starttimer JNB TF0,$;Awaitoverflow

CLR TR0

;Stoptimer CLR TF0 ;ClearTFflag CPL WAVE;Toggleoutput

SJMP NEXTExample6.1.10.3SquarewaveonP3.0.Freq=1kHz;Timer0Mode1;Clock=12MHzPleasetrythefollowing..Question6.3.7(Pages133)Question6.3.7P0.0Example6.1.10.4TimeforbulletP0.1GunTarget0011Puttimein21h(highbyte)&20h(lowbyte)UseTimer0inMode1.Nointerrupts1machinecycle=1ms8051InitialisationStepsSettheTimermodeTMOD Timer0,Mode1,TMOD=01h2.Calculatethepreloadvalue--T0

3.DisabletheinterruptIE

4.StartthetimerrunningTR0

C/TM1M0GATEC/TM1M0Timer1Timer0TMODregisterGATE00010000InitialisationStepsSettheTimermodeTMOD Timer0,Mode1,TMOD=01h2.Calculatethepreloadvalue--T0 0000h3.DisabletheinterruptIE Disableinterrupts4.StartthetimerrunningTR0

SetTR0 GUN BIT P0.0 TARGET BIT P0.1 ORG 0000h MAIN: MOV P0,#0FFh ;SetP0forinput MOV TMOD,#01h;Timer0,Mode1 MOV TL0,#00h MOV TH0,#00h;Preload=0 JNB GUN,$;WaitforGun SETB TR0;Starttiming

JNB TARGET,$;

WaitforTarget CLR TR0;Stoptimer MOV 21h,TH0 ;Timeinms MOV 20h,TL0 ;inT0

SJMP $GATEC/TM1M0GATEC/TM1M0GATEbitsTimer1Timer0TMODregister6.1.9GATEcontroloftimersTR1bit:Controlbyasoftwareinstructionintheprogram.SETBTR1CLRTR1GATE:ControlbyanexternalvoltageonpinINT0/INT1.StartingandstoppingthetimerT1TH1TL1GTFTRC/T=0TMODTCONINT10=OpenT1Timer1Osc/12C/TP3.5P3.3T1TH1TL1GTFTRC/T=0TMODTCONINT10=OpenT1Timer1Osc/12C/T01P3.5P3.3T1TH1TL1GTFTRC/T=0TMODTCONINT10=OpenT1Timer1Osc/12C/T10INT1*TR1P3.5P3.3IFG=1THENtimerwillrunifTR1*INT1=1(ifbothINT1andTR1arehigh)

IFG=0THENtimerwillrunifTR1=1TR1bit:Controlbyasoftwareinstructionintheprogram.SETBTR1CLRTR1GATE:ControlbyanexternalvoltageonpinINT0/INT1.Startingandstoppingthetimer6.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHz6.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHzINT08051P3.0Start(1)Stop(0)SSGATEinputPhotocell6.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHzINT08051timeINT0P3.0Start(1)Stop(0)SStcartimeINT0GStartStopNotcountingNotcountingCountingGATEinputSettheTimermodeTMOD

Timer0Mode1GATE=12.Calculatethepreloadvalue--T0/T1

3.EnabletheinterruptsIE

4.StartthetimerrunningTR0/TR1

6.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHzGATEC/TM1M0GATEC/TM1M0Timer1Timer0TMODregister11006.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHz1.SettheTimermodeTMOD

Timer0Mode1GATE=ON,TMOD=09h2.Calculatethepreloadvalue--T0/T1

T0=00003.EnabletheinterruptsIE

ET0=0(Disabled)4.StartthetimerrunningTR0

SetTR0

6.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHz SS EQU P3.0 ;Start/stopbutton ORG 0000h MAIN: MOV TMOD,#09h;

Timer0,Mode1 ;GATE=1 MOV TL0,#00h MOV TH0,#00h;

Timer=0000h

JNB SS,$ ;WaitforStart SETB TR0;

Starttimer

JB SS,$ ;WaitforStop

CLR TR0 ;Stoptimer ;T0nowcontainsthevalueoftcar

;inms6.1.10.5Measurethespeedofacar Timer0Mode1.Clock=12MHzPleasetrythefollowing..Question6.3.15Question6.3.16(Page134)Question6.3.15Longestdelay Mode1Clock=11.0592MHzQuestion6.3.16Mode1 Clock=6MHzPreload=03AEhTXRX6.2SERIALDATACOMMUNICATION(RS232Protocol)协议接收发送1101001011010010TXRX110100101101001011110000发送接收LeastSignificantBit最低位MostSignificantBit最高位6.2SERIALDATACOMMUNICATION(RS232Protocol)TXRXParallelCommunication10000111100001111101001011010010HigherspeedMorewiresComplexcircuitboardOnedirectionatonetimeLPTportsoncomputerTXRX1101001011010010SERIALDATACOMMUNICATION(RS232Protocol)11110000发送接收OnewireeachwaySimplecircuitboardBothdirectionsatsametimeSlowspeedCOMportsoncomputerDatabits数据StartbitIdle闲Paritybit校验位StopbitsClock01234567AnRS232Packet

SERIALDATACOMMUNICATION(RS232Protocol)RXDTXDGNDGNDRTSCTSSERIALDATACOMMUNICATIONDB9ConnectorPinsDevice1SERIALDATACOMMUNICATION(RS232Protocol)Device2TXDTXDRXDRXDTheRS232ProtocolLogicLevelTTLvoltageRS232voltage00volts+12volts15volts-12voltsSpeedsoftransmission:-600,1200,2400,4800,9600bits/s (baud)SERIALDATACOMMUNICATION(RS232Protocol)TTLtoRS232converterTTLtoRS232converterLevelConversion:TTLtoRS232(Page129)MAX232+5VTXDTXDRXDRXDTTLRS232++++GNDGND6.2.4TheSerialPortonthe8051SetifTransmitSBUFemptySetifReceiveSBUFhasabyteCPUbusSM0SM1SM2RENTB8RB8TIRISerialControlRegister-SCONTransmitFlag发送标志ReceiveFlag接收标志TheSerialPortonthe8051ModeSelectbitsReceiveENableTheSerialPortonthe8051ModeSM0SM1DescriptionBaudrate000ShiftregisterOsc/121018-bitUARTVariable2109-bitUARTOsc/64Osc/323119-bitUARTVariable通用异步收发器Clock01234567AnRS232Packet

HowdowegeneratetheSerialPortclock?UseTimer1inMode2BaudRateforserialportmode1UseTimer1inMode2.SubroutinetoinitialisetheSerialPort:-INIT: CLR SM0 SETB SM1 ;Setmode1 MOV PCON,#00h;SMOD=0 MOV TH1,#0FDh;9600 SETB REN;RXenable RETTotransmitdatafromA: JNB TI,$;Waitforemptybuffer MOV SBUF,AToreceivedataintoA: JNB RI,$;Waitforbyteinbuffer MOV A,SBUFPollingofSerialPortBusywaitingBusywaiting6.2.11SerialportinterruptflagsRI

- SetwhenabyteisreceivedTI

- Setwhenabytehasbeen transmittedMustbeclearedintheISR6.2.11.2Task:8-bitserialcommunicationat9600baudEachreceivedbyteputinto40hto6Fh.

40h6FhRXD01SBUFRI

ORG 0000h AJMP MAIN ORG 0023hISR_SERIAL: MOV @R0,SBUF INC R0 CJNE R0,#70h,DONE MOV R0,#40hDONE: CLR RI RETIMAIN: _______________ _______________ 6.2.11.2Task:MAIN: MOV R0,#40h ;Startofbuff MOV TMOD,#20h ;T1Mode2 MOV TH1,#0FDh;Baud=9600 SETB EA SETB ES ;Serialinterrupt CL

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论