Professional Documents
Culture Documents
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Counter Register
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Simple Design (Counting 4 People)
First Design (Counter Register has initial value of zero)
Comparator
=
Karşılaştırıcısız çözüm
olur mu?
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Simple Design (Counting 4 People)
Second Design (Counter Register has initial value of 0xFC)
$FC
Karşılaştırıcısız çözüm
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Simple Design (Making 4 Clock Cycles Delay)
$FC
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Generic Timer/Counter
Delay generating
Counting
Wave-form generating
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Timers/Counters in AVR ATMega328PU
3 Timers/Counters
Two 8-bit Timers/Counters (0-255)
Timer 0
Timer 2
Timer 1
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Timer Registers and Flags
TCNTn (Timer/CouNTer Register)
TOVn
= OCFn
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Normal Mode (Timer 0)
TCNT0
FF
FE 0xFF
TOV TOV TOV
0 time
2
1
0 TOV0 = 1
TOV0: 0
1
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Example 1: Write a Program that Waits 14 Machine
Cycles in Normal Mode.
14 = $0E UP
COUT
Counter Register Load
$100 8
-$0E $F2
$F2
Answer:
1) Calculate how much a machine clock lasts.
T= 1/f
2) Calculate how many machine clocks it
waits.
3) Delay = T * number of machine cycles
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Finding Values to be Loaded into the Timer 0
1. Calculate the period of clock source.
Period = 1 / Frequency
E.g. For XTAL = 8 MHz T = 1/8MHz
2. Divide the desired time delay by period of
clock.
3. Perform 256 - n, where n is the decimal
value we got in Step 2.
4. Set TCNT0 = 256 - n
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
CTC (Clear Timer on Compare Match) Mode (Timer 0)
TCNT0
0xFF
OCR0
OCR0
xx
2
1 TOV0: 0
0 TOV0 = no change
OCF0 = 1
OCF0: 0
1
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Prescaler
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Timer 1 (A 16-bit Timer)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Timer 1
OCR1B (OCR1BH + OCR1BL)
= OCF1B
TOV1 = OCF1A
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Timer 1 Registers
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
6
PSR10 Timer Clock Selector (CS1x)
7 Clear
clkIO 10-bit T/C Prescaler
clk/8
clk/64
clk/256
clk/1024
T1
0
CS10 0 1 2 3 4 5 6 7
CS11
CS12
Timer/Counter1 clock
source
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Timer Mode Selector (WGM1x)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Wrap Times (Fcpu=16 MHz)
TIMER 0 is 8-bit (0−255)
when prescaler = 1, reaches full count in 16 ms
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Complete Timer 1 AVR C Example (V1)
(Normal Mode)
Yoklama (Polling).
Kesmeden Farkı?
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Complete Timer 1 AVR C Example (V2)
(Normal Mode)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
A Complete Timer 1 AVR C Example
(CTC Mode)
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
AVR C Definitions for ATMega328P
The bit values WGM12, CS10, etc. are defined
in iom328p.h
Its location is hardware/tools/avr/avr/include/avr/
For example:
#define CS10 0
#define CS11 1
#define CS12 2
#define WGM12 3
#define WGM13 4
#define ICES1 6
#define ICNC1 7
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Generating Large Delays
Using loop
Prescaler
Bigger counters
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
The Difference Between Timer 0 and Timer 2
Timer 0 Timer 2
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.
Atmel AVR ATMega328P Mikrodenetleyicisinin
Pinout Diyagramı
T0 T1
AVR Microcontroller and Embedded System Using Assembly and C © 2011 Pearson Higher Education,
Mazidi, Naimi, and Naimi Upper Saddle River, NJ 07458. • All Rights Reserved.