Timer questions

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

Hi :)

I am trying to learn about using the timers in C.
Is this code all I need to use timer0 in compare mode?
I'd load it into an AVR and test it but I'm waiting on a
replacement butterfly as I smoked my original...this
seems to compile OK though...but I can't simulate it
as it freezes in the SIGNAL(SIG_OUTPUT_COMPARE1)
function.

// timer2.c
//for atmega169

#include

unsigned char tickcount = 0;

void main(void)
{

DDRD = 0xFF; //portd all outputs
TIMSK0 = (1<<OCIE0A);
OCR0A = 250; //compare value
// Setup for (CTC) mode, CLK/8 prescale
TCCR0A = (1<<WGM01)|(0<<WGM00)|(1<<CS01);
}

SIGNAL(SIG_OUTPUT_COMPARE0)
{
PORTD = tickcount++;
}

And how would I change this code to use the 16bit timer1 ???
It is more complicated because it has low and high bytes...

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

Gwen wrote:
Is this code all I need to use timer0 in compare mode?

Almost, but not quite. There are two things necessary to enable the interrupt you want, and you're only doing one of them. Read the register description of OCIE0A again, and you'll see what I mean.

I needed to add a "#include " to clear up some compiling issues, but maybe that's just my environment.

And for simple testing you'll also want a doferever loop to keep from falling out the bottom of main(), Otherwise, your ISR will be but a brief distraction to the CPU on its merry traipse through the weeds.

Mike

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

You probably also need an sei() in the main() to globally enable interrupts too.

I think you'll end up #include'ing both interrupt.h and signal.h

Cliff

PS I don't see the all important #include of avr/io.h there either?

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

Thanks for the hints guys..does this look better?

// timer4.c
//for atmega169

#include
#include
#include
#include

unsigned char tickcount = 0;

void main(void)
{
sei(); //enable interrupts
DDRD = 0xFF; //portd all outputs
TIMSK0 = (1<<OCIE0A);
OCR0A = 250; //compare value
// Set Clear on Timer Compare (CTC) mode, CLK/8 prescaler
TCCR0A = (1<<WGM01)|(0<<WGM00)|(1<<CS01);
while(1); //endless loop
}

SIGNAL(SIG_OUTPUT_COMPARE0)
{
PORTD = tickcount++;
}

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

volatile unsigned char tickcount;

Initialization with 0 is redundant, as this value is already
guaranteed by the C standard -- so you are guaranteed to gain
nothing by writing the "= 0".

See FAQ#1 for why you certainly want the volatile type qualifier.

Jörg Wunsch

Please don't send me PMs, use email if you want to approach me personally.

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

Thanks jorg :)
guess I am all set now...

// timer5.c
//for atmega169

#include
#include
#include
#include

volatile unsigned char tickcount;

void main(void)
{
sei(); //enable interrupts
DDRD = 0xFF; //portd all outputs
TIMSK0 = (1<<OCIE0A);
OCR0A = 250; //compare value
// Set Clear on Timer Compare (CTC) mode, CLK/8 prescaler
TCCR0A = (1<<WGM01)|(0<<WGM00)|(1<<CS01);
while(1); //endless loop
}

SIGNAL(SIG_OUTPUT_COMPARE0)
{
PORTD = tickcount++;
}