Pages: [1]   Go Down
Author Topic: ATtiny85 Interrupt toggle port  (Read 215 times)
0 Members and 1 Guest are viewing this topic.
Colorado Springs, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 * 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.
« Last Edit: May 01, 2014, 06:27:40 pm by Coding Badly » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-grin
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Colorado Springs, CO
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!   smiley

Code:
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;
  }
}
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4084
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1]   Go Up
Jump to: