Você está na página 1de 2

int select = 1; // Seleciona a escala de temperatura

sbit LCD_RS at RE2_bit; // Configurações do LCD


sbit LCD_EN at RE1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISE2_bit;
sbit LCD_EN_Direction at TRISE1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;

unsigned int adcread () // Lê o valor do conversor AD


{
ADCON0 |= 0x04;
while (ADCON0 & 0x04);
return (ADRESH & 0x03)*256 + ADRESL;
}

void convert (float x, char * text) // Converte o nro em ponto flutuante em uma string
{ // +48, converte o algarismo em seu codigo ascii
unsigned int y = x*100;
text[0] = y/10000+48;
text[1] = (y%10000)/1000+48;
text[2] = (y%1000)/100+48;
text[4] = (y%100)/10+48;
text[5] = y%10+48;
}

void mostrar (int select) // Exibe a temperatura na escala selecionada


{
unsigned int valor_ad = 0;
float temp = 0;
char text[7] = {'0','0','0',',','0','0','\0'};

valor_ad = adcread (); // Lê o valor do conversor AD

switch (select)
{
case 1: // Caso select = 1, mostra a temperatura em celsius
temp = (float)valor_ad*((float)500/(float)1023);
convert (temp, text);
lcd_out (1,1, "Temperatura oC");
lcd_out (2,5, text);
break;
case 2: // Caso select = 2, mostra a temperatura em fahrenheit
temp = (float)valor_ad*((float)900/(float)1023)+32;
convert (temp, text);
lcd_out (1,1, "Temperatura oF");
lcd_out (2,5, text);
break;
case 3: // Caso select = 3, mostra a temperatura em kelvin
temp = (float)valor_ad*((float)500/(float)1023)+(float)273,15;
convert (temp, text);
lcd_out (1,1, "Temperatura oK");
lcd_out (2,5, text);
break;
}
}

void main()
{
TRISA = 0xFF; // Configura o porta como entrada
TRISB = 0x01; // Configura o portb como saida, RB0 como entrada
TRISC = 0x00; // Configura o portc como saida
TRISD = 0x00; // Configura o portd como saida
ADCON0 = 0xD5; // Configura o conversor A/D
ADCON1 = 0xC2;

portc.rc2 = 1; // Liga a ventoinha


portc.rc5 = 1; // Liga o aquecedor

Lcd_Init(); // Inicializa o display


Lcd_Cmd(_LCD_CLEAR); // Limpa o display
Lcd_Cmd(_LCD_CURSOR_OFF); // Desabilita o cursor

while(1) // Loop infinito


{
if (portb.rb0 == 0) select ++; // Se apertar o RB0, muda a escala de temperatura
if (select == 4) select = 1;
mostrar (select); // Exibe no display a temperatura na escala selecionada
delay_ms (100);
}
}

Você também pode gostar