Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@0:fca53941e5d5, 2018-09-18 (annotated)
- Committer:
- Gijsvanoort
- Date:
- Tue Sep 18 07:52:50 2018 +0000
- Revision:
- 0:fca53941e5d5
- Child:
- 1:fd5f5be335b7
Working PWM example in which you can set the frequency and duty cycle
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Gijsvanoort | 0:fca53941e5d5 | 1 | // For this example, you need the BioRobotics shield. |
| Gijsvanoort | 0:fca53941e5d5 | 2 | // Connect POT2 (frequency)to A0 |
| Gijsvanoort | 0:fca53941e5d5 | 3 | // Connect POT1 (pwm) to A1 |
| Gijsvanoort | 0:fca53941e5d5 | 4 | #include "mbed.h" |
| Gijsvanoort | 0:fca53941e5d5 | 5 | #include <math.h> |
| Gijsvanoort | 0:fca53941e5d5 | 6 | |
| Gijsvanoort | 0:fca53941e5d5 | 7 | DigitalOut gpo(D0); |
| Gijsvanoort | 0:fca53941e5d5 | 8 | DigitalOut led(LED_RED); |
| Gijsvanoort | 0:fca53941e5d5 | 9 | AnalogIn freq(A0); |
| Gijsvanoort | 0:fca53941e5d5 | 10 | AnalogIn pwm(A1); |
| Gijsvanoort | 0:fca53941e5d5 | 11 | Ticker ReadPotmetersTicker; |
| Gijsvanoort | 0:fca53941e5d5 | 12 | volatile double frequency_Hz=1; |
| Gijsvanoort | 0:fca53941e5d5 | 13 | volatile double pwm_pct = 50; |
| Gijsvanoort | 0:fca53941e5d5 | 14 | volatile int on_time_us; // The time the LED should be on, in microseconds |
| Gijsvanoort | 0:fca53941e5d5 | 15 | volatile int off_time_us; |
| Gijsvanoort | 0:fca53941e5d5 | 16 | |
| Gijsvanoort | 0:fca53941e5d5 | 17 | void ReadPotmeterValues(void) |
| Gijsvanoort | 0:fca53941e5d5 | 18 | { |
| Gijsvanoort | 0:fca53941e5d5 | 19 | frequency_Hz = 1 + pow((double)freq.read(),4) * 50; // Frequency ranges from 1 to 50 Hz |
| Gijsvanoort | 0:fca53941e5d5 | 20 | pwm_pct = pwm.read() * 100; |
| Gijsvanoort | 0:fca53941e5d5 | 21 | on_time_us = (int) ((pwm_pct/100.0) * (1.0/frequency_Hz) * 1.0e6); |
| Gijsvanoort | 0:fca53941e5d5 | 22 | off_time_us = (int) (( (100.0-pwm_pct)/100.0) * (1.0/frequency_Hz) * 1.0e6); |
| Gijsvanoort | 0:fca53941e5d5 | 23 | } |
| Gijsvanoort | 0:fca53941e5d5 | 24 | |
| Gijsvanoort | 0:fca53941e5d5 | 25 | int main() |
| Gijsvanoort | 0:fca53941e5d5 | 26 | { |
| Gijsvanoort | 0:fca53941e5d5 | 27 | ReadPotmetersTicker.attach(ReadPotmeterValues, 0.1); |
| Gijsvanoort | 0:fca53941e5d5 | 28 | |
| Gijsvanoort | 0:fca53941e5d5 | 29 | while (true) |
| Gijsvanoort | 0:fca53941e5d5 | 30 | { |
| Gijsvanoort | 0:fca53941e5d5 | 31 | led = 0; // Turn led on |
| Gijsvanoort | 0:fca53941e5d5 | 32 | wait_us(on_time_us); |
| Gijsvanoort | 0:fca53941e5d5 | 33 | led = 1; // Turn led on |
| Gijsvanoort | 0:fca53941e5d5 | 34 | wait_us(off_time_us); |
| Gijsvanoort | 0:fca53941e5d5 | 35 | } |
| Gijsvanoort | 0:fca53941e5d5 | 36 | } |