UniPWM.cpp@0:559139f59504, 2012-06-20 (annotated)
- Committer:
- PA
- Date:
- Wed Jun 20 06:53:35 2012 +0000
- Revision:
- 0:559139f59504
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
PA | 0:559139f59504 | 1 | /* UniPWM PWM Library |
PA | 0:559139f59504 | 2 | * Copyright (c) 2012 Matt Parsons |
PA | 0:559139f59504 | 3 | * |
PA | 0:559139f59504 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
PA | 0:559139f59504 | 5 | * of this software and associated documentation files (the "Software"), to deal |
PA | 0:559139f59504 | 6 | * in the Software without restriction, including without limitation the rights |
PA | 0:559139f59504 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
PA | 0:559139f59504 | 8 | * copies of the Software, and to permit persons to whom the Software is |
PA | 0:559139f59504 | 9 | * furnished to do so, subject to the following conditions: |
PA | 0:559139f59504 | 10 | * |
PA | 0:559139f59504 | 11 | * The above copyright notice and this permission notice shall be included in |
PA | 0:559139f59504 | 12 | * all copies or substantial portions of the Software. |
PA | 0:559139f59504 | 13 | * |
PA | 0:559139f59504 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
PA | 0:559139f59504 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
PA | 0:559139f59504 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
PA | 0:559139f59504 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
PA | 0:559139f59504 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
PA | 0:559139f59504 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
PA | 0:559139f59504 | 20 | * THE SOFTWARE. |
PA | 0:559139f59504 | 21 | */ |
PA | 0:559139f59504 | 22 | |
PA | 0:559139f59504 | 23 | #include "mbed.h" |
PA | 0:559139f59504 | 24 | #include "UniPWM.h" |
PA | 0:559139f59504 | 25 | |
PA | 0:559139f59504 | 26 | UniPWM::UniPWM(PinName pin) : PWMPin(pin){ |
PA | 0:559139f59504 | 27 | Period=16667; |
PA | 0:559139f59504 | 28 | PWMMin=100; |
PA | 0:559139f59504 | 29 | PWMDuty=0; //Default to Off |
PA | 0:559139f59504 | 30 | PWMStatus=0; //All ints are all off. |
PA | 0:559139f59504 | 31 | PWMPin=0; //Outputs are off |
PA | 0:559139f59504 | 32 | PWMMax=Period; |
PA | 0:559139f59504 | 33 | |
PA | 0:559139f59504 | 34 | //Pulse.attach_us(this,&UniPWM::SigStart,PWMDuty); //no longer needed as the PWM defaults to off |
PA | 0:559139f59504 | 35 | } |
PA | 0:559139f59504 | 36 | |
PA | 0:559139f59504 | 37 | void UniPWM::SigStart(){ |
PA | 0:559139f59504 | 38 | PWMPin=1; |
PA | 0:559139f59504 | 39 | PulseEnd.attach_us(this,&UniPWM::SigStop,PWMDuty); |
PA | 0:559139f59504 | 40 | } |
PA | 0:559139f59504 | 41 | |
PA | 0:559139f59504 | 42 | void UniPWM::SigStop(){ |
PA | 0:559139f59504 | 43 | PWMPin=0; |
PA | 0:559139f59504 | 44 | } |
PA | 0:559139f59504 | 45 | |
PA | 0:559139f59504 | 46 | void UniPWM::write_us(int PosIn){ |
PA | 0:559139f59504 | 47 | PWMDuty=PosIn; |
PA | 0:559139f59504 | 48 | |
PA | 0:559139f59504 | 49 | if(PWMStatus==0){ |
PA | 0:559139f59504 | 50 | |
PA | 0:559139f59504 | 51 | if(PosIn>PWMMin){ |
PA | 0:559139f59504 | 52 | PWMStatus=1; |
PA | 0:559139f59504 | 53 | Pulse.attach_us(this,&UniPWM::SigStart,Period); |
PA | 0:559139f59504 | 54 | } |
PA | 0:559139f59504 | 55 | }else if(PosIn <= PWMMin){ |
PA | 0:559139f59504 | 56 | PWMStatus=0; |
PA | 0:559139f59504 | 57 | PosIn=0; |
PA | 0:559139f59504 | 58 | Pulse.detach(); |
PA | 0:559139f59504 | 59 | } |
PA | 0:559139f59504 | 60 | |
PA | 0:559139f59504 | 61 | |
PA | 0:559139f59504 | 62 | if(PosIn>PWMMax){PWMDuty=PWMMax;} |
PA | 0:559139f59504 | 63 | } |
PA | 0:559139f59504 | 64 | |
PA | 0:559139f59504 | 65 | void UniPWM::duty(float dutIn){ |
PA | 0:559139f59504 | 66 | dutIn=Period*dutIn; |
PA | 0:559139f59504 | 67 | int pos=(int)dutIn; |
PA | 0:559139f59504 | 68 | write_us(pos); |
PA | 0:559139f59504 | 69 | } |
PA | 0:559139f59504 | 70 | |
PA | 0:559139f59504 | 71 | int UniPWM::read_us(){ |
PA | 0:559139f59504 | 72 | return PWMDuty; |
PA | 0:559139f59504 | 73 | } |
PA | 0:559139f59504 | 74 | |
PA | 0:559139f59504 | 75 | |
PA | 0:559139f59504 | 76 |