Learning to use timers - triggering twice as fast as expected?

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

Hi

 

I've decided to make the jump from Arduino to 'pure' AVR C code so am just getting up to speed.

 

I'm following some of the Atmel online tutorials and have run into an issue where my timer appears to be triggering twice as fast as expected. I've attached the code below.

 

I'd expect this to flash the LED once every two seconds i.e. the timer is triggering at 1Hz so one trigger turns it on and one turns it off. However the LED is flashing once per second i.e. 1/2 sec on, 1/2 sec off.

 

The chip is an ATMEGA328PB using the onboard 8MHz oscillator with the CKDIV8 bit set so a frequency of 1MHz?

 

My only thought is that it could be a terminology thing. The equation in the data sheet is for Foc which I couldn't find defined anywhere but I assume means oscillation frequency. Is this taking oscillation in the definition of on and off again i.e. the frequency of two triggers? I'd assume that if this was the case it would be called out a bit more specifically?

 

#define F_CPU 1E6

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

#define LED_Red_ON	PORTD &= ~(1<<PORTD6)
#define LED_Red_OFF	PORTD |= (1<<PORTD6)
#define LED_Red_Toggle	PIND |= (1<<PIND6)

ISR(TIMER1_COMPA_vect)
{
	LED_Red_Toggle;
}

void Timer_Frequency(uint8_t freq)
{
	TCCR1B |= (1<<CS12) | (1<<WGM12); // Set prescaler to 256 & Set timer mode of operation to CTC
	TIMSK1 |= (1<<OCIE1A);
	OCR1A = (F_CPU/(freq*2*256)-1);
}

int main(void)
{

	DDRD |= (1<<DDRD6);	//RGB LED Red. Set as output pin
	sei();
	Timer_Frequency(1);

    while (1) 
    {
    }
}

Thanks

 

Tommy 

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

The AppBuilder in Imagecraft came up these values, see how they compare to what your getting.

 

//TIMER1 initialize - prescale:256
// WGM: &4) CTC, TOP=OCRnA
// desired value: 1Hz
// actual value:  1.000Hz (0.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xF0; //setup
 TCNT1L = 0xBF;
 OCR1AH = 0x0F;
 OCR1AL = 0x41;
 OCR1BH = 0x0F;
 OCR1BL = 0x41;
 ICR1H  = 0x0F;
 ICR1L  = 0x41;
 TCCR1A = 0x00;
 TCCR1B = 0x0C; //start Timer
}

Jim

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

ki0bk wrote:

The AppBuilder in Imagecraft came up these values, see how they compare to what your getting.

 

This worked fine with the expected one second period.

 

The more I think about it the more I think the formula given in the datasheet is for an oscillation i.e. pulse on and off. It would make sense if the function of the timer was to provide an external clock source. It would also explain the factor of two multiplication in the formula but that could just be me seeing an answer that fits :-)

 

thanks

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

GilchristT wrote:
It would also explain the factor of two multiplication in the formula ...

I'd have to study the timer section for that particular model.  Generally, there are a number of formulas, depending on the mode etc.

 

Verify your timer setup numbers with paper and calculator.  In the end, a mode like CTC is pretty straightforward.

 

Once you understand, then there are tools.  I let CodeVision Wizard help.  Above was Imagecraft app builder.  There is also Jack Tidwell's "avrcalc".  A bit dated, but still useful.  Might be in the Projects section here.

There is a more extensive Kevin Rosenberg "kavrcalc", also sometimes called "avrcalc".

http://www.dysphagia.com/

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Wed. Aug 16, 2017 - 08:44 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The more I think about it the more I think the formula given in the datasheet is for an oscillation i.e. pulse on and off.

Yes, the formula is for frequency 

 

If you want 1sec on, 1sec off

then frequency is 0.5 Hz, not 1 Hz.

 

The formula for a pulse time looks like this: 

OCR = (time *  F_CPU / prescaler)  - 1

Last Edited: Thu. Aug 17, 2017 - 06:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks folks