liang brain / Mbed 2 deprecated Ex_IO_pwm

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
brainliang
Date:
Sat Mar 16 01:58:24 2019 +0000
Commit message:
IO_pwm

Changed in this revision

IO_pwm/IO_pwm.cpp Show annotated file Show diff for this revision Revisions of this file
IO_pwm/IO_pwm.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IO_pwm/IO_pwm.cpp	Sat Mar 16 01:58:24 2019 +0000
@@ -0,0 +1,53 @@
+/*
+cpp文件:
+类名::构造函数:
+    _对象名(属性)        //给类中的变量赋值,也可以在花括号中用等于号赋值,主要针对是构造函数中的变量
+{
+    _对象名.函数(参数);        //在生成构造函数的时候执行这个代码块
+}
+
+void 类名::函数名(参数)
+{
+    函数体;
+}
+*/
+
+#include "IO_pwm.h"
+#include "mbed.h"
+
+IO_pwm::IO_pwm(PinName n):
+    _n(n)
+{
+//    DigtalOut mypwm(_n);
+    on_delay=0;
+    off_delay=0;  
+}
+void IO_pwm::toggleOn(void) {
+    DigitalOut mypwm(_n);
+    mypwm = 1;
+    timer.attach_us(this,&IO_pwm::toggleOff, on_delay);                         //类里启动中断,函数名需要特殊处理,按照这个方式拷贝就可以了
+}
+
+void IO_pwm::toggleOff(void) {
+    DigitalOut mypwm(_n);
+    mypwm = 0;
+    timer.attach_us(this,&IO_pwm::toggleOn, off_delay);
+}
+
+// 周期p_us = signal period in micro_seconds
+// 占空比dc   = signal duty-cycle (0.0 to 1.0)
+void IO_pwm::pwm_io(int p_us, float dc) {
+    DigitalOut mypwm(_n);
+    timer.detach();
+    if ((p_us == 0) || (dc == 0)) {
+        mypwm = 0;
+        return;
+    }
+    if (dc >= 1) {
+        mypwm = 1;
+        return;
+    }
+    on_delay = (int)(p_us * dc);
+    off_delay = p_us - on_delay;
+    this->toggleOn();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/IO_pwm/IO_pwm.h	Sat Mar 16 01:58:24 2019 +0000
@@ -0,0 +1,35 @@
+/*
+h文件
+class 类名
+{
+public:
+    类名::构造函数(变量参数);
+    公有函数声明;
+protected:
+    私有对象声明;
+    私有变量声明;
+    私有函数声明;
+};
+*/
+
+#ifndef IO_pwm_H_
+#define IO_pwm_H_
+#include "mbed.h"
+class IO_pwm
+{
+public:
+    IO_pwm(PinName n);
+    void pwm_io(int p_us, float dc);
+    
+protected:
+    //PWM的周期=on_delay+off_delay,高电平时间=on_delay
+    int on_delay;
+    int off_delay;
+    
+    PinName _n;
+   // DigitalOutr mypwm;
+    Timeout timer;
+    void toggleOn(void);
+    void toggleOff(void);
+};
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Mar 16 01:58:24 2019 +0000
@@ -0,0 +1,21 @@
+/*
+用普通IO模拟输出PWM
+*/
+
+#include "mbed.h"
+#include "IO_pwm.h"
+
+DigitalOut my_led(PC_13);
+IO_pwm my_pwm(PA_5); // IO used by pwm_io function
+int main() {
+    
+    my_pwm.pwm_io(20000, 0.075); // 20ms - 25%,舵机控制周期20ms占空比范围0.025--0.125
+    
+    while(1) {
+        my_pwm.pwm_io(20000, 0.025); // 20ms - 2.5%
+        my_led = !my_led;
+        wait(0.5);
+        my_pwm.pwm_io(20000, 0.125); // 20ms - 12.5%
+        wait(0.5);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Mar 16 01:58:24 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/5aab5a7997ee
\ No newline at end of file