简体   繁体   English

在 atmega32 中使用 8 位的 32khz pwm

[英]32khz pwm using 8 bit in atmega32

I'm learning coding in a robotics team .我正在一个机器人团队学习编码。 My seniors give me tasks and I complete it .We code on ATMEGA32 microcontroller using atmel studio (c language).Recent task was to generate a 32khz frequency pwm .I did the task using 16 bit timers as I thought its not possible with 8 bit as there weren't any suitable prescalers (in fast mode you get 62 khz with no prescalers and the next prescalers is f/8 !!) .我的前辈给我任务,我完成了它。我们使用 atmel studio(c 语言)在 ATMEGA32 微控制器上编码。最近的任务是生成一个 32khz 频率的 pwm。我使用 16 位定时器完成了任务,因为我认为它不可能用 8 位因为没有任何合适的预分频器(在快速模式下,您可以在没有预分频器的情况下获得 62 khz,而下一个预分频器是 f/8 !!)。 But my senior says it is possible in 8 bit using any mode !!但是我的前辈说可以使用任何模式在 8 位中使用!! Please tell me how to obtain 32khz with 8 bit timer because I don't see how it is possible unless I change the clock frequency itself (which isn't allowed).请告诉我如何使用 8 位计时器获得 32khz,因为除非我更改时钟频率本身(这是不允许的),否则我不知道这是怎么可能的。 Fast mode is preferable.快速模式更可取。 Do I have to do something with the TCNT0 register??我必须对 TCNT0 寄存器做些什么吗??

you can generate an 32kHz with an 8 bit timer on 16MHz by using the compare register twice per cycle, to get 32kHz you can use the clock divider to /8 and count to from 0 62 (which gives you about 32.2kHz) so, first you set the compare register OCR0 to your pwm value which should be between 0 to 61, and after you get a compare interrupt on OCF0 you change OCR to 62 and wait the next interrupt on OCF0.您可以通过每个周期使用比较寄存器两次,在 16MHz 上使用 8 位定时器生成 32kHz,要获得 32kHz,您可以使用时钟分频器到 /8 并从 0 62(这给您大约 32.2kHz)开始计数,因此,首先您将比较寄存器 OCR0 设置为应在 0 到 61 之间的 pwm 值,在 OCF0 上获得比较中断后,您将 OCR 更改为 62,然后等待 OCF0 上的下一个中断。 There, you clear the TCNT0 to start the process again (or maybe set this second interrupt to use the CTC mode but you still need to get inside the interrupt to set the next interrupt on normal mode)...在那里,您清除 TCNT0 以再次启动该过程(或者可能将第二个中断设置为使用 CTC 模式,但您仍然需要进入中断以将下一个中断设置为正常模式)...

basically there's no way to setup the waveform generator to do a 32KHz PWM signal automatically on the ATmega32 on 16MHz, but you can still use the timer module with it's compare register to generate the signal you need...基本上没有办法设置波形发生器在 16MHz 的 ATmega32 上自动执行 32KHz PWM 信号,但是您仍然可以使用带有比较寄存器的定时器模块来生成您需要的信号......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM