/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * DEFINIÇÃO PIC * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include "LCD_Lib.h" #include "usart.h" #include //Register definitions #include #include #include /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * INCLUDES DAS FUNÇÕES DE PERIFÉRICOS DO PIC * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Configurações para gravação * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #pragma config FOSC = HS #pragma config CPUDIV = OSC1_PLL2 #pragma config WDT = OFF #pragma config WDTPS = 128 #pragma config LVP = OFF #pragma config PWRT = ON #pragma config BOR = OFF #pragma config BORV = 0 #pragma config PBADEN = OFF #pragma config DEBUG = OFF #pragma config PLLDIV = 1 #pragma config USBDIV = 1 #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config VREGEN = OFF #pragma config MCLRE = ON #pragma config LPT1OSC = OFF #pragma config CCP2MX = ON #pragma config STVREN = OFF #pragma config ICPRT = OFF #pragma config XINST = OFF /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Definição e inicialização das variáveis globais * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ //Neste bloco estão definidas as variáveis globais do programa. //Este programa não utiliza nenhuma variável de usuário float ResultTensao=0; float ResultTensao2=0; float ConvTensao=0; float ConvTensao2=0; int TensaoInt; int TensaoInt2; char dados[]; char dados2[]; int cent,cent2=0; int dez,dez2=0; int unid,unid2=0; int dec1,dec12=0; int dec2,dec22=0; int dec3,dec32=0; int botao=0; /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * PROTOTIPAGEM DE FUNÇÕES * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // Prototipagem das funções do conversor A/D void config_ADC(void) //configuração do conversor ligado ao AN0 { ADCON0=0b00000001; ADCON1=0b00001110; ADCON2=0b10101100; } int conv_AD(void) { int Result_AD; ADCON0bits.GO=1; while(ADCON0bits.GO) Result_AD=(((int)ADRESH)<<8)|(ADRESL); return Result_AD; } void conversao(void) { // CENTENA da variável ConvTensao cent=ConvTensao/100; // DEZENA da variável ConvTensao dez=(ConvTensao-cent*100)/10; // UNIDADE da variável ConvTensao unid=(ConvTensao-cent*100-dez*10); // DECIMAL 1 da variável ConvTensao dec1=(ConvTensao-cent*100-dez*10)*10-unid*10; // DECIMAL 2 da variável ConvTensao dec2=((ConvTensao-cent*100-dez*10)*10-unid*10)*10-dec1*10; // DECIMAL 3 da variável ConvTensao dec3=((ConvTensao-cent*100-dez*10)*10-unid*10)*100-dec1*100-dec2*10; } void escreverdados (void) { dados[1]=cent; dados[2]=dez; dados[3]=unid; dados[4]='.'; dados[5]=dec1; dados[6]=dec2; dados[7]=dec3; } //_________________________________________________________________________________ void config_ADC2(void) //configuração do conversor ligado ao AN1 ( sensor 2) { ADCON0=0b00000101; //porta an1 (RA1) ADCON1=0b00001110; ADCON2=0b10101100; } int conv_AD2(void) { int Result_AD2; ADCON0bits.GO=1; while(ADCON0bits.GO) Result_AD2=(((int)ADRESH)<<8)|(ADRESL); return Result_AD2; } void conversao2(void) { // CENTENA da variável ConvTensao cent2=ConvTensao2/100; // DEZENA da variável ConvTensao dez2=(ConvTensao2-cent2*100)/10; // UNIDADE da variável ConvTensao unid2=(ConvTensao2-cent2*100-dez2*10); // DECIMAL 1 da variável ConvTensao dec12=(ConvTensao2-cent2*100-dez2*10)*10-unid2*10; // DECIMAL 2 da variável ConvTensao dec22=((ConvTensao2-cent2*100-dez2*10)*10-unid2*10)*10-dec12*10; // DECIMAL 3 da variável ConvTensao dec32=((ConvTensao2-cent2*100-dez2*10)*10-unid2*10)*100-dec12*100-dec22*10; } void escreverdados2 (void) { dados2[1]=cent2; dados2[2]=dez2; dados2[3]=unid2; dados2[4]='.'; dados2[5]=dec12; dados2[6]=dec22; dados2[7]=dec32; } //______________________________________________________________________________ /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Função Principal * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ void main(void) { /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Inicialização do Sistema * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ // inicializando (limpando) as portas de I/O PORTA = 0x00; //Limpa PORTA PORTB = 0x00; //Limpa PORTB PORTC = 0x00; //Limpa PORTC PORTD = 0x00; //Limpa PORTD PORTE = 0x00; //Limpa PORTE LATA = 0x00; //Limpa PORTA LATB = 0x00; //Limpa PORTB LATC = 0x00; //Limpa PORTC LATD = 0x00; //Limpa PORTD LATE = 0x00; //Limpa PORTE //configurando a diração dos pinos das portas de I/O TRISA = 0b00000001; //CONFIG DIREÇÃO DOS PINOS PORTA TRISB = 0b01000000; //CONFIG DIREÇÃO DOS PINOS PORTB TRISC = 0b10111000; //CONFIG DIREÇÃO DOS PINOS PORTC TRISD = 0b00000000; //CONFIG DIREÇÃO DOS PINOS PORTD TRISE = 0b00000111; //CONFIG DIREÇÃO DOS PINOS PORTE ADCON1 = 0b00001111; //DESLIGA CONVERSORES A/D /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Inicialização de Variáveis de Pinos de Saída do Sistema * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ config_ADC(); //Configuração do Conversor A/D OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 25); // Configuração da Porta Serial inicializa_lcd(); // Configuração do LCD. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Rotina Principal * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ while(1) { comando_lcd(0x83); escreve_lcd('C'); escreve_lcd('U'); escreve_lcd('I'); escreve_lcd('D'); escreve_lcd('A'); escreve_lcd('D'); escreve_lcd('O'); comando_lcd(0xC1); escreve_lcd('A'); escreve_lcd('R'); escreve_lcd('E'); escreve_lcd('A'); escreve_lcd(' '); escreve_lcd('D'); escreve_lcd('E'); escreve_lcd(' '); escreve_lcd('R'); escreve_lcd('I'); escreve_lcd('S'); escreve_lcd('C'); escreve_lcd('0'); escreve_lcd('!'); ResultTensao=conv_AD(); ConvTensao = 5.0*(ResultTensao/1024.0);//*10.0; TensaoInt=ConvTensao; conversao(); ResultTensao2=conv_AD2(); // nessa parte é quando chama o segundo sensor ConvTensao2 = 5.0*(ResultTensao2/1024.0);//*10.0; TensaoInt2=ConvTensao2; conversao2(); if (ResultTensao > 1){ comando_lcd(0x83); escreve_lcd('P'); escreve_lcd('E'); escreve_lcd('R'); escreve_lcd('I'); escreve_lcd('G'); escreve_lcd('O'); escreve_lcd('!'); comando_lcd(0xC1); escreve_lcd('P'); escreve_lcd('E'); escreve_lcd('R'); escreve_lcd('I'); escreve_lcd('G'); escreve_lcd('O'); escreve_lcd('!'); escreve_lcd(' '); escreve_lcd('A'); escreve_lcd('R'); escreve_lcd('E'); escreve_lcd('A'); escreve_lcd(' '); escreve_lcd('1'); escreve_lcd('!'); } Delay1KTCYx(200); } if (ResultTensao2 > 1){ comando_lcd(0x83); escreve_lcd('P'); escreve_lcd('E'); escreve_lcd('R'); escreve_lcd('I'); escreve_lcd('G'); escreve_lcd('O'); escreve_lcd('!'); comando_lcd(0xC1); escreve_lcd('P'); escreve_lcd('E'); escreve_lcd('R'); escreve_lcd('I'); escreve_lcd('G'); escreve_lcd('O'); escreve_lcd('!'); escreve_lcd(' '); escreve_lcd('A'); escreve_lcd('R'); escreve_lcd('E'); escreve_lcd('A'); escreve_lcd(' '); escreve_lcd('2'); escreve_lcd('!'); Delay1KTCYx(200); } }