ATTiny45, TCCR1-CTC1 enabled, interrupt flags only working with OCR1C

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

I have this code using global interrupts to toggle pin 0 on PORTB.

The CTC1' clear timer/counter on compare match is presumably only working with TCNT1 'matching' with OCR1C and then TCNT1 is clearing. If i use OCR1A or OCR1B they dont match to clear the counter/timer1, also if i leave out an OCR1 the interupt still happens and the LED flashes at a rate as if OCR1C was set to 255. 

~Using the compiler that comes with the newest WinAVR.

I did read in the data sheet that timer/counter1 counting to OCR1C would work with CTC, but also says OCR1A will work which is why im puzzled. pg.89+100 the datasheet. I have not tried the code yet with the TCCR0/ 0 side of timers....

 

Im going to bed, will go through my code and the data sheet again in the morning. 

/* ATTiny45 @8Mhz Internal Clock */

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

int x;
int main(void)
{
    sei(); //Enable global interrupts
    DDRB = 0b00000111;  //data direction registry setting PortB 0-2 as outputs
    OCR1C = 25; //Timer/counter1 output Compare Match Register1C
    ///CTC1 Not working with OCR1A, only works with OCR1C

    TCCR1 |= 0b10001001; //enables timer/counter1 (TCNT1) and prescales it to ck/256
    //also enables CTC mode
    /// NOT working with OCR1A or OCR1B
    TIMSK |= (1<< OCIE1A); //OCIE1A timer/counter1 output compare match A interrupt enable.

    while(1){};// blank loop
}

//Interrupt Service Routine + interrupt vector name
//Interrupt vector names/addresses on pg.48 of data-sheet.
// ISR(VectorName_vect){code to execute on ISR fire}
ISR(TIMER1_COMPA_vect){
    x++;
    if (x >= 125){
        PORTB ^= (1<<0); // LED toggle
        x = 0;
    }
}

 

This topic has a solution.

~GuitarDude

Last Edited: Mon. Nov 3, 2014 - 10:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

CTC mode uses OCR1C register.     It tells you in the data sheet.

 

It means that you can have separate COMPA and COMPB with the period set by OCRnC.

 

Most AVRs work the same way.     Some Tinys have differences in how their Timers work.

 

David.

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

I did find two instances in the Attiny45 data sheet pages '89+92 that states OCR1C will reset the timer/counter on compare match in CTC1 mode in the TCCR1 registry.

The discrepancy i found was on page '100 that states OCR1A will reset the counter on compare match in CTC1 mode. Oddly at the end of the register it states its register TCCR1A, which doesnt exist 

as theres only one TCCR1 register on the Attiny45. I could chalk it up to data sheet error. 

~GuitarDude

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

I did find two instances in the Attiny45 data sheet pages '89+92 that states OCR1C will reset the timer/counter on compare match in CTC1 mode in the TCCR1 registry.

The discrepancy i found was on page '100 that states OCR1A will reset the counter on compare match in CTC1 mode. Oddly at the end of the register it states its register TCCR1A, which doesnt exist 

as theres only one TCCR1 register on the Attiny45. I could chalk it up to data sheet error. 

It would help a lot if you tell datasheet version when quoting page numbers.  And/or section titles.

 

In any case, if I match your first mention with page 92 in the rev. J datasheet version, then page 100 falls into "

15. 8-bit Timer/Counter1 in ATtiny15 Mode
", and indeed there might be different actions if you invoke that mode.  Perhaps advantageous for your app.

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.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oddly at the end of the register it states its register TCCR1A, which doesnt exist 

as theres only one TCCR1 register on the Attiny45. I could chalk it up to data sheet error. 

I suppose.  What does it say in bold print 1/2" above that [only one in the datasheet] mention of TCCR1A?

 

"> 15.3.1 TCCR1 – Timer/Counter1 Control Register
 

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

Obviously,   you might have important things to do in an ISR().

 

However,  if you only want to toggle a pin or create variable duty-cycle PWM,    just use the hardware !

There is no need for interrupts at all.

 

As a general rule,   I find AVR data sheets pretty well written.     However,    they all have a few copy-paste "features".

If you find one,   compare it to the most recent version.     And then tell Atmel.     They probably won't do anything but you can get a warm smug feeling!

 

David.

Last Edited: Mon. Nov 3, 2014 - 03:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Data Sheet rev is: 2586Q–AVR–08/2013

ahh! What i though was an error' i read on pg.100 is  13.3.1, which when you scroll up to the 

beginning of chapter 13: 8-bit Timer/Counter1 in ATtiny15 Mode.

 

 

 

 

 

~GuitarDude

Last Edited: Mon. Nov 3, 2014 - 04:08 PM