Assembly - how to set frequency of interrupt

∥☆過路亽.° 提交于 2019-12-25 05:30:21

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/16730519/assembly-how-to-set-frequency-of-interrupt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!