![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mbed PWM output to play siren
main.cpp@0:ed48c1d4c02f, 2016-10-21 (annotated)
- Committer:
- rkuo2000
- Date:
- Fri Oct 21 07:35:16 2016 +0000
- Revision:
- 0:ed48c1d4c02f
mbed PWM siren
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rkuo2000 | 0:ed48c1d4c02f | 1 | //NuMaker-PFM-NUC472 PWM0 output Siren to a speaker |
rkuo2000 | 0:ed48c1d4c02f | 2 | #include "mbed.h" |
rkuo2000 | 0:ed48c1d4c02f | 3 | |
rkuo2000 | 0:ed48c1d4c02f | 4 | #define Ambulance_Siren_Low 650 |
rkuo2000 | 0:ed48c1d4c02f | 5 | #define Ambulance_Siren_High 900 |
rkuo2000 | 0:ed48c1d4c02f | 6 | #define Ambulance_Siren_L_period 400 // 0.4 sec |
rkuo2000 | 0:ed48c1d4c02f | 7 | #define Ambulance_Siren_H_period 600 // 0.6 sec |
rkuo2000 | 0:ed48c1d4c02f | 8 | |
rkuo2000 | 0:ed48c1d4c02f | 9 | #define PoliceCar_Siren_Low 650 |
rkuo2000 | 0:ed48c1d4c02f | 10 | #define PoliceCar_Siren_High 1450 |
rkuo2000 | 0:ed48c1d4c02f | 11 | #define PoliceCar_Siren_L_duration 230 // 0.23 sec |
rkuo2000 | 0:ed48c1d4c02f | 12 | #define PoliceCar_Siren_H_duration 100 // 0.1 sec |
rkuo2000 | 0:ed48c1d4c02f | 13 | |
rkuo2000 | 0:ed48c1d4c02f | 14 | PwmOut pwm0(PF_9); // PWM0 pin |
rkuo2000 | 0:ed48c1d4c02f | 15 | |
rkuo2000 | 0:ed48c1d4c02f | 16 | void Ambulance_Siren(uint32_t siren_no) |
rkuo2000 | 0:ed48c1d4c02f | 17 | { |
rkuo2000 | 0:ed48c1d4c02f | 18 | int i; |
rkuo2000 | 0:ed48c1d4c02f | 19 | for (i=0; i<siren_no; i++) { |
rkuo2000 | 0:ed48c1d4c02f | 20 | pwm0.period_us(1000000/Ambulance_Siren_Low); |
rkuo2000 | 0:ed48c1d4c02f | 21 | pwm0.pulsewidth_us(1000000/Ambulance_Siren_Low/2); // set duty cycle to 50% |
rkuo2000 | 0:ed48c1d4c02f | 22 | Thread::wait(Ambulance_Siren_L_period); |
rkuo2000 | 0:ed48c1d4c02f | 23 | pwm0.period_us(1000000/Ambulance_Siren_High); |
rkuo2000 | 0:ed48c1d4c02f | 24 | pwm0.pulsewidth_us(1000000/Ambulance_Siren_High/2); // set duty cycle to 50% |
rkuo2000 | 0:ed48c1d4c02f | 25 | Thread::wait(Ambulance_Siren_H_period); |
rkuo2000 | 0:ed48c1d4c02f | 26 | } |
rkuo2000 | 0:ed48c1d4c02f | 27 | pwm0.pulsewidth_us(0); |
rkuo2000 | 0:ed48c1d4c02f | 28 | } |
rkuo2000 | 0:ed48c1d4c02f | 29 | |
rkuo2000 | 0:ed48c1d4c02f | 30 | void PoliceCar_Siren(uint32_t siren_no) |
rkuo2000 | 0:ed48c1d4c02f | 31 | { |
rkuo2000 | 0:ed48c1d4c02f | 32 | int i, j; |
rkuo2000 | 0:ed48c1d4c02f | 33 | int siren_freq; |
rkuo2000 | 0:ed48c1d4c02f | 34 | |
rkuo2000 | 0:ed48c1d4c02f | 35 | for (i=0; i<siren_no; i++) { |
rkuo2000 | 0:ed48c1d4c02f | 36 | siren_freq = PoliceCar_Siren_Low; |
rkuo2000 | 0:ed48c1d4c02f | 37 | for (j=0; j<(PoliceCar_Siren_L_duration); j++) { |
rkuo2000 | 0:ed48c1d4c02f | 38 | pwm0.period_us(1000000/siren_freq); |
rkuo2000 | 0:ed48c1d4c02f | 39 | pwm0.pulsewidth_us(1000000/siren_freq/2); // set duty cycle to 50% |
rkuo2000 | 0:ed48c1d4c02f | 40 | Thread::wait(1); |
rkuo2000 | 0:ed48c1d4c02f | 41 | siren_freq = siren_freq + (PoliceCar_Siren_High - PoliceCar_Siren_Low)/230; |
rkuo2000 | 0:ed48c1d4c02f | 42 | } |
rkuo2000 | 0:ed48c1d4c02f | 43 | siren_freq = PoliceCar_Siren_High; |
rkuo2000 | 0:ed48c1d4c02f | 44 | for (j=0; j<PoliceCar_Siren_L_duration; j++) { |
rkuo2000 | 0:ed48c1d4c02f | 45 | pwm0.period_us(1000000/siren_freq); |
rkuo2000 | 0:ed48c1d4c02f | 46 | pwm0.pulsewidth_us(1000000/siren_freq/2); // set duty cycle to 50% |
rkuo2000 | 0:ed48c1d4c02f | 47 | Thread::wait(1); |
rkuo2000 | 0:ed48c1d4c02f | 48 | siren_freq = siren_freq - (PoliceCar_Siren_High - PoliceCar_Siren_Low)/100; |
rkuo2000 | 0:ed48c1d4c02f | 49 | } |
rkuo2000 | 0:ed48c1d4c02f | 50 | } |
rkuo2000 | 0:ed48c1d4c02f | 51 | pwm0.pulsewidth_us(0); |
rkuo2000 | 0:ed48c1d4c02f | 52 | } |
rkuo2000 | 0:ed48c1d4c02f | 53 | |
rkuo2000 | 0:ed48c1d4c02f | 54 | int main() |
rkuo2000 | 0:ed48c1d4c02f | 55 | { |
rkuo2000 | 0:ed48c1d4c02f | 56 | while(1) { |
rkuo2000 | 0:ed48c1d4c02f | 57 | Ambulance_Siren(5); |
rkuo2000 | 0:ed48c1d4c02f | 58 | PoliceCar_Siren(5); |
rkuo2000 | 0:ed48c1d4c02f | 59 | } |
rkuo2000 | 0:ed48c1d4c02f | 60 | } |