AVR_Course_Day7 timers counters and interrupt programming

Preview:

Citation preview

Timers\Counters & Interrupt

Programming

Contents

1. Introduction

2. 8 bit Timers

3. Interrupts.

4. 16 bit Timers

Timer\Counters1. Introduction(Time Terminology)

FrequencyConsider signal x(t) that repeats itself. This signal called a periodic with period T if it satisfies:

x(t)=x(t+T).To measure the frequency of a periodic signal, we count the number of times a particular event repeats within a 1-s period.

Period The flip side of a frequency is a period. If an event occurs with a rate of 1 Hz, the period of that event is 1 s. To find a period, given a frequency, or vice versa, f=1/T.

Timer\Counters1. Introduction(Time Terminology)

Duty Cycle In many applications, periodic pulses are used as control signals. A good example is the use of a periodic pulse to control a servo motor. To control the direction and sometimes the speed of a motor, a periodic

pulse signal with a changing duty cycle over time is used.

Timer\Counters1. Introduction(Time Terminology)

Timing System Overview: The heart of the timing system is the crystal time base.

The crystal’s frequency of a microcontroller is used to generate a baseline clock signal.

For a timer system, the system clock is used to update the contents of a special register called a free-running counter.

For input time-related activities, all microcontrollers typically have timer hardware components that detect signal logic changes on one or more input pins. Such components rely on a free-running counter to capture external event times.

Timer\Counters1. Introduction(Time Terminology)

For output timer functions, a microcontroller uses a comparator, a free-running counter , logic switches, and special-purpose registers to generate time-related signals on one or more output pins.

Timer\Counters1. Introduction(Applications)

Measure an input signal timing event.

Count the number of external signal occurrences.

Generate timed signals---termed output compare.

Generate PWM signals.

Timer\Counters1. Introduction

Timer\Counter used for the following:1) Count an event (by connecting external source to increment the counter registers).2) Generate time delay (Oscillators or internal clock is used to increment the counter registers).

There are three timers on Atmega328:1) Timer (0) which is 8 bit timer.2) Timer (1) which is 16 bit timer.3) Timer (2) which is 8 bit timer

Timer\Counters1. Introduction

Timer\Counters2. 8 bit Timers(Timer 0)

Features:

• Two Independent Output Compare Units

• Double Buffered Output Compare Registers

• Clear Timer on Compare Match (Auto Reload)

• Phase Correct Pulse Width Modulator (PWM)

• Variable PWM Period

• Frequency Generator

• Three Independent Interrupt Sources (TOV0, OCF0A, and OCF0B)

Timer\CountersThe architecture of Timer (0) shown in the following figure:

2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Normal Mode:the timer will continually count from 0x00 (BOTTOM) to 0xFF (TOP).

When the TCNT0 returns to 0 on each cycle of the counter, the Timer/Counter Overflow Flag (TOV0) will be set.

The normal mode is useful for generating a periodic ‘‘clock tick’’ that may be used to calculate elapsed real time or provide delays within a system.

Timer\Counters2. 8 bit Timers(Timer 0)

Clear Timer on Compare Match:In the CTC modem, the TCNT0 timer is reset to 0 every time the TCNT0 counter reaches the value set in OCR0.

The Output Compare Flag 0 (OCF0) is set when this event occurs.

The OCF0 flag is enabled by asserting the Timer/Counter 0 Output Compare Match Interrupt Enable (OCIE0) flag in the TIMSK and when the I-bit in the Status Register is set to 1

The CTC mode is used to generate a precision digital waveform such as a periodic signal or a single pulse.

Timer\Counters2. 8 bit Timers(Timer 0)

Phase Correct PWM Mode: In the Phase Correct PWM mode, the TCNT0 registercounts from 0x00 to 0xFF and back down to 0x00 continually.

Every time the TCNT0 value matches the value set in the OCR0 register, the OCF0 flag is set and a change in the PWM signaloccurs.

Timer\Counters2. 8 bit Timers(Timer 0)

Fast PWM:is used to generate a precision PWMsignal of a desired frequency and duty cycle.

It is called the fast PWM because its maximum frequency is twice that of the Phase Correct PWM mode.

When the TCNT0 register value reaches the value set in the OCR0 register, it will cause a change in the PWM output as prescribed by the system designer.

