Arduino的模拟输出-analogWrite()
2011-03-27 17:49
363 查看
analogWrite()
Description 介绍
Writes an analog value (PWM wave) to a pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to [b]analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal is approximately 490 Hz.将模拟值(PWM波)输出到管脚。可用于在不同的光线亮度调节发光二极管亮度或以不同的速度驱动马达。调用analogWrite()后,该引脚将产生一个指定占空比的稳定方波,直到下一次调用analogWrite()(或在同一引脚调用digitalRead()或digitalWrite())。 PWM的信号频率约为490赫兹。
On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11. You do not need to call pinMode() to set the pin as an output before calling analogWrite().
在大多数Arduino板(带有ATmega168或ATmega328),这个函数工作在引脚3,5,6,9,10和11。在ArduinoMega,它适用于2-13号引脚。老的带有ATmega8的Arduino板只支持9,10,11引脚上使用。你并不需要在调用analogWrite()之前为设置输入引脚而调用pinMode()。
The analogWrite function has nothing whatsoever to do with the analog pins or the analogRead function.
这个analogWrite方法与模拟引脚或者analogRead方法毫不相干
Syntax 语法
analogWrite(pin, value)Parameters 参数
pin: the pin to write to.pin:输出的引脚号
value: the duty cycle: between 0 (always off) and 255 (always on).
value:占用空:从0(常关)到255(常开)
Returns 返回值
nothingNotes and Known Issues 备注和已知问题
The PWM outputs generated on pins 5 and 6 will have higher-than-expected duty cycles. This is because of interactions with the millis() and delay() functions, which share the same internal timer used to generate those PWM outputs. This will be noticed mostly on low duty-cycle settings (e.g 0 - 10) and may result in a value of 0 not fully turning off the output on pins 5 and 6.引脚5和6的PWM输出将产生高于预期的占空比。这是因为millis()和delay()函数,它们共享同一个内部定时器用于产生PWM输出所产生的相互作用。这提醒我们引脚5和6在多数低占空比的设置(如0- 10)的情况下0数值的结果并没有完全关闭。
Example 例子
Sets the output to the LED proportional to the value read from the potentiometer.int ledPin = 9; // LED connected to digital pin 9 int analogPin = 3; // potentiometer connected to analog pin 3 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the pin as output } void loop() { val = analogRead(analogPin); // read the input pin analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255 }
[/b]
相关文章推荐
- Arduino教程 模拟输入输出以及电机和舵机控制
- 【Arduino官方教程第一辑】示例程序 3-3 模拟信号输出[Mega]
- 关于上拉输入、下拉输入、模拟输入、浮空输入、推挽输出、开漏输出、复用输出的区别
- 【Arduino官方教程】基础示例(六):读取模拟电压
- Arduino初初教程2——I/O口输出应用
- python模拟浏览器webdriver登陆网站后抓取页面并输出
- 有关推挽输出、开漏输出、复用开漏输出、复用推挽输出以及上拉输入、下拉输入、浮空输入、模拟输入区别
- android 模拟IR输出
- 洛谷 1067——多项式输出(简单的模拟)
- STM32中GPIO_ReadOutputDataBit及GPIO_ReadInputDataBit区别;上拉输入_IPU,下拉输入_IPD,模拟输入_AIN ;开漏输出_Out_OD, 推挽输出_O
- 用Android手机通过蓝牙模块HC-06连接Arduino串口输出
- C语言:模拟实现strstr函数,若是子串,输出子串后面的字符串,否则输出null
- Python模拟C++输出流
- 模拟输入H.264流,输出封装格式文件
- Arduino学习经验(一)之解决舵机库和pwm输出冲突
- Arduino的模拟输入-analogRead()
- 编写多线程程序,模拟多个人通过一个山洞。这个山洞每次只能通过一个人,每个人通过山洞的时间为2秒(sleep)。随机生成10个人,都要通过此山洞,用随机值对应的字符串表示人名,打印输出每次通过山洞的人名
- (转载)有关推挽输出、开漏输出、复用开漏输出、复用推挽输出以及上拉输入、下拉输入、浮空输入、模拟输入区别
- 关于上拉输入、下拉输入、模拟输入、浮空输入、推挽输出、开漏输出、复用输出的区别
- Arduino初初教程1——I/O口输出