Unformatted text preview:

ECE473/573Microprocessor System Design, Dr. Shiue 1Timer and CounterECE473/573Microprocessor System Design, Dr. Shiue 2Timer and Counter• Timer and Counter– Measure the time/frequency of input signal. – Generate outputs with variable frequency and pulse width. – Can be controlled with software. – Can be accessed thru interrupt and software. • Structure of Timer/Counter (a series of divide-by-two flip flop:) • 4 bit counterECE473/573Microprocessor System Design, Dr. Shiue 3Timer and Counter• Not a power of 2 multiple of the clock freq. – Q1: Count-by-10 counter• Timer: when the incoming clock frequency is known. – Get clock from Oscillator frequency (1/12), then by setting the pre-load value. We can generate a fixed period of time known to the designers. • Counter: when the incoming clock is ‘irregular’ – Get clock from external pin, and we are only interested in the number of occurrence of this pulse. This is called counter (counting events)ECE473/573Microprocessor System Design, Dr. Shiue 4Timer 0 and Timer 1• Two 16-bit timers: Timer 0 and Timer 1• Timer 0 (16-bit)• Timer 1 (16-bit)TH0 TL0TH1 TL1ECE473/573Microprocessor System Design, Dr. Shiue 5Setup the Timer and Counter• 2 registers: TMOD and TCON• TMOD: Timer Mode Register  Set the various timer operation mode.– TMOD is an 8-bit register where the lower 4 bits are set aside for timer 0 and the upper 4 bits are set aside for timer 1. MSBLSBGATE C/!T M1 M0 GATE C/!T M1 M0Timer 1 Timer 0ECE473/573Microprocessor System Design, Dr. Shiue 6TMOD RegisterGATE C/!T M1 M0• GATE: To start and stop the timer– GATE=1  HW control : is enabled only while INTx pin is ‘1’and TRx control pin (in TCON) is set. – GATE=0  SW control (used frequently) !INT1!INT0P3.3P3.2ECE473/573Microprocessor System Design, Dr. Shiue 7TMOD Register• C/!T: Timer or counter selection–C/!T = 0  Timer (input from internal system clock)the Osc. Crystal (1/12) is used to trigger the timer.–C/!T = 1  Counter (input from Tx input pin)• M1 and M0: Mode selection for timer and counterMode M1 M0000 13-bit timer/counter mode1 0 1  16-bit timer/counter mode21 0 8-bit auto reload timer/counter mode311 split timer/counter modeT1T0P3.5P3.4ECE473/573Microprocessor System Design, Dr. Shiue 8TCON RegisterMSBLSBTF1TR1 TF0 TR0 IE1 IT1 IE0 IT0Timer 1Timer 1Timer 0 Timer 0• TF1: Timer 1 overflow flag – TF1=1: Timer/counter 1 overflows.– TF1=0: processor vectors to the interrupt services.• TR1: Timer 1 run control bit –TR1=1: turn Timer 1 ON–TR1=0: turn Timer 1 OFFECE473/573Microprocessor System Design, Dr. Shiue 9TCON RegisterMSBLSBTF1 TR1 TF0 TR0 IE1IT1 IE0 IT0Timer 1Timer 1Timer 0 Timer 0• IE1: External interrupt 1 edge flag– IE1=1: external interrupt is detected.– IE1=0: when interrupt is processed. • IT1: Interrupt 1 type control bit – IT1=1: falling edge.– IT1=0: low level triggered external interrupt.ECE473/573Microprocessor System Design, Dr. Shiue 10Run Timer (SW Control)MSBLSBTR1 TR0Timer 1Timer 1Timer 0 Timer 0• Gate=0, SETB TR1  Run Timer 1SETB TR0  Run Timer 0• Gate=0, CLR TR1  OFF Timer 1CLR TR0  OFF Timer 0MSBLSBGATE=0 GATE=0Timer 1 Timer 0TMODTCONECE473/573Microprocessor System Design, Dr. Shiue 11Timer and Counter• Q2: Indicate which timer and which mode are selected for each of the following (a) MOV TMOD, #01H, (b) MOV TMOD, #20H, and (c) MOV TMOD, #12H.• Q3: Find the timers’ clock frequency and its period for various 8051-based systems with the following crystal frequencies. (a) 12MHz, (b) 16MHz, and (c) 11.0592MHz. • Q4: Find the value for TMOD if we want to program Timer 0 in mode 2, use 8051 XTAL for the clock source, and use instructions to start and stop the timer.ECE473/573Microprocessor System Design, Dr. Shiue 12Timer Mode 1 Program• Mode 1: 16-bit timer operation• Steps – 0000 ~ FFFFH to be loaded into the timers’ registers TH and TL. – Start timer: SETB TR0 (i.e. Run Timer 0) – Timer overflow flag (TF1 or TF0) should be monitored– Stop timer: CLR TR0 (OFF Timer 0)– Repeat: TH and TL are loaded again– TF reset to 000000001:FFFF0000OVECE473/573Microprocessor System Design, Dr. Shiue 13Steps to Program for Timer 0, Mode 1 Example• Steps: 1. Load the TMOD value (eg. MOV TMOD, #21H)2. Load TH0 and TL0 (eg. MOV TL0, #9CH & MOV TH0, 0FFH) 3. Start the timer (SETB TR0)4. Keep monitor the timer flag (TF0)target: JNB TF0, target  Jump out only if TF0=15. Stop the timer 0 (CLR TR0)6. CLR TF07. Repeat Step 2.ECE473/573Microprocessor System Design, Dr. Shiue 14Timer Delay Calculation• For XTAL=11.0592MHz(FFFF-YYXX+1) *1.085µs(65535-NNNNN) *1.085µsTH TLHexdecimalORNNNNN is a decimal value derived from YYXXHECE473/573Microprocessor System Design, Dr. Shiue 15Examples• Q5: In the following, we are creating a square wave of 50% duty cycle (with equal portions high and low) on the P1.5 bit. Timer 0 is used to generate the time delay. MOV TMOD, #01Here: MOV TL0, #0F2HMOV TH0, 0FFHCPL P1.5ACALL DelaySJMP HereDelay: SETB TR0Again: JNB TF0, AgainCLR TR0CLR TF0RETECE473/573Microprocessor System Design, Dr. Shiue 16Examples• Q6: See the example in Q5, calculate the amount of time delay in the DELAY subroutine generated by the timer. Assume XTAL =11.0592MHz. • Q7: Find the delay generated by timer 0 in the following code (Do not include the overhead due to instructions). CLR P2.3MOV TMOD, #01Here: MOV TL0, #3EHMOV TH0, #0B8HSETB P2.3SETB TR0Again: JNB TF0, AgainCLR TR0CLR TF0CLR P2.3ECE473/573Microprocessor System Design, Dr. Shiue 17Examples• Q8: Modify TL and TH in Q7 to get the largest time delay possible. Find the delay in ms. (Do not include the overhead due to instructions)• Q9: examples in Q7 and Q8 did not reload TH and TL, since it was a single pulse. The following program generates a square wave on P1.5 continuously using Timer 1 for a time delay. Find the frequency of the square wave if XTAL=11.0592MHz. (Not include overhead due to instructions)ECE473/573Microprocessor System Design, Dr. Shiue 18Examples• Assume we know the amount of timer delay, then how to find the values for TL and TH• Q10: Assume the XTAL =11.0592MHz, what value do we need to load into the timers’ registers if we


View Full Document

OSU ECE 473 - Timer and Counter

Download Timer and Counter
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Timer and Counter and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Timer and Counter 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?