![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
chad
Dependencies: MCP23017 WattBob_TextLCD mbed-rtos mbed
main.cpp@17:bc25d5f47bab, 2017-03-13 (annotated)
- Committer:
- f_legge
- Date:
- Mon Mar 13 14:51:55 2017 +0000
- Revision:
- 17:bc25d5f47bab
- Parent:
- 16:bebcc7d24f3e
- Child:
- 18:bda795e28ed7
Final Commit. ; All tasks working.; Timing added.; Commented and formatted.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
f_legge | 4:70553c83c65a | 1 | // Paramater Allocation |
f_legge | 4:70553c83c65a | 2 | // Paramater: 17 |
f_legge | 4:70553c83c65a | 3 | // Log Option: 1 (uSD) |
f_legge | 4:70553c83c65a | 4 | // Watchdog Pulse Length: 6ms |
f_legge | 4:70553c83c65a | 5 | // Watchdog Repetion Rate: 0.5 |
f_legge | 4:70553c83c65a | 6 | // Error Display: 1 (LCD) |
f_legge | 16:bebcc7d24f3e | 7 | // Execution time display: Task5 (LDC display) |
f_legge | 4:70553c83c65a | 8 | |
f_legge | 13:ad04937ca366 | 9 | #include "main.h" |
f_legge | 0:10e3e2697134 | 10 | |
f_legge | 4:70553c83c65a | 11 | // Pointers to LCD Screen |
f_legge | 4:70553c83c65a | 12 | MCP23017 *par_port; // pointer to 16-bit parallel I/O chip |
f_legge | 4:70553c83c65a | 13 | WattBob_TextLCD *lcd; // pointer to 2*16 character LCD object |
f_legge | 13:ad04937ca366 | 14 | FILE *fp; // pointer to uSD object |
f_legge | 4:70553c83c65a | 15 | |
f_legge | 16:bebcc7d24f3e | 16 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //uSD pinout |
f_legge | 14:ede0e7ed2745 | 17 | |
f_legge | 4:70553c83c65a | 18 | // Digital I/O p11 to p20 |
f_legge | 16:bebcc7d24f3e | 19 | DigitalIn Fq(p14); // Digital frequency in for measurement |
f_legge | 14:ede0e7ed2745 | 20 | DigitalIn DS(p12); // Digital switch input |
f_legge | 14:ede0e7ed2745 | 21 | DigitalIn DS_s(p13); // Digital shutdown switch |
f_legge | 17:bc25d5f47bab | 22 | DigitalOut WD_pulse(p11); // Watchdog Pulse |
f_legge | 14:ede0e7ed2745 | 23 | DigitalOut led1(LED1); |
f_legge | 14:ede0e7ed2745 | 24 | DigitalOut led2(LED2); |
f_legge | 4:70553c83c65a | 25 | |
f_legge | 4:70553c83c65a | 26 | // Analogue I/O p15 to p20 |
f_legge | 16:bebcc7d24f3e | 27 | AnalogIn A1_(p15); // Analogue input to be filtered |
f_legge | 14:ede0e7ed2745 | 28 | AnalogIn A2_(p16); // Analogue input to be filtered |
f_legge | 4:70553c83c65a | 29 | |
f_legge | 4:70553c83c65a | 30 | // Timer objects |
f_legge | 17:bc25d5f47bab | 31 | Ticker ticker; // Ticker for CycExec |
f_legge | 15:85616bc0e2ae | 32 | Timer timer; // Frequency Timer |
f_legge | 15:85616bc0e2ae | 33 | Timer T5; // Timer for idle states |
f_legge | 4:70553c83c65a | 34 | |
f_legge | 4:70553c83c65a | 35 | // Constant Declaration |
f_legge | 5:e1ade53bce83 | 36 | const int WD = 6000; // Watchdog pulse length |
f_legge | 4:70553c83c65a | 37 | |
f_legge | 4:70553c83c65a | 38 | // Variable Declaration |
f_legge | 17:bc25d5f47bab | 39 | int tck = 0; // Used to define what task is called (CycExec) |
f_legge | 0:10e3e2697134 | 40 | |
f_legge | 17:bc25d5f47bab | 41 | float freq; // Frequency read variable |
f_legge | 4:70553c83c65a | 42 | |
f_legge | 17:bc25d5f47bab | 43 | int switch_state; // Output switch 1 |
f_legge | 12:d088f8024cf0 | 44 | |
f_legge | 17:bc25d5f47bab | 45 | float A1_In; // Analogue 1 Input |
f_legge | 17:bc25d5f47bab | 46 | float A2_In; // Analogue 2 Input |
f_legge | 17:bc25d5f47bab | 47 | float A1_val; // Analogue 1 return variable (Analogue In) |
f_legge | 17:bc25d5f47bab | 48 | float A2_val; // Analogue 2 return variable (Analogue In) |
f_legge | 10:2414d28be4ba | 49 | |
f_legge | 17:bc25d5f47bab | 50 | int error_code; // Error code variable |
f_legge | 4:70553c83c65a | 51 | |
f_legge | 17:bc25d5f47bab | 52 | int logcount; // Keep track of log number |
f_legge | 4:70553c83c65a | 53 | |
f_legge | 14:ede0e7ed2745 | 54 | |
f_legge | 14:ede0e7ed2745 | 55 | |
f_legge | 4:70553c83c65a | 56 | //////////////////////////////////////////////////////////////////////////////// |
f_legge | 4:70553c83c65a | 57 | // |
f_legge | 4:70553c83c65a | 58 | // Main Program |
f_legge | 4:70553c83c65a | 59 | // |
f_legge | 13:ad04937ca366 | 60 | |
f_legge | 13:ad04937ca366 | 61 | void CycExec() |
f_legge | 14:ede0e7ed2745 | 62 | { |
f_legge | 17:bc25d5f47bab | 63 | |
f_legge | 17:bc25d5f47bab | 64 | if(tck % 20 == 0) { // Every 1 sec freq |
f_legge | 17:bc25d5f47bab | 65 | Task1(); |
f_legge | 17:bc25d5f47bab | 66 | } else if(tck % 6 == 1) { // Every 1/3 sec Dig In |
f_legge | 16:bebcc7d24f3e | 67 | Task2(); |
f_legge | 17:bc25d5f47bab | 68 | } else if(tck % 40 == 2) { // Every 2 sec WD |
f_legge | 16:bebcc7d24f3e | 69 | Task3(); |
f_legge | 17:bc25d5f47bab | 70 | } else if(tck % 10 == 3) { // Every 1/2 sec Ana In |
f_legge | 15:85616bc0e2ae | 71 | Task4(); |
f_legge | 17:bc25d5f47bab | 72 | } else if(tck % 40 == 4) { // Every 2 sec LCD |
f_legge | 17:bc25d5f47bab | 73 | Task5a(); |
f_legge | 17:bc25d5f47bab | 74 | } else if(tck % 40 == 5) { // Every 1/2 sec LCD |
f_legge | 17:bc25d5f47bab | 75 | Task5b(); |
f_legge | 17:bc25d5f47bab | 76 | } else if(tck % 10 == 6) { // Every 0.5 sec Error |
f_legge | 17:bc25d5f47bab | 77 | Task6(); |
f_legge | 17:bc25d5f47bab | 78 | } else if(tck % 10 == 7) { // Every 1/2 sec Log |
f_legge | 17:bc25d5f47bab | 79 | Task7(); |
f_legge | 17:bc25d5f47bab | 80 | } else if(tck % 10 == 8) { // Every 1/2 sec Shutdown |
f_legge | 17:bc25d5f47bab | 81 | Task8(); |
f_legge | 16:bebcc7d24f3e | 82 | } |
f_legge | 16:bebcc7d24f3e | 83 | |
f_legge | 17:bc25d5f47bab | 84 | led2 = !led2; // Shows CycExec running |
f_legge | 16:bebcc7d24f3e | 85 | |
f_legge | 17:bc25d5f47bab | 86 | tck++; // Increment tck register every cycle. Starts tasks. |
f_legge | 14:ede0e7ed2745 | 87 | } |
f_legge | 14:ede0e7ed2745 | 88 | |
f_legge | 4:70553c83c65a | 89 | |
f_legge | 4:70553c83c65a | 90 | int main() |
f_legge | 16:bebcc7d24f3e | 91 | { |
f_legge | 4:70553c83c65a | 92 | // LCD Init |
f_legge | 4:70553c83c65a | 93 | par_port = new MCP23017(p9, p10, 0x40); // initialise 16-bit I/O chip |
f_legge | 14:ede0e7ed2745 | 94 | par_port->config(0x0F00, 0x0F00, 0x0F00); |
f_legge | 16:bebcc7d24f3e | 95 | |
f_legge | 4:70553c83c65a | 96 | lcd = new WattBob_TextLCD(par_port); // initialise 2*26 char display |
f_legge | 4:70553c83c65a | 97 | par_port->write_bit(1,BL_BIT); // turn LCD backlight ON |
f_legge | 16:bebcc7d24f3e | 98 | |
f_legge | 16:bebcc7d24f3e | 99 | lcd->cls(); // clear display |
f_legge | 16:bebcc7d24f3e | 100 | |
f_legge | 17:bc25d5f47bab | 101 | // Create .txt file on uSD |
f_legge | 16:bebcc7d24f3e | 102 | mkdir("/sd/logs", 0777); |
f_legge | 17:bc25d5f47bab | 103 | fp = fopen ("/sd/logs/log.txt", "w"); // Pointer to log file on uSD |
f_legge | 16:bebcc7d24f3e | 104 | fprintf(fp, "Log of Frazer Legge's Embedded Software Assignment 2\n\n"); |
f_legge | 17:bc25d5f47bab | 105 | fprintf(fp, "Log,Frequency,DigitalIn,Analogue1,Analogue2\n\n"); |
f_legge | 16:bebcc7d24f3e | 106 | |
f_legge | 17:bc25d5f47bab | 107 | ticker.attach(&CycExec, 0.05); // Period set to 50ms |
f_legge | 16:bebcc7d24f3e | 108 | |
f_legge | 17:bc25d5f47bab | 109 | while(1) { // Indicator to show CycExec was started |
f_legge | 14:ede0e7ed2745 | 110 | led1 = !led1; |
f_legge | 16:bebcc7d24f3e | 111 | wait(0.2); |
f_legge | 16:bebcc7d24f3e | 112 | } |
f_legge | 14:ede0e7ed2745 | 113 | } |
f_legge | 14:ede0e7ed2745 | 114 | |
f_legge | 14:ede0e7ed2745 | 115 | |
f_legge | 14:ede0e7ed2745 | 116 | |
f_legge | 14:ede0e7ed2745 | 117 |