It continues to count up to the TOP value, at which time the Timer/Counter 0 Overflow Flag(TOV0) is set.

Timer\Counters2. 8 bit Timers(Timer 0)

TCNTn: Timer\Counter register it counts up every pulse. Also it can be loaded by a certain value.In each TCNTn there are:WGMnm: Wave generation mode (Normal mode ,CTC mode ,Fast PWM ,..etc.).CSnm: Prescalar(8,64,256,1024).TOVn: Timer overflow flag, when a timer overflow a flag will be set.TCCRn: Timer\Counter control register ,its for setting mode of operation by loading the proper value for ex(Normal mode ,CTC mode ,Fast PWM ,..etc.)will be explained later.

Timer\Counters2. 8 bit Timers(Timer 0)

TIMSK :Timer/Counter Interrupt Mask Register register is used by all three timer channels.

Timer 0 uses the Timer/Counter 0 Output Compare Match Interrupt Enable (OCIE0) bit and the Timer/Counter 0 Overflow Interrupt Enable (TOIE0) bit.

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers(Timer 0)

Timer\Counters2. 8 bit Timers (Normal Mode)

There are two equation to calculate the value of TCNT0:

1) In Decimal256-(Td\Tc)=TCNT0

2) In HexaFF+1-(Td\Tc)=TCNT0

Td: Time on desired from MCU.Tc: Timer Clock of the MCU ((1\Fosc)*Prescalar=Tosc*Prescalar).OSC: Oscillators (the Crystal).Prescalar: is used to divide this clock frequency and produce a clock for TIMER.

Timer\Counters2. 8 bit Timers (Normal Mode)

Ex(1):Assuming xtal=8MHZ,and required to generate a square wave with period of 12.5 us.Td=12.5us/2=6.25us.Tc=1/8MHZ=0.125us.256-(6.25/0.125)=206=0xCE.TCNT0=0xCE.

EX(2):Assuming xtal=8MHZ,Calculate the frequency of square wave knowing that the TCNT0=0x3ETd=(FF+1-3E)*0.125us=24.25us.F=1/2*Td=1\2*24.25us=20.61KHZ.

Timer\Counters2. 8 bit Timers (Normal Mode)

Prescaler and generating a large time delay:To get the maximum delay time Td=(256-0)*Tc. (for xtal=8MHZ) ,Td=32usF=1\2*Td=1\2*32us=15KHZTo maximize the period prescaler is usedUsing prescaler=8 Td=256us.Prescaler=64 Td=2048 us.Prescaler=256 Td=8192us.Prescaler=1024 Td=32768us.Ex(3):Assuming xtal=8Mhz generate a square wave of 125 Hz,using timer 0 and prescaler of 256T=1\125=8ms Td=4ms.Tc=1\8MHZ *256=32us.256-(4ms\32us)=256-125=131.TCNT0=0x83.

Timer\Counters

Steps of Programming Timer 0 for Normal mode :

1) Load the TCCR0 (A,B) with the proper value according to the mode of

operation.

2) Load the TCNTn with proper values according to the oscillator

frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or

without the prescalar.

3) If the timer overflow set the TOV to clear the timer and to count again in

(normal mode) Clear the flag or OCF0.

5) Repeat it again.

2. 8 bit Timers (Normal Mode)

Ex(4) Find the time delay (Td) when the OCR0=9, assuming that xtal=8MhzThe TCNT0 will be cleared after 9+1=10 countsTd=10*0.125us=1.25us.

EX(5)Using the CTC mode find the OCR0 value to generate a time delay of (25.6ms) using the prscaler 1024Td=25.6msTc=0.125us*1024=128usNumber of counts=Td/Tc=200OCR0=200-1=99.

Timer\Counters2. 8 bit Timers (CTC mode)

Timer\Counters

Steps of Programming Timer 0 for Normal mode :

1) Load the TCCR0 (A,B) with the proper value according to the mode of

operation.

2) Load the TCNTn with proper values according to the oscillator

frequency(1/f*Prescalar=Tc*Prescalar) and the desired (Td)time required with or

without the prescalar.

3) Set the OCF0 when the TCNT0 = OCR0 to clear the timer and count again.

4) Clear the flag or OCF0.

5) 5) Repeat it again.

2. 8 bit Timers (CTC Mode)

InterruptInterrupts are basically events that require immediate attention by the microcontroller. When an interrupt event occurs the microcontroller pause its current task and attend to the interrupt by executing an Interrupt Service Routine (ISR) at the end of the ISR the microcontroller returns to the task it had pause and continue its normal operations.

In order for the microcontroller to respond to an interrupt event the interrupt feature of the microcontroller must be enabled along with the specific interrupt. This is done by setting the Global Interrupt Enabled bit and the Interrupt Enable bit of the specific interrupt.

Interrupt Types:1. Internal Interrupt (e.g. Timer interrupt).2. External Interrupt ( will be discussed in the next session).

InterruptEach Interrupt associated to vector number shown in the following vector table (See other routines in the data sheet)

Interrupt

Why using Interrupt in Timers??!!!

Interrupt VS Polling:

There are two methods to receive a service from microcontroller.

1) Polling method: the microcontroller continuously monitors the state of the device ,when the state of the device met the condition the microcontroller perform the service. After that it returns again to monitor the state again.

2) Interrupt method: whenever any device needs a service, it notifies the microcontroller by sending an interrupt signal. Upon receiving the signal the microcontroller gets out the infinite loop and serves the device which is associated with the interrupt it’s called interrupt service routine.

Interrupt

Advantages of Interrupts over Polling:

1) Every device can get the attention of the microcontroller by sending an interrupt signal, when two devices send an interrupt signal at same time the microcontroller serves the device with the high priority.

2) Polling wastes the microcontroller time by polling a device(s)that don’t need a service and tying it down from performing any other tasks.

Interrupt

InterruptSteps of Executing an Interrupt:

1) The microcontroller finished the instruction that currently executing and then saved the address of the next instruction (program counter) on the stack.

2) It jumps to a fixed location in the memory (interrupt vector).The interrupt vector directs the microcontroller to the address of ISR.

3) The microcontroller starts to executes the ISR until the it reaches the last instruction RETI(return from interrupt).

4) The microcontroller returns to the place where its interrupted first, it gets the program counter from the stack then starts to execute from that address.

InterruptEnabling the Interrupts:To enable the interrupt is should be unmasked as shown in the following figure.

SREG is the Status register ,to enable the interrupt (unmask the interrupt) the bit D7(I) should be activated using sei() set interrupt then use TMISK register to enable interrupts of Timer (0 or 1 or 2)this will covered later.

To disable the interrupt the bit D7(I) should be cleared by cli()clear the interrupt.

InterruptRegisters associated with Timer interrupt

Timer\Counters

AssignmentDo the same programs of timer 0 on timer 2

Timer\Counters3. 16 bit Timers

Features:

• True 16-bit Design (i.e., Allows 16-bit PWM)• Two independent Output Compare Units• Double Buffered Output Compare Registers.• One Input Capture Unit.• Input Capture Noise Canceler.• Clear Timer on Compare Match (Auto Reload).• Phase Correct Pulse Width Modulator (PWM).• Variable PWM Period.• Frequency Generator.• External Event Counter.• Four independent interrupt Sources (TOV1, OCF1A, OCF1B, and ICF1).

Timer\Counters3. 16 bit Timers

The architecture of Timer (1) shown in the following figure:

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

ICNC1: Input Capture Noise Canceler Setting this bit (to one) activates the Input Capture Noise Canceler. When the noise canceler is activated, the inputfrom the Input Capture pin (ICP1) is filtered.

ICES1: Input Capture Edge Select This bit selects which edge on the Input Capture pin (ICP1) that is used to trigger a capture event. When the ICES1bit is written to zero, a falling (negative) edge is used as trigger, and when the ICES1 bit is written to one, a rising (positive) edge will trigger the capture.

ICIE1: Timer/Counter1, Input Capture Interrupt Enable When this bit is written to one, and the I-flag in the Status Register is set (interrupts globally enabled), the Timer/Counter1 Input Capture interrupt is enabled.

ICF1: Timer/Counter1, Input Capture Flag This flag is set when a capture event occurs on the ICP1 pin

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

Timer\Counters3. 16 bit Timers

AssignmentGenerate a square wave of

1 sec using the normal mode and the CTC mode.Repeat it using interrupt

method.

Recommended