
Main code of our project.
Dependencies: TextLCD mbed PID
main.cpp
- Committer:
- nicovv44
- Date:
- 2018-09-19
- Revision:
- 0:1f66eaf1013d
- Child:
- 1:f31a46c62d10
File content as of revision 0:1f66eaf1013d:
// Hello World! for the TextLCD #include "mbed.h" #include "TextLCD.h" TextLCD lcd(D2, D3, D4, D5, D6, D7); // rs, e, d4-d7 Serial pc(USBTX, USBRX); // tx, rx PwmOut pwm1(D9); AnalogIn analog_pin1(A0); const float sqrt2 = 1.414213562; Timeout timeout; volatile bool looping = false; // ############################################## // ########## PROTOTYPES ######################## // ############################################################################# void stopLooping(void); float getVolageRMS(AnalogIn ana_pin); float getVolageReadedMax(AnalogIn ana_pin); float getFrequency(AnalogIn ana_pin); // ############################################## // ########## MAIN ############################## // ############################################################################# int main() { float Vm; float freq1; /*// specify period first led.period(4.0f); // 4 second period led.write(0.50f); // 50% duty cycle, relative to period //led = 0.5f; // shorthand for led.write() //led.pulsewidth(2); // alternative to led.write, set duty cycle time in seconds while(1);*/ Vm = getVolageRMS(analog_pin1); lcd.locate(0,0); lcd.printf("Vm=%f", Vm); freq1 = getFrequency(analog_pin1); lcd.locate(0,1); lcd.printf("freq1=%f", freq1); } // ############################################## // ########## FUNCTIONS ######################### // ############################################################################# // ISR to stop loping void stopLooping(void) { looping = false;//looping is volatile bool } // ############################################################################# float getVolageRMS(AnalogIn ana_pin){ float v1;//readed voltage float v1Max = 0;//max readed voltage float VRMS; //RMS voltage looping = true; timeout.attach(callback(&stopLooping), 0.002);//T=20ms because f=50Hz while(looping){ v1 = ana_pin.read()*3.3; if(v1 > v1Max){ v1Max = v1; } } VRMS = (v1Max+0.685)*20.8/sqrt2; return VRMS; } // ############################################################################# float getVolageReadedMax(AnalogIn ana_pin){ float v1;//readed voltage float v1Max = 0;//max readed voltage looping = true; timeout.attach(callback(&stopLooping), 0.002);//T=20ms because f=50Hz while(looping){ v1 = ana_pin.read()*3.3; if(v1 > v1Max){ v1Max = v1; } } return v1Max; } // ############################################################################# // getFrequency ####### float getFrequency(AnalogIn ana_pin){ float freq; //frequency float maxReadedVoltage;//maximum voltage readed by the ADC //float vOld;//old readed voltage //float vNew;//new readed voltage float readedVoltage;//readed voltage int nbrRisingEdge=0;// number of rising edge detected float T;//Periode Timer timer; maxReadedVoltage = getVolageReadedMax(ana_pin); /*vOld = ana_pin.read()*3.3; vNew = ana_pin.read()*3.3; while(nbrRisingEdge<2){ if(vOld<(maxReadedVoltage/2) && (maxReadedVoltage/2)<vNew){//rising detected if(nbrRisingEdge==0) timer.start(); if(nbrRisingEdge==1) timer.stop(); nbrRisingEdge++; } vOld = vNew; vNew = ana_pin.read()*3.3; }*/ bool aboveLine = true; while(nbrRisingEdge<2){ readedVoltage = ana_pin.read()*3.3; if(readedVoltage<(maxReadedVoltage/2)){//rising edge detection ready aboveLine = false; } if((maxReadedVoltage/2)<readedVoltage && aboveLine==false){//rising edge detected aboveLine = true; if(nbrRisingEdge==0) timer.start(); if(nbrRisingEdge==1) timer.stop(); nbrRisingEdge++; } } T = timer.read(); freq = 1/T; return freq; }