[fpc-devel] AVR embedded Atmega 328p interrupts

Jeppe Johansen jeppe at j-software.dk
Sun Nov 29 20:13:55 CET 2015


Not sure the timer initialization is right, but it looks ok.

One thing you need is the interrupt procedure directive:

procedure TIMER0_overflow; interrupt; public name 'TIMER0_COMPA_ISR';

Otherwise it'll probably reset at some point.

What compiler commandline are you using?

Best Regards,
Jeppe

On 11/29/2015 05:47 PM, Andrew Haines wrote:
> Hi,  I am experimenting with the avr embedded compiler from trunk and 
> am trying to set an interrupt. I am not sure how to do it. The 
> interrupt I want to change is TIMER0_COMPA_ISR.
>
> I see in the startup code there is:
> .weak TIMER0_COMPA_ISR
>
> and later there is:
> .set TIMER0_COMPA_ISR, Default_IRQ_handler
>
> I have in my test project added a procedure:
> procedure TIMER0_overflow; public name 'TIMER0_COMPA_ISR';
> begin
>   ToggleLED;
> end;
>
> OK I'll just include my test program. I am using an arduino 
> duemilanove clone. I got most of the code from:
> https://sites.google.com/site/qeewiki/books/avr-guide/timers-on-the-atmega328 
>
>
> Anyway there is no LED visible. I have verified using a delay in the 
> main loop toggling the LED works.
>
> What am I not doing, or doing wrong? And btw it's awesome that I can 
> do anything at all on this controller!
>
> Thanks,
>
> Andrew
>
> program test;
> {$mode objfpc}{$H-}
> {uses
>   ATmega328P;}
> const
>   PIN13 = $20;
>   WGM01 = 2;
>   CS02 = 2;
> procedure ToggleLED;
> begin
>   PORTB := PORTB xor PIN13;
> end;
>
> procedure TIMER0_overflow; public name 'TIMER0_COMPA_ISR';
> begin
>   ToggleLED;
> end;
>
> procedure TimerInit;
> begin
>   TIMSK0:= 1 shl TOIE0; // 1
>   TCNT0:=0;
>   TCCR0A := TCCR0A or 1 shl WGM01;
>   TCCR0B := TCCR0B or 1 shl CS02; // 1 shl 2 = 4
>   OCR0A:=$F9;
>
>   TIMSK0:= TIMSK0 or 1 shl OCIE0A;
>   asm
>     sei;
>   end;
> end;
>
> begin
>   DDRB := DDRB or $FF;// PIN13;
>   TimerInit;
>   while True do
>   begin
>     asm
>      nop;
>     end;
>   end;
> end.
>




More information about the fpc-devel mailing list