简体   繁体   English

PB7上的Atmega2560 PWM

[英]Atmega2560 PWM on PB7

I am trying to get PWM output from PB7 pin with timer0 on Atmega2560 with no luck. 我试图通过Atmega2560上的timer0从PB7引脚获取PWM输出,但是没有运气。 It should generate tone for the connected repro.My PWM settings are: 它会为连接的repro产生声音。我的PWM设置是:

DDRB    = 0b11100000;
PORTB   = 0b00000000;

OCR0A = 0x04;
TCCR0A = (0 << COM0A1) | (1 << COM0A0) | (1 << WGM01) | (0 << WGM00);
TCCR0B = (0 << WGM02) | (0 << CS02) | (0 << CS01) | (0 << CS00);

and then I have this function, which should generate the sound: 然后我有这个功能,应该会产生声音:

void SoundOutput(unsigned int uintTone)
{
    if (uintTone != 0)
    {
        LED_2(1);
        OCR0A = uintTone;

        TCCR0B |= (1 << CS02) | (1 << CS01) | (1 << CS00);
    }
    else
    {
        TCCR0B &= ~((1 << CS02) | (1 << CS01) | (1 << CS00));
    }
}

but nothing happens when i call it with tone parameter. 但是当我使用tone参数调用它时,什么也没有发生。 Can you please help me? 你能帮我么?

Based on your comments, you are using ~12MHz clock as the input to your timer, and from your code, you are using 8 bit timer 0 in CTC mode with OCR0A as your top. 根据您的评论,您将使用〜12MHz时钟作为定时器的输入,并且从代码中,您将在CTC模式下使用8位定时器0,并以OCR0A为顶部。 You have OC0A set to toggle on a compare match. 您已将OC0A设置为切换比较匹配。

According to the 2560 datasheet, the frequency of your timer is given by: 根据2560数据表,您的计时器的频率由下式给出:

F_CLK/(2*(1 + OCR0A)) | F_CLK /(2 *(1 + OCR0A))| F_CLK ~= 12MHz F_CLK〜= 12MHz

This is an 8 bit timer, so that means that your minimum frequency that your PWM can generate is given by: 这是一个8位定时器,因此,您的PWM可以产生的最小频率由下式给出:

12e6/(2*(1 + 255)) ~= 20KHz 12e6 /(2 *(1 + 255))〜= 20KHz

You simply aren't going to be able to generate an audible tone with that configuration unless you slow down the clock you are using for your timer or use a timer that can count higher. 除非您放慢用于计时器的时钟或使用可以计数更高的计时器,否则您将无法使用该配置生成可听见的声音。

1) Use a 16 bit counter (ie timer1). 1)使用16位计数器(即timer1)。 That will give you a min frequency of ~90Hz and a max frequency of ~6MHz, which should give you plenty of range to generate tones: 这将为您提供〜90Hz的最小频率和〜6MHz的最大频率,这将为您提供足够的范围来产生音调:

/* WGM BITS = 0100: CTC Mode */
/* COMA BITS = 01: Toggle OC1A on compare match */
/* CS BITS = 111: External clock source on rising edge */

TCCR1A = (0 << COM1A1) | (1 << COM1A0) | (0 << WGM01) | (0 << WGM00);
TCCR1B = (1 << WGM12) | (1 << WGM13) | (1 << CS02) | (1 << CS01) | (1 << CS00);

2) Use the internal clock source as the timer clock instead of an external source. 2)使用内部时钟源代替外部时钟作为定时器时钟。 Unless you changed fuse bits or you are changing it in the code somewhere, the clock will be 1MHz. 除非您更改保险丝位或在代码中的某个地方更改它,否则时钟将为1MHz。 Prescaling the clock by 8 gives you a frequency range of ~250Hz - ~60KHz. 将时钟预分频为8,可以得到〜250Hz-〜60KHz的频率范围。

/* WGM BITS = 010: CTC Mode */
/* COMA BITS = 01: Toggle OC1A on compare match */
/* CS BITS = 010: Prescale the internal clock by 8 */

TCCR0A = (0 << COM0A1) | (1 << COM0A0) | (1 << WGM01) | (0 << WGM00);
TCCR0B = (0 << WGM02) | (0 << CS02) | (1 << CS01) | (0 << CS00);

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

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