![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Embedded software Assessment 2
Dependencies: MCP23017 SDFileSystem USBDevice WattBob_TextLCD mbed
Function.h
- Committer:
- muaiyd
- Date:
- 2014-03-04
- Revision:
- 18:f485d46a7acb
- Parent:
- 17:dba09fb3f508
- Child:
- 20:00a9a95ef083
File content as of revision 18:f485d46a7acb:
#include "MCP23017.h" #include "WattBob_TextLCD.h" #include "mbed.h" #include "SDFileSystem.h" MCP23017 Port(p9,p10,0x40) ; // 16-bit object with I2C Chip MCP23017 WattBob_TextLCD LCD(&Port); // A 2*16 chacater LCD object BusOut BinLed(LED4, LED3, LED2, LED1); DigitalIn FrequencyIn(p15); DigitalIn Switch1(p17); DigitalIn Switch2(p18); /* These pins are used to show the time between every execution. Because of the execution time is very small I used the pins to flip high and low with every time the functions are called. Consequently, the period of high or of low represent the complete time. */ DigitalOut TickerPin(p30); DigitalOut ReadDigitalinPin(p21); DigitalOut ReadAnaloginPin(p22); DigitalOut FreqMsurPin(p23); DigitalOut BinaryCounterPin(p24); DigitalOut InputCheckPin(p25); DigitalOut DisplayPin(p26); DigitalOut LogFilePin(p27); AnalogIn Analogue_in_1(p19); AnalogIn Analogue_in_2(p20); SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board uint8_t Analug1_valu[5]={0,0,0,0,0}; uint8_t Analug2_valu[5]={0,0,0,0,0}; uint8_t Bin=0x00; bool Swch1; bool Swch2; bool Ena_BinCounter=0; bool FreqTimeOutHpn; uint8_t Error_Code; uint16_t Freq; char Temp[4]; FILE *fp; Timer FreqMsurT; Timer FreqTimeOut; Timer LogTimer; Ticker Cycle; void Init_LCD(); void FreqMsur(); void ReadDigitalin(); void ReadAnalogin(); void Display(); void InputCheck(); void BinaryCounter(); void InitFile(); void LogFile(); /******************************************************************************** Functions ############################################################################### Small function to writ on the LCD with one instruction ********************************************************************************/ void Write_LCD(const char STR[],int8_t PY,int8_t PX,bool clr){ if(clr) LCD.cls(); LCD.locate(PY,PX); LCD.printf(STR); } /************************************************************************** Intialise the LCD to be ready to writ on ***************************************************************************/ void Init_LCD(){ Port.write_bit(1,BL_BIT); Write_LCD("F=",0,0,1); Write_LCD("A1=",1,0,0); Write_LCD("A2=",1,7,0); Write_LCD("S1=",0,7,0); Write_LCD("S2=",0,12,0); Write_LCD("E",1,14,0); } /********************************************************************** Read TWO digital inputs every 400mS (switch_1 and switch_2) ***********************************************************************/ void ReadDigitalin(){ ReadDigitalinPin = !(ReadDigitalinPin); Swch1=Switch1; Swch2=Switch2; } /******************************************************************* Read TWO analogue inputs every 800mS (Analogue_in_1 at pin 19 and Analogue_in_2 at pin 20). Also, averaging the last 4 readings ********************************************************************/ void ReadAnalogin(){ ReadAnaloginPin = ! (ReadAnaloginPin); Analug1_valu[4]=0; Analug2_valu[4]=0; for(int i=0;i<3;i++){ Analug1_valu[i]=Analug1_valu[i+1]; Analug1_valu[4]=Analug1_valu[4]+Analug1_valu[i]/4; Analug2_valu[i]=Analug2_valu[i+1]; Analug2_valu[4]=Analug2_valu[4]+Analug2_valu[i]/4; } Analug1_valu[3]=Analogue_in_1.read()*255; Analug2_valu[3]=Analogue_in_2.read()*255; Analug1_valu[4]=Analug1_valu[4]+Analug1_valu[3]/4; Analug2_valu[4]=Analug2_valu[4]+Analug2_valu[3]/4; } /*************************************************************************************** Measure the frequency of a 3.3v square wave signal once every second. The frequency range is 500Hz to 1000Hz with 50% duty cycle). In this code the on complet cycle is peiod is masured after detcting the edge also a timeout timer is seting to detect open loop. *****************************************************************************************/ void FreqMsur(){ FreqMsurPin = ! (FreqMsurPin); FreqTimeOut.start(); // Ignore first part of the signal to detect the edge if(!FrequencyIn){ while(!FrequencyIn){ if (FreqTimeOut.read_ms() > 4) break; } } else{ while(FrequencyIn){ if (FreqTimeOut.read_ms() > 4) break; } } //This is the begining of the edge start the timer to masure FreqMsurT.start(); if (FreqTimeOut.read_ms() > 4){ FreqTimeOutHpn=1; } else{ FreqTimeOutHpn=0; } FreqTimeOut.stop(); FreqTimeOut.reset(); if(!FreqTimeOutHpn){ if(!FrequencyIn){ while(!FrequencyIn){ } while(FrequencyIn){ } } else{ while(FrequencyIn){ } while(!FrequencyIn){ } } FreqMsurT.stop(); float Period=FreqMsurT.read_us(); Freq=1/Period*1000000.0+0.5; //Adding 0.5 to round the frequency to nearst int } else{ Freq=0xffff; } FreqTimeOut.stop(); FreqMsurT.reset(); } /************************************************************* Display the following on the LCD display every 2 seconds a. Frequency value (show as integer) b. analogue values (show as integers) c. digital values d. Possible error code **************************************************************/ void Display(){ DisplayPin = ! (DisplayPin); Write_LCD(" ",0,2,0); if( Freq<1010 && Freq>490){ sprintf(Temp, "%d", Freq); Write_LCD(Temp,0,2,0); } else{ Write_LCD("Err",0,2,0); } sprintf(Temp, "%d", Analug1_valu[4]); Write_LCD(" ",1,3,0); Write_LCD(Temp,1,3,0); sprintf(Temp, "%d", Analug2_valu[4]); Write_LCD(" ",1,10,0); Write_LCD(Temp,1,10,0); sprintf(Temp, "%d", Swch1); Write_LCD(Temp,0,10,0); sprintf(Temp, "%d", Swch2); Write_LCD(Temp,0,15,0); sprintf(Temp, "%d", Error_Code); Write_LCD(Temp,1,15,0); } /************************************************************************* Cheaking the four input ************************************************************************/ void InputCheck(){ InputCheckPin = ! (InputCheckPin); if(Swch1&&(Analug1_valu[4]>Analug2_valu[4])){ Error_Code=3; } else{ Error_Code=0; } if(Swch2){ Ena_BinCounter=1; } else{ Ena_BinCounter=0; } } /*************************************************************** Binery counter on the four Led *****************************************************************/ void BinaryCounter(){ BinaryCounterPin = !(BinaryCounterPin); if(Ena_BinCounter){ BinLed=Bin; Bin++; } else{ BinLed=0x00; Bin=0x00; } } /*************************************************************** Initialising the SD card file *****************************************************************/ void InitFile(){ fp = fopen( "/sd/LogDir/LogHistory.csv" , "a"); if(fp == NULL) { Write_LCD("SD Card Error",0,0,1); Write_LCD("Try Reset",1,0,0); } fprintf(fp,"Time;Freq;Sw1;Sw2;An1;An2\r\n"); fclose(fp); } /************************************************************** Saving the flowing data in a file a. Frequency value b. digital input values c. Filtered analogue values ***************************************************************/ void LogFile(){ LogFilePin = ! (LogFilePin); fp = fopen( "/sd/LogDir/LogHistory.csv" , "a"); fprintf(fp,"%i;",(LogTimer.read_ms()/1000)); if (Freq !=0xffff){ fprintf(fp,"%i;",Freq); } else{ fprintf(fp,"%s;","Err"); } fprintf(fp,"%i;",Swch1); fprintf(fp,"%i;",Swch2); fprintf(fp,"%d%3;",Analug1_valu[4]); fprintf(fp,"%d%3;",Analug2_valu[4]); fprintf(fp," %s","\r\n"); fclose(fp); }