Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 9:cec9620ad205, committed 2020-04-10
- Comitter:
- hzsun
- Date:
- Fri Apr 10 01:02:40 2020 +0000
- Parent:
- 8:1f2ac4a0744c
- Commit message:
- dada
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Apr 10 00:36:47 2020 +0000 +++ b/main.cpp Fri Apr 10 01:02:40 2020 +0000 @@ -1,101 +1,113 @@ #include "mbed.h" #include "wdt.h" - - // ------------Fault Injection Button------------- -// lab6 first part of the demostration, -//all the functions have been tested + +// -----------DESCRIBTION------------- +// lab6 second part of the demostration, +//all the functions have been tested // ----------------------------------------------- -#define ON 1 +#define ON 1 #define OFF 0 DigitalOut led_red(LED_RED, ON); DigitalIn button(PTD0, PullUp); DigitalOut led1(PTC12, OFF); -DigitalOut led2(PTC13, OFF); // all the beginning sets +AnalogIn ain(A0) ; // Analog input Serial pc(USBTX, USBRX); // tx, rx, useful for debugging - // This ticker is used to feed the watch dog Ticker tick; - + // Threads -Thread threadT ; // timer thread +Thread threadT ; // timer thread Thread threadLED1 ; // thread LED1 -Thread threadLED2 ; // thread LED2 - -# define ON1 0x01 -# define ON2 0x02 -# define OFF1 0x04 -# define OFF2 0x08 +Thread threadADC ; //thread for ADC +# define ON1 0x01 +# define OFF1 0x02 + EventFlags signals; // event flags for signalling - - -void waitButton() { - while (!button) ; +void waitButton() +{ + while (!button) ; } - + +float voltage; //value for voltage +int t; // ---Thread for controlling LED 1---------------- -// Turn LED1 on/off in response to signals +// Turn LED1 on/off in response to signals // ----------------------------------------------- -void led1_thread() { // method to run in thread +void led1_thread() // method to run in thread +{ int evt ; while (true) { evt = signals.wait_any(ON1 | OFF1); // wait for either signal if (evt & ON1) led1 = ON ; if (evt & OFF1) led1 = OFF ; - wdt_kickA(); - //waitButton() ; // POSSIBLE FAULT HERE + waitButton() ; // POSSIBLE FAULT HERE } } - + // ---Thread for controlling LED 2---------------- -// Turn LED2 on/off in response to signals +// Turn LED2 on/off in response to signals // ----------------------------------------------- -void led2_thread() { // method to run in thread +/* +void led2_thread() // method to run in thread +{ int evt ; while (true) { evt = signals.wait_any(ON2 | OFF2); // wait for any signal if (evt & ON2) led2 = ON ; if (evt & OFF2) led2 = OFF ; wdt_kickB(); - //waitButton() ; // POSSIBLE FAULT HERE - } -} - -// ---Thread for timing -------------------------- -// Send signals to the other threads -// ----------------------------------------------- -void timer_thread() { // method to run in thread - while (true) { - ThisThread::sleep_for(1000) ; - signals.set(ON1 | ON2) ; - ThisThread::sleep_for(1000) ; - signals.set(OFF1 | OFF2) ; waitButton() ; // POSSIBLE FAULT HERE } } - +*/ +void adc_thread() // method to run in thread +{ + while(true) { + voltage = ain.read()*3.3f; //read from adc + if (t == 0 && voltage > 0) { //if led should be off and the volt is out of range + led_red = 0; //turn on internal led + } + if(t == 1 && (voltage < 1 || voltage > 2)) { //if led should be on and the voltage is out of range + led_red = 0; //turn on internal led + } + pc.printf("currrnt_Voltage%3.3f%\n", voltage); // print on the screen + ThisThread::sleep_for(1000) ; //delay + } +} + +// ---Thread for timing -------------------------- +// Send signals to the other threads +// ----------------------------------------------- +void timer_thread() // method to run in thread +{ + while (true) { + ThisThread::sleep_for(1000) ; //delay + signals.set(ON1) ; + t = 1; //check the signal + ThisThread::sleep_for(1000) ; //delay + signals.set(OFF1) ; + t = 0; //check the signal + } +} + // -----------MAIN------------------------------- -// Configure watchdog. Start threads. -// Show start up with RED for 1sec -// Remember the watchdog is running -// - 1024ms to set it once -// - then must feed it every 32ms +//run the three threads // ---------------------------------------------- - -int main(void) { - wdt_1sec() ; // initialise watchdog - 1sec timeout - wdt_kick_all() ; + +int main(void) +{ + wdt_32ms() ; // initialise watchdog - 32ms timeout + wdt_kick_all() ; + tick.attach_us(callback(&wdt_kick_all), 20000); // ticks every 20ms + // start threads - threadT.start(timer_thread) ; // start the timer thread - threadLED1.start(led1_thread) ; // start the LED1 control thread - threadLED2.start(led2_thread) ; // start the LED2 control thread - // show start-up - led_red = OFF; - ThisThread::sleep_for(5000) ; - led_red = ON; - + threadT.start(timer_thread) ; // start the timer thread + threadLED1.start(led1_thread) ; // start the LED1 control thread + threadADC.start(adc_thread) ; // start the adc thread + // main thread does nothing more while (true) { - ThisThread::sleep_for(10000) ; //delay + ThisThread::sleep_for(10000) ; //delay } } \ No newline at end of file