Microcontroller Based Design Muhammad Usman Rafique

Week 5 – Timers and Counters

2

M. Usman Rafique

Air University, Spring 2015

Course Outline Week 1

Introduction

2

8051Microcontroller  Architecture and Hardware

3



Assembly Language

4



Assembly Language Contd.

5



Timers and Counters

6



Serial Port

7



Interrupt

8, 9



Design and Interface Examples

10

Midterm Exam

11

PIC 18 F Microcontroller  Introduction, Architecture, I/O Pins

12



Programming in C

13



Timers / Counters

14



Using Internal ADC of PIC

15, 16



Peripherals of PIC 18F Microcontrollers

17

3

Topic

Revision

M. Usman Rafique

Air University, Spring 2015

Organization 

Introduction



Timers 



Delay Generation Measuring Time

Counters



4

M. Usman Rafique

Air University, Spring 2015

Timers and Counters 2 Timers in 8051







T0 T1

Timers can be used to 1. Measure time between events 2. Generate required amount of time 3. Count events 4. Generate baud rate for Serial communication 

5

M. Usman Rafique

Air University, Spring 2015

Timers and Counters Modes of operation are 2 Timers

Timers

Measure Time

6

Counters

Serial Port

Generate Time

M. Usman Rafique

Air University, Spring 2015

Timers and Counters Always counts up in any operational mode







Timer Counter

Timer





Timer increments by 1 every machine cycle

Counter





Increments at every input pulse

Time of 1 Machine Cycle, for 12 MHZ crystal? 1 micro second for crystal of 12 MHz 7

M. Usman Rafique

Air University, Spring 2015

Timers and Counters Time of 1 Machine Cycle for 11.0592 MHz crystal?  Oscillator (Crystal) : 11.0592 MHz 12 Clock cycles make 1 Machine cycle



1 Machine Cycle = 12 Clock cycles 

Time of 1 clock cycle = 1 / 11.0592M



Time of 1 Machine Cycle = 12* (1 / 11.0592M ) 1 Machine Cycle 8

= 1.0851 Micro Seconds M. Usman Rafique

Air University, Spring 2015

Timers and Counters Timer SFRs

9

M. Usman Rafique

Air University, Spring 2015

Timers and Counters Timer SFRs

10

SFR

Timer

Description

TMOD

T0 and T1

Set Timer Mode

TCON

T0 and T1

Control Timer Operation

TL0

T0

Timer 0 Low Byte

TH0

T0

Timer 0 High Byte

TL1

T1

Timer 1 Low Byte

TH1

T1

Timer 1 High Byte

M. Usman Rafique

Air University, Spring 2015

Timers and Counters 

Separate Registers – TH and TL  

High and Low byte of Timer Timer 0: TH0 (1 Byte)

TL0 (1 Byte) TIMER 0



Timer 1: TH1 (1 Byte)

TL1 (1 Byte) TIMER 1

11

M. Usman Rafique

Air University, Spring 2015

Timers and Counters Configuring Timers TMOD

12

Bit

Name

Explanation of Function

Timer

GATE1

= 1 Timer will only run when INT1 is high. (Calculate Time between events) = 0 for Timer1

1

6

C/T1

=1 Input on P3.5 drives time. Counter Mode = 0 Crystal drives Timer. Timer Mode

1

5

T1M1

Timer mode bit

1

4

T1M0

Timer mode bit

1

3

GATE0

= 1 Timer will only run when INT0 is high. (Calculate Time between events) = 0 for Timer0

0

2

C/T0

=1 Counter = 0 Timer

0

1

T0M1

Timer mode bit

0

0

T0M0

Timer mode bit

0

7

Timers and Counters

13

M. Usman Rafique

Air University, Spring 2015

Timers

14

M. Usman Rafique

Air University, Spring 2015

Timers and Counters Modes of operation are 2 Timers

Timers

Measure Time

15

Counters

Serial Port

Generate Time

M. Usman Rafique

Air University, Spring 2015

Timers 

Timer Operation 1. 2. 3.

16

Configure timers (TMOD) Initialize values (TH and TL) Start and Stop Timer (TCON)

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 Timers 



TH0 (1 Byte)

TH and TL contain the ‘value’ of timer Timers count UP. This value will be incremented by 1 every machine cycle

TL0 (1 Byte) TIMER 0

TH1 (1 Byte)

TL1 (1 Byte) TIMER 1



When TH and TL have maximum value, next machine cycle would set them both to ZERO.



Practice 

17

What is the maximum value TH and TL (1 Byte each) can have?

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 Timers TH0 (1 Byte)



What is the maximum value TH and TL (1 Byte each) can have?



Maximum value = 1111 111 1111 1111 TH



18

TL0 (1 Byte) TIMER 0

TL

