prince^_^ 发表于 2018-4-28 22:47:24

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

本帖最后由 prince^_^ 于 2018-4-28 23:15 编辑

得益于弘大发的库文件做出的一款简单PWM输出程序新手入门
另有疑问如何改此库文件可以实现双路PWM互补输出?
欢迎各路少侠前来指教
http://v.youku.com/v_show/id_XMzU3MTcyNTAxNg==.html?spm=a2h3j.8428770.3416059.1
http://v.youku.com/v_show/id_XMzU3MTcyNTAxNg==.html?spm=a2h3j.8428770.3416059.1
还有为什么我添加的视频无法播放
/********************************************
* 本程序实现多路PWM*频率*占空比*可调输出
* 配合蓝牙串口app Bluetooth spp pro使用更佳
* 烧写程序时请注意先不要接蓝牙串口线(共用)
* 否则会上传失败
* 由蓝牙串口发送指令实时回传频率及占空比信息
* 使用本程序需要添加库文件PWM.zip
* 添加方法:项目-->加载库-->添加一个.zip库
* 对此库文件作者做出的贡献深表谢意
* *******************************************
* This program to achieve multiple PWM * frequency * duty cycle * adjustable output
* Better use with Bluetooth app serial Bluetooth spp pro
* Please be careful not to connect the bluetooth serial cable (shared) when you write the program.
* Otherwise it will fail to upload.
* Real-time return frequency and duty cycle information sent by Bluetooth serial port
* Use this program need to add library file PWM.zip
* Add Method:Project -> Load Library -> Add a .zip Library
* Thanks to the author of this library file
**********************************************/
#include <PWM.h>

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

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

   switch (inByte)
   {
      case '0':
      Hz = Hz - 1000;
         if(Hz < 1000)                      //最小频率1K
         {Hz = 1000;}      
      Serial.print("The Frequency is ");
      Serial.print(Hz);
      Serial.println("Hz");
      inByte = 9;                         //安全值
      break;
      case '1':
      Hz = Hz + 1000;                     //递增 1K Hz
         if(Hz > 100000)                  //最大频率100K
         {Hz = 100000;}      
      Serial.print("The Frequency is ");
      Serial.print(Hz);
      Serial.println("Hz");
      inByte = 9;
      break;
      case '2':
      DUE = DUE - 13;                     //递减 5%
         if(DUE < 0)                        //最小占空比
         {DUE = 1;}      
      Serial.print("The Duty is ");
      Serial.print(DUE/2.55,2);
      Serial.println("%");
      inByte = 9;
      break;
      case '3':
      DUE = DUE + 13;
         if(DUE > 255)                      //最大占空比
         {DUE = 254;}      
      Serial.print("The Duty is ");
      Serial.print(DUE/2.55,2);
      Serial.println("%");
      inByte = 9;
      break;   
   }
}



页: [1]
查看完整版本: Arduino 频率1-100k 占空比可调多路输出可蓝牙连接手机端监控调试