简体   繁体   English

ATMEGA 328P变频

[英]ATMEGA 328P varying frequency

I am trying to generate a 16kHz pwm... this is the code i am working with right now. 我正在尝试生成16kHz pwm ...这是我现在正在使用的代码。 ` `

int main(void){
DDRD |= (1 << DDD6);
// PD6 is now an output

OCR0A = 128;
// set PWM for 50% duty cycle


TCCR0A |= (1 << COM0A1);
// set none-inverting mode

TCCR0A |= (1 << WGM01) | (1 << WGM00);
// set fast PWM Mode

TCCR0B |= (1 << CS01);
// set prescaler to 8 and starts PWM


while (1);
{
    // we have a working Fast PWM
}}`

The default frequency is set to 64kHz... is there any way i can change the default frequency ? 默认频率设置为64kHz ...有什么方法可以更改默认频率? Because changing the prescalars does not help me get a frequency of 16kHz... 因为更改预分频器并不能帮助我获得16kHz的频率...

If you can use CTC mode instead of fast PWM (which means, you don't need to vary the pulse-pause-width ratio), you can use CTC mode to accomplish that: 如果您可以使用CTC模式代替快速PWM(这意味着您不需要更改脉冲暂停宽度比),则可以使用CTC模式来实现:

DDRD |= (1 << DDD6);
// PD6 is now an output

OCR0A = 32;
// set PWM for 12.5% duty cycle


TCCR0A |= (1 << COM0A0);
// set toggle mode

TCCR0A |= (1 << WGM01);
// set CTC Mode

TCCR0B |= (1 << CS01);
// set prescaler to 8 and start PWM

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

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