gpt4 book ai didi

c - avr-c atmega324a 中的定时器

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

#define F_CPU 8000000UL
#include <avr/io.h>

/*
* main -- Main program
*/
int main(void)
{
/* Set OC1A pin to be an output */
DDRD|=(1<<5);

/* Set output compare register value */
OCR1A = 4000;


/* Set timer counter control registers A and B so that
* - mode is - clear counter on compare match
* - output compare match action is to toggle pin OC1A
* - correct clock prescale value is chosen.
* TCCR1C can just stay as default value (0).
*/

TCCR1A |=(1<<COM0A0) | (1<<WGM12);
TCCR1B |= (0<<CS12) | (1<<CS11) | (1<<CS10) | (1<<WGM12) | (1<<WGM12);

while(1){

}
}

我有一个连接到 OC1A 端口的 LED,但它从不闪烁,非常感谢您的帮助。

我已经浏览了数据表,但不明白为了制作 LED 闪光灯还必须做什么,我相信对于具有任何 C 知识的人来说这会很简单。

最佳答案

首先,在设置像 TCCR1A 这样的寄存器时,您只需分配要放入其中的值,而不是将要设置的位与其中已有的值进行“或”操作,因为您会得到不需要的旧值混合和新的。

然后尝试更改此设置:

TCCR1A = (1 << COM1A0);    //COM1A0 in stead of COM0A0, and WGM12 is not part of TCCR1A
TCCR1B = (1 << CS11) | (1 << CS10) | (1 << WGM12); //No need to write (0 << x) for bits you don't want set, WGM12 is part of TCCR1B

如果 LED 闪烁太快,请增加 OCR1A 和/或更改预分频器(cs 10、11 和 12)

关于c - avr-c atmega324a 中的定时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30219812/

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