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.
Dependencies: C12832 mbed-rtos mbed
Fork of UTHM_TOT_MiniProject by
main.cpp@0:004b3a49f410, 2016-08-17 (annotated)
- Committer:
- mfauzi
- Date:
- Wed Aug 17 05:35:38 2016 +0000
- Revision:
- 0:004b3a49f410
- Child:
- 1:33b6bf21fbd4
This is for ToT ARM mbed in UTHM
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfauzi | 0:004b3a49f410 | 1 | #include "mbed.h" |
mfauzi | 0:004b3a49f410 | 2 | #include "rtos.h" |
mfauzi | 0:004b3a49f410 | 3 | #include "C12832.h" |
mfauzi | 0:004b3a49f410 | 4 | |
mfauzi | 0:004b3a49f410 | 5 | DigitalOut myled[]={LED1,LED2,LED3,LED4}; |
mfauzi | 0:004b3a49f410 | 6 | AnalogIn pot(p20); |
mfauzi | 0:004b3a49f410 | 7 | C12832 lcd(p5, p7, p6, p8, p11); |
mfauzi | 0:004b3a49f410 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
mfauzi | 0:004b3a49f410 | 9 | PwmOut Speaker(p26); |
mfauzi | 0:004b3a49f410 | 10 | |
mfauzi | 0:004b3a49f410 | 11 | int frequency[ ] = {440,0,440,0,440,0,349,0,523,0,440,0,349,0,523,0,440,0, |
mfauzi | 0:004b3a49f410 | 12 | 659,0,659,0,659,0,698,0,523,0,784,0,698,0,523,0,440,0}; |
mfauzi | 0:004b3a49f410 | 13 | |
mfauzi | 0:004b3a49f410 | 14 | float beat[ ] = {1,0.4,1,0.4,1,0.4,0.8,0.1,0.4,0.1,1.2,0.2,0.8,0.1,0.4,0.1,1.2,1.6, |
mfauzi | 0:004b3a49f410 | 15 | 1,0.4,1,0.4,1,0.4,0.8,0.1,0.4,0.1,1.2,0.2,0.8,0.1,0.4,0.1,1.2,0.8}; |
mfauzi | 0:004b3a49f410 | 16 | |
mfauzi | 0:004b3a49f410 | 17 | |
mfauzi | 0:004b3a49f410 | 18 | typedef struct { |
mfauzi | 0:004b3a49f410 | 19 | uint32_t raw_data; // ADC raw data |
mfauzi | 0:004b3a49f410 | 20 | float voltage; // AD result of measured voltage |
mfauzi | 0:004b3a49f410 | 21 | } message_t; |
mfauzi | 0:004b3a49f410 | 22 | |
mfauzi | 0:004b3a49f410 | 23 | MemoryPool<message_t, 16> mpool; |
mfauzi | 0:004b3a49f410 | 24 | Queue<message_t, 16> queue; |
mfauzi | 0:004b3a49f410 | 25 | |
mfauzi | 0:004b3a49f410 | 26 | void Knight_Rider(void const *args) { |
mfauzi | 0:004b3a49f410 | 27 | while(true){ |
mfauzi | 0:004b3a49f410 | 28 | for(int i=0;i<4;i++) { |
mfauzi | 0:004b3a49f410 | 29 | myled[i]=1; |
mfauzi | 0:004b3a49f410 | 30 | Thread::wait(100); |
mfauzi | 0:004b3a49f410 | 31 | myled[i]=0; |
mfauzi | 0:004b3a49f410 | 32 | } |
mfauzi | 0:004b3a49f410 | 33 | for(int i=2;i>0;i--) { |
mfauzi | 0:004b3a49f410 | 34 | myled[i]=1; |
mfauzi | 0:004b3a49f410 | 35 | Thread::wait(100); |
mfauzi | 0:004b3a49f410 | 36 | myled[i]=0; |
mfauzi | 0:004b3a49f410 | 37 | } |
mfauzi | 0:004b3a49f410 | 38 | } |
mfauzi | 0:004b3a49f410 | 39 | } |
mfauzi | 0:004b3a49f410 | 40 | |
mfauzi | 0:004b3a49f410 | 41 | void ADC_Update(void const *args) { |
mfauzi | 0:004b3a49f410 | 42 | pc.printf("\n\r|*----------------------------*|\n\r"); |
mfauzi | 0:004b3a49f410 | 43 | pc.printf("|* Example ADC with Interrupt. |\n\r"); |
mfauzi | 0:004b3a49f410 | 44 | pc.printf("|*----------------------------*|\n\r\n\r"); |
mfauzi | 0:004b3a49f410 | 45 | while(1) { |
mfauzi | 0:004b3a49f410 | 46 | //osSignalWait(0x01,osWaitForever); |
mfauzi | 0:004b3a49f410 | 47 | message_t *message = mpool.alloc(); |
mfauzi | 0:004b3a49f410 | 48 | message->raw_data = pot.read_u16(); |
mfauzi | 0:004b3a49f410 | 49 | message->voltage = pot*3.3; |
mfauzi | 0:004b3a49f410 | 50 | queue.put(message); // Place a value in the message queue |
mfauzi | 0:004b3a49f410 | 51 | pc.printf("ADC Value is: %d \n\r\n\r", message->raw_data);// Display ADC value on UART |
mfauzi | 0:004b3a49f410 | 52 | Thread::wait(1000); |
mfauzi | 0:004b3a49f410 | 53 | } |
mfauzi | 0:004b3a49f410 | 54 | } |
mfauzi | 0:004b3a49f410 | 55 | |
mfauzi | 0:004b3a49f410 | 56 | |
mfauzi | 0:004b3a49f410 | 57 | void LCD_Display(void const *args) { |
mfauzi | 0:004b3a49f410 | 58 | while (true) { |
mfauzi | 0:004b3a49f410 | 59 | osEvent evt = queue.get(); |
mfauzi | 0:004b3a49f410 | 60 | if (evt.status == osEventMessage) { |
mfauzi | 0:004b3a49f410 | 61 | message_t *message = (message_t*)evt.value.p; |
mfauzi | 0:004b3a49f410 | 62 | lcd.locate(0,11); |
mfauzi | 0:004b3a49f410 | 63 | lcd.printf("Raw Data: %u" , message->raw_data); |
mfauzi | 0:004b3a49f410 | 64 | lcd.locate(0,20); |
mfauzi | 0:004b3a49f410 | 65 | lcd.printf("Voltage: %.2f volt" , message->voltage); |
mfauzi | 0:004b3a49f410 | 66 | mpool.free(message); |
mfauzi | 0:004b3a49f410 | 67 | } |
mfauzi | 0:004b3a49f410 | 68 | Thread::wait(1000); |
mfauzi | 0:004b3a49f410 | 69 | } |
mfauzi | 0:004b3a49f410 | 70 | } |
mfauzi | 0:004b3a49f410 | 71 | |
mfauzi | 0:004b3a49f410 | 72 | void Music(void const *args) { |
mfauzi | 0:004b3a49f410 | 73 | while(1) { |
mfauzi | 0:004b3a49f410 | 74 | |
mfauzi | 0:004b3a49f410 | 75 | for (uint8_t i=0; i<=35; i++) { |
mfauzi | 0:004b3a49f410 | 76 | Speaker.period(1.0/frequency[i]); // 4 second period |
mfauzi | 0:004b3a49f410 | 77 | Speaker=0.5; |
mfauzi | 0:004b3a49f410 | 78 | Thread::wait(400*beat[i]); // hold for beat period |
mfauzi | 0:004b3a49f410 | 79 | } |
mfauzi | 0:004b3a49f410 | 80 | |
mfauzi | 0:004b3a49f410 | 81 | /* |
mfauzi | 0:004b3a49f410 | 82 | for (float i=2000.0; i<10000.0; i+=100) { |
mfauzi | 0:004b3a49f410 | 83 | Speaker.period(1.0/i); |
mfauzi | 0:004b3a49f410 | 84 | Speaker=0.5; |
mfauzi | 0:004b3a49f410 | 85 | Thread::wait(100); |
mfauzi | 0:004b3a49f410 | 86 | } |
mfauzi | 0:004b3a49f410 | 87 | */ |
mfauzi | 0:004b3a49f410 | 88 | } |
mfauzi | 0:004b3a49f410 | 89 | } |
mfauzi | 0:004b3a49f410 | 90 | |
mfauzi | 0:004b3a49f410 | 91 | int main() { |
mfauzi | 0:004b3a49f410 | 92 | lcd.cls(); |
mfauzi | 0:004b3a49f410 | 93 | lcd.locate(0,0); |
mfauzi | 0:004b3a49f410 | 94 | lcd.printf("mBed-RTOS Mini Project"); |
mfauzi | 0:004b3a49f410 | 95 | |
mfauzi | 0:004b3a49f410 | 96 | //Thread thread1(Knight_Rider, (void *)"Th 1"), osPriorityNormal, (uint32_t) DEFAULT_STACK_SIZE, (unsigned char *)NULL); |
mfauzi | 0:004b3a49f410 | 97 | Thread thread1(Knight_Rider); |
mfauzi | 0:004b3a49f410 | 98 | Thread thread2(ADC_Update,(void *)"Th 2", osPriorityAboveNormal,DEFAULT_STACK_SIZE,NULL); |
mfauzi | 0:004b3a49f410 | 99 | Thread thread3(LCD_Display); |
mfauzi | 0:004b3a49f410 | 100 | Thread thread4(Music); |
mfauzi | 0:004b3a49f410 | 101 | |
mfauzi | 0:004b3a49f410 | 102 | while (true); |
mfauzi | 0:004b3a49f410 | 103 | } |