final project update
Dependencies: mbed
Fork of DISCO_SMT32L4_pwm_PB3 by
main.cpp@3:9aa9bfe031bb, 2018-03-19 (annotated)
- Committer:
- jblackann
- Date:
- Mon Mar 19 20:01:16 2018 +0000
- Revision:
- 3:9aa9bfe031bb
- Parent:
- 0:4860a91fb495
- Child:
- 4:9d26d117fa69
PWM on the STM32L4 Discovery board with on PB3. Counts up to 100% and then down to 0 and repeats
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:4860a91fb495 | 1 | #include "mbed.h" |
bcostm | 0:4860a91fb495 | 2 | |
bcostm | 0:4860a91fb495 | 3 | PwmOut mypwm(PWM_OUT); |
bcostm | 0:4860a91fb495 | 4 | |
bcostm | 0:4860a91fb495 | 5 | DigitalOut myled(LED1); |
bcostm | 0:4860a91fb495 | 6 | |
jblackann | 3:9aa9bfe031bb | 7 | #define UP 1 |
jblackann | 3:9aa9bfe031bb | 8 | #define DOWN 2 |
jblackann | 3:9aa9bfe031bb | 9 | |
bcostm | 0:4860a91fb495 | 10 | int main() { |
bcostm | 0:4860a91fb495 | 11 | |
jblackann | 3:9aa9bfe031bb | 12 | uint8_t i; |
jblackann | 3:9aa9bfe031bb | 13 | uint8_t count_dir; |
jblackann | 3:9aa9bfe031bb | 14 | |
jblackann | 3:9aa9bfe031bb | 15 | |
bcostm | 0:4860a91fb495 | 16 | mypwm.period_ms(10); |
bcostm | 0:4860a91fb495 | 17 | mypwm.pulsewidth_ms(1); |
bcostm | 0:4860a91fb495 | 18 | |
bcostm | 0:4860a91fb495 | 19 | printf("pwm set to %.2f %%\n", mypwm.read() * 100); |
bcostm | 0:4860a91fb495 | 20 | |
jblackann | 3:9aa9bfe031bb | 21 | count_dir = UP; |
bcostm | 0:4860a91fb495 | 22 | while(1) { |
bcostm | 0:4860a91fb495 | 23 | myled = !myled; |
jblackann | 3:9aa9bfe031bb | 24 | for(i = 0; i < 10; i++ ) |
jblackann | 3:9aa9bfe031bb | 25 | { |
jblackann | 3:9aa9bfe031bb | 26 | wait(0.1); |
jblackann | 3:9aa9bfe031bb | 27 | if(count_dir == UP) |
jblackann | 3:9aa9bfe031bb | 28 | { |
jblackann | 3:9aa9bfe031bb | 29 | mypwm = mypwm + 0.01; |
jblackann | 3:9aa9bfe031bb | 30 | if(mypwm == 1.00) // if hit one, start counting down |
jblackann | 3:9aa9bfe031bb | 31 | { |
jblackann | 3:9aa9bfe031bb | 32 | count_dir = DOWN; |
jblackann | 3:9aa9bfe031bb | 33 | } |
jblackann | 3:9aa9bfe031bb | 34 | } |
jblackann | 3:9aa9bfe031bb | 35 | else if(count_dir == DOWN) |
jblackann | 3:9aa9bfe031bb | 36 | { |
jblackann | 3:9aa9bfe031bb | 37 | mypwm = mypwm - 0.01; |
jblackann | 3:9aa9bfe031bb | 38 | if(mypwm == 0.0) // if hit zero, start counting up |
jblackann | 3:9aa9bfe031bb | 39 | { |
jblackann | 3:9aa9bfe031bb | 40 | count_dir = UP; |
jblackann | 3:9aa9bfe031bb | 41 | } |
jblackann | 3:9aa9bfe031bb | 42 | } |
jblackann | 3:9aa9bfe031bb | 43 | |
jblackann | 3:9aa9bfe031bb | 44 | } |
jblackann | 3:9aa9bfe031bb | 45 | printf("pwm set to %.2f %%\n\r", mypwm.read() * 100); |
bcostm | 0:4860a91fb495 | 46 | } |
bcostm | 0:4860a91fb495 | 47 | } |