Eduard Sjoukes
/
PWMout_AnalogIn
How to use a potentiometer to change the duty cycle of a PWM signal
main.cpp@0:1ece86581178, 2012-11-04 (annotated)
- Committer:
- Eduard
- Date:
- Sun Nov 04 18:23:56 2012 +0000
- Revision:
- 0:1ece86581178
Make some explonation text
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eduard | 0:1ece86581178 | 1 | #include "mbed.h" |
Eduard | 0:1ece86581178 | 2 | |
Eduard | 0:1ece86581178 | 3 | PwmOut led(LED1); // Led on board |
Eduard | 0:1ece86581178 | 4 | PwmOut green(p25); // A green led is connected at Pin25 with resistor to ground |
Eduard | 0:1ece86581178 | 5 | |
Eduard | 0:1ece86581178 | 6 | AnalogIn ain(p20); // Potmeter 810k connected between VOUT(3.3V Pin40) and GND (Pin1) with the wiper connect to Pin20 |
Eduard | 0:1ece86581178 | 7 | |
Eduard | 0:1ece86581178 | 8 | int main() { |
Eduard | 0:1ece86581178 | 9 | green.period_us(100); // Set the period to 100uS = 10kHz |
Eduard | 0:1ece86581178 | 10 | led.period_ms(10); // Set the period to 10mS = 100Hz |
Eduard | 0:1ece86581178 | 11 | // Both periods will be set on 10mS, because the same timer is used for both |
Eduard | 0:1ece86581178 | 12 | // Changing the period for one wil change the period for the others also |
Eduard | 0:1ece86581178 | 13 | while(1) { |
Eduard | 0:1ece86581178 | 14 | led = ain; // Set duty cycle for LED1 |
Eduard | 0:1ece86581178 | 15 | green = ain/2; // Set duty cycle for the green led, the "on" time will only the half of the period |
Eduard | 0:1ece86581178 | 16 | } |
Eduard | 0:1ece86581178 | 17 | } |