liang brain / Mbed 2 deprecated Ex_IO_pwm

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers IO_pwm.cpp Source File

IO_pwm.cpp

00001 /*
00002 cpp文件:
00003 类名::构造函数:
00004     _对象名(属性)        //给类中的变量赋值,也可以在花括号中用等于号赋值,主要针对是构造函数中的变量
00005 {
00006     _对象名.函数(参数);        //在生成构造函数的时候执行这个代码块
00007 }
00008 
00009 void 类名::函数名(参数)
00010 {
00011     函数体;
00012 }
00013 */
00014 
00015 #include "IO_pwm.h"
00016 #include "mbed.h"
00017 
00018 IO_pwm::IO_pwm(PinName n):
00019     _n(n)
00020 {
00021 //    DigtalOut mypwm(_n);
00022     on_delay=0;
00023     off_delay=0;  
00024 }
00025 void IO_pwm::toggleOn(void) {
00026     DigitalOut mypwm(_n);
00027     mypwm = 1;
00028     timer.attach_us(this,&IO_pwm::toggleOff, on_delay);                         //类里启动中断,函数名需要特殊处理,按照这个方式拷贝就可以了
00029 }
00030 
00031 void IO_pwm::toggleOff(void) {
00032     DigitalOut mypwm(_n);
00033     mypwm = 0;
00034     timer.attach_us(this,&IO_pwm::toggleOn, off_delay);
00035 }
00036 
00037 // 周期p_us = signal period in micro_seconds
00038 // 占空比dc   = signal duty-cycle (0.0 to 1.0)
00039 void IO_pwm::pwm_io(int p_us, float dc) {
00040     DigitalOut mypwm(_n);
00041     timer.detach();
00042     if ((p_us == 0) || (dc == 0)) {
00043         mypwm = 0;
00044         return;
00045     }
00046     if (dc >= 1) {
00047         mypwm = 1;
00048         return;
00049     }
00050     on_delay = (int)(p_us * dc);
00051     off_delay = p_us - on_delay;
00052     this->toggleOn();
00053 }