ATMEGA16 Timer 0 Normal mode and CTC Mode

Thread Starter

kareem101

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



Code 1:

Code:
#include <avr/io.h>
#include <avr/interrupt.h>
char x=0;

int main(void)
{
   DDRA=0xFF;
   PORTA=0x00;
  
   OCR0=0xFF;
   TIMSK=(1<<OCIE0);
   TCCR0=(1<CS00)|(1<CS02)|(1<WGM01);
   sei();
  
  while(1)
  {
  }
}
ISR(TIMER0_COMP_vect)
{
   if(x==4)
   {
     PORTA++;
     x=0;
     if (PORTA == 10)
     {
       PORTA=0;
     }
   }
   x++;
}

Code 2 :


Code:
#include <avr/io.h>
#include <avr/interrupt.h>

int x=0;

int main(void)
{
   DDRC|=0xf;
   PORTC=0x00;
  
   TCCR0=(1<<CS00)|(1<<CS02);
   TIMSK=(1<<TOIE0);
   sei();
  
  while(1)
  {
  }
}
ISR(TIMER0_OVF_vect)
{
   if(x==4)
   {
   if (PORTC==9)
   {
     PORTC=0;
     x=0;
   }
   else
   {
     PORTC+=1;
     x=0;
   }
   }
   x++;  
}
Moderators note: Please use code tags for pieces of code
 
Last edited by a moderator:
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.
 

Kazuya07

Joined Feb 27, 2015
1
"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
 
Top