ATMEGA16 Timer 0 Normal mode and CTC Mode

Discussion in 'Embedded Systems and Microcontrollers' started by kareem101, Jan 27, 2015.

  1. kareem101

    Thread Starter New Member

    Jan 5, 2015
    5
    1
    Shouldnt these codes work the same, or i am messing something ?? because in CTC mode it doesnt work the same



    Code 1:

    Code (Text):
    1.  
    2. #include <avr/io.h>
    3. #include <avr/interrupt.h>
    4. char x=0;
    5.  
    6. int main(void)
    7. {
    8.    DDRA=0xFF;
    9.    PORTA=0x00;
    10.  
    11.    OCR0=0xFF;
    12.    TIMSK=(1<<OCIE0);
    13.    TCCR0=(1<CS00)|(1<CS02)|(1<WGM01);
    14.    sei();
    15.  
    16.   while(1)
    17.   {
    18.   }
    19. }
    20. ISR(TIMER0_COMP_vect)
    21. {
    22.    if(x==4)
    23.    {
    24.      PORTA++;
    25.      x=0;
    26.      if (PORTA == 10)
    27.      {
    28.        PORTA=0;
    29.      }
    30.    }
    31.    x++;
    32. }
    33.  

    Code 2 :


    Code (Text):
    1.  
    2. #include <avr/io.h>
    3. #include <avr/interrupt.h>
    4.  
    5. int x=0;
    6.  
    7. int main(void)
    8. {
    9.    DDRC|=0xf;
    10.    PORTC=0x00;
    11.  
    12.    TCCR0=(1<<CS00)|(1<<CS02);
    13.    TIMSK=(1<<TOIE0);
    14.    sei();
    15.  
    16.   while(1)
    17.   {
    18.   }
    19. }
    20. ISR(TIMER0_OVF_vect)
    21. {
    22.    if(x==4)
    23.    {
    24.    if (PORTC==9)
    25.    {
    26.      PORTC=0;
    27.      x=0;
    28.    }
    29.    else
    30.    {
    31.      PORTC+=1;
    32.      x=0;
    33.    }
    34.    }
    35.    x++;  
    36. }
    37.  
    Moderators note: Please use code tags for pieces of code
     
    Last edited by a moderator: Jan 27, 2015
  2. Paul of Alexandria

    New Member

    Jan 28, 2015
    3
    0
    First of all, for any Atmel questions, you might want to first try AVR Freaks at www.avrfreaks.net That's where the AVR experts hang out.
    What exactly is it supposed to be doing, and what's misbehaving?
    I'd start by declaring x as volatile. Otherwise the compiler may not see it in the ISR.
     
  3. Kazuya07

    New Member

    Feb 27, 2015
    1
    0
    "The left shift instruction is written “LSL Rd” and its machine code is 0000 11dd dddd dddd (the bold d’s are replaced by the binary code for the register and the other d’s are replaced by the same five bits). How can this be? Interpret this instruction as an ADD instruction and see what it really does. Compare this to the effect of a logical shift left. Is there a problem?"


    DdD DeViL
     
  4. kareem101

    Thread Starter New Member

    Jan 5, 2015
    5
    1
    oh thx for ur reply, but some 1 in avr freaks already figured out the problem, look at TCCR register initialization how i used compare operator < instead of shift operator << :) dumb mistake, but thx anyway
     
  5. kareem101

    Thread Starter New Member

    Jan 5, 2015
    5
    1
    x is a global variable, any function written in same file with main can see x :)
     
  6. Paul of Alexandria

    New Member

    Jan 28, 2015
    3
    0
    Depends on the optimization. In subroutines, especially ISR's, the optimizer can do some strange things with global variables. Declaring it "volatile" ensures that the optimizer doesn't mess with it.
     
Loading...