AVR Timer Normal mode and Compare mode

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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

 

Code 1:

 

#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 :

 

#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++;    
}

This topic has a solution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well for one thing you haven't told us which AVR but for another one uses PORTA in the ISR and the other uses PORTC so, of course, they'll behave differently irrespective of how you use the timer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using atemga16, here is my problem, ok it doestn matter which port, it's an output port, so A or C what difference does it make, My problem is in normal mode i am using timer 0 which is 8 bit register so it over flows after FF and gives interrupt after, in Compare mode i put FF in the compare register "OCR0" so in the same way it would give interrupt also after FF, but that doesnt happen, IN CTC mode it is working a lot much faster, WHY ! and i know that it doesnt make sense to use CTC in that way, but i am new to microcontrollers and trying everything.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

so A or C what difference does it make,

A world of difference in some cases as 4 of the pins on PORTC are not accessible by default settings.

 if (PORTC==9)

Maybe try PINC?? You READ from PINx and WRITE to PORTx

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thx a lot, thats Right, I should write to PORT and read from PIN, i tried it and i even made the ISR function the same [ISR(TIMER0_OVF_vect) for over flow operation] and [ISR(TIMER0_COMP_vect) ] but the body is exACTLY THE SAME, still, working differently, is there something wrong with timer initialization in compare mode ?? i want to add that in normal mode its working right, i have 1 Mhz clock and i prescaled /1024 so i have interrupt almost every 250 ms, so multiplying by 4 i have almost 1 sec counter on the port, in  ctc mode its a lot faster as if i am not even prescaling.

 

 

Last Edited: Wed. Jan 28, 2015 - 09:06 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kareem101 wrote:

... is there something wrong with timer initialization in compare mode ?? 

Yes, there is something wrong, notice how you have used the < compare operator and not the << shift operator.


    TCCR0=(1<CS00)|(1<CS02)|(1<WGM01);

/Lars

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thx a lot man, what a stupid mistake :) thx all who helped..... and really sorry for wasting ur time on this stupid mistake. :D

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lajon wrote:
kareem101 wrote:

 

... is there something wrong with timer initialization in compare mode ?? 

 

 

Yes, there is something wrong, notice how you have used the < compare operator and not the << shift operator.


    TCCR0=(1<CS00)|(1<CS02)|(1<WGM01);

/Lars

No warning?

International Theophysical Year seems to have been forgotten..
Anyone remember the song Jukebox Band?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No warning.  It is a valid C statement, but will likely lead to a 1 setting for TCCR0.

It all starts with a mental vision.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kareem101 wrote:

Thx a lot man, what a stupid mistake :) thx all who helped..... and really sorry for wasting ur time on this stupid mistake. :D

 

No worries, WE have all done that one at one time! q:-)

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No warning.  It is a valid C statement

Warnings are not issued for invalid C statements, errors are. Warnings are for pointing out possible unwanted behavior. The implicit conversion of a boolean result to a value used in a bitwise operation could very well qualify for a warning, though the warning I would expect would be the use of '|' instead of '||' rather than the use of '<' instead of '<<'. I'm wondering that no warning was emitted might be because everything is a compile time constant. We could try making the values variables and see what happens.

Regards,
Steve A.

The Board helps those that help themselves.

Last Edited: Thu. Jan 29, 2015 - 10:29 PM