极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27587|回复: 5

(求助求助)mega2560怎么设置PWM的频率呀????

[复制链接]
发表于 2015-7-27 20:11:17 | 显示全部楼层 |阅读模式
急求急求~~~感谢感谢{:soso_e183:}
回复

使用道具 举报

发表于 2015-7-27 22:27:54 | 显示全部楼层
本帖最后由 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
回复 支持 0 反对 1

使用道具 举报

 楼主| 发表于 2015-7-27 20:12:06 | 显示全部楼层
小白求前辈们能详细点{:soso_e101:}
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-7-28 00:21:41 | 显示全部楼层
Paderboy 发表于 2015-7-27 22:27
For Arduino Mega: (tested on Arduino Mega 2560)
timer 0 (controls pin 13, 4)
timer 1 (controls pin ...

首先非常感谢前辈的提示。但是我看了很久还是对这些代码不清楚,能麻烦你在说得详细一点吗?具体实现设置PWM为400HZ的代码是什么呢?麻烦你了
回复 支持 反对

使用道具 举报

发表于 2015-7-28 23:59:59 | 显示全部楼层
本帖最后由 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
回复 支持 反对

使用道具 举报

发表于 2015-7-29 09:37:57 | 显示全部楼层

輸出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
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 20:57 , Processed in 0.049575 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表