Image Writer
/
Nucleo_ondelay_onshot
Ondelay OneShot timer example
main.cpp
- Committer:
- ImageWriter
- Date:
- 2018-11-21
- Revision:
- 0:d31d989a13fc
- Child:
- 1:2416bbc70934
- Child:
- 2:2e40a6f0c892
File content as of revision 0:d31d989a13fc:
// Nucleo OnDeley Oneshot #include "mbed.h" // IO instance //#define F746ZG #undef F746ZG #ifdef F746ZG DigitalOut myled(PB_0); // Green DigitalOut myled2(PB_7); // Blue DigitalOut myled3(PB_14); // Red DigitalIn mySw(PC_13); // User button #define MYSWLOGIC 1 #endif #define F411RE //#undef F411RE #ifdef F411RE DigitalOut myled(D13); // LED DigitalOut myled2(D12); // DigitalOut myled3(D11); // DigitalIn mySw(PC_13); // User button #define MYSWLOGIC 0 #endif // Timer instance Timeout delay1; Timeout oneShot1; // Timer flag bool swon = false; // Switch status. Set true when press. Set false when Time up and release. bool tmon = false; // Timer status. Set to the true when the OneShot timer starts. Set to the false when time up. // Function prototype void upDelay1(); void upOneShot1(); // Functions // Up time from delay timer. void upDelay1(){ myled2 = 1; // LED on oneShot1.attach_us(&upOneShot1, 50000); // Set oneShot tmon = true; // tmon flag set. } // Up time from oneShot timeer. void upOneShot1(){ myled2 = 0; // LED off delay1.detach(); // Timeout::detach oneShot1.detach(); // Timeout::detach tmon = false; // tmon flag reset. } int main() { while(1) { myled3 = !myled3; if (mySw == MYSWLOGIC){ // If sw on, myled = 1; // Monitor LED ON if(myled2 == 0 && swon == false){ // If timer is not started, swon = true; // flag on delay1.attach_us(&upDelay1, 50000); // Start delay timer. } }else{ // If sw off, myled = 0; // LED is OFF // Monitor LED off if(tmon == false){ // If timer is completed, swon = false; // frag reset. delay1.detach(); // Cansel the on delay timer. } } } }