liang brain
/
Ex_Blocky_template_4rotor_qianyuyang
sdaf
Revision 5:bfd6de86ecbb, committed 2019-03-21
- Comitter:
- brainliang
- Date:
- Thu Mar 21 05:12:00 2019 +0000
- Parent:
- 4:1b985e622d26
- Commit message:
- erw
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 |
diff -r 1b985e622d26 -r bfd6de86ecbb IO_pwm/IO_pwm.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IO_pwm/IO_pwm.cpp Thu Mar 21 05:12:00 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(); +}
diff -r 1b985e622d26 -r bfd6de86ecbb IO_pwm/IO_pwm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/IO_pwm/IO_pwm.h Thu Mar 21 05:12:00 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