Ir ao conteúdo
  • Cadastre-se

Sensor de temperatura LM35 (como colocar casas após a virgula ? )


Visitante

Posts recomendados

Olá, pessoal. Não cosigo colocar casas decimais após a virgula, no controle de temperatura com o sensor LM35. Eu trabalho com o MikroC. Peço a ajuda para este caso. Muito obrigado

Exemplo completo:

http://embedded-lab.com/blog/?p=4001

RevisedLM35Circuit.jpgLM35_OP.jpg


Digital Thermometer using PIC16F688 and LM35 sensor
Copyright@Rajendra Bhatt
Nov 3, 2011
*/
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections

// Define Messages
char message1[] = "Temperature:-";

// String array to store temperature value to display
char *tempC = "000.0";
char *tempF = "000.0";

// Variables to store temperature values
unsigned int tempinF, tempinC;
unsigned long temp_value;

void Display_Temperature() {
// convert Temp to characters
if (tempinC/10000)
tempC[0] = tempinC/10000 + 48;
else tempC[0] = ' ';
tempC[1] = (tempinC/1000)%10 + 48; // Extract tens digit
tempC[2] = (tempinC/100)%10 + 48; // Extract ones digit

// convert temp_fraction to characters
tempC[4] = (tempinC/10)%10 + 48; // Extract tens digit

// print temperature on LCD
Lcd_Out(2, 1, tempC);

if (tempinF/10000)
tempF[0] = tempinF/10000 + 48;
else tempF[0] = ' ';

tempF[1] = (tempinF/1000)%10 + 48; // Extract tens digit
tempF[2] = (tempinF/100)%10 + 48;
tempF[4] = (tempinF/10)%10 + 48;
// print temperature on LCD
Lcd_Out(2, 10, tempF);
}

void main() {
ANSEL = 0b00000100; // RA2/AN2 is analog input
ADCON0 = 0b01001000; // Connect AN2 to S/H, select Vref=2.5V
CMCON0 = 0x07 ; // Disbale comparators
TRISC = 0b00000000; // PORTC All Outputs
TRISA = 0b00001110; // PORTA All Outputs, Except RA3 and RA2
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,message1); // Write message1 in 1st row
// Print degree character
Lcd_Chr(2,6,223);
Lcd_Chr(2,15,223);
// different LCD displays have different char code for degree
// if you see greek alpha letter try typing 178 instead of 223

Lcd_Chr(2,7,'C');
Lcd_Chr(2,16,'F');

do {
temp_value = ADC_Read(2);
temp_value = temp_value*244;
tempinC = temp_value/10;
tempinF = 9*tempinC/5 + 3200;
Display_Temperature();
Delay_ms(1000);
} while(1);
}
/*

Você pode utilizar a seguinte biblioteca open-source para mais precisão, com pequena modificação no codigo anterior:


/*

* Based on MikroElectronika ADC on LCD example

* Uses self written ADC_Read function

*/

unsigned char ch;
unsigned int adc_rd;
char *text;
long tlong;

sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

unsigned adc_read2(char channel)
{

switch (channel) {
case 0: {
ADCON0.CHS0 = 0;
ADCON0.CHS1 = 0;
ADCON0.CHS2 = 0;
ADCON0.CHS3 = 0;
}
case 1: {
ADCON0.CHS0 = 1;
ADCON0.CHS1 = 0;
ADCON0.CHS2 = 0;
ADCON0.CHS3 = 0;
}
case 2: {
ADCON0.CHS0 = 0;
ADCON0.CHS1 = 1;
ADCON0.CHS2 = 0;
ADCON0.CHS3 = 0;
}
case 3: {
ADCON0.CHS0 = 1;
ADCON0.CHS1 = 1;
ADCON0.CHS2 = 0;
ADCON0.CHS3 = 0;
}
case 4: {
ADCON0.CHS0 = 0;
ADCON0.CHS1 = 0;
ADCON0.CHS2 = 1;
ADCON0.CHS3 = 0;
}


}



ADCON0.GO_DONE = 1; // start conversion
while (ADCON0.GO_DONE); // wait for conversion
return (ADRESH << 8) + ADRESL; // return value
}


void main() {
INTCON = 0; // disable all interrupts
ANSEL = 0x04; // Configure AN2 pin as analog input
TRISA = 0x04;
ANSELH = 0; // Configure other AN pins as digital I/O

ADCON1.VCFG0 = 0; // set internal
ADCON1.VCFG1 = 0;
ADCON0.ADCS0 = 1; // use Vdd as reference
ADCON0.ADCS1 = 0; // use Vcc as reference
ADCON1.ADFM = 1; // result is right Justified
ADCON0.ADON = 1; // enable A/D converter



Lcd_Init();
LCD_Cmd(_LCD_CURSOR_OFF); // send command to LCD (cursor off)
LCD_Cmd(_LCD_CLEAR); // send command to LCD (clear LCD)

text = "mikroElektronika"; // assign text to string
LCD_Out(1,1,text); // print string a on LCD, 1st row, 1st column
text = "LCD example"; // assign text to string
LCD_Out(2,1,text); // print string a on LCD, 2nd row, 1st column

ADCON1 = 0x82; // configure VDD as Vref, and analog channels
TRISA = 0xFF; // designate PORTA as input
Delay_ms(2000);
text = "voltage:"; // assign text to string
while (1) {
// let's use our open source function
adc_rd = ADC_read2(2); // get ADC value from 2nd channel
LCD_Out(2,1,text); // print string a on LCD, 2nd row, 1st column

tlong = (long)adc_rd * 5000; // covert adc reading to milivolts
tlong = tlong / 1023; // 0..1023 -> 0-5000mV

ch = tlong / 1000; // extract volts digit
LCD_Chr(2,9,48+ch); // write ASCII digit at 2nd row, 9th column
LCD_Chr_CP('.');

ch = (tlong / 100) % 10; // extract 0.1 volts digit
LCD_Chr_CP(48+ch); // write ASCII digit at cursor point

ch = (tlong / 10) % 10; // extract 0.01 volts digit
LCD_Chr_CP(48+ch); // write ASCII digit at cursor point

ch = tlong % 10; // extract 0.001 volts digit
LCD_Chr_CP(48+ch); // write ASCII digit at cursor point
LCD_Chr_CP('V');

Delay_ms(1);
}
}
 

http://www.sgvulcan.com/open-source-adc-read-function/

Abraço.

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...

APRENDA TUDO SOBRE HARDWARE!

HARDWARE 2022

 

CLIQUE AQUI E BAIXE AGORA MESMO!