Luis Silvio Cordova Rivadeneira
/
UPSM_PWM
Ejemplos varios de PWM
main.cpp@0:b3c72630f04c, 2020-02-07 (annotated)
- Committer:
- lscordovar
- Date:
- Fri Feb 07 20:18:51 2020 +0000
- Revision:
- 0:b3c72630f04c
Ejemplos de PWM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lscordovar | 0:b3c72630f04c | 1 | /* |
lscordovar | 0:b3c72630f04c | 2 | Exercise : Create a PWM signal which we can see on an |
lscordovar | 0:b3c72630f04c | 3 | oscilloscope. The following code will generate a 100 Hz |
lscordovar | 0:b3c72630f04c | 4 | pulse with 50% duty cycle |
lscordovar | 0:b3c72630f04c | 5 | */ |
lscordovar | 0:b3c72630f04c | 6 | |
lscordovar | 0:b3c72630f04c | 7 | |
lscordovar | 0:b3c72630f04c | 8 | #include "mbed.h" |
lscordovar | 0:b3c72630f04c | 9 | PwmOut PWM1(p21); |
lscordovar | 0:b3c72630f04c | 10 | int main() |
lscordovar | 0:b3c72630f04c | 11 | { |
lscordovar | 0:b3c72630f04c | 12 | PWM1.period(0.010); // set PWM period to 10 ms |
lscordovar | 0:b3c72630f04c | 13 | PWM1=0.5; // set duty cycle to 50% |
lscordovar | 0:b3c72630f04c | 14 | } |
lscordovar | 0:b3c72630f04c | 15 | |
lscordovar | 0:b3c72630f04c | 16 | /* |
lscordovar | 0:b3c72630f04c | 17 | Exercise : This example code uses a pulse width |
lscordovar | 0:b3c72630f04c | 18 | modulation signal to increase and decrease |
lscordovar | 0:b3c72630f04c | 19 | the brightness of the onboard LED |
lscordovar | 0:b3c72630f04c | 20 | The program requires the use of a host terminal |
lscordovar | 0:b3c72630f04c | 21 | application to communicate the |
lscordovar | 0:b3c72630f04c | 22 | brightness value to the mbed, in this example by |
lscordovar | 0:b3c72630f04c | 23 | using the ‘u’ and ‘d’ keys |
lscordovar | 0:b3c72630f04c | 24 | */ |
lscordovar | 0:b3c72630f04c | 25 | |
lscordovar | 0:b3c72630f04c | 26 | |
lscordovar | 0:b3c72630f04c | 27 | |
lscordovar | 0:b3c72630f04c | 28 | // host terminal LED dimmer control |
lscordovar | 0:b3c72630f04c | 29 | #include "mbed.h" |
lscordovar | 0:b3c72630f04c | 30 | Serial pc(USBTX, USBRX); // tx, rx |
lscordovar | 0:b3c72630f04c | 31 | PwmOut led(LED1); |
lscordovar | 0:b3c72630f04c | 32 | float brightness=0.0; |
lscordovar | 0:b3c72630f04c | 33 | int main() |
lscordovar | 0:b3c72630f04c | 34 | { |
lscordovar | 0:b3c72630f04c | 35 | pc.printf(“Control of LED dimmer by host terminal\n\r"); |
lscordovar | 0:b3c72630f04c | 36 | pc.printf("Press 'u‘ = brighter, 'd‘ = dimmer\n\r"); |
lscordovar | 0:b3c72630f04c | 37 | while(1) { |
lscordovar | 0:b3c72630f04c | 38 | char c = pc.getc(); |
lscordovar | 0:b3c72630f04c | 39 | wait(0.001); |
lscordovar | 0:b3c72630f04c | 40 | if((c == 'u') && (brightness < 0.1)) { |
lscordovar | 0:b3c72630f04c | 41 | brightness += 0.001; |
lscordovar | 0:b3c72630f04c | 42 | led = brightness; |
lscordovar | 0:b3c72630f04c | 43 | } |
lscordovar | 0:b3c72630f04c | 44 | if((c == 'd') && (brightness > 0.0)) { |
lscordovar | 0:b3c72630f04c | 45 | brightness -= 0.001; |
lscordovar | 0:b3c72630f04c | 46 | led = brightness; |
lscordovar | 0:b3c72630f04c | 47 | } |
lscordovar | 0:b3c72630f04c | 48 | pc.printf("%c %1.3f \n \r",c,brightness); |
lscordovar | 0:b3c72630f04c | 49 | } |
lscordovar | 0:b3c72630f04c | 50 | } |
lscordovar | 0:b3c72630f04c | 51 | |
lscordovar | 0:b3c72630f04c | 52 | /* |
lscordovar | 0:b3c72630f04c | 53 | |
lscordovar | 0:b3c72630f04c | 54 | Exercise 7: We are going to use the PWM to play the start of an |
lscordovar | 0:b3c72630f04c | 55 | old English folk tune ‘Oranges and Lemons’. If you’re a reader of |
lscordovar | 0:b3c72630f04c | 56 | music you will recognise this in the Figure below |
lscordovar | 0:b3c72630f04c | 57 | • You just need to know that any note which is a minim lasts twice |
lscordovar | 0:b3c72630f04c | 58 | as long as a crotchet, which in turn lasts twice as long as a quaver |
lscordovar | 0:b3c72630f04c | 59 | |
lscordovar | 0:b3c72630f04c | 60 | The following program implements the ‘Oranges and Lemons‘ musical |
lscordovar | 0:b3c72630f04c | 61 | output |
lscordovar | 0:b3c72630f04c | 62 | */ |
lscordovar | 0:b3c72630f04c | 63 | |
lscordovar | 0:b3c72630f04c | 64 | // Oranges and Lemons program |
lscordovar | 0:b3c72630f04c | 65 | #include "mbed.h" |
lscordovar | 0:b3c72630f04c | 66 | PwmOut buzzer(p21); |
lscordovar | 0:b3c72630f04c | 67 | float frequency[]= {659,554,659,554,550,494,554,587,494,659,554,440}; |
lscordovar | 0:b3c72630f04c | 68 | //frequency array |
lscordovar | 0:b3c72630f04c | 69 | float beat[]= {1,1,1,1,1,0.5,0.5,1,1,1,1,2}; |
lscordovar | 0:b3c72630f04c | 70 | //beat array |
lscordovar | 0:b3c72630f04c | 71 | int main() |
lscordovar | 0:b3c72630f04c | 72 | { |
lscordovar | 0:b3c72630f04c | 73 | while (1) { |
lscordovar | 0:b3c72630f04c | 74 | for (int i=0; i<=11; i++) { |
lscordovar | 0:b3c72630f04c | 75 | buzzer.period(1/(frequency[i])); // set PWM period |
lscordovar | 0:b3c72630f04c | 76 | buzzer=0.5; // set duty cycle |
lscordovar | 0:b3c72630f04c | 77 | wait(0.5*beat[i]); // hold for beat period |
lscordovar | 0:b3c72630f04c | 78 | } |
lscordovar | 0:b3c72630f04c | 79 | } |
lscordovar | 0:b3c72630f04c | 80 | } |
lscordovar | 0:b3c72630f04c | 81 |