Ziva
Ziva

Reputation: 3501

Assembly - how to set frequency of interrupt

Using assembly and AVR Microcontroller I want to write program which causes the interrupt handling with a certain frequency, eg 10 Hz. first I set the stack and the timer:

.cseg

.org jmp restart;

.org 0x002E tjmp timer_fun


restart:
cli
ldi R16, HIGH(RAMEND)
out SPH, R16
ldi R16, LOW(RAMEND)
out SPL, R16
sei

ldi R17, 1<<CSOO
out TCCR0, R17
ldi R16, 1<<TOIE0
out TIMSK, R16

But now, I don't know how to set this frequency?

Upvotes: 0

Views: 1395

Answers (1)

Michael
Michael

Reputation: 58467

You can do this by counting the number of interrupts triggered. It would depend on the input frequency for the timer (which I guess would typically be the same as the CPU frequency).

Let's say that the input frequency is 16000000 Hz:

Starting at a count of 0 (TCNT0 set to 0), and a prescaler of 256 (TCCR0 set to 1<<CS02) would cause a timer overflow at 16000000/256 == 62500 Hz.

Your timer interrupt service routine could then do something like this (I'm using C here, but you get the idea):

counter++;
if (counter == 6250) {
    // We should end up in here approximately 10 times/second
    counter = 0;
}

Upvotes: 2

Related Questions