Вы находитесь на странице: 1из 31

Advanced Features of Atmega8

Microcontroller
Topics to be covered
• Timers/counters
• ADC
TIMERS/COUNTERS
What are timers?
• Timer counts the time sequence or the clock
pulses of the microcontroller.

• They are set of registers integrated in most of


the microcontroller.
Number of Timers in Atmega8
There are three timers/Counters in Atmega8

Timer 0 Timer 1 Timer 2


Number of Timers in Atmega8
There are three timers/Counters in Atmega8

Timer 0 Timer 1 Timer 2

8-bit 16-bit 8-bit


Registers of Timers/Counters
• There are four registers to control the
operation & data storage.
– TCNTx= Timer Counter Register
– TCCRx= Timer/Counter Control Register

– TIMSK= Timer Interrupt Mask Register


– TIFR= Timer Interrupt Flag Register
TIMER/COUNTER 0 REGISTER
TCNTx

• This magical 8 bit register increases its own value


with a fix rate. This register don’t require CPU to
increase its value. TCNTx keeps the count value
from 0-255, it must be initialized in program code.
When the count value reaches maximum, it raises
a interrupt bit in another register and reset to 0.
Timer Frequency and Prescaling
• Timer frequency is the rate by which TCNTx
register increases its own value.

• A prescalar measures an output clock/


time related to an input clock/time by a
fractional scale factor.
CS0, CS1. CS2: Prescalar select inputs.
TIMER/COUNTER CONTROL REGISTER

• The timer control register controls Timer


Frequency, this is achieved by setting the
prescalar value.
• CS00-CS02 are used to select PRESCALER value
and hence determines the frequency of the timer.
PRESCALAR

TIMER FREQUENCY =CPU CLOCK FREQUENCY / PRESCALER


TIMER INTERRUPT MASK REGISTER

• Bit 0 or TOIE0 bit of TIMSK is used to enable/disable overflow


interrupt of TIMER0.
• Storing 1 at this place will Enable the overflow interrupt of
TIMER0.
• Storing 0 at this place will Disable the overflow interrupt of
TIMER0.
• Other bits are used for other timers.
OVERFLOW MODE
• Once the timer reaches it highest counts and if
it not resets it is said to be in Overflow Mode.
• For this mode we have to set 3 registers
TCNT0 (To initialize counter)
TCCR0 (To set Prescalar)
TIMSK (To enable overflow interrupt)
Program for creating 1 sec delay using OVERFLOW mode

#include<avr/io.h>
#include<avr/interrupt.h>
volatile uint8_t count;
main()
{
int second=0;
char buffer[10];
TCCR0=0b00000011; //to set the prescalar to64

TIMSK |=(1<<TOIE0); //for enabling overflow


interrupt
TCNT0=0; //for initialization of
Timer count
Contd…
count=0;
sei(); //Enable Global Interrupt
while(1);
} //main close

ISR(TIMER0_OVF_vect) // interrupt service routine


{
If(count==61) // if count reaches 1 sec
{
second++;
sprintf(“seconds:%d”, second);
lcd_puts(buffer); // for printing seconds on LCD

count=0; // reset the count variable


}
else
count++;
} //ISR function close
Description of program
If(count==61) // if count reaches 1 sec

61 is decided by clock frequency and the prescalar.


Formula for calculating number of interrupts for 1 second time duration
is :
Timer frequency= CPU clock frequency/Prescalar
No of overflow interrupts= Timer frequency/256

In our case:
Timer frequency=1000000/64= 15625
No of interrupts=15625/256 = 61.035≈ 61
PULSE WIDTH MODULATION
(PWM)
WHAT IS PWM?

• In this modulation technique, the width of the


modulated signal is varied in accordance with
input.
Why it is used in Microcontroller?
• Pulse Width Modulation (PWM) is a method
of getting intermediate voltage between 0V
and 5V for e.g. 3.6V.
• Intermediate voltage is achieved by increasing
and decreasing the width of digital pulse.
• PWM is defined by “Duty Cycle” of the pulse.
DUTY CYCLE
• It is the ratio of active time(ON time) to the
total time period of the clock cycle.

total time period= ON time(TON) + OFF time(TOFF)


For e.g.- If an CPU Clock pulse is “ON” or “HIGH” for half
time period of the pulse so duty cycle is 50%
FORMULA OF DUTY CYCLE
% DUTY CYCLE= × 100
Total Time Period= TON + TOFF
PWM GENERATION in AVR
• AVR contains separate hardware.

• PWM can be generated by Timer1 in ATmega8


microcontroller.

• TCNTx, OCR1 A/B, ICR1 registers generates


PWM.
FAST PWM GENERATION USING TIMER1
• In this mode TCNTx register count its value from
BOTTOM to TOP.
• Reset to BOTTOM as it reaches TOP to count again.
• While counting up when TCNT1 matched with
compare register (OCRxx) an output pin associated
with output compare register pulled HIGH.
• When TCNT1 reset to Bottom that pin pulled LOW.
FAST PWM MODE
• For this mode, we have to set
– TCNT1 (Timer Counter Register)
– OCR1A (Output Compare Register)
– ICR1 (Input Compare Register)
– TCCR1B & TCCR1A (Timer/Counter Control
Register)
– TIMSK (Timer Interrupt Mask Register)
Program to make PWM of 20ms time period using FAST
PWM mode of TIMER1
#include<avr/io.h>
#include<avr/interrupt.h>
#include<util/delay.h>
int count =1;
Void main()
{
DDRB=0xFF; //DIRECTION OF PORT B AS OUTPUT
TCCR1B=0b00011001; //Bit 0-2 for CLOCK SELECT(NO
PRESCALING) bit 3-4 for “fast PWM”WAVEFORM
GENERATION MODE

TCNT1=0; //for initializing timer1 count


TCCR1A=0b10100010; //bit 0-1 for “fast PWM”WAVEFORM GENERATION
MODE bit 4-5 for
COM1B1:0, bit 6-7 for COM1A1:0
(10 for both)
ICR1=20000; // setting upper limit for count
register (Input compare register)
TIMSk=0b00000000; //Timer Interrupt mask set register
sei(); //Set Interrupt Enable
OCR1A=0x0000; //initiating output compare
register from 0
While(1)
{
if(OCR1A==20000) //checking if OCR1A==ICR1
{
OCR1A=0x0000; //resetting OCR1A to 0 value again
Count=1; //resetting count value to 1 again
}

else
{
OCR1A=(100*count);
_delay_ms(10);
count++;
}
} // while close
} //main close
THANK YOU

Вам также может понравиться