Aplicações de Microcontroladores
/
Aula_2
Multímetro
main.cpp@0:be36515913f8, 2019-06-13 (annotated)
- Committer:
- felipealboy
- Date:
- Thu Jun 13 14:34:19 2019 +0000
- Revision:
- 0:be36515913f8
- Child:
- 1:808bed0f2433
-
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
felipealboy | 0:be36515913f8 | 1 | #include "mbed.h" |
felipealboy | 0:be36515913f8 | 2 | #include "TextLCD.h" |
felipealboy | 0:be36515913f8 | 3 | |
felipealboy | 0:be36515913f8 | 4 | |
felipealboy | 0:be36515913f8 | 5 | DigitalOut led1(PC_13); |
felipealboy | 0:be36515913f8 | 6 | TextLCD lcd(PA_4,PA_5,PA_6,PB_12,PB_13,PB_14,PB_15); |
felipealboy | 0:be36515913f8 | 7 | AnalogIn adc(PA_0); |
felipealboy | 0:be36515913f8 | 8 | |
felipealboy | 0:be36515913f8 | 9 | InterruptIn botao(PA_7); |
felipealboy | 0:be36515913f8 | 10 | volatile bool botao_pressed = false; // Used in the main loop |
felipealboy | 0:be36515913f8 | 11 | volatile bool botao_enabled = true; // Used for debouncing |
felipealboy | 0:be36515913f8 | 12 | Timeout botao_timeout; // Used for debouncing |
felipealboy | 0:be36515913f8 | 13 | |
felipealboy | 0:be36515913f8 | 14 | // Enables button when bouncing is over |
felipealboy | 0:be36515913f8 | 15 | void botao_enabled_cb(void) |
felipealboy | 0:be36515913f8 | 16 | { |
felipealboy | 0:be36515913f8 | 17 | botao_enabled = true; |
felipealboy | 0:be36515913f8 | 18 | } |
felipealboy | 0:be36515913f8 | 19 | |
felipealboy | 0:be36515913f8 | 20 | // ISR handling button pressed event |
felipealboy | 0:be36515913f8 | 21 | void botao_onpressed_cb(void) |
felipealboy | 0:be36515913f8 | 22 | { |
felipealboy | 0:be36515913f8 | 23 | if (botao_enabled) { // Disabled while the button is bouncing |
felipealboy | 0:be36515913f8 | 24 | botao_enabled = false; |
felipealboy | 0:be36515913f8 | 25 | botao_pressed = true; // To be read by the main loop |
felipealboy | 0:be36515913f8 | 26 | botao_timeout.attach(callback(botao_enabled_cb), 0.3); // Debounce time 300 ms |
felipealboy | 0:be36515913f8 | 27 | } |
felipealboy | 0:be36515913f8 | 28 | } |
felipealboy | 0:be36515913f8 | 29 | |
felipealboy | 0:be36515913f8 | 30 | |
felipealboy | 0:be36515913f8 | 31 | int main() |
felipealboy | 0:be36515913f8 | 32 | { |
felipealboy | 0:be36515913f8 | 33 | float tensao; |
felipealboy | 0:be36515913f8 | 34 | int cont; |
felipealboy | 0:be36515913f8 | 35 | cont = 1; |
felipealboy | 0:be36515913f8 | 36 | //button1.mode(PullUp); // Activate pull-up |
felipealboy | 0:be36515913f8 | 37 | botao.fall(callback(botao_onpressed_cb)); // Attach ISR to handle button press event |
felipealboy | 0:be36515913f8 | 38 | |
felipealboy | 0:be36515913f8 | 39 | while(1) { |
felipealboy | 0:be36515913f8 | 40 | |
felipealboy | 0:be36515913f8 | 41 | tensao = 3.3*adc.read(); |
felipealboy | 0:be36515913f8 | 42 | |
felipealboy | 0:be36515913f8 | 43 | if (botao_pressed == true) { // Set when button is pressed |
felipealboy | 0:be36515913f8 | 44 | botao_pressed = false; |
felipealboy | 0:be36515913f8 | 45 | cont = cont++; |
felipealboy | 0:be36515913f8 | 46 | led1 = !led1; |
felipealboy | 0:be36515913f8 | 47 | } |
felipealboy | 0:be36515913f8 | 48 | if(cont == 5){ |
felipealboy | 0:be36515913f8 | 49 | cont = 1; |
felipealboy | 0:be36515913f8 | 50 | } |
felipealboy | 0:be36515913f8 | 51 | if(cont == 1){ |
felipealboy | 0:be36515913f8 | 52 | lcd.gotoxy(1,1); |
felipealboy | 0:be36515913f8 | 53 | lcd.printf("Tensao: %.2fV ",tensao); |
felipealboy | 0:be36515913f8 | 54 | } |
felipealboy | 0:be36515913f8 | 55 | if(cont == 2){ |
felipealboy | 0:be36515913f8 | 56 | lcd.gotoxy(1,1); |
felipealboy | 0:be36515913f8 | 57 | lcd.printf("Corrente: %.2fA ",tensao); |
felipealboy | 0:be36515913f8 | 58 | } |
felipealboy | 0:be36515913f8 | 59 | if(cont == 3){ |
felipealboy | 0:be36515913f8 | 60 | lcd.gotoxy(1,1); |
felipealboy | 0:be36515913f8 | 61 | lcd.printf("Resist.:% .2fohm",tensao); |
felipealboy | 0:be36515913f8 | 62 | } |
felipealboy | 0:be36515913f8 | 63 | if(cont == 4){ |
felipealboy | 0:be36515913f8 | 64 | lcd.gotoxy(1,1); |
felipealboy | 0:be36515913f8 | 65 | lcd.printf("Freq.:% .2fHz ",tensao); |
felipealboy | 0:be36515913f8 | 66 | } |
felipealboy | 0:be36515913f8 | 67 | } |
felipealboy | 0:be36515913f8 | 68 | } |