极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14085|回复: 0

Arduino 频率1-100k 占空比可调多路输出可蓝牙连接手机端监控调试

[复制链接]
发表于 2018-4-28 22:47:24 | 显示全部楼层 |阅读模式
本帖最后由 prince^_^ 于 2018-4-28 23:15 编辑

得益于弘大发的库文件做出的一款简单PWM输出程序  新手入门
另有疑问如何改此库文件可以实现双路PWM互补输出?
欢迎各路少侠前来指教

http://v.youku.com/v_show/id_XMzU3MTcyNTAxNg==.html?spm=a2h3j.8428770.3416059.1
还有为什么我添加的视频无法播放
  1. /********************************************
  2. * 本程序实现多路PWM*频率*占空比*可调输出
  3. * 配合蓝牙串口app Bluetooth spp pro使用更佳
  4. * 烧写程序时请注意先不要接蓝牙串口线(共用)
  5. * 否则会上传失败
  6. * 由蓝牙串口发送指令实时回传频率及占空比信息
  7. * 使用本程序需要添加库文件PWM.zip
  8. * 添加方法:项目-->加载库-->添加一个.zip库
  9. * 对此库文件作者做出的贡献深表谢意
  10. * *******************************************
  11. * This program to achieve multiple PWM * frequency * duty cycle * adjustable output
  12. * Better use with Bluetooth app serial Bluetooth spp pro
  13. * Please be careful not to connect the bluetooth serial cable (shared) when you write the program.
  14. * Otherwise it will fail to upload.
  15. * Real-time return frequency and duty cycle information sent by Bluetooth serial port
  16. * Use this program need to add library file PWM.zip
  17. * Add Method:Project -> Load Library -> Add a .zip Library
  18. * Thanks to the author of this library file
  19. **********************************************/
  20. #include <PWM.h>

  21. int PWMOUT = 9;                           // PWM输出引脚
  22. float DUE = 126;                          // 初始占空比50%范围是0-255
  23. int32_t Hz = 1000;                        // 频率 (单位是Hz)
  24. char inByte;                              // 定义条件指令
  25. void setup()
  26. {
  27.   InitTimersSafe();                       // 初始化除了0号计时器以外的其他计时器
  28.   Serial.begin(9600);                    //配置波特率
  29. }

  30. void loop()
  31. {
  32.       SetPinFrequencySafe(PWMOUT, Hz);     //设置指定引脚的频率
  33.       delay(1);
  34.       pwmWrite(PWMOUT, DUE);               //设置指定引脚的占空比
  35.       delay(1);
  36. /*******************添加以上四行程序即可实现多路PWM输出***********************/
  37. /** Add the above four lines of program to realize multi-channel PWM output **/
  38. if (Serial.available() > 0)
  39.      {
  40.        inByte = Serial.read();            //缓存条件指令
  41.      }
  42.      delay(10);

  43.      switch (inByte)
  44.      {
  45.       case '0':  
  46.         Hz = Hz - 1000;
  47.          if(Hz < 1000)                      //最小频率1K
  48.          {  Hz = 1000;  }      
  49.         Serial.print("The Frequency is ");
  50.         Serial.print(Hz);
  51.         Serial.println("Hz");
  52.         inByte = 9;                         //安全值
  53.       break;
  54.       case '1':  
  55.         Hz = Hz + 1000;                     //递增 1K Hz
  56.          if(Hz > 100000)                    //最大频率100K
  57.          {  Hz = 100000;  }      
  58.         Serial.print("The Frequency is ");
  59.         Serial.print(Hz);
  60.         Serial.println("Hz");
  61.         inByte = 9;
  62.       break;
  63.       case '2':  
  64.         DUE = DUE - 13;                     //递减 5%
  65.          if(DUE < 0)                        //最小占空比
  66.          {  DUE = 1;  }      
  67.         Serial.print("The Duty is ");
  68.         Serial.print(DUE/2.55,2);
  69.         Serial.println("%");
  70.         inByte = 9;
  71.       break;
  72.       case '3':  
  73.         DUE = DUE + 13;
  74.          if(DUE > 255)                      //最大占空比
  75.          {  DUE = 254;  }      
  76.         Serial.print("The Duty is ");
  77.         Serial.print(DUE/2.55,2);
  78.         Serial.println("%");
  79.         inByte = 9;
  80.       break;     
  81.      }  
  82. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-28 17:04 , Processed in 0.046034 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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