Multímetro

Dependencies:   mbed LCDLib

main.cpp

Committer:
MaikonJT
Date:
2019-06-27
Revision:
1:808bed0f2433
Parent:
0:be36515913f8
Child:
2:fb35f75110ee

File content as of revision 1:808bed0f2433:

#include "mbed.h"
#include "TextLCD.h"


DigitalOut led1(PC_13);
TextLCD lcd(PA_4,PA_5,PA_6,PB_12,PB_13,PB_14,PB_15);
AnalogIn adc(PA_0);
AnalogIn tensaofixa(PA_1);

InterruptIn botao(PA_7);
volatile bool botao_pressed = false; // Used in the main loop
volatile bool botao_enabled = true; // Used for debouncing
Timeout botao_timeout; // Used for debouncing

// Enables button when bouncing is over
void botao_enabled_cb(void)
{
    botao_enabled = true;
}

// ISR handling button pressed event
void botao_onpressed_cb(void)
{
    if (botao_enabled) { // Disabled while the button is bouncing
        botao_enabled = false;
        botao_pressed = true; // To be read by the main loop
        botao_timeout.attach(callback(botao_enabled_cb), 0.3); // Debounce time 300 ms
    }
}


int main()
{
    float alim, tensao, corrente, resistencia;
    int cont;
    cont = 1;
    //button1.mode(PullUp); // Activate pull-up
    botao.fall(callback(botao_onpressed_cb)); // Attach ISR to handle button press event

    while(1) {
        
        alim = 3.3*tensaofixa.read();
        tensao = 3.3*adc.read();
        resistencia = (tensao*1000)/(alim-tensao);
        corrente = (tensao/resistencia) * 1000;
        
        if (botao_pressed == true) { // Set when button is pressed
            botao_pressed = false;
            cont = cont++;        
            led1 = !led1;
        }
        if(cont == 4){
            cont = 1;    
        }
        if(cont == 1){
            lcd.gotoxy(1,1);
            lcd.printf("Tensao: %.2fV       ",tensao);
            wait(0.2);
            
        }
        if(cont == 2){
            lcd.gotoxy(1,1);
            lcd.printf("Corrente: %.2fmA       ",corrente);
            wait(0.2);
        }
        if(cont == 3){
            lcd.gotoxy(1,1);
            lcd.printf("Res.:% .0fohm     ",resistencia);
            wait(0.2);
        }   
         
    }
}