
example for using fast pwm
Example for the use of fast pwm on an stm nucleo F302R8.
main.cpp@2:82d62dda5cc9, 2016-03-03 (annotated)
- Committer:
- rouaze
- Date:
- Thu Mar 03 14:01:22 2016 +0000
- Revision:
- 2:82d62dda5cc9
- Parent:
- 0:e89048c4d596
- Child:
- 3:14ba055f4bb7
working pwm max 10% and 2x analog in
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rouaze | 0:e89048c4d596 | 1 | #include "mbed.h" |
rouaze | 0:e89048c4d596 | 2 | #include "FastPWM.h" |
rouaze | 0:e89048c4d596 | 3 | //setting pwm outputs |
rouaze | 0:e89048c4d596 | 4 | FastPWM buck(D11,1); |
rouaze | 0:e89048c4d596 | 5 | FastPWM boost(D10,1); |
rouaze | 0:e89048c4d596 | 6 | //seting analog input |
rouaze | 2:82d62dda5cc9 | 7 | AnalogIn analog0_value(A0); |
rouaze | 2:82d62dda5cc9 | 8 | AnalogIn analog1_value(A1); |
rouaze | 0:e89048c4d596 | 9 | //setup serial coms |
rouaze | 0:e89048c4d596 | 10 | Serial pc(USBTX, USBRX); |
rouaze | 0:e89048c4d596 | 11 | //setup ticker |
rouaze | 0:e89048c4d596 | 12 | Ticker toggle_sample_ticker; |
rouaze | 0:e89048c4d596 | 13 | //setup gobal variables |
rouaze | 2:82d62dda5cc9 | 14 | |
rouaze | 0:e89048c4d596 | 15 | //setup interrupt sample function |
rouaze | 2:82d62dda5cc9 | 16 | |
rouaze | 0:e89048c4d596 | 17 | |
rouaze | 0:e89048c4d596 | 18 | //setup pwm function |
rouaze | 0:e89048c4d596 | 19 | void buckpwm(int pwm){ |
rouaze | 0:e89048c4d596 | 20 | if (pwm <=100) { |
rouaze | 0:e89048c4d596 | 21 | buck.pulsewidth_ticks( pwm ); |
rouaze | 0:e89048c4d596 | 22 | } |
rouaze | 0:e89048c4d596 | 23 | } |
rouaze | 0:e89048c4d596 | 24 | |
rouaze | 0:e89048c4d596 | 25 | int main() { |
rouaze | 0:e89048c4d596 | 26 | //setup pwm |
rouaze | 0:e89048c4d596 | 27 | uint32_t period_ticks=720; |
rouaze | 0:e89048c4d596 | 28 | buck.prescaler(1); |
rouaze | 0:e89048c4d596 | 29 | boost.prescaler(1); |
rouaze | 0:e89048c4d596 | 30 | // define period of pwm... |
rouaze | 0:e89048c4d596 | 31 | buck.period_ticks (period_ticks); |
rouaze | 0:e89048c4d596 | 32 | boost.period_ticks (period_ticks); |
rouaze | 0:e89048c4d596 | 33 | //define duty cycle |
rouaze | 2:82d62dda5cc9 | 34 | int duty1 = 10; |
rouaze | 2:82d62dda5cc9 | 35 | int duty2 = 3; |
rouaze | 2:82d62dda5cc9 | 36 | buckpwm(duty1); |
rouaze | 2:82d62dda5cc9 | 37 | boost.pulsewidth_us(duty2); |
rouaze | 0:e89048c4d596 | 38 | //boostpwm(10); |
rouaze | 0:e89048c4d596 | 39 | while(1) { |
rouaze | 2:82d62dda5cc9 | 40 | buckpwm(duty1); |
rouaze | 2:82d62dda5cc9 | 41 | boost.pulsewidth_us(duty2); |
rouaze | 2:82d62dda5cc9 | 42 | //wait(1); |
rouaze | 2:82d62dda5cc9 | 43 | float sample1= analog0_value.read()*3500; |
rouaze | 2:82d62dda5cc9 | 44 | float sample2= analog1_value.read()*3500; |
rouaze | 2:82d62dda5cc9 | 45 | pc.printf("%.0f and %.0f duty1 %d \n\r ", sample1,sample2,duty1); |
rouaze | 2:82d62dda5cc9 | 46 | if (duty1 >= 100 ) { // If the value is greater than 2V then switch the LED on |
rouaze | 2:82d62dda5cc9 | 47 | duty1 = 100; |
rouaze | 2:82d62dda5cc9 | 48 | } |
rouaze | 2:82d62dda5cc9 | 49 | if (duty1 <= 1 ) { // If the value is greater than 2V then switch the LED on |
rouaze | 2:82d62dda5cc9 | 50 | duty1 = 1; |
rouaze | 2:82d62dda5cc9 | 51 | } |
rouaze | 2:82d62dda5cc9 | 52 | if (sample1 > 2000 ) { // If the value is greater than 2V then switch the LED on |
rouaze | 2:82d62dda5cc9 | 53 | duty1 = duty1-1; |
rouaze | 2:82d62dda5cc9 | 54 | } |
rouaze | 2:82d62dda5cc9 | 55 | if (sample1 < 2000 ) { // If the value is greater than 2V then switch the LED on |
rouaze | 2:82d62dda5cc9 | 56 | duty1 = duty1+1; |
rouaze | 2:82d62dda5cc9 | 57 | } |
rouaze | 0:e89048c4d596 | 58 | } |
rouaze | 0:e89048c4d596 | 59 | } |