Creates square-wave frequency output on pin p22. Custom frequency between 1Hz and 48 MHz. (LPC 1768)
Creates square-wave frequency output on pin22 with custom frequency.
Tested for frequencies from 1Hz to 48MHz. The accuracy depends heavily on the frequency - at 12, 24, 48 MHz the frequency deviation is less than 1%, but at 20 MHz it is about 20%.
Uses Library FastPWM ( http://mbed.org/users/Sissors/code/FastPWM/ ). For LPC 1768.
How to use this Library:
#include "mbed.h" #include "PwmOscillator.h" PwmOscillator oscillator; DigitalOut led1(LED1); int main() { oscillator.initWithFrequency(8000000); // initialize oscillator instance (pin p22) with custom frequency, here: 8MHz led1=1; // optional: indicator LED shows that oscillator is running oscillator.start(); // start oscillator wait(5); // optional: oscillator is running 5 seconds oscillator.stop(); // stop oscillator led1=0; }
PwmOscillator.cpp@0:9e0f295a55a4, 2012-12-14 (annotated)
- Committer:
- geotec
- Date:
- Fri Dec 14 14:34:22 2012 +0000
- Revision:
- 0:9e0f295a55a4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
geotec | 0:9e0f295a55a4 | 1 | #include "mbed.h" |
geotec | 0:9e0f295a55a4 | 2 | #include "PwmOscillator.h" |
geotec | 0:9e0f295a55a4 | 3 | #include "FastPWM.h" |
geotec | 0:9e0f295a55a4 | 4 | |
geotec | 0:9e0f295a55a4 | 5 | FastPWM pinOut(p22); |
geotec | 0:9e0f295a55a4 | 6 | |
geotec | 0:9e0f295a55a4 | 7 | double _period_us = 0; |
geotec | 0:9e0f295a55a4 | 8 | double _pulsewidth_us = 0; |
geotec | 0:9e0f295a55a4 | 9 | |
geotec | 0:9e0f295a55a4 | 10 | |
geotec | 0:9e0f295a55a4 | 11 | void PwmOscillator::initWithFrequency(int frequencyHz) |
geotec | 0:9e0f295a55a4 | 12 | { |
geotec | 0:9e0f295a55a4 | 13 | _period_us = (1.0 / (double)frequencyHz)*1000000; |
geotec | 0:9e0f295a55a4 | 14 | _pulsewidth_us = _period_us / 2.0; |
geotec | 0:9e0f295a55a4 | 15 | pinOut.period_us(_period_us); |
geotec | 0:9e0f295a55a4 | 16 | } |
geotec | 0:9e0f295a55a4 | 17 | |
geotec | 0:9e0f295a55a4 | 18 | void PwmOscillator::start() |
geotec | 0:9e0f295a55a4 | 19 | { |
geotec | 0:9e0f295a55a4 | 20 | pinOut.pulsewidth_us(_pulsewidth_us); |
geotec | 0:9e0f295a55a4 | 21 | } |
geotec | 0:9e0f295a55a4 | 22 | |
geotec | 0:9e0f295a55a4 | 23 | void PwmOscillator::stop() |
geotec | 0:9e0f295a55a4 | 24 | { |
geotec | 0:9e0f295a55a4 | 25 | pinOut = 0; |
geotec | 0:9e0f295a55a4 | 26 | } |