Go Down

Topic: ATtiny85 Interrupt toggle port (Read 267 times) previous topic - next topic

norlander

May 02, 2014, 01:18 am Last Edit: May 02, 2014, 01:27 am by Coding Badly Reason: 1
I am experimenting with using timer interrupts.  having trouble getting ISR interrupt to fire...at least I think that is the problem.

Any help would be appreciated.  

Here is the code I have:

Code: [Select]
/*
* tiny85_blink_by_timer_interrupt.ino
* testing porgramming attiny portB pin0
*
* using AVR_TINY_PROGRAMMER from sparkfun
* using USBtinyISP in "tools-Programmer"
* using ATTiny85 @ 8 MHz (internal oscilator; BOD disable) in "tools-Board"
* or using ATTiny85 (internal 8MHz Clock) in "tools-Board"
*
* Created: 4/29/2014
* Modified: 5/01/2014
* Author: Krist Norlander; krist_norlander@msn.com
*/


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


void setup()
{
 // Disable global interrupts before initialization
 cli();

 //initialize the PortB Pin0 to output; same as pinMode(ledPin, OUTPUT);
 DDRB |= (1<<PINB0);

 //initialize the timer0
 TCCR0A  = (1<<WGM01);      // Put timer0 in CTC mode and clear other bits
 TCCR0B  = (1<<CS02);       // Timer0 prescaling 256 and clearing other bits
 TIMSK  |= (1<<OCIE0A);     // enable timer1 compare interrupt

 //set the timer0 count
 OCR0A = 255;  // Count 255 cycles before calling ISR interrupt

 PORTB |= (1<<PINB0); // Set PortB Pin0 high to turn on LED

 //initialize global interrupts before beginning loop
 sei();
}

void loop()
{
 //do nothing
 //everything is done by timer interrupt
}

ISR(TIM0_COMPA_vect) {
 //toggle the PortB pin0 to HIGH or LOW
 PORTB ^= (1<<PINB0);

}



Moderator edit: [code] [/code] tags added.

Jack Christensen

What are you expecting to see, and what are you actually seeing?

If the system clock is 8MHz, the timer interrupt will run at 8,000,000/256/256 = 122.07Hz, so the LED will go on and off about 61 times per second. This is too fast for me to see :D
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

norlander

Perfect help.   resolved.  Yes, your math was correct. 

I feel like such a noob considering how easy a fix it was. 

I will modify for better efficiency, but I simply added a "counter" with decrement to check resolution.  and that worked.

again, Thank you!   :)

Code: [Select]
ISR(TIM0_COMPA_vect) {
  //set a counter to control cycles
  if (counter-- == 0) {
    //toggle the PortB pin0 to HIGH or LOW
    PORTB ^= (1<<PINB0);
    //reset counter
    counter = CYCLE;
  }
}


Jack Christensen

Hey you had the concept right, pretty good I'd say! Have you tried the output compare? With that the interrupt (and ISR) can be eliminated and the timer will blink the LED on its own.
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Go Up