Task 5.2.5 Solution
main.cpp@2:7f90edec13b7, 2019-09-20 (annotated)
- Committer:
- noutram
- Date:
- Fri Sep 20 13:46:41 2019 +0000
- Revision:
- 2:7f90edec13b7
- Parent:
- 0:3506f73fe611
2019
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:3506f73fe611 | 1 | #include "mbed.h" |
| noutram | 0:3506f73fe611 | 2 | |
| noutram | 0:3506f73fe611 | 3 | //Global PWM object |
| noutram | 2:7f90edec13b7 | 4 | #ifdef TARGET_NUCLEO_F429ZI |
| noutram | 2:7f90edec13b7 | 5 | PwmOut pwmRed(D6); //D7 is not a PWM output on the F429, so use D6 instead |
| noutram | 2:7f90edec13b7 | 6 | #else |
| noutram | 0:3506f73fe611 | 7 | PwmOut pwmRed(D7); |
| noutram | 2:7f90edec13b7 | 8 | #endif |
| noutram | 0:3506f73fe611 | 9 | |
| noutram | 0:3506f73fe611 | 10 | InterruptIn SWOn(D4); //Fade ON |
| noutram | 0:3506f73fe611 | 11 | InterruptIn SWOff(D3); //Fade OFF |
| noutram | 0:3506f73fe611 | 12 | |
| noutram | 0:3506f73fe611 | 13 | int T = 100; //100uS |
| noutram | 0:3506f73fe611 | 14 | volatile int Tmark = 0; //0us |
| noutram | 0:3506f73fe611 | 15 | volatile int delta = 1; //1us |
| noutram | 0:3506f73fe611 | 16 | |
| noutram | 0:3506f73fe611 | 17 | //Timer |
| noutram | 0:3506f73fe611 | 18 | Ticker t; |
| noutram | 0:3506f73fe611 | 19 | |
| noutram | 0:3506f73fe611 | 20 | //Function prototype |
| noutram | 0:3506f73fe611 | 21 | void doTwinkle(); |
| noutram | 0:3506f73fe611 | 22 | void doSwitchON(); |
| noutram | 0:3506f73fe611 | 23 | void doSwitchOFF(); |
| noutram | 0:3506f73fe611 | 24 | |
| noutram | 0:3506f73fe611 | 25 | //Flags |
| noutram | 0:3506f73fe611 | 26 | volatile int switchONFlag=0; |
| noutram | 0:3506f73fe611 | 27 | volatile int switchOFFFlag=0; |
| noutram | 0:3506f73fe611 | 28 | volatile int tickerFlag = 0; |
| noutram | 0:3506f73fe611 | 29 | |
| noutram | 0:3506f73fe611 | 30 | int main() { |
| noutram | 0:3506f73fe611 | 31 | //Setup switch interrupts |
| noutram | 0:3506f73fe611 | 32 | SWOn.rise(doSwitchON); |
| noutram | 0:3506f73fe611 | 33 | SWOff.rise(doSwitchOFF); |
| noutram | 0:3506f73fe611 | 34 | |
| noutram | 0:3506f73fe611 | 35 | //Initial PWM state |
| noutram | 0:3506f73fe611 | 36 | pwmRed.period_us(T); |
| noutram | 0:3506f73fe611 | 37 | pwmRed.pulsewidth_us(Tmark); |
| noutram | 0:3506f73fe611 | 38 | |
| noutram | 0:3506f73fe611 | 39 | |
| noutram | 0:3506f73fe611 | 40 | printf("Ready\n"); |
| noutram | 0:3506f73fe611 | 41 | |
| noutram | 0:3506f73fe611 | 42 | while(1) { |
| noutram | 0:3506f73fe611 | 43 | sleep(); |
| noutram | 0:3506f73fe611 | 44 | |
| noutram | 0:3506f73fe611 | 45 | //Which interrupt occured? |
| noutram | 0:3506f73fe611 | 46 | if (switchONFlag == 1) { |
| noutram | 0:3506f73fe611 | 47 | switchONFlag = 0; |
| noutram | 0:3506f73fe611 | 48 | delta = 5; |
| noutram | 0:3506f73fe611 | 49 | t.attach(doTwinkle, 0.1); |
| noutram | 0:3506f73fe611 | 50 | printf("ON button pressed\n"); |
| noutram | 0:3506f73fe611 | 51 | } |
| noutram | 0:3506f73fe611 | 52 | else if (switchOFFFlag == 1) { |
| noutram | 0:3506f73fe611 | 53 | switchOFFFlag = 0; |
| noutram | 0:3506f73fe611 | 54 | delta = -5; |
| noutram | 0:3506f73fe611 | 55 | t.attach(doTwinkle, 0.1); |
| noutram | 0:3506f73fe611 | 56 | printf("OFF button pressed\n"); |
| noutram | 0:3506f73fe611 | 57 | } |
| noutram | 0:3506f73fe611 | 58 | |
| noutram | 0:3506f73fe611 | 59 | //Has ticker hit the end stop? |
| noutram | 0:3506f73fe611 | 60 | if (tickerFlag == 1) { |
| noutram | 0:3506f73fe611 | 61 | tickerFlag = 0; |
| noutram | 0:3506f73fe611 | 62 | delta = 0; //Pedantic |
| noutram | 0:3506f73fe611 | 63 | t.detach(); //Turn off ticker |
| noutram | 0:3506f73fe611 | 64 | printf("Timer finished\n"); |
| noutram | 0:3506f73fe611 | 65 | } |
| noutram | 0:3506f73fe611 | 66 | //Update PWM |
| noutram | 0:3506f73fe611 | 67 | pwmRed.pulsewidth_us(Tmark); |
| noutram | 0:3506f73fe611 | 68 | |
| noutram | 0:3506f73fe611 | 69 | printf("Tmark = %d\n", Tmark); //Debug |
| noutram | 0:3506f73fe611 | 70 | } |
| noutram | 0:3506f73fe611 | 71 | } |
| noutram | 0:3506f73fe611 | 72 | |
| noutram | 0:3506f73fe611 | 73 | void doSwitchON() |
| noutram | 0:3506f73fe611 | 74 | { |
| noutram | 0:3506f73fe611 | 75 | switchONFlag=1; |
| noutram | 0:3506f73fe611 | 76 | } |
| noutram | 0:3506f73fe611 | 77 | |
| noutram | 0:3506f73fe611 | 78 | void doSwitchOFF() |
| noutram | 0:3506f73fe611 | 79 | { |
| noutram | 0:3506f73fe611 | 80 | switchOFFFlag=1; |
| noutram | 0:3506f73fe611 | 81 | } |
| noutram | 0:3506f73fe611 | 82 | |
| noutram | 0:3506f73fe611 | 83 | //ISR for Timer |
| noutram | 0:3506f73fe611 | 84 | void doTwinkle() |
| noutram | 0:3506f73fe611 | 85 | { |
| noutram | 0:3506f73fe611 | 86 | //Add on delta |
| noutram | 0:3506f73fe611 | 87 | Tmark += delta; |
| noutram | 0:3506f73fe611 | 88 | |
| noutram | 0:3506f73fe611 | 89 | //Cap at extremes |
| noutram | 0:3506f73fe611 | 90 | if (Tmark > 100) { |
| noutram | 0:3506f73fe611 | 91 | Tmark = 100; |
| noutram | 0:3506f73fe611 | 92 | } |
| noutram | 0:3506f73fe611 | 93 | else if (Tmark < 0) { |
| noutram | 0:3506f73fe611 | 94 | Tmark = 0; |
| noutram | 0:3506f73fe611 | 95 | } |
| noutram | 0:3506f73fe611 | 96 | |
| noutram | 0:3506f73fe611 | 97 | //Check bounds |
| noutram | 0:3506f73fe611 | 98 | if ((Tmark == 100) || (Tmark == 0)) { |
| noutram | 0:3506f73fe611 | 99 | tickerFlag = 1; |
| noutram | 0:3506f73fe611 | 100 | } |
| noutram | 0:3506f73fe611 | 101 | } |