Advanced Features of Atmega8 Micro Controller

Embed Size (px)

Citation preview

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    1/31

    Advanced Features of Atmega8

    Microcontroller

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    2/31

    Topics to be covered

    Timers/counters

    ADC

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    3/31

    TIMERS/COUNTERS

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    4/31

    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.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    5/31

    Number ofTimers in Atmega8There are three timers/Counters in Atmega8

    Timer 0 Timer 1 Timer 2

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    6/31

    Number ofTimers in Atmega8There are three timers/Counters in Atmega8

    Timer 0 Timer 1 Timer 2

    8-bit 16-bit 8-bit

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    7/31

    Registers ofTimers/Counters

    There are four registers to control the

    operation & data storage. TCNT

    x= Timer Counter Register

    TCCRx

    = Timer/Counter Control Register

    TIMSK= Timer Interrupt Mask Register

    TIFR= Timer Interrupt Flag Register

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    8/31

    TIMER/COUNTER 0 REGISTER

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    9/31

    TCNTx

    This magical 8 bit register increases its ownvalue with a fix rate. This register dont requireCPU to increase its value. TCNTx keeps thecount value from 0-255, it must be initialized

    in program code. When the count valuereaches maximum, it raises a interrupt bit inanother register and reset to 0.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    10/31

    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 afractional scale factor.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    11/31

    CS0, CS1. CS2: Prescalar select inputs.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    12/31

    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.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    13/31

    PRESCALAR

    TIMER FREQUENCY =CPU CLOCK FREQUENCY / PRESCALER

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    14/31

    TIMER INTERRUPT MASK REGISTER

    Bit 0 or TOIE0 bit of TIMSK is used to enable/disableoverflow interrupt of TIMER0.

    Storing 1 at this place will Enable the overflow

    interrupt ofTI

    MER0. Storing 0 at this place will Disable the overflow

    interrupt ofTIMER0.

    Other bits are used for other timers.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    15/31

    OVERFLOWMODE

    Once the timer reaches it highest counts and

    if it not resets it is said to be in OverflowMode.

    For this mode we have to set 3 registers

    TCNT0 (To initialize counter)

    TCCR0 (To set Prescalar)

    TIMSK (To enable overflow interrupt)

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    16/31

    Program for creating 1 sec delay using

    OVERFLOW mode#include#includevolatile uint8_t count;main(){int second=0;char buffer[10];TCCR0=0b00000011; //to set the prescalar to64

    TIMSK |=(1

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    17/31

    Contdcount=0;sei(); //Enable Global Interruptwhile(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}elsecount++;} //ISR function close

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    18/31

    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 secondtime 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

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    19/31

    PULSEWIDTH MODULATION(PWM)

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    20/31

    WHATIS PWM?

    In this modulation technique, the width of themodulated signal is varied in accordance withinput.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    21/31

    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.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    22/31

    DUT

    Y 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%

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    23/31

    FORMULA OF DUTY CYCLE% DUTY CYCLE= 100

    T

    otalT

    ime Period=

    T

    ON +T

    OFF

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    24/31

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    25/31

    PWM GENERATION in AVR AVR contains separate hardware.

    PWM can be generated by Timer1 in ATmega8microcontroller.

    TCNTx, OCR1 A/B, ICR1 registers generatesPWM.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    26/31

    FAST

    PW

    M GENERATI

    ON USI

    NGTIMER1

    In this mode TCNTx

    register count its value fromBOTTOM to TOP.

    Reset to BOTTOM as it reaches TOP to countagain. While counting up when TCNT1 matched with

    compare register (OCRxx

    ) an output pinassociated with output compare register pulled

    HIGH. When TCNT1 reset to Bottom that pin pulled

    LOW.

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    27/31

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    28/31

    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)

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    29/31

    Program to make PWM of 20ms time

    period using FAST PWM mode ofTIMER1#include#include#includeint count =1;Void main(){DDRB=0xFF; //DIRECTION OF PORT B AS OUTPUTTCCR1B=0b00011001; //Bit 0-2 for CLOCK SELECT(NO

    PRESCALING) bit 3-4 for fastPWMWAVEFORM GENERATION MODE

    TCNT1=0; //for initializing timer1 count

    TCCR1A=0b10100010; //bit 0-1 for fast PWMWAVEFORMGENERATION MODE bit 4-5 forCOM1B1:0, bit 6-7 for COM1A1:0

    (10 for both)ICR1=20000; // setting upper limit for count

    register (Input compare register)

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    30/31

  • 8/6/2019 Advanced Features of Atmega8 Micro Controller

    31/31

    THANK YOU