ATTiny45 Timer1 CTC value not changing

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

So I'm pretty new with AVRs and I'm trying to set up a simple timer.  I'm using an ATTiny45 right now.

Below is a stripped-down code just to get the timer working right.

I've got an LED attached to PB4 to see the output.

It seems that changing the value of OCR1A has no effect on the frequency of the LED.

I'm running at 1MHz, and my output is like 3.8Hz... which seems like the ISR is running on overflow and not compare

 

#include <avr/io.h>
#include <avr/interrupt.h> //for the timer interrupt

int main(void)
{
	sei(); //enable global interrupts
	
	TCCR1 |= (1<<CTC1) | 0x0A; //enable CTC, scale f/512
	TIMSK |= (1<<OCIE1A); //enable CTC interrupt for timer1
	
	
	OCR1A = 0x0A; //8-bit compare value... no effect on frequency?
	
	DDRB=0x10; //set PB4 as output, LED
	PORTB=0x10; //write it high to start
	
	while(1)
	{
		
	}
	return 1;
}

ISR(TIMER1_COMPA_vect) //interrupt on compare for timer 1A
{
	PORTB ^= 0x10; //toggle the LED
	
}

Am I just missing something obvious?

This topic has a solution.

Last Edited: Tue. Mar 17, 2015 - 04:30 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Am I just missing something obvious?

Yes:

When the CTC1 control bit is set (one), Timer/Counter1 is reset to $00 in the CPU clock cycle after a compare match with OCR1C register value.

Regards,
Steve A.

The Board helps those that help themselves.

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

Did you get any warnings about "misspelled interrupt handler"?

 

http://www.nongnu.org/avr-libc/u...

 

TIM1_COMPA_vect  Timer/Counter1 Compare Match A

ATtiny24, ATtiny44, ATtiny84, ATtiny45, ATtiny25, ATtiny85

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.

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

Right you are.  Thanks a bunch!

It was simply using OCR1C instead of OCR1A

I got no warnings.  Datasheet shows TIMER1_COMPA_vect as correct.

Last Edited: Tue. Mar 17, 2015 - 04:42 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you get any warnings about "misspelled interrupt handler"?

$ grep _vect iotnx5.h
#define INT0_vect			_VECTOR(1)
#define PCINT0_vect			_VECTOR(2)
#define TIM1_COMPA_vect			_VECTOR(3)
#define TIMER1_COMPA_vect		_VECTOR(3)
#define TIM1_OVF_vect			_VECTOR(4)
#define TIMER1_OVF_vect			_VECTOR(4)
#define TIM0_OVF_vect			_VECTOR(5)
#define TIMER0_OVF_vect			_VECTOR(5)
#define EE_RDY_vect			_VECTOR(6)
#define ANA_COMP_vect			_VECTOR(7)
#define ADC_vect			_VECTOR(8)
#define TIM1_COMPB_vect			_VECTOR(9)
#define TIMER1_COMPB_vect		_VECTOR(9)
#define TIM0_COMPA_vect			_VECTOR(10)
#define TIMER0_COMPA_vect		_VECTOR(10)
#define TIM0_COMPB_vect			_VECTOR(11)
#define TIMER0_COMPB_vect		_VECTOR(11)
#define WDT_vect			_VECTOR(12)
#define USI_START_vect			_VECTOR(13)
#define USI_OVF_vect			_VECTOR(14)

Two names for the same thing ;-)

 

(there's actually a lot of duplicates in the above).

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

Some of the earlier WinAVR releases only have the TIM1 version.

 

There are often minor anomalies with the vector names.    For example,   most Megas use TIMER1

 

IMHO,   "mis-spelled signal handlers" should really be an ERROR.

 

David.