From bcf234f5883edb850d844ffceb9905c890d9498e Mon Sep 17 00:00:00 2001 From: Vasil Zlatanov Date: Thu, 4 May 2017 15:14:36 +0100 Subject: Add pwm function --- src/main.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index eecc5da..19f280d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,14 +7,15 @@ #define SW_PERIOD 20000 // 20ms void tout(void); +void pwm_invert(void); // Onboard LED -DigitalOut alive(LED1); - +DigitalOut out_wave(LED1); PinName switch_pin[] = { SW_PINS }; Counter *switch_position[4]; -Ticker timer; +Ticker timer; +Ticker pwm; volatile uint16_t switch_count[4] = { 0, 0, 0, 0 }; volatile uint16_t switch_pressed[4] = { 0, 0, 0, 0 }; @@ -60,12 +61,15 @@ int main(void) uint16_t frequency = 1000*current_f[3] + 100*current_f[2] + 10*current_f[1] + current_f[0]; - oled.printf("\nF:%u F:%d", frequency, frequency); + oled.printf("\nF:%u ", frequency); + + if (frequency < 25) + pwm.attach_ms(&pwm_invert, 500/frequency); + else + pwm.attach_us(&pwm_invert, 500000/frequency); //Copy the display buffer to the display oled.display(); - - alive = !alive; } } @@ -90,3 +94,7 @@ void tout(void) // Update display update = 1; } + +void pwm_invert(void){ + out_wave = !out_wave; +} -- cgit v1.2.3-54-g00ecf