Image Writer
/
Nucleo_ondelay_onshot
Ondelay OneShot timer example
main.cpp@2:2e40a6f0c892, 2018-11-22 (annotated)
- Committer:
- ImageWriter
- Date:
- Thu Nov 22 00:03:53 2018 +0000
- Revision:
- 2:2e40a6f0c892
- Parent:
- 0:d31d989a13fc
Edit now.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ImageWriter | 0:d31d989a13fc | 1 | // Nucleo OnDeley Oneshot |
ImageWriter | 0:d31d989a13fc | 2 | #include "mbed.h" |
ImageWriter | 0:d31d989a13fc | 3 | |
ImageWriter | 0:d31d989a13fc | 4 | // IO instance |
ImageWriter | 2:2e40a6f0c892 | 5 | #define F746ZG |
ImageWriter | 0:d31d989a13fc | 6 | #undef F746ZG |
ImageWriter | 0:d31d989a13fc | 7 | #ifdef F746ZG |
ImageWriter | 0:d31d989a13fc | 8 | DigitalOut myled(PB_0); // Green |
ImageWriter | 0:d31d989a13fc | 9 | DigitalOut myled2(PB_7); // Blue |
ImageWriter | 0:d31d989a13fc | 10 | DigitalOut myled3(PB_14); // Red |
ImageWriter | 0:d31d989a13fc | 11 | DigitalIn mySw(PC_13); // User button |
ImageWriter | 0:d31d989a13fc | 12 | #define MYSWLOGIC 1 |
ImageWriter | 0:d31d989a13fc | 13 | #endif |
ImageWriter | 0:d31d989a13fc | 14 | |
ImageWriter | 0:d31d989a13fc | 15 | #define F411RE |
ImageWriter | 0:d31d989a13fc | 16 | //#undef F411RE |
ImageWriter | 0:d31d989a13fc | 17 | #ifdef F411RE |
ImageWriter | 0:d31d989a13fc | 18 | DigitalOut myled(D13); // LED |
ImageWriter | 0:d31d989a13fc | 19 | DigitalOut myled2(D12); // |
ImageWriter | 0:d31d989a13fc | 20 | DigitalOut myled3(D11); // |
ImageWriter | 0:d31d989a13fc | 21 | DigitalIn mySw(PC_13); // User button |
ImageWriter | 0:d31d989a13fc | 22 | #define MYSWLOGIC 0 |
ImageWriter | 0:d31d989a13fc | 23 | #endif |
ImageWriter | 0:d31d989a13fc | 24 | |
ImageWriter | 2:2e40a6f0c892 | 25 | class OND{ |
ImageWriter | 2:2e40a6f0c892 | 26 | public: |
ImageWriter | 2:2e40a6f0c892 | 27 | // Timer instance |
ImageWriter | 2:2e40a6f0c892 | 28 | static Timeout delay1; |
ImageWriter | 2:2e40a6f0c892 | 29 | static Timeout oneShot1; |
ImageWriter | 2:2e40a6f0c892 | 30 | |
ImageWriter | 2:2e40a6f0c892 | 31 | // Timer flag |
ImageWriter | 2:2e40a6f0c892 | 32 | static bool swOn; // Switch status. Set true when press. Set false when Time up and release. |
ImageWriter | 2:2e40a6f0c892 | 33 | static bool tmon; // Timer status. Set to the true when the OneShot timer starts. Set to the false when time up. |
ImageWriter | 0:d31d989a13fc | 34 | |
ImageWriter | 2:2e40a6f0c892 | 35 | // Function prototype |
ImageWriter | 2:2e40a6f0c892 | 36 | static void upDelay1(void); |
ImageWriter | 2:2e40a6f0c892 | 37 | static void upOneShot1(void); |
ImageWriter | 2:2e40a6f0c892 | 38 | |
ImageWriter | 2:2e40a6f0c892 | 39 | OND(void); |
ImageWriter | 2:2e40a6f0c892 | 40 | }; |
ImageWriter | 0:d31d989a13fc | 41 | |
ImageWriter | 0:d31d989a13fc | 42 | // Functions |
ImageWriter | 2:2e40a6f0c892 | 43 | OND::OND(void){ |
ImageWriter | 2:2e40a6f0c892 | 44 | swOn = false; |
ImageWriter | 2:2e40a6f0c892 | 45 | tmon = false; |
ImageWriter | 2:2e40a6f0c892 | 46 | } |
ImageWriter | 0:d31d989a13fc | 47 | // Up time from delay timer. |
ImageWriter | 2:2e40a6f0c892 | 48 | void OND::upDelay1(void){ |
ImageWriter | 0:d31d989a13fc | 49 | myled2 = 1; // LED on |
ImageWriter | 0:d31d989a13fc | 50 | oneShot1.attach_us(&upOneShot1, 50000); // Set oneShot |
ImageWriter | 0:d31d989a13fc | 51 | tmon = true; // tmon flag set. |
ImageWriter | 0:d31d989a13fc | 52 | } |
ImageWriter | 0:d31d989a13fc | 53 | |
ImageWriter | 0:d31d989a13fc | 54 | // Up time from oneShot timeer. |
ImageWriter | 2:2e40a6f0c892 | 55 | void OND::upOneShot1(void){ |
ImageWriter | 0:d31d989a13fc | 56 | myled2 = 0; // LED off |
ImageWriter | 0:d31d989a13fc | 57 | delay1.detach(); // Timeout::detach |
ImageWriter | 0:d31d989a13fc | 58 | oneShot1.detach(); // Timeout::detach |
ImageWriter | 0:d31d989a13fc | 59 | tmon = false; // tmon flag reset. |
ImageWriter | 0:d31d989a13fc | 60 | } |
ImageWriter | 0:d31d989a13fc | 61 | |
ImageWriter | 2:2e40a6f0c892 | 62 | int main(void) { |
ImageWriter | 2:2e40a6f0c892 | 63 | OND ond; |
ImageWriter | 0:d31d989a13fc | 64 | while(1) { |
ImageWriter | 0:d31d989a13fc | 65 | myled3 = !myled3; |
ImageWriter | 0:d31d989a13fc | 66 | if (mySw == MYSWLOGIC){ // If sw on, |
ImageWriter | 0:d31d989a13fc | 67 | myled = 1; // Monitor LED ON |
ImageWriter | 2:2e40a6f0c892 | 68 | if(myled2 == 0 && ond.swOn == false){ // If timer is not started, |
ImageWriter | 2:2e40a6f0c892 | 69 | ond.swOn = true; // flag on |
ImageWriter | 2:2e40a6f0c892 | 70 | ond.delay1.attach_us(&ond.upDelay1, 50000); // Start delay timer. |
ImageWriter | 0:d31d989a13fc | 71 | } |
ImageWriter | 0:d31d989a13fc | 72 | }else{ // If sw off, |
ImageWriter | 0:d31d989a13fc | 73 | myled = 0; // LED is OFF // Monitor LED off |
ImageWriter | 2:2e40a6f0c892 | 74 | if(ond.tmon == false){ // If timer is completed, |
ImageWriter | 2:2e40a6f0c892 | 75 | ond.swOn = false; // frag reset. |
ImageWriter | 2:2e40a6f0c892 | 76 | ond.delay1.detach(); // Cansel the on delay timer. |
ImageWriter | 0:d31d989a13fc | 77 | } |
ImageWriter | 0:d31d989a13fc | 78 | } |
ImageWriter | 0:d31d989a13fc | 79 | } |
ImageWriter | 0:d31d989a13fc | 80 | } |