[En-Nut-Discussion] Interrupt question

Lars Andersson laran at ikp.liu.se
Wed Nov 6 15:38:16 CET 2002

Hi all,

My question is related to the initiation of interrupt routines using Nut-Os. What I really want to do is to setup timer1 on the Mega128 to output a changeable frequency. At the same time I want a function to be executed when output toggles.  So far I have managed to output and control the frequency of the signal by testing it up as CTC using the OCR1A register as top value.
My next problem is to execute the function as I get the compare match on timer1.
Below is some code showing what I have come up with so far. The idea is that the timer output should toggle, and the interrupt should toggle PORTB pin 0. The timer output works fine, but it does not toggle on pin 0.

Please help!
/Lars Andersson

void Timer1CompA_ISR(void *arg)
	// Toggle pin 0 on PORTB.
	if((inp(PORTB) & 0x01) == 0x01)
	 cbi(PORTB, 0);
	 sbi(PORTB, 0);

THREAD(NutMain, arg)
    DDRB=0xFF;		//Set direction of PORTB to output
    PORTB=0x00;	//Set all pinns to low.

    TCCR1A=0x40;	//Toggle output A on compare match
    TCCR1B=0x0D;	//Set operating mode to CTC anf freq. to 14,4 kHz
    OCR1AL=0xFF;	//Set compare match togenerate an output freq of aprox. 28Hz.

    NutRegisterInterrupt  (  IRQ_TIMER1_COMPA, Timer1CompA_ISR, 0);	//Register interrupt
      NutSleep (1000);	//Just so that we do something;

More information about the En-Nut-Discussion mailing list