= FF FF = 65,535

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers  Maximum value of Timer Registers= FF FF = 65,535

Timer Operation

TH0 (1 Byte)

TL0 (1 Byte) TIMER 0

Timer Value (TH , TL)

Time 19

M. Usman Rafique

Air University, Spring 2015

Timers Timer Modes    

20

Mode 0 – 13 Bit Timer Mode 1 – 16 Bit Timer Mode 2 – 8 Bit Timer Mode 3 – Split Timer

M. Usman Rafique

Air University, Spring 2015

Timers Timer Modes Mode 0 – 13 Bit Timer





To keep compatibility with 8048 (old) TL counts 0 to 31 From 31, TL resets to ZERO and increments TH by 1



Not used generally

 

21

M. Usman Rafique

Air University, Spring 2015

Timers Timer Modes 65,535d FF FFh

Mode 1 – 16 Bit Timer

 

Commonly used Counts up to FF FF and resets to zero



Working: as discussed previously



Timer Value (TH , TL)

22

M. Usman Rafique

Air University, Spring 2015

Timers Timer Modes Mode 2 – 8 Bit Timer

   

8 Bit Auto-Reload Mode TH = Reload value TL = Time Value Timer Value

Reset Reload Value

Time 23

M. Usman Rafique

Air University, Spring 2015

Timers Timer Modes Mode 3 – Split Timer

   



 

24

When Timer 0 is placed in mode 3, it becomes 2 x 8 bit Timers Timer 0 is TL0 Timer 1 is TH0 Bits of Timer1 are related to TH0 Timer 1 can be in mode 0,1 or 2. Timer 1 will be incremented every machine cycle in any case – no control

M. Usman Rafique

Air University, Spring 2015

Timers Configuring Timers TMOD

25

Bit

Name

Explanation of Function

Timer

GATE1

= 1 Timer will only run when INT1 is high. (Calculate Time between events) = 0 for Timer1

1

6

C/T1

=1 Input on P3.5 drives time. Counter Mode = 0 Crystal drives Timer. Timer Mode

1

5

T1M1

Timer mode bit

1

4

T1M0

Timer mode bit

1

3

GATE0

= 1 Timer will only run when INT0 is high. (Calculate Time between events) = 0 for Timer0

0

2

C/T0

=1 Counter = 0 Timer

0

1

T0M1

Timer mode bit

0

0

T0M0

M. Usman Timer mode bit Rafique

7

Air University, Spring02015

Timers Configuring Timers TMOD Timer Mode TxM1

26

TxM0

Timer Mode

Description of Mode

0

0

0

13-bit Timer.

0

1

1

16-bit Timer

1

0

2

8-bit auto-reload

1

1

3

Split timer mode

M. Usman Rafique

Air University, Spring 2015

Bit

Name

Explanation of Function

Timer

7

GATE1

= 1 for External Time Measurement = 0 for Timer 1

1

6

C/T1

=1 Counter = 0 Timer

1

5

T1M1

Timer mode bit

1

4

T1M0

Timer mode bit

1

3

GATE0

= 1 for External Time Measurement = 0 for Timer 0

0

Description of Mode

2

C/T0

=1 Counter = 0 Timer

0

Timers Configuring Timers TMOD Practice 



Timer 0 in Mode 1 Timer 1 in Mode 2 Timer Mode

TxM1

TxM0

0

0

0

13-bit Timer.

1

T0M1

Timer mode bit

0

0

1

1

16-bit Timer

0

T0M0

Timer mode bit

0

1

0

2

8-bit autoreload

1

1

3

Split timer mode

27

M. Usman Rafique

Air University, Spring 2015

Timers Configuring Timers TMOD  

Timer0 in Mode1 Timer1 in Mode3

MOV tmod,#00100001b OR MOV tmod,#21h 28

M. Usman Rafique

Air University, Spring 2015

Timers Controlling Timers TCON Bit

Name

Explanation of Function

Timer

7

TF1

Timer 1 Overflow. = 1 when T1 Overflows

1

6

TR1

Timer 1 Run. = 1 Timer1 Runs = 0 Timer 1 Stops

1

5

TF0

Timer 0 Overflow. = 1 when T0 Overflows

0

TR0

Timer 0 Run. = 1 Timer0 Runs = 0 Timer 0 Stops

0

4

Lower Bits are related to Interrupts and will be covered later on 29

M. Usman Rafique

Air University, Spring 2015

Timers Controlling Timers TCON setb tr0

;running the timer

Label: JNB tf0, Label

; waiting till overflow flag is set

30

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers 

How to run timers for a given time?



How to measure time between 2 events?

31

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers 

How to run timers for a given time?

2 Timers

Timers

Measure Time 32

Counters

Serial Port

Generate Time M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers 

1. 2. 3.

33

