(求助求助)mega2560怎么设置PWM的频率呀????
急求急求~~~感谢感谢{:soso_e183:} 本帖最后由 Paderboy 于 2015-7-27 22:36 编辑For Arduino Mega: (tested on Arduino Mega 2560)
timer 0 (controls pin 13, 4)
timer 1 (controls pin 12, 11)
timer 2 (controls pin 10, 9)
timer 3 (controls pin 5, 3, 2)
timer 4 (controls pin 8, 7, 6)
TCCRnB, where 'n' is the number for the timer.
TCCR2B for timer 2, TCCR3B for timer 3.
Eg:
TCCR2B = TCCR2B & 0b11111000 | 0x01;
//sets Arduino Mega's pin 10 and 9 to frequency 31250.
//code typically inserted in setup()
Setting Divisor Frequency
0x01 1 62500
0x02 8 7812.5
0x03 64 976.5625 <--DEFAULT
0x04 256 244.140625
0x05 1024 61.03515625
Setting Divisor Frequency
0x01 1 31372.55
0x02 8 3921.16
0x03 64 490.20 <--DEFAULT
0x04 256 122.55
0x05 1024 30.64
http://playground.arduino.cc/Main/TimerPWMCheatsheet 小白求前辈们能详细点{:soso_e101:} Paderboy 发表于 2015-7-27 22:27 static/image/common/back.gif
For Arduino Mega: (tested on Arduino Mega 2560)
timer 0 (controls pin 13, 4)
timer 1 (controls pin ...
首先非常感谢前辈的提示。但是我看了很久还是对这些代码不清楚,能麻烦你在说得详细一点吗?具体实现设置PWM为400HZ的代码是什么呢?麻烦你了 本帖最后由 Paderboy 于 2015-7-29 00:02 编辑
配置定时器的预分频。。。mega 2560 有5个定时器。。你选择你想设置的定时器。。
For Arduino Mega: (tested on Arduino Mega 2560)
timer 0 (controls pin 13, 4)
timer 1 (controls pin 12, 11)
timer 2 (controls pin 10, 9)
timer 3 (controls pin 5, 3, 2)
timer 4 (controls pin 8, 7, 6)
TCCR(这里修改你想修改的timer 0到4 之间)B = TCCR(这里修改你想修改的timer 0到4 之间)B & 0b11111000 | 0x01(这个就是预分频设置参数)从这个2个列表里选择。。。
这个是例子修改了timer的默认频率为31.25khz。。
sets Arduino Mega's pin 10 and 9 to frequency 31250.
TCCR2B = TCCR2B & 0b11111000 | 0x01;
不同的定时器能实现的最高设置频率不一样,最好不要修改millis()使用的定时器会影响delay()。。。
先用示波器,查看正常analogWrite() - PWM输出的频率。。然后对比列表里默认的频率,就可以判断这个定时器,最高可以设置的频率了。。。
Setting Divisor Frequency
0x01 1 62500
0x02 8 7812.5
0x03 64 976.5625 <--DEFAULT
0x04 256 244.140625
0x05 1024 61.03515625
Setting Divisor Frequency
0x01 1 31372.55
0x02 8 3921.16
0x03 64 490.20 <--DEFAULT
0x04 256 122.55
0x05 1024 30.64
輸出pwm需要直接設定TCCR,設定方式直接去atmel官網挖datasheet
看一下CTC跟快速PWM,如果要做50%duty cycle兩種都可以,不過網上建議用快速PWM
https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
須操作TCCRnA跟TCCRnB
下面設定沒試過
16MHz / 256 / (77+1) / 2 = 400.6Hz
Timer2 WGM=7 Prescale=256 OCRA=77
页:
[1]