Pierre rouaze
/
fastpwmexample
example for using fast pwm
Example for the use of fast pwm on an stm nucleo F302R8.
main.cpp@1:1ac1f46ae264, 2016-03-01 (annotated)
- Committer:
- rouaze
- Date:
- Tue Mar 01 10:57:29 2016 +0000
- Revision:
- 1:1ac1f46ae264
- Parent:
- 0:e89048c4d596
interup doesnt work
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 | 0:e89048c4d596 | 7 | AnalogIn analog_value(A0); |
rouaze | 0:e89048c4d596 | 8 | //setup serial coms |
rouaze | 1:1ac1f46ae264 | 9 | Serial pc(SERIAL_TX, SERIAL_RX); |
rouaze | 1:1ac1f46ae264 | 10 | DigitalOut myled(LED1); |
rouaze | 0:e89048c4d596 | 11 | //setup ticker |
rouaze | 0:e89048c4d596 | 12 | Ticker toggle_sample_ticker; |
rouaze | 0:e89048c4d596 | 13 | //setup gobal variables |
rouaze | 0:e89048c4d596 | 14 | int count=0; |
rouaze | 0:e89048c4d596 | 15 | int SampleSum=0; |
rouaze | 0:e89048c4d596 | 16 | float sample[8]; |
rouaze | 0:e89048c4d596 | 17 | //setup interrupt sample function |
rouaze | 0:e89048c4d596 | 18 | void sampling() { |
rouaze | 1:1ac1f46ae264 | 19 | if(count<=8) { |
rouaze | 0:e89048c4d596 | 20 | count=count+1; |
rouaze | 0:e89048c4d596 | 21 | sample[count]=(analog_value.read()); |
rouaze | 0:e89048c4d596 | 22 | SampleSum=(sample[0]+sample[1]+sample[2]+sample[3]+sample[4]+sample[5]+sample[6]+sample[7]); |
rouaze | 0:e89048c4d596 | 23 | } |
rouaze | 0:e89048c4d596 | 24 | else |
rouaze | 0:e89048c4d596 | 25 | count=0; |
rouaze | 0:e89048c4d596 | 26 | } |
rouaze | 0:e89048c4d596 | 27 | |
rouaze | 0:e89048c4d596 | 28 | //setup pwm function |
rouaze | 0:e89048c4d596 | 29 | void buckpwm(int pwm){ |
rouaze | 0:e89048c4d596 | 30 | if (pwm <=100) { |
rouaze | 0:e89048c4d596 | 31 | buck.pulsewidth_ticks( pwm ); |
rouaze | 0:e89048c4d596 | 32 | } |
rouaze | 0:e89048c4d596 | 33 | } |
rouaze | 0:e89048c4d596 | 34 | |
rouaze | 0:e89048c4d596 | 35 | int main() { |
rouaze | 1:1ac1f46ae264 | 36 | int i = 1; |
rouaze | 1:1ac1f46ae264 | 37 | pc.printf("Hello World !\n"); |
rouaze | 0:e89048c4d596 | 38 | //setup pwm |
rouaze | 0:e89048c4d596 | 39 | uint32_t period_ticks=720; |
rouaze | 0:e89048c4d596 | 40 | buck.prescaler(1); |
rouaze | 0:e89048c4d596 | 41 | boost.prescaler(1); |
rouaze | 0:e89048c4d596 | 42 | // define period of pwm... |
rouaze | 0:e89048c4d596 | 43 | buck.period_ticks (period_ticks); |
rouaze | 0:e89048c4d596 | 44 | boost.period_ticks (period_ticks); |
rouaze | 0:e89048c4d596 | 45 | //define duty cycle |
rouaze | 0:e89048c4d596 | 46 | buckpwm(1); |
rouaze | 0:e89048c4d596 | 47 | boost.pulsewidth_us(3); |
rouaze | 0:e89048c4d596 | 48 | //boostpwm(10); |
rouaze | 0:e89048c4d596 | 49 | while(1) { |
rouaze | 1:1ac1f46ae264 | 50 | // toggle_sample_ticker.attach(&sampling, 0.1); // will cal mycode every 1/8000 second |
rouaze | 1:1ac1f46ae264 | 51 | //test serial |
rouaze | 1:1ac1f46ae264 | 52 | wait(1); |
rouaze | 1:1ac1f46ae264 | 53 | pc.printf("This program runs since %d seconds.\n", SampleAverage); |
rouaze | 1:1ac1f46ae264 | 54 | int SampleAverage=SampleSum/9; |
rouaze | 0:e89048c4d596 | 55 | } |
rouaze | 0:e89048c4d596 | 56 | } |