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.
main.c
00001 #include "mbed.h" 00002 #include "scheduler.h" 00003 00004 #define ON 1 00005 #define OFF 0 00006 00007 DigitalOut myLED(LED1); // LED for task A and B 00008 DigitalOut myLEDTaskC(LED2); // LED for Task C 00009 DigitalOut myLEDTaskD(LED3); // LED for Task D 00010 DigitalOut myLEDTaskE(LED4); // LED for Task E 00011 00012 Timer t; 00013 Ticker keepAlive; 00014 00015 // ******************************************************************************* 00016 // * @brief "taskA" task code 00017 // * @param[in] None 00018 // * @param[out] None 00019 // * @retval bool 00020 // * @par Description 00021 // * @details This task use to print message "taskA running". Indicate "taskA" 00022 // * had been executed. 00023 // ******************************************************************************* 00024 bool taskA(void* pData) 00025 { 00026 static unsigned int cnt_numA = 0; 00027 00028 myLED = ON; 00029 cnt_numA++; 00030 00031 return true; 00032 } 00033 00034 // ******************************************************************************* 00035 // * @brief "taskB" task code 00036 // * @param[in] None 00037 // * @param[out] None 00038 // * @retval bool 00039 // * @par Description 00040 // * @details This task use to print message "taskB running". Indicate "taskB" 00041 // * had been executed. 00042 // ******************************************************************************* 00043 bool taskB(void* pData) 00044 { 00045 static unsigned int cnt_numB = 0; 00046 00047 myLED = OFF; 00048 cnt_numB++; 00049 00050 return true; 00051 } 00052 00053 // ******************************************************************************* 00054 // * @brief "taskC" task code 00055 // * @param[in] None 00056 // * @param[out] None 00057 // * @retval bool 00058 // * @par Description 00059 // * @details This task use to print message "tasC running". Indicate "taskC" 00060 // * had been executed. 00061 // ******************************************************************************* 00062 bool taskC(void* pData) 00063 { 00064 static unsigned int cnt_numC = 0; 00065 00066 myLEDTaskC = !myLEDTaskC.read(); 00067 cnt_numC++; 00068 00069 return true; 00070 } 00071 00072 // ******************************************************************************* 00073 // * @brief "taskD" task code 00074 // * @param[in] None 00075 // * @param[out] None 00076 // * @retval bool 00077 // * @par Description 00078 // * @details This task use to print message "taskB running". Indicate "taskD" 00079 // * had been executed. 00080 // ******************************************************************************* 00081 bool taskD(void* pData) 00082 { 00083 static unsigned int cnt_numD = 0; 00084 00085 myLEDTaskD = !myLEDTaskD.read(); 00086 cnt_numD++; 00087 00088 return true; 00089 } 00090 00091 // ******************************************************************************* 00092 // * @brief "taskE" task code 00093 // * @param[in] None 00094 // * @param[out] None 00095 // * @retval bool 00096 // * @par Description 00097 // * @details This task use to print message "taskE running". Indicate "taskE" 00098 // * had been executed. 00099 // ******************************************************************************* 00100 bool taskE(void* pData) 00101 { 00102 static unsigned int cnt_numE = 0; 00103 00104 myLEDTaskE = !myLEDTaskE.read(); 00105 cnt_numE++; 00106 00107 return true; 00108 } 00109 00110 // ******************************************************************************* 00111 // * @brief "taskF" task code 00112 // * @param[in] None 00113 // * @param[out] None 00114 // * @retval bool 00115 // * @par Description 00116 // * @details This task use to print message "taskF running". Indicate "taskF" 00117 // * had been executed. 00118 // ******************************************************************************* 00119 bool taskF(void* pData) 00120 { 00121 static unsigned int cnt_numF = 0; 00122 00123 cnt_numF++; // task is only running for 30 seconds -> cnt_numF has the value 5 than 00124 00125 return true; 00126 } 00127 00128 // ******************************************************************************* 00129 // * @brief "runningTasks" a Ticker routine 00130 // * @param[in] None 00131 // * @param[out] None 00132 // * @retval None 00133 // * @par Description 00134 // * @details This method is used to keep the scheduler alife 00135 // * 00136 // ******************************************************************************* 00137 void runningTasks() 00138 { 00139 // you can do this here in running Tasks or in the main routine while loop -> see below 00140 schedulerRun(t.read_ms()); 00141 } 00142 00143 int main() 00144 { 00145 schedulerInit(); // init and start scheduler 00146 00147 t.start(); // start task timer 00148 00149 int handleTaskA = schedulerAddTask(t.read_ms(), 0, 500, (taskfuncptr) taskA); // Task A is switching LED1 on 00150 int handleTaskB = schedulerAddTask(t.read_ms(), 0, 1000, (taskfuncptr) taskB); // Task B is switching LED1 off 00151 int handleTaskC = schedulerAddTask(t.read_ms(), 0, 200, (taskfuncptr) taskC); // Task C is switching LED2 on and off - every 200 ms -> five times per sec. 00152 int handleTaskD = schedulerAddTask(t.read_ms(), 0, 100, (taskfuncptr) taskD); // Task D is switching LED3 on anf off - every 100 ms -> ten times per sec. 00153 int handleTaskE = schedulerAddTask(t.read_ms(), 0, 500, (taskfuncptr) taskE); // Task E is switching LED4 on and off - vice versa to LED1 -> starting with on 00154 int handleTaskF = schedulerAddTask(t.read_ms(), 6, 5000, (taskfuncptr) taskF); // Task F finished after 30 seconds 00155 00156 00157 // the address of the function to be attached (runTasks) and the interval (,5 micro seconds) 00158 keepAlive.attach_us(&runningTasks, 500); 00159 00160 while (1) 00161 { 00162 //// we can feed the scheduler here, too 00163 ////schedulerRun(t.read_ms()); 00164 ////wait(0.001); //wait one ms 00165 // with a short delay we can power down the controller here... 00166 // or poll very fast variables that are set in interrupt service routines (ISR's) etc. 00167 } 00168 // The code don't reach here -> if so, a great mess occured 00169 } 00170
Generated on Tue Jul 12 2022 20:58:06 by
1.7.2