gpt4 book ai didi

c - 如何设置 ISR 每秒运行一次 - C Atmega328p

转载 作者:行者123 更新时间:2023-11-30 17:07:08 25 4
gpt4 key购买 nike

我正在编写一个代码,需要每 0.5 秒检查一次传感器的输入。我想使用 ISR,因为我希望我的代码一直执行,直到传感器的输入发生变化。

我如何设置此 ISR 每 0.5 秒执行一次?

谢谢:)

最佳答案

我建议使用定时器中断。举个例子,请点击这里。 http://www.avrfreaks.net/forum/tut-c-newbies-guide-avr-timers?page=all

我自己还没有测试过,但这里有一段代码。

#include 
#include

int main (void)
{
DDRB |= (1 << 0); // Set LED as output

TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode

TIMSK |= (1 << OCIE1A); // Enable CTC interrupt

sei(); // Enable global interrupts

OCR1A = 15624; // Set CTC compare value to 1Hz at 1MHz AVR clock, with a prescaler of 64

TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64

for (;;)
{

}
}

ISR(TIMER1_COMPA_vect)
{
PORTB ^= (1 << 0); // Toggle the LED
}

关于c - 如何设置 ISR 每秒运行一次 - C Atmega328p,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com