Presentation is loading. Please wait.

Presentation is loading. Please wait.

The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi.

Similar presentations


Presentation on theme: "The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi."— Presentation transcript:

1 The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timers/Counters

2 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Counter Register

3 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Simple Design (Counting 4 People) First Design (Counter Register has initial value of zero) Sensor Karşılaştırıcısız çözüm olur mu?

4 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Simple Design (Counting 4 People) Second Design (Counter Register has initial value of 0xFC) Sensor Karşılaştırıcısız çözüm

5 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Simple Design (Making 4 Clock Cycles Delay) Bu sayıcı bir sensör yerine periyodik bir saat sinyali ile tetiklendiği için bir zamanlayıcıya dönüşmüştür.

6 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Generic Timer/Counter Delay generating Counting Wave-form generating

7 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, 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 One 16-bit Timer/Counter (0-65535) Timer 1

8 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timer Registers and Flags TCNTn TCCRn TOVn OCRn = OCFn TCNTn (Timer/CouNTer Register) TOVn (Timer OVerflow Flag) TCCRn (Timer/Counter Control Register) OCRn (Output Compare Register) OCFn (Output Compare Match Flag) TIFRn (Timer Interrupt Flag Register) n 0, 1 veya 2 değerini alır.

9 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Normal Mode (Timer 0) 0xFF TCNT0 0 TOV time 0 TOV0: 1 FE FF TOV0 = 1 2 1 0

10 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Example 1: Write a Program that Waits 14 Machine Cycles in Normal Mode. $100 -$0E $F2 14 = $0E $F2 Question: How to calculate the delay generated by the timer? 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 Question: How to calculate the delay generated by the timer? 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

11 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, 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

12 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. CTC (Clear Timer on Compare Match) Mode (Timer 0) TCNT0 0 OCF0 time 0 TOV0: 0 1 2 xx TOV0 = no change OCF0 = 1 OCR0 0 OCF0: 1 0xFF

13 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Prescaler

14 The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timer 1 (A 16-bit Timer)

15 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timer 1 TCNT1 (TCNT1H + TCNT1L)TCCR1B TOV1 OCR1A (OCR1AH + OCR1AL) = OCF1A = OCF1B OCR1B (OCR1BH + OCR1BL) TCCR1A

16 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timer 1 Registers

17 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timer Clock Selector (CS1x) 6 7

18 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Timer Mode Selector (WGM1x)

19 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, 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  s when prescaler = 1024, full count in 16.384 ms TIMER 1 is 16-bit (0−65536) when prescaler = 1, reaches full count in 4.096 ms when prescaler = 1024, full count in 4.194 seconds TIMER 2 is 8-bit (0−255) when prescaler = 1, reaches full count in 16  s when prescaler = 1024, full count in 16.384 ms These wrap times set limits on timed interrupts makes TIMER 1 attractive, for its 16 bits

20 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Complete Timer 1 AVR C Example (V1) (Normal Mode) Yoklama (Polling). Kesmeden Farkı? Bayrağı Silmek İçin Set Etmek Gerekli??

21 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Complete Timer 1 AVR C Example (V2) (Normal Mode)

22 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. A Complete Timer 1 AVR C Example (CTC Mode)

23 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, 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 #define OCR1A _SFR_MEM16(0x88) #define OCR1AL _SFR_MEM8(0x88) #define OCR1AH _SFR_MEM8(0x89) #define TIMER1_COMPA_vect _VECTOR(11) // Timer1 Compare Match A

24 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Generating Large Delays Using loop Prescaler Bigger counters

25 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. The Difference Between Timer 0 and Timer 2 Timer 0 Timer 2 CS02 CS01 CS00 Comment 0 0 0 Timer/Counter stopped 0 0 1 clk (No Prescaling) 0 1 0clk / 8 0 1 1 clk / 64 1 0 0 clk / 256 1 0 1 clk / 1024 1 1 0 External clock (falling edge) 1 1 1 External clock (rising edge) CS22 CS21 CS20 Comment 0 0 0 Timer/Counter stopped 0 0 1 clk (No Prescaling) 0 1 0clk / 8 0 1 1 clk / 32 1 0 0 clk / 64 1 0 1 clk / 128 1 1 0 clk / 256 1 1 1 clk / 1024

26 AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi © 2011 Pearson Higher Education, Upper Saddle River, NJ 07458. All Rights Reserved. Atmel AVR ATMega328P Mikrodenetleyicisinin Pinout Diyagramı T0T1


Download ppt "The AVR microcontroller and embedded systems using assembly and c AVR Microcontroller and Embedded System Using Assembly and C Mazidi, Naimi, and Naimi."

Similar presentations


Ads by Google