Last active
November 4, 2017 01:21
-
-
Save guilhermebene/7d11b502689613fd55297767a0914aa3 to your computer and use it in GitHub Desktop.
This is the code developed (in a hurry) for an introductory electric circuits laboratory class. The goal was to use a FRDM-KL25Z board and its microcontroller, connected to a LCD screen module, creating a multimeter that should measure frequency and voltage for AC inputs and only voltage for DC inputs. It uses the [mbed library] (https://os.mbed…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mbed.h" | |
#include "Timer.h" | |
#include "math.h" | |
#include "TextLCD.h" | |
#define CONVERSAO(x) (3.3*x) | |
TextLCD lcd(PTA13, PTD5, PTC9, PTC8, PTA5, PTA4); | |
AnalogIn medida(PTB0); | |
AnalogIn switchMode(PTB1); | |
Timer timer; | |
Serial PC (USBTX, USBRX); | |
int main () | |
{ | |
float max = 0.0, min = 0.0, max_parcial, min_parcial; //Variavel da medida AC | |
int i = 0, j = 0, l = 0, k = 0; // | |
float amostraInicial, referencia; // | |
int periodoParcial; // | |
float frequencia = 0.0; // | |
int t2 = 0, t1 = 0; // | |
float v2, vRMS = 0.0; //Variavel da medida AC | |
float periodo; | |
float media = 0; //Variavel da medida DC | |
float tensaoMedida = 0; //Variavel da medida DC | |
while(1) { | |
while(3.3*switchMode.read() < 1.0) { | |
frequencia = periodo = vRMS = v2 = amostraInicial = referencia = max = min = max_parcial = min_parcial = 0.0; | |
periodoParcial = i = j = 0; | |
t2 = t1 = 0; | |
while(i < 20 && switchMode.read()*3.3 < 1.0) { | |
max_parcial = min_parcial = medida.read(); | |
timer.start(); | |
while(timer.read() < 0.03) { | |
if(medida.read() > max_parcial) { | |
max_parcial = medida.read(); | |
} | |
if(medida.read() < min_parcial) { | |
min_parcial = medida.read(); | |
} | |
} | |
timer.stop(); | |
timer.reset(); | |
max += max_parcial; | |
min += min_parcial; | |
i++; | |
} | |
max = max/20.0; | |
min = min/20.0; | |
max = max*3.3/0.3;//valor do ganho correto | |
min = min*3.3/0.3; | |
referencia = (max+min)/(2.0); | |
referencia = referencia - 5.53; | |
max = max - 5.53;//valor do offset | |
min = min - 5.53; | |
amostraInicial = medida.read(); | |
while(j < 10 && switchMode.read()*3.3 < 1.0) { //Medida da frequencia | |
j++; | |
timer.stop(); | |
timer.reset(); | |
if((amostraInicial*3.3/0.3) - 5.53 > referencia) { | |
while((medida.read()*3.3/0.3) - 5.53 > referencia) {} | |
timer.start(); | |
while((medida.read()*3.3/0.3) - 5.53 < referencia) {} | |
periodoParcial = timer.read_us(); | |
} | |
else { | |
while((medida.read()*3.3/0.3) - 5.53 < referencia) {} | |
timer.start(); | |
while((medida.read()*3.3/0.3) - 5.53 > referencia) {} | |
periodoParcial = timer.read_us(); | |
} | |
periodo += periodoParcial; | |
} | |
if(switchMode.read()*3.3 > 1.0) break; | |
periodo = 2*periodo/10; | |
frequencia = (1.0/periodo)*1000000.0; | |
/**************************************************************************************/ | |
// Analise da frequencia | |
k = j = 0; | |
if(frequencia <= 1050) | |
k = 1000; | |
if(frequencia <= 750) | |
k = 600; | |
if(frequencia <= 500) | |
k = 200; | |
if(frequencia <= 250) | |
k = 5; | |
while(j < k && switchMode.read()*3.3 < 1.0) { //Medida da frequencia | |
j++; | |
timer.stop(); | |
timer.reset(); | |
amostraInicial = medida.read(); | |
if((amostraInicial*3.3/0.3) - 5.53 > referencia) { | |
while((medida.read()*3.3/0.3) - 5.53 > referencia) {} | |
timer.start(); | |
while((medida.read()*3.3/0.3) - 5.53 < referencia) {} | |
periodoParcial = timer.read_us(); | |
} | |
else { | |
while((medida.read()*3.3/0.3) - 5.53 < referencia) {} | |
timer.start(); | |
while((medida.read()*3.3/0.3) - 5.53 > referencia) {} | |
periodoParcial = timer.read_us(); | |
} | |
periodo += periodoParcial; | |
} | |
if(switchMode.read()*3.3 > 1.0) break; | |
periodo = 2*periodo/(k+1); | |
frequencia = (1.0/periodo)*1000000.0; | |
/*****************************************************/ | |
timer.stop(); | |
timer.reset(); | |
timer.start(); | |
while((float)timer.read_us() < 50.0 * periodo) { //Integraçao para obter a tensao RMS | |
v2 = ((medida.read()*3.3) - 1.65)/0.3; | |
t2 = timer.read_us(); | |
vRMS += (v2)*(v2)*(t2-t1)*0.000001; | |
t1 = t2; | |
} | |
vRMS = sqrt(frequencia*vRMS/50); | |
//referencia = referencia - 5.53; | |
periodo = periodo/1000000; | |
if(switchMode.read()*3.3 > 1.0) break; | |
lcd.cls(); | |
lcd.locate(0,0); | |
lcd.printf("Freq: %.2f", frequencia); | |
lcd.locate(0,1); | |
lcd .printf("Vrms: %.2f", vRMS); | |
wait(1); | |
} | |
//**************************************************************************************** | |
//******MODO DC*************************************************************************** | |
media = 0; | |
for(l = 0; l < 1000; l++) { | |
media = media + CONVERSAO(medida.read()); | |
} | |
media = media/0.3; | |
media = media /1000; | |
tensaoMedida = (media - 0.0004952)/1.00246; | |
if(tensaoMedida <= 0.02) { | |
lcd.cls(); | |
lcd.locate(0,0); | |
lcd.printf("Tensao baixa"); | |
lcd.locate(0,1); | |
lcd.printf("inferior a 20mV"); | |
} | |
else { | |
lcd.cls(); | |
lcd.locate(0,0); | |
lcd.printf("VDC: %.3f", tensaoMedida); | |
} | |
wait(0.05); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment