A very simple example of an RTOS executing on an LPC1768 Platform

Dependencies:   mbed-rtos mbed

Fork of app-board-RTOS-Threads by jim hamblen

Committer:
TheFella
Date:
Mon Aug 08 02:54:39 2016 +0000
Revision:
6:7f833d7ef195
Parent:
5:d6da74ac1cc3
Child:
7:ea9f0cd97179

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 2:a69c8c5f5b03 1 // example to test the mbed Lab Board lcd lib with the mbed rtos
4180_1 4:79863d2ea5a0 2 // Pot1 changes the contrast
4180_1 4:79863d2ea5a0 3 // Pot2 changes the speed of the sin wave
dreschpe 1:1c6a9eaf55b5 4
dreschpe 0:f6a57b843f79 5 #include "mbed.h"
dreschpe 0:f6a57b843f79 6 #include "rtos.h"
dreschpe 0:f6a57b843f79 7 #include "stdio.h"
dreschpe 0:f6a57b843f79 8
TheFella 5:d6da74ac1cc3 9 #define LEDON 1
TheFella 5:d6da74ac1cc3 10 #define LEDOFF 0
4180_1 4:79863d2ea5a0 11
TheFella 5:d6da74ac1cc3 12
TheFella 5:d6da74ac1cc3 13 Serial pc(USBTX, USBRX);
dreschpe 1:1c6a9eaf55b5 14 AnalogIn Pot1(p19);
dreschpe 1:1c6a9eaf55b5 15 AnalogIn Pot2(p20);
TheFella 5:d6da74ac1cc3 16 DigitalOut led1(LED1);
TheFella 5:d6da74ac1cc3 17 DigitalOut led2(LED2);
dreschpe 2:a69c8c5f5b03 18
TheFella 5:d6da74ac1cc3 19
TheFella 5:d6da74ac1cc3 20 // mutex to make the serial thread safe
TheFella 5:d6da74ac1cc3 21 Mutex pc_mutex;
dreschpe 0:f6a57b843f79 22
dreschpe 2:a69c8c5f5b03 23 // Thread 1
dreschpe 2:a69c8c5f5b03 24 // print counter into first line and wait for 1 s
dreschpe 0:f6a57b843f79 25 void thread1(void const *args)
dreschpe 0:f6a57b843f79 26 {
dreschpe 0:f6a57b843f79 27 int i;
dreschpe 0:f6a57b843f79 28 while(true) { // thread loop
TheFella 5:d6da74ac1cc3 29 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 30 pc.printf("Thread1 count: %d\n\r",i);
TheFella 5:d6da74ac1cc3 31 pc_mutex.unlock();
dreschpe 0:f6a57b843f79 32 i++;
dreschpe 0:f6a57b843f79 33 Thread::wait(1000);
dreschpe 0:f6a57b843f79 34 }
dreschpe 0:f6a57b843f79 35 }
dreschpe 0:f6a57b843f79 36
dreschpe 2:a69c8c5f5b03 37 // Thread 2
dreschpe 2:a69c8c5f5b03 38 // print counter into third line and wait for 0,5s
dreschpe 0:f6a57b843f79 39 void thread2(void const *args)
dreschpe 0:f6a57b843f79 40 {
dreschpe 1:1c6a9eaf55b5 41 int k;
dreschpe 0:f6a57b843f79 42 while(true) { // thread loop
TheFella 5:d6da74ac1cc3 43 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 44 pc.printf("Thread 2 count : %d\n\r",k);
TheFella 5:d6da74ac1cc3 45 pc_mutex.unlock();
dreschpe 1:1c6a9eaf55b5 46 k++;
dreschpe 2:a69c8c5f5b03 47 Thread::wait(500); // wait 0.5s
dreschpe 0:f6a57b843f79 48 }
dreschpe 0:f6a57b843f79 49 }
dreschpe 0:f6a57b843f79 50
dreschpe 2:a69c8c5f5b03 51 // Thread 4
TheFella 5:d6da74ac1cc3 52 // the value of pot 1 changes the DC of LED1
dreschpe 2:a69c8c5f5b03 53 void thread4(void const *args)
dreschpe 1:1c6a9eaf55b5 54 {
TheFella 5:d6da74ac1cc3 55 while(true) { // thread loop
TheFella 5:d6da74ac1cc3 56 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 57 if( Pot1.read_u16() > 0xEFFF)
TheFella 5:d6da74ac1cc3 58 {
TheFella 5:d6da74ac1cc3 59 led1 = LEDON;
TheFella 5:d6da74ac1cc3 60 }
TheFella 5:d6da74ac1cc3 61 else
TheFella 5:d6da74ac1cc3 62 {
TheFella 5:d6da74ac1cc3 63 led1 = LEDOFF;
TheFella 5:d6da74ac1cc3 64 }
TheFella 5:d6da74ac1cc3 65
TheFella 5:d6da74ac1cc3 66 pc_mutex.unlock();
4180_1 4:79863d2ea5a0 67 }
TheFella 5:d6da74ac1cc3 68 Thread::wait(500); // value of pot1 / 100
4180_1 4:79863d2ea5a0 69 }
TheFella 5:d6da74ac1cc3 70
4180_1 4:79863d2ea5a0 71 // Thread 5
TheFella 5:d6da74ac1cc3 72 // the value of pot 2 changes the DC of LED2
4180_1 4:79863d2ea5a0 73 void thread5(void const *args)
4180_1 4:79863d2ea5a0 74 {
TheFella 5:d6da74ac1cc3 75 while(true) { // thread loop
TheFella 5:d6da74ac1cc3 76 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 77 if( Pot2.read_u16() > 0xEFFF)
TheFella 5:d6da74ac1cc3 78 {
TheFella 5:d6da74ac1cc3 79 led2 = LEDON;
TheFella 5:d6da74ac1cc3 80 }
TheFella 5:d6da74ac1cc3 81 else
TheFella 5:d6da74ac1cc3 82 {
TheFella 5:d6da74ac1cc3 83 led2 = LEDOFF;
TheFella 5:d6da74ac1cc3 84 }
TheFella 5:d6da74ac1cc3 85
TheFella 5:d6da74ac1cc3 86 pc_mutex.unlock();
4180_1 4:79863d2ea5a0 87 }
TheFella 5:d6da74ac1cc3 88 Thread::wait(500); // value of pot1 / 100
dreschpe 1:1c6a9eaf55b5 89 }
4180_1 4:79863d2ea5a0 90
dreschpe 0:f6a57b843f79 91 int main()
dreschpe 0:f6a57b843f79 92 {
dreschpe 2:a69c8c5f5b03 93 Thread t1(thread1); //start thread1
dreschpe 2:a69c8c5f5b03 94 Thread t2(thread2); //start thread2
dreschpe 2:a69c8c5f5b03 95 Thread t4(thread4); //start thread4
4180_1 4:79863d2ea5a0 96 Thread t5(thread5); //start thread5
TheFella 5:d6da74ac1cc3 97
dreschpe 2:a69c8c5f5b03 98 while(true) { // main is the next thread
TheFella 5:d6da74ac1cc3 99 pc_mutex.lock();
TheFella 5:d6da74ac1cc3 100 pc_mutex.unlock();
dreschpe 2:a69c8c5f5b03 101 Thread::wait(500); // wait 0.5s
dreschpe 0:f6a57b843f79 102 }
dreschpe 0:f6a57b843f79 103 }