How to run timers for a given time? Select Timer Mode – TMOD Store a Value in Timer Register – TH and TL Wait for Timer to Over-flow or Reset – TCON

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers 

1. 2.

How to run timers for a given time? Select Timer Mode Set an appropriate Value in Timer Register Timer Value = 65536 – (Desired Number of Machine Cycle) Value

Reset

Timer Value

34

Time

Timers Practice  Set P3.0 high for 0.05 seconds using Timer 0

35

M. Usman Rafique

Air University, Spring 2015

Timers 

Set P3.0 high for 0.05 seconds using Timer 0



Finding Value of Timer 

0.05 sec = 50,000 micro seconds



1.0851 micro sec 1 micro sec 50,000 micro sec

 

36

= 1 Machine Cycle = (1 / 1.0851) Machine Cycles = 50,000 * (1/1.0851) = 46079 Machine Cycles

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers  Set P3.0 high for 0.05 seconds using Timer 0 Value to load in TH and TL? 

Timer Value = 65536 – (Desired Number of Pulses) = 65536 – 46079 = 19456 = 4C 01h



TH = 4Ch ; TL = 01h

37

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers  Set P3.0 high for 0.05 seconds using Timer 0 Timer Mode  Mode 1 = 16 Bit timer Mode can be used  TMOD = 0000 0001b T1 T0

38

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers  Set P3.0 high for 0.05 seconds using Timer 0 MOV TMOD,#00000001b; MOV TH0,#4Ch; MOV TL0,#01h; setb P3^0 setb TR0

Here: JNB TF0, Here CLR P3^0 39

M. Usman Rafique

Air University, Spring 2015

Timers 

How to measure time between 2 events?

40

M. Usman Rafique

Air University, Spring 2015

Timers Using 8051 / 8052 Timers 

How to measure time between 2 events?

2 Timers

Timers

Measure Time 41

Counters

Serial Port

Generate Time M. Usman Rafique

Air University, Spring 2015

Timers 

How to measure time between 2 events? 

42

Set GATE Bit in TMOD

M. Usman Rafique

Air University, Spring 2015

Timers 

43

How to measure time between 2 events?

M. Usman Rafique

Air University, Spring 2015

Timers 

How to measure time between 2 events?

44

M. Usman Rafique

Air University, Spring 2015

Timers 

How to measure time between 2 events?



Set GATE Bit in TMOD C / T = 0 to ensure that crystal is connected to timers



When TR = 1, timer will run ONLY WHEN P3.2or P3.3 is set



So, timer value be corresponding to the time P3.2 or P3.3 remains high



45

M. Usman Rafique

Air University, Spring 2015

Counters

46

M. Usman Rafique

Air University, Spring 2015

Counters

47

M. Usman Rafique

Air University, Spring 2015

Counter Counter  

If TCON – Bit “C/T” = 1 Timer runs as counter



Timer 0 – Pin 3.4 (T0) Timer 1 – Pin 3.5 (T1)



Events (pulses) on these pins will be counted



Frequency consideration



48

M. Usman Rafique

Air University, Spring 2015

Counter Counter 

Value of Timer is incremented by 1 when there is 1-to-0 (High to Low) transition on the pin



For counter to count, there must be 1 during one machine cycle and 0 during next machine cycle



Maximum frequency: 2 machine cycles = 2 micro sec (for 12 MHz crystal) Frequency = 500 kHz 49

M. Usman Rafique

Air University, Spring 2015

Acknowledgement Material used with permission of 

Dr. Javaid Iqbal 



Head of Department, Mechatronics Engineering College of EME, NUST

I am extremely thankful to Dr. Javaid who has been a great teacher and still helps and supports me

50

M. Usman Rafique

Air University, Spring 2015

Microprocessor and Microcontroller - Lec 1

Introduction. 2. 8051Microcontroller. • Architecture and Hardware. 3. • Assembly Language. 4. • Assembly Language Contd. 5. • Timers and Counters. 6. • Serial Port. 7. • Interrupt. 8, 9. • Design and Interface Examples. 10. Midterm Exam. 11. PIC 18 F Microcontroller. • Introduction, Architecture, I/O Pins. 12. • Programming in ...

774KB Sizes 0 Downloads 196 Views

Recommend Documents

EE6612-MICROPROCESSOR-AND-MICROCONTROLLER ...
LIST OF EXPERIMENTS: 1. Simple arithmetic operations: addition ... Programming Practices with Simulators/Emulators/open source. 7. Read a key, interface display. 8. ... EasyEngineering.net. Page 3 of 86. EE6612-MICROPROCESSOR-AND-MICROCONTROLLER-LABO

CS6412-MICROPROCESSOR-AND-MICROCONTROLLER ...
www.EasyEngineering.net. Page 3 of 101. CS6412-MICROPROCESSOR-AND-MICROCONTROLLER-LABORATORY- By EasyEngineering.net.pdf.

EE6612-MICROPROCESSOR-AND-MICROCONTROLLER ...
TOTAL: 45 PERIODS. Visit : www.EasyEngineering.net. www.EasyEngineering.net. Page 3 of 86. EE6612-MICROPROCESSOR-AND-MICROCONTROLLER-LABORATORY- By EasyEngineering.net.pdf. EE6612-MICROPROCESSOR-AND-MICROCONTROLLER-LABORATORY- By EasyEngineering.net.

Chapter 1 Microprocessor, Microcontroller and Programming Basics
Microprocessor, Microcontroller and Programming Basics. Course objectives .... o Used mainly in “embedded” applications and often involves real-time control.

EC6504 Microprocessor and Microcontroller 12- By EasyEngineering ...
... programmable, clock-driven , register-based electronic. device that reads binary information from a storage device called memory, accepts binary. data as ...

EC6513-Microprocessor-Microcontroller-Lab-1_2013_regulation- By ...
EasyEngineering.net. www.EasyEngineering.net. Page 3 of 94. EC6513-Microprocessor-Microcontroller-Lab-1_2013_regulation- By EasyEngineering.net.pdf.

EC6504 Microprocessor and Microcontroller 1234- By ...
EC6504 Microprocessor and Microcontroller 1234- By EasyEngineering.net.pdf. EC6504 Microprocessor and Microcontroller 1234- By EasyEngineering.net.pdf.

EC6504 Microprocessor and Microcontroller 1234- By ...
(b) (i) Write an 8086 ALP to find the largest element in an array elements. (6). (ii) Explain the data transfer group and logical group of 8086 instructions. (10). 13.

Lec 2
Compute anything inside 1st ! pre ++ -- (e.g. ++var, --var) Left to right. Type cast. Left to right. * / %. Left to right. + -. Left to right. < >= Left to right. == != Left to right. && ||. Left to right. = assignment post ++ -- (e.g. var++, var--)

The Z80 microprocessor -architecture,interfacing,programming,and ...
The Z80 microprocessor -architecture,interfacing,programming,and design Ramesh Gaonkar - 1988.pdf. The Z80 microprocessor -architecture,interfacing ...

LEC Dietary Requests (optional).pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. LEC Dietary ...Missing:

Microprocessor & Microcontroller.pdf
8051 assembly language programming. 8051instruction sets, addressing modes, bit level operations. Arithmetic routines, counting and timing under interrupt ...

Microprocessor Based System Design 2011 June (2006 Ad)1.PDF ...
Microprocessor Based System Design 2011 June (2006 Ad)1.PDF. Microprocessor Based System Design 2011 June (2006 Ad)1.PDF. Open. Extract. Open with.

MP-Advanced microprocessor and peripherals-A.K.Ray.pdf ...
... 1 of 1,324. Page 2 of 1,324. Page 2 of 1,324. Page 3 of 1,324. Page 3 of 1,324. MP-Advanced microprocessor and peripherals-A.K.Ray.pdf. MP-Advanced microprocessor and peripherals-A.K.Ray.pdf. Open. Extract. Open with. Sign In. Main menu. Displayi

EC6504 Microprocessor and Microcontroller1- By EasyEngineering ...
EC6504 Microprocessor and Microcontroller1- By EasyEngineering.net.pdf. EC6504 Microprocessor and Microcontroller1- By EasyEngineering.net.pdf. Open.

EC6504 Microprocessor and Microcontroller1- By EasyEngineering ...
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. EC6504 Microprocessor and Microcontroller1- By EasyEngineering.net.pdf. EC6504 Microprocessor and Microcontr

MICROPROCESSOR & MICROCONTROLLER.pdf
MICROPROCESSOR & MICROCONTROLLER.pdf. MICROPROCESSOR & MICROCONTROLLER.pdf. Open. Extract. Open with. Sign In. Main menu.

microprocessor
NSC and ARM's joint project aims to create circuits, software, and tools that ... Adaptive Power Controller (APC) and Hardware Performance Monitor. (HPC) that ...

Microprocessor Theory and Applications with 68000-68020 and ...
Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Microprocessor Theory and Applications with 68000-68020 and Pentium (2008).pdf. Microprocessor Theor

Microprocessor breakpoint apparatus
Sep 28, 1995 - Moreover, for high speed processors it does not react quickly enough to provide a ..... The cross-coupled inverters form an ordinary ?ip-?op or.

The 8051 Microcontroller and Embedded Systems - Mazidi.pdf ...
The 8051 Microcontroller and Embedded Systems - Mazidi.pdf. The 8051 Microcontroller and Embedded Systems - Mazidi.pdf. Open. Extract. Open with. Sign In.