组装-如何设置中断频率 [英] Assembly - how to set frequency of interrupt
问题描述
使用程序集和AVR微控制器,我想编写一个程序,该程序以一定的频率(例如10 Hz)引起中断处理. 首先,我设置堆栈和计时器:
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?
推荐答案
您可以通过计算触发的中断数来做到这一点.它取决于计时器的输入频率(我想通常与CPU频率相同).
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).
假设输入频率为16000000 Hz:
Let's say that the input frequency is 16000000 Hz:
从计数0(TCNT0
设置为0)开始,并且预分频器为256(TCCR0
设置为1<<CS02
),将导致计时器溢出16000000/256 == 62500 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.
您的计时器中断服务例程可以执行以下操作(我在这里使用C,但是您知道了)
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;
}
这篇关于组装-如何设置中断频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!