gpt4 book ai didi

c - ATmega32u4 定时器 3 溢出不工作

转载 作者:行者123 更新时间:2023-11-30 16:20:45 25 4
gpt4 key购买 nike

我在使用 ATmega32u4 时遇到了问题,我成功地使用了定时器 1 溢出,但是当我尝试重新调整定时器 3 的代码时,它不起作用。

当定时器 1(16 位)溢出时,以下代码将打开和关闭 LED(引脚 b7 上)。它有效。

#include  <avr/io.h>
#include <avr/interrupt.h>

int main (void) {

DDRB |= (1<<7);; //PortB Output
PORTB = 0x00; //PortB All LEDs off

TCCR1B |= (1<<CS10) | (1<<CS12); //Set Prescaler to 1024

TIMSK1 |= (1<<TOIE1); //Enable Timer Overflowinterrupt
sei(); //Enable Interrupts

while(1);

return 0;
}

ISR(TIMER1_OVF_vect)
{

PORTB ^= (1<<7); //toggle LED

}

接下来的代码旨在使用计时器 3 执行相同的功能,但不起作用。

#include  <avr/io.h>
#include <avr/interrupt.h>

int main (void) {

DDRB |= (1<<7);; //PortB Output
PORTB = 0x00;

TCCR3B |= (1<<CS30) | (1<<CS32); //Set prescaler to 1024

TIMSK3 |= (1<<TOIE3); //Enable Timer Overflowinterrupt
sei(); //Enable Interrupts

while(1);

return 0;
}

ISR(TIMER3_OVF_vect)
{

PORTB ^= (1<<7);

}

最后的代码块用于测试定时器 3 的计数值是否正在增加(不涉及中断),事实确实如此。 (我在此测试中使用不同的 LED)

#include <avr/io.h>

int main()
{

// Prescaler of 1024
TCCR3B |= (1<<CS32)|(1<<CS30);

// Initialize Counter
TCNT3 = 0;

// Initialize LED
DDRE |= (1 << 6); // LED0

// Infinite Loop
while (1)
{
// Flash every 0.016 secs
// COUNTER = 0.016 / (PRE SCALER / CPU FREQ)
// 250
if( TCNT3 >= 250 )
{
// Toggle LED
PORTE ^= (1 << 6); // If output use PORT, If input use PIN

TCNT3 = 0;
}
}
return 0;
}

由此,我假设我在调用中断时做错了什么,我只是不确定是什么

最佳答案

我不确定您使用的是 LilyPad 还是 Leonardo。

我从上面的代码中假设它是 LilyPad。将 TCCR3B 更改为 256,看看它是否可以解决您的问题。

       TCCR3B = 0x0C; // prescaler = 256

关于c - ATmega32u4 定时器 3 溢出不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55181995/

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