荣耀彩票代理

IT技术互动交流平台

用Arduino剖析PWM脉宽调制

作者:代码钢琴家  来源:IT165收集  发布日期:2016-11-24 21:08:21

PWM(Pulse Width Modulation)简介

荣耀彩票代理PWM,YEJIUSHIMAICHONGKUANDUDIAOZHI,YONGYUJIANGYIDUANXINHAOBIANMAWEIMAICHONGXINHAO,YEJIUSHIFANGBOXINHAO。DUOYONGYUZAISHUZIDIANLUZHONGQUDONGFUZAISUISHIJIANBIANHUADEDIANZIYUANJIAN,RULED,DIANJIDENG。

ZAIDANPIANJIZHONG,WOMENCHANGYONGPWMLAIQUDONGLEDDEANLIANGCHENGDU,DIANJIDEZHUANSUDENG。

WOMENZHIDAO,ZAISHUZIDIANLUZHONG,DIANYAXINHAOSHILISANDE: BUSHI 0(0V)  JIUSHI 1(5VHUOZHE3.3V), NEIMERUHESHUCHUJIEYU 0v HE  5VZHIJIANDEMOUGEDIANYAZHINI?

WOMENXIANLAIJUGESHIJIDELIZI,YIKANJIUDONG,SHENGGUOQIANYANWANYU。

荣耀彩票代理RUXIATU,YAORANGRANGSHUZIXINHAOMONICHU 3.7V DEDIANYASHUCHU。KEYIXIANJIAXIANG:3.7VDEDIANYASHUCHUSHIYOUDUOGEZHOUQISHIJIANWEIT ,DIANYAZHI DOU WEI3.7VDEXINHAO CHIXUSHUCHUXINGCHENGDE。

GENJUPWMYUANLI,WOHUACHUXIAMIANDENGXIAOMAICHONGXINHAOBOXINGTU(HONGSE) 。PWMDELINIANSHI:LIANXUDEXINHAOKEYISHIYONGDULIDEFANGBOXINHAOMONICHULAI,SHOUDUANJIUSHIDIAOZHENGSHUCHUDEMAICHONGKUANDU,YIDADAOTONGYANGDEXIAOGUO。NIHUIHUAIYI:ZHEIZHENDEKEYIDADAOTONGYANGDEXIAOGUOMA?

BIEJI,RANGWOMENJIXUWANGHOUKAN。

荣耀彩票代理 RUGUOYIGEZHOUQITNEIDEJUNZHIDIANYADENGYU3.7V,NEIME,ZHENGTIDESHUCHUJIUSHI3.7V,YINWEIZHENGTIZHIBUGUOSHInGEZHOUQIBUDUANDEZHONGFUERYI。NEIMEWOMENDEZHUYAOWENTIJIUSHIRUHERANGYIGEDIAOZHIZHOUQITSHIJIANNEIDEJUNZHIDIANYADENGYU3.7V。XIAMIANJIUKAISHIJISUAN。

设:脉冲信号的值随时间变化的函数为:

荣耀彩票代理YINWEIZHEILISHISHUZIDIANLUDEBEIJINGXIADE,SUOYIvDEZHIZHINENGQU 0v HUOZHE   5v。

荣耀彩票代理SHE:ZAIYIGEZHOUQITSHIJIANZHONG,GAODIANPINGCHIXUSHIJIANZHANTDEBAIFENBIWEI D,ZEDIDIANPINGCHIXUSHIJIANZAIZHOUQITZHONGZHANDEBAIFENBIWEI 1-D。

WOMENDUI YIGEDIAOZHIZHOUQITNEIDEDIANYAZHIDUISHIJIANJIFEN,RANHOUCHUYIZHOUQIT,JIUDEDAOLEZHEIGEZHOUQIDESHUCHUDIANYAJUNZHI。

YOUYUZHEIGEJIFENTUXINGSHIFANGBO,SUOYIHENHAOJISUAN(JIUSHIMIANJI CHUYIT)。

rsse73szXlTD3T81Tpbg21HIB+91dnYuWLAgNTU1Li5uw4YNzFIHVJGVlaXoVdHf35+fn+/k5BQdHX348OGioqLU1FR/f/8bN25gGCYSiRITE7du3YrvtsjMzNyzZ09oaKhRn64/MDAwefJkQ56rg8N12U+ePJnZwB6nra3Nzc0tPj5e/WNbt24lb0rDMCwvLw/fPimRSLy9vc+dO8fMAEApeG2uWB3X19dbWFiQd+a8++6777zzTktLCzHnn56e7uHhgQ/6ioqK8O0DRgq+Ym/IM3NxOC37H3/80cLCgsGh32Q2bNhgZ2enxjUSw7BFixaZmZmRT8iOiYkhXsjx5ptvMjiTD1BDYGCgo6Oj4oazwsJCGxubx48fE1e2b9/u4uJSWlpKHEE5c+ZMYjU3Nzf3/PnzhrFZH8TFxQkEAsOf6cJF2Uul0ra2tp9//nnDhg0IocjIyLa2NsanMre0tLi6uqp5r4ZUKvX29pabNXzttdcIqUdFRRn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vJlx8SwKAeAKjCbdnLzN8pn9KTgegVqHsIAPiOEcmeVmQgfABQiSFkr2qNbahPrqGTzzA10D0AqICjrb3clB6D2ED2AKAKbrb2oHoA0CMcbO2JmXsqwm2qEIvFYuJRWmEBgKdwSPYyUNWtkrAgeQBQDxdkDwCAQQHZAwDvANkDAO8A2QMA7wDZAwDvANkDAO8A2QMA7wDZAwDvANkDAO8A2QMA7wDZAwDvANkDAO8A2QMA7wDZAwDv+D+xdqmy/Fd/cwAAAABJRU5ErkJggg==" />

荣耀彩票代理KEYIKANCHU,1GEDIAOZHIZHOUQINEI,SHUCHUDEDIANYAJUNZHIZHIHEDYOUGUAN。YEJIUSHIGAODIANPINGXINHAOZHANCHIXUSHIJIANZHANZHEIGEZHOUQIDEBAIFENBIJUEDINGZHEIGEZHOUQINEIDESHUCHUDIANYA。

SHANGMIANSHUOLE,YAORANGZHEIGEJUNZHIDENGYU3.7v,ZEQIUCHUDWEI:0.74   

那也就是说:如果在一个调制周期中,高电平持续时间占周期的百分比为74%,则整体输出的信号就是3.7V。这个百分比就是下面要说的占空比

占空比(duty cycle)

YOULEQIANMIANDEZHISHI,XIANGXINNIYIJINGDUIZHANKONGBILIJIELE,QISHIHENHAOLIJIE ,ZHANKONGBIJIUSHI ZAIYIDUANDIAOZHIZHOUQISHIJIANNEI,MOUGEXINHAOCHIXUDESHIJIANZHANZHEIGESHIJIANDUANDEBAIFENBI。

XIAMIANJICHUZHANKONGBIDEGONGSHI

  D: 占空比 PW: 脉冲宽度(调制周期中脉冲持续时间) T:  一个调制周期

荣耀彩票代理XIAMIANSHIYIGEZHANKONGBIBUDUANBIANHUADETUSHI

所以我们可以很自然的得出结论:

DIZHANKONGBIYIWEIZHESHUCHUDENENGLIANGDI,YINWEIZAIYIGEZHOUQINEIDABUFENSHIJIANXINHAOCHUYUGUANBIZHUANGTAI,RUGUOpwmKONGZHIDEFUZAIWEIled,ZEJUTIBIAOXIANLIRUledDENGHENAN.

GAOZHANKONGBIYIWEIZHESHUCHUDENENGLIANGGAO,ZAIYIGEZHOUQINEI,DABUFENSHIJIANXINHAOCHUYUonZHUANGTAI,JUTIBIAOXIANWEILEDBIJIAOLIANG。

DANGZHANKONGBIWEI100%SHI,BIAOSHI fully on,YEJIUSHIZAIYIGEZHOUQINEI,XINHAODOUCHUYUonZHUANGTAI,JUTIBIAOXIANWEIledLIANGDUDAODA100%

WEI0%SHIZEBIAOSHI fully off,ZAIYIGEZHOUQINEI,YIZHICHUYUoffZHUANGTAI.JUTIBIAOXIANWEIledXIMIE。

现在一切都明了了:脉宽调制,脉宽调制,脉宽调制,这个宽,不是物体的宽度,而是信号的持续时间大小,它可以用占空比去衡量,占空比越大,脉冲越宽,反之亦然。

下面扩展一下,加深理解:用PWM模拟出如下的正玄波

道理是一样的,只不过,占空比要变化,这里就是逐渐增加。

PWM的频率 (PWM frequency)

荣耀彩票代理pwmDEPINLVJUEDINGLESHUCHUXINHAO   on HE off DEQIEHUANSUDU。BIRUNIXIANGRANGYIZHANDIANDENGDEZAI1sNEIDELIANGDUJIANGDIWEIYUANLAIDEYIBAN,NIKEYIQIAN0.5s DAKAIKAIGUAN,HOU0.5sGUANBIKAIGUAN。YEJIUSHIJINJINYONGLE1GEDIAOZHIZHOUQI,NEIMEZHEIGEPINLVJIUSHI1Hz,XIANRANZHEIGEXIAOGUOSHIBUMINGXIANDE,WOMENDEDIANNAOPINGMUDESHUAXIN60Hz,CAINENGZUGOUSHUNHUA。RENYANDEJIXIANYIBANSHI50Hz。YAODADAO50HzZAI,YIWEIZHENIYAOZAI1sSHIJIANNEIQIEHUANKAIGUAN50CI,DANSHENYIBEIZIDESHOUSUYEBUKENENGDADAOZHEIMEKUAI~。HAISHIRANGDANPIANJIQUZUOBA。

YIBANpwmPINLVDOUSHIYINYINGJIANSHEJIERGUDINGDE,SHIYOUpwmFASHENGQIJUEDINGDE。

荣耀彩票代理PWMPINLVYUEGAO,DIAOZHICHULAIDESHUCHUQUXIANJIUGENGJIADEDEsmooth,XIAOGUOYUEHAO。ZHEIGEHESHOUJIDEppiYUEGAO,XIANSHIYUEQINGXISHIYIGEDAOLI。DANGRANWOXIANGPWMDEPINLVYUEGAO,DUIYINGJIANDEYAOQIUJIUYEYUEGAO。

XIATUZHONG,YOUBIANDEPINLVSHIZUOBIANDE2BEI,DIAOZHICHUDEQUXIANGENGJIAYUANHUA,TIEJINYUANSHIBOXING。

使用Arduino来实战!

荣耀彩票代理WOYIZHIJUEDE,YONGSHIJIANQUYANZHENGJIAOKESHUSHANGDESUOWEIZHENLISHIYIJIANLINGRENJIDONGRENXINDESHI,KEXIDESHIZHIDAODAXUEWOCAIYOUZHEIYANGDEJIHUI!

SHOUXIANYAOQUEDINGNIDEArduino DENAXIEYINJIAOZHICHIPWMSHUCHU,YIBANBIAOJILE1GE  ~  JIUSHIZHICHIPWMDE。

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 - 13 and 44 - 46. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

The frequency of the PWM signal on most pins is approximately 490 Hz. On the Uno and similar boards, pins 5 and 6 have a frequency of approximately 980 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.

荣耀彩票代理                                                                                                     --  arduino.cc

Arduino XINPIANWEIATmega168 HUOZHEATmega328DE3, 5, 6, 9, 10, HE 11ZHICHIPWM, Arduino MegaDE 2~13 , 44~46ZHICHIPWM,LAOBANZIATmega8DE9, 10,11JIAOZHICHIPWM

荣耀彩票代理YIBANDEArduino PWMDEPINLVDAYUEWEI490Hz,Uno YIJIXIANGTONGLEIXINGDEBANZIDE5,6JIAOKEDA980Hz,LeonardoDE 3,11JIAOYEKEYIDADAO980Hz

PWMDEPINLVSHIBUNENGYOUWOMENKONGZHILE,SUOYIWOMENBAZHUYILIFANGZAIZHANKONGBISHANG。

Arduino  PWMSHUCHUHANSHU,ZHUYIZHEIGEHANSHUHEMONIYINJIAOMEIBANMAOQIANDEGUANXI。SHIYONGZHEIGEHANSHUDEYINJIAOWUXU SHIYONG pinMode PEIZHIWEI OUTPUT

荣耀彩票代理analogWrite(pin,value)   

value DEZHIZAI 0~255ZHIJIAN,valueDEZHICHUYI256JIUSHIPWMSHUCHUDEZHANKONGBI,RU analogWrite(pin,128) SHUCHUDEZHANKONGBIWEI50%。

SHIYANLIANXIANXIANTU

 

const int ledPin = 3;     //pwm输出引脚
const int button = 6;     //按键引脚
byte pwmVal = 0;


bool isKeyPressed(byte pin);


void setup() {
  pinMode(button,INPUT_PULLUP);
  Serial.begin(9600);
}

void loop() {

  if(isKeyPressed(button))    //如果检测到按键按下,就让pwmVal 增加2
  {
       pwmVal+=2;        //pwmVal 的类型为byte,到了256会自动溢出回0,所以为没做检查,不过不要过度依赖这个技巧啊,规范些好
  }

  analogWrite(ledPin,pwmVal);
  Serial.println(map(pwmVal,0,255,0,5));    //使用map函数映射为 0~5v的电压信号
  delay(30);
  
   

}


bool isKeyPressed(byte pin)
{
    bool pre = false;
    if(digitalRead(pin)==LOW)
    {
        delay(10);
        if(digitalRead(pin)==LOW)
        {
             pre = true;
             for(int a = 5;digitalRead(pin)==LOW&&a;--a)
             {
                 delay(5);
             }
        }
    } 

    return pre;
}

ZAIIDEDECHUANKOUHUITUQIZHONGCHAKANSHUCHUDEBOXING。YINWEISHISHOUDONGANJIANLAIDIAOZHENGZHANKONGBIDE,SUOYIBOXINGBUHAOKAN。YONGDIANWEIQIDIAOGENGJIAHAO。

WOMENQUDIAOmapHANSHU,ZHIJIESHUCHUpwmValDEZHI,KEYIKANDAOGENGJIAXINI。

YEWANDEXIAOGUO。

 

荣耀彩票代理  CHUCHU:www.cnblogs.com/lulipro

 WEILEHUODEGENGHAODEYUEDUTIYAN,QINGFANGWENYUANBOKEDIZHI。

荣耀彩票代理XIANYUBENRENSHUIPING,RUGUOWENZHANGHEDAIMAYOUBIAOSHUBUDANGZHICHU,HAIQINGBULINCIJIAO。

 

延伸阅读:

Tag标签:   
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规