![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded software Assessment 2
Dependencies: MCP23017 SDFileSystem USBDevice WattBob_TextLCD mbed
Function.h@16:0a2138a18f26, 2014-02-26 (annotated)
- Committer:
- muaiyd
- Date:
- Wed Feb 26 22:09:17 2014 +0000
- Revision:
- 16:0a2138a18f26
- Parent:
- 15:d33c51364fac
- Child:
- 17:dba09fb3f508
Finish comment and pin out to check the taskes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
muaiyd | 0:86bba6bf9b6f | 1 | #include "MCP23017.h" |
muaiyd | 0:86bba6bf9b6f | 2 | #include "WattBob_TextLCD.h" |
muaiyd | 0:86bba6bf9b6f | 3 | #include "mbed.h" |
muaiyd | 8:16a77a68e4b4 | 4 | #include "SDFileSystem.h" |
muaiyd | 0:86bba6bf9b6f | 5 | |
muaiyd | 16:0a2138a18f26 | 6 | MCP23017 Port(p9,p10,0x40) ; // 16-bit parallel I/O object |
muaiyd | 7:d9f86e1b8185 | 7 | WattBob_TextLCD LCD(&Port); // Varaible to 2*16 chacater LCD object |
muaiyd | 7:d9f86e1b8185 | 8 | |
muaiyd | 7:d9f86e1b8185 | 9 | |
muaiyd | 7:d9f86e1b8185 | 10 | BusOut BinLed(LED4, LED3, LED2, LED1); |
muaiyd | 16:0a2138a18f26 | 11 | |
muaiyd | 8:16a77a68e4b4 | 12 | DigitalIn FrequencyIn(p15); |
muaiyd | 8:16a77a68e4b4 | 13 | DigitalIn Switch1(p17); |
muaiyd | 8:16a77a68e4b4 | 14 | DigitalIn Switch2(p18); |
muaiyd | 16:0a2138a18f26 | 15 | |
muaiyd | 16:0a2138a18f26 | 16 | DigitalOut ReadDigitalinPin(p21); |
muaiyd | 16:0a2138a18f26 | 17 | DigitalOut ReadAnaloginPin(p22); |
muaiyd | 16:0a2138a18f26 | 18 | DigitalOut FreqMsurPin(p23); |
muaiyd | 16:0a2138a18f26 | 19 | DigitalOut BinaryCounterPin(p24); |
muaiyd | 16:0a2138a18f26 | 20 | DigitalOut InputCheckPin(p25); |
muaiyd | 16:0a2138a18f26 | 21 | DigitalOut DisplayPin(p26); |
muaiyd | 16:0a2138a18f26 | 22 | DigitalOut LogFilePin(p27); |
muaiyd | 16:0a2138a18f26 | 23 | DigitalOut TickerPin(p30); |
muaiyd | 16:0a2138a18f26 | 24 | |
muaiyd | 7:d9f86e1b8185 | 25 | AnalogIn Analogue_in_1(p19); |
muaiyd | 7:d9f86e1b8185 | 26 | AnalogIn Analogue_in_2(p20); |
muaiyd | 7:d9f86e1b8185 | 27 | |
muaiyd | 8:16a77a68e4b4 | 28 | SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board |
muaiyd | 0:86bba6bf9b6f | 29 | |
muaiyd | 7:d9f86e1b8185 | 30 | uint8_t Analug1_valu[5]={0,0,0,0,0}; |
muaiyd | 7:d9f86e1b8185 | 31 | uint8_t Analug2_valu[5]={0,0,0,0,0}; |
muaiyd | 7:d9f86e1b8185 | 32 | uint8_t Bin=0x00; |
muaiyd | 7:d9f86e1b8185 | 33 | bool Swch1; |
muaiyd | 7:d9f86e1b8185 | 34 | bool Swch2; |
muaiyd | 16:0a2138a18f26 | 35 | bool Ena_BinCounter=0; |
muaiyd | 14:17be0f2d153f | 36 | bool FreqTimeOutHpn; |
muaiyd | 7:d9f86e1b8185 | 37 | uint8_t Error_Code; |
muaiyd | 7:d9f86e1b8185 | 38 | uint16_t Freq; |
muaiyd | 9:539c51b7db52 | 39 | char Temp[4]; |
muaiyd | 12:582753a4f1fb | 40 | FILE *fp; |
muaiyd | 12:582753a4f1fb | 41 | |
muaiyd | 7:d9f86e1b8185 | 42 | Timer FreqMsurT; |
muaiyd | 7:d9f86e1b8185 | 43 | Timer FreqTimeOut; |
muaiyd | 9:539c51b7db52 | 44 | Timer LogTimer; |
muaiyd | 7:d9f86e1b8185 | 45 | |
muaiyd | 16:0a2138a18f26 | 46 | Ticker Cycle; |
muaiyd | 12:582753a4f1fb | 47 | |
muaiyd | 12:582753a4f1fb | 48 | void Init_LCD(); |
muaiyd | 12:582753a4f1fb | 49 | void FreqMsur(); |
muaiyd | 12:582753a4f1fb | 50 | void ReadDigitalin(); |
muaiyd | 12:582753a4f1fb | 51 | void ReadAnalogin(); |
muaiyd | 12:582753a4f1fb | 52 | void Display(); |
muaiyd | 12:582753a4f1fb | 53 | void InputCheck(); |
muaiyd | 12:582753a4f1fb | 54 | void BinaryCounter(); |
muaiyd | 12:582753a4f1fb | 55 | void InitFile(); |
muaiyd | 12:582753a4f1fb | 56 | void LogFile(); |
muaiyd | 12:582753a4f1fb | 57 | |
muaiyd | 12:582753a4f1fb | 58 | /******************************************************************************** |
muaiyd | 12:582753a4f1fb | 59 | Functions |
muaiyd | 16:0a2138a18f26 | 60 | ############################################################################### |
muaiyd | 16:0a2138a18f26 | 61 | Small function to writ on the LCD with one instruction |
muaiyd | 16:0a2138a18f26 | 62 | ********************************************************************************/ |
muaiyd | 0:86bba6bf9b6f | 63 | void Write_LCD(const char STR[],int8_t PY,int8_t PX,bool clr){ |
muaiyd | 0:86bba6bf9b6f | 64 | if(clr) |
muaiyd | 0:86bba6bf9b6f | 65 | LCD.cls(); |
muaiyd | 0:86bba6bf9b6f | 66 | LCD.locate(PY,PX); |
muaiyd | 0:86bba6bf9b6f | 67 | LCD.printf(STR); |
muaiyd | 0:86bba6bf9b6f | 68 | } |
muaiyd | 16:0a2138a18f26 | 69 | /************************************************************************** |
muaiyd | 16:0a2138a18f26 | 70 | Intialise the LCD to be ready to writ on |
muaiyd | 16:0a2138a18f26 | 71 | ***************************************************************************/ |
muaiyd | 0:86bba6bf9b6f | 72 | void Init_LCD(){ |
muaiyd | 0:86bba6bf9b6f | 73 | Port.write_bit(1,BL_BIT); |
muaiyd | 16:0a2138a18f26 | 74 | Write_LCD("Muaiyd Al-Zandi",0,4,1); |
muaiyd | 0:86bba6bf9b6f | 75 | Write_LCD("Heriot-Watt Uni.",1,0,0); |
muaiyd | 13:6094b94b3b7c | 76 | wait(1.5); |
muaiyd | 13:6094b94b3b7c | 77 | Write_LCD("F=",0,0,1); |
muaiyd | 13:6094b94b3b7c | 78 | Write_LCD("A1=",1,0,0); |
muaiyd | 13:6094b94b3b7c | 79 | Write_LCD("A2=",1,7,0); |
muaiyd | 13:6094b94b3b7c | 80 | Write_LCD("S1=",0,7,0); |
muaiyd | 13:6094b94b3b7c | 81 | Write_LCD("S2=",0,12,0); |
muaiyd | 13:6094b94b3b7c | 82 | Write_LCD("E",1,14,0); |
muaiyd | 16:0a2138a18f26 | 83 | } |
muaiyd | 16:0a2138a18f26 | 84 | /********************************************************************** |
muaiyd | 16:0a2138a18f26 | 85 | Read TWO digital inputs every 400mS (switch_1 and switch_2) |
muaiyd | 16:0a2138a18f26 | 86 | ***********************************************************************/ |
muaiyd | 16:0a2138a18f26 | 87 | void ReadDigitalin(){ |
muaiyd | 16:0a2138a18f26 | 88 | ReadDigitalinPin = !(ReadDigitalinPin); |
muaiyd | 16:0a2138a18f26 | 89 | Swch1=Switch1; |
muaiyd | 16:0a2138a18f26 | 90 | Swch2=Switch2; |
muaiyd | 4:4bb228d1f6c2 | 91 | } |
muaiyd | 16:0a2138a18f26 | 92 | /******************************************************************* |
muaiyd | 16:0a2138a18f26 | 93 | Read TWO analogue inputs every 800mS (Analogue_in_1 at pin 19 |
muaiyd | 16:0a2138a18f26 | 94 | and Analogue_in_2 at pin 20). Also, averaging the last 4 readings |
muaiyd | 16:0a2138a18f26 | 95 | ********************************************************************/ |
muaiyd | 16:0a2138a18f26 | 96 | void ReadAnalogin(){ |
muaiyd | 16:0a2138a18f26 | 97 | ReadAnaloginPin = ! (ReadAnaloginPin); |
muaiyd | 16:0a2138a18f26 | 98 | Analug1_valu[4]=0; |
muaiyd | 16:0a2138a18f26 | 99 | Analug2_valu[4]=0; |
muaiyd | 16:0a2138a18f26 | 100 | for(int i=0;i<3;i++){ |
muaiyd | 16:0a2138a18f26 | 101 | Analug1_valu[i]=Analug1_valu[i+1]; |
muaiyd | 16:0a2138a18f26 | 102 | Analug1_valu[4]=Analug1_valu[4]+Analug1_valu[i]/4; |
muaiyd | 16:0a2138a18f26 | 103 | Analug2_valu[i]=Analug2_valu[i+1]; |
muaiyd | 16:0a2138a18f26 | 104 | Analug2_valu[4]=Analug2_valu[4]+Analug2_valu[i]/4; |
muaiyd | 16:0a2138a18f26 | 105 | } |
muaiyd | 16:0a2138a18f26 | 106 | Analug1_valu[3]=Analogue_in_1.read()*255; |
muaiyd | 16:0a2138a18f26 | 107 | Analug2_valu[3]=Analogue_in_2.read()*255; |
muaiyd | 16:0a2138a18f26 | 108 | Analug1_valu[4]=Analug1_valu[4]+Analug1_valu[3]/4; |
muaiyd | 16:0a2138a18f26 | 109 | Analug2_valu[4]=Analug2_valu[4]+Analug2_valu[3]/4; |
muaiyd | 16:0a2138a18f26 | 110 | } |
muaiyd | 13:6094b94b3b7c | 111 | /*************************************************************************************** |
muaiyd | 7:d9f86e1b8185 | 112 | Measure the frequency of a 3.3v square wave signal once every second. The frequency range |
muaiyd | 16:0a2138a18f26 | 113 | is 500Hz to 1000Hz with 50% duty cycle). In this code the on complet cycle is peiod is |
muaiyd | 16:0a2138a18f26 | 114 | masured after detcting the edge also a timeout timer is seting to detect open loop. |
muaiyd | 13:6094b94b3b7c | 115 | *****************************************************************************************/ |
muaiyd | 7:d9f86e1b8185 | 116 | |
muaiyd | 7:d9f86e1b8185 | 117 | void FreqMsur(){ |
muaiyd | 16:0a2138a18f26 | 118 | FreqMsurPin = ! (FreqMsurPin); |
muaiyd | 7:d9f86e1b8185 | 119 | FreqTimeOut.start(); |
muaiyd | 16:0a2138a18f26 | 120 | // Ignore first part of the signal to detect the edge |
muaiyd | 14:17be0f2d153f | 121 | if(!FrequencyIn){ |
muaiyd | 14:17be0f2d153f | 122 | while(!FrequencyIn){ |
muaiyd | 16:0a2138a18f26 | 123 | if (FreqTimeOut.read_ms() > 4) |
muaiyd | 14:17be0f2d153f | 124 | break; |
muaiyd | 14:17be0f2d153f | 125 | } |
muaiyd | 7:d9f86e1b8185 | 126 | } |
muaiyd | 14:17be0f2d153f | 127 | else{ |
muaiyd | 14:17be0f2d153f | 128 | while(FrequencyIn){ |
muaiyd | 16:0a2138a18f26 | 129 | if (FreqTimeOut.read_ms() > 4) |
muaiyd | 14:17be0f2d153f | 130 | break; |
muaiyd | 14:17be0f2d153f | 131 | } |
muaiyd | 16:0a2138a18f26 | 132 | } |
muaiyd | 16:0a2138a18f26 | 133 | //This is the begining of the edge start the timer to masure |
muaiyd | 14:17be0f2d153f | 134 | FreqMsurT.start(); |
muaiyd | 16:0a2138a18f26 | 135 | if (FreqTimeOut.read_ms() > 4){ |
muaiyd | 14:17be0f2d153f | 136 | FreqTimeOutHpn=1; |
muaiyd | 7:d9f86e1b8185 | 137 | } |
muaiyd | 14:17be0f2d153f | 138 | else{ |
muaiyd | 14:17be0f2d153f | 139 | FreqTimeOutHpn=0; |
muaiyd | 14:17be0f2d153f | 140 | } |
muaiyd | 14:17be0f2d153f | 141 | FreqTimeOut.stop(); |
muaiyd | 14:17be0f2d153f | 142 | FreqTimeOut.reset(); |
muaiyd | 14:17be0f2d153f | 143 | if(!FreqTimeOutHpn){ |
muaiyd | 14:17be0f2d153f | 144 | if(!FrequencyIn){ |
muaiyd | 14:17be0f2d153f | 145 | while(!FrequencyIn){ |
muaiyd | 14:17be0f2d153f | 146 | } |
muaiyd | 14:17be0f2d153f | 147 | while(FrequencyIn){ |
muaiyd | 14:17be0f2d153f | 148 | } |
muaiyd | 7:d9f86e1b8185 | 149 | } |
muaiyd | 14:17be0f2d153f | 150 | else{ |
muaiyd | 14:17be0f2d153f | 151 | while(FrequencyIn){ |
muaiyd | 14:17be0f2d153f | 152 | } |
muaiyd | 14:17be0f2d153f | 153 | while(!FrequencyIn){ |
muaiyd | 14:17be0f2d153f | 154 | } |
muaiyd | 7:d9f86e1b8185 | 155 | } |
muaiyd | 7:d9f86e1b8185 | 156 | FreqMsurT.stop(); |
muaiyd | 7:d9f86e1b8185 | 157 | float Period=FreqMsurT.read_us(); |
muaiyd | 14:17be0f2d153f | 158 | Freq=1/Period*1000000.0+0.5; //Adding 0.5 to round the frequency to nearst int |
muaiyd | 7:d9f86e1b8185 | 159 | } |
muaiyd | 7:d9f86e1b8185 | 160 | else{ |
muaiyd | 7:d9f86e1b8185 | 161 | Freq=0xffff; |
muaiyd | 7:d9f86e1b8185 | 162 | } |
muaiyd | 14:17be0f2d153f | 163 | FreqTimeOut.stop(); |
muaiyd | 7:d9f86e1b8185 | 164 | FreqMsurT.reset(); |
muaiyd | 4:4bb228d1f6c2 | 165 | } |
muaiyd | 4:4bb228d1f6c2 | 166 | |
muaiyd | 13:6094b94b3b7c | 167 | /************************************************************* |
muaiyd | 7:d9f86e1b8185 | 168 | Display the following on the LCD display every 2 seconds |
muaiyd | 7:d9f86e1b8185 | 169 | a. Frequency value (show as integer) |
muaiyd | 7:d9f86e1b8185 | 170 | b. analogue values (show as integers) |
muaiyd | 7:d9f86e1b8185 | 171 | c. digital values |
muaiyd | 7:d9f86e1b8185 | 172 | d. Possible error code |
muaiyd | 13:6094b94b3b7c | 173 | **************************************************************/ |
muaiyd | 13:6094b94b3b7c | 174 | void Display(){ |
muaiyd | 16:0a2138a18f26 | 175 | DisplayPin = ! (DisplayPin); |
muaiyd | 13:6094b94b3b7c | 176 | Write_LCD(" ",0,2,0); |
muaiyd | 14:17be0f2d153f | 177 | if( Freq<1010 && Freq>490){ |
muaiyd | 7:d9f86e1b8185 | 178 | sprintf(Temp, "%d", Freq); |
muaiyd | 13:6094b94b3b7c | 179 | Write_LCD(Temp,0,2,0); |
muaiyd | 7:d9f86e1b8185 | 180 | } |
muaiyd | 7:d9f86e1b8185 | 181 | else{ |
muaiyd | 13:6094b94b3b7c | 182 | Write_LCD("Err",0,2,0); |
muaiyd | 7:d9f86e1b8185 | 183 | } |
muaiyd | 7:d9f86e1b8185 | 184 | sprintf(Temp, "%d", Analug1_valu[4]); |
muaiyd | 13:6094b94b3b7c | 185 | Write_LCD(" ",1,3,0); |
muaiyd | 13:6094b94b3b7c | 186 | Write_LCD(Temp,1,3,0); |
muaiyd | 7:d9f86e1b8185 | 187 | sprintf(Temp, "%d", Analug2_valu[4]); |
muaiyd | 13:6094b94b3b7c | 188 | Write_LCD(" ",1,10,0); |
muaiyd | 13:6094b94b3b7c | 189 | Write_LCD(Temp,1,10,0); |
muaiyd | 7:d9f86e1b8185 | 190 | sprintf(Temp, "%d", Swch1); |
muaiyd | 13:6094b94b3b7c | 191 | Write_LCD(Temp,0,10,0); |
muaiyd | 7:d9f86e1b8185 | 192 | sprintf(Temp, "%d", Swch2); |
muaiyd | 13:6094b94b3b7c | 193 | Write_LCD(Temp,0,15,0); |
muaiyd | 7:d9f86e1b8185 | 194 | sprintf(Temp, "%d", Error_Code); |
muaiyd | 13:6094b94b3b7c | 195 | Write_LCD(Temp,1,15,0); |
muaiyd | 4:4bb228d1f6c2 | 196 | } |
muaiyd | 16:0a2138a18f26 | 197 | /************************************************************************* |
muaiyd | 16:0a2138a18f26 | 198 | Cheaking the four input |
muaiyd | 16:0a2138a18f26 | 199 | ************************************************************************/ |
muaiyd | 7:d9f86e1b8185 | 200 | void InputCheck(){ |
muaiyd | 16:0a2138a18f26 | 201 | InputCheckPin = ! (InputCheckPin); |
muaiyd | 7:d9f86e1b8185 | 202 | if(Swch1&&(Analug1_valu[4]>Analug2_valu[4])){ |
muaiyd | 7:d9f86e1b8185 | 203 | Error_Code=3; |
muaiyd | 7:d9f86e1b8185 | 204 | } |
muaiyd | 7:d9f86e1b8185 | 205 | else{ |
muaiyd | 7:d9f86e1b8185 | 206 | Error_Code=0; |
muaiyd | 7:d9f86e1b8185 | 207 | } |
muaiyd | 7:d9f86e1b8185 | 208 | if(Swch2){ |
muaiyd | 16:0a2138a18f26 | 209 | Ena_BinCounter=1; |
muaiyd | 7:d9f86e1b8185 | 210 | } |
muaiyd | 7:d9f86e1b8185 | 211 | else{ |
muaiyd | 16:0a2138a18f26 | 212 | Ena_BinCounter=0; |
muaiyd | 7:d9f86e1b8185 | 213 | } |
muaiyd | 4:4bb228d1f6c2 | 214 | } |
muaiyd | 16:0a2138a18f26 | 215 | /*************************************************************** |
muaiyd | 16:0a2138a18f26 | 216 | Binery counter on the four Led |
muaiyd | 16:0a2138a18f26 | 217 | *****************************************************************/ |
muaiyd | 7:d9f86e1b8185 | 218 | void BinaryCounter(){ |
muaiyd | 16:0a2138a18f26 | 219 | BinaryCounterPin = !(BinaryCounterPin); |
muaiyd | 16:0a2138a18f26 | 220 | if(Ena_BinCounter){ |
muaiyd | 7:d9f86e1b8185 | 221 | BinLed=Bin; |
muaiyd | 7:d9f86e1b8185 | 222 | Bin++; |
muaiyd | 7:d9f86e1b8185 | 223 | } |
muaiyd | 7:d9f86e1b8185 | 224 | else{ |
muaiyd | 7:d9f86e1b8185 | 225 | BinLed=0x00; |
muaiyd | 7:d9f86e1b8185 | 226 | Bin=0x00; |
muaiyd | 7:d9f86e1b8185 | 227 | } |
muaiyd | 4:4bb228d1f6c2 | 228 | } |
muaiyd | 13:6094b94b3b7c | 229 | /*************************************************************** |
muaiyd | 16:0a2138a18f26 | 230 | Initialising the SD card file |
muaiyd | 13:6094b94b3b7c | 231 | *****************************************************************/ |
muaiyd | 12:582753a4f1fb | 232 | void InitFile(){ |
muaiyd | 13:6094b94b3b7c | 233 | fp = fopen( "/sd/LogDir/LogHistory.txt" , "a"); |
muaiyd | 8:16a77a68e4b4 | 234 | if(fp == NULL) { |
muaiyd | 16:0a2138a18f26 | 235 | Write_LCD("SD Card Error",0,0,1); |
muaiyd | 16:0a2138a18f26 | 236 | Write_LCD("Try Reset",1,0,0); |
muaiyd | 12:582753a4f1fb | 237 | } |
muaiyd | 13:6094b94b3b7c | 238 | fprintf(fp,"%s","**************************************************************\r\n"); |
muaiyd | 13:6094b94b3b7c | 239 | fprintf(fp,"%s","Time Freq Sw1 Sw2 An1 An2 ;\r\n"); |
muaiyd | 13:6094b94b3b7c | 240 | fprintf(fp,"%s","**************************************************************\r\n"); |
muaiyd | 12:582753a4f1fb | 241 | fclose(fp); |
muaiyd | 12:582753a4f1fb | 242 | } |
muaiyd | 13:6094b94b3b7c | 243 | /************************************************************** |
muaiyd | 16:0a2138a18f26 | 244 | Saving the flowing data in a file |
muaiyd | 16:0a2138a18f26 | 245 | a. Frequency value |
muaiyd | 16:0a2138a18f26 | 246 | b. digital input values |
muaiyd | 16:0a2138a18f26 | 247 | c. Filtered analogue values |
muaiyd | 13:6094b94b3b7c | 248 | ***************************************************************/ |
muaiyd | 16:0a2138a18f26 | 249 | void LogFile(){ |
muaiyd | 16:0a2138a18f26 | 250 | LogFilePin = ! (LogFilePin); |
muaiyd | 16:0a2138a18f26 | 251 | fp = fopen( "/sd/LogDir/LogHistory.txt" , "a"); |
muaiyd | 16:0a2138a18f26 | 252 | fprintf(fp,"%i ",(LogTimer.read_ms()/1000)); |
muaiyd | 16:0a2138a18f26 | 253 | if (Freq !=0xffff){ |
muaiyd | 16:0a2138a18f26 | 254 | fprintf(fp," %i ,",Freq); |
muaiyd | 11:0b2801513f93 | 255 | } |
muaiyd | 16:0a2138a18f26 | 256 | else{ |
muaiyd | 16:0a2138a18f26 | 257 | fprintf(fp,"Err "); |
muaiyd | 16:0a2138a18f26 | 258 | } |
muaiyd | 13:6094b94b3b7c | 259 | fprintf(fp," %i ,",Swch1); |
muaiyd | 13:6094b94b3b7c | 260 | fprintf(fp," %i ,",Swch2); |
muaiyd | 13:6094b94b3b7c | 261 | fprintf(fp," %d%3 ,",Analug1_valu[4]); |
muaiyd | 13:6094b94b3b7c | 262 | fprintf(fp," %d%3 ",Analug2_valu[4]); |
muaiyd | 11:0b2801513f93 | 263 | fprintf(fp," %s","\r\n"); |
muaiyd | 16:0a2138a18f26 | 264 | fclose(fp); |
muaiyd | 0:86bba6bf9b6f | 265 | } |