avrprog

 

InterruptsCorrection

Page history last edited by hb 4 yrs ago

Updated AVR Interrupt Example

 

Craig Limber's AVR interrupt page is perhaps the only decent page on interrupts on the AVR not directed toward experts.

Anyway, I did have a bit of challenge in getting it working on my atmega8515 and atmega16 using my STK500

A few changes were required:

 

  • avr-gcc 4.0.2 has trouble compiling cbi and sbi. I replaced standard code cribbed from the AVR butterfly source to directly twiddle the bit to flip the LED.
  • the key fix, which took much googling, was to call sei() appropriately.
  • the actual include files are not displayed in his examples. They are there in the source view, but the browser doesn't like the open/close brackets and does not display them.

 

//
// Craig Limber, July 2004
//
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#define cbiBF(port,bit)  (port &= ~(1<<bit))  //clear bit in port
#define sbiBF(port,bit)  (port |= (1<<bit))   //set bit in port
//-----------------------------------------------------------------------------
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
static uint8_t ledon;
if (ledon)
{
ledon = 0;
cbiBF(PORTB, PB4);
}
else
{
ledon = 1;
sbiBF(PORTB, PB4);
}
}
//-----------------------------------------------------------------------------
int main(void)
{
DDRB = _BV(DDB4);     // enable output
TIMSK = _BV(OCIE1A);
sei();
TCCR1B = _BV(CS12)    // 256 prescale
| _BV(WGM12);  // CTC mode, TOP = OCR1A
//OCR1A = 15625;        // count up to TOP   1hz with 8 meg system clock
OCR1A = 1625;        // count up to TOP   1hz with 8 meg system clock
while (1)
asm volatile("nop" ::);  // we spin!  Better if put processor to sleep
return 0;
}

 

Contact

 

Contact newsaccount dot h at gmail dot com with questions or comments

It's my intention to take down this page as soon as Craig has updated his page, and/or if he prefers that I not have this up...

Comments (0)

You don't have permission to comment on this page.