kbtan
Member level 2
- Joined
- Jul 8, 2011
- Messages
- 45
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,583
I am trying to do a temperature sensor using pic16f887 and then display on LCD. The output from the LM35 is constant but the display temperature on LCD won't be constant, the reading always jump. So is my code got problem??
i am using c programing
PHP:
// LCD module connections
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;
// End LCD module connections
char disp[] = "Temp: ";
char cel[] = "'C";
int temp_res = 123 ;
float temp;
char *test= "00.00";
int tempint;
void main(){
ANSEL = 0x04; // Configure AN2 pin as analog
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
TRISA = 0xFF; // PORTA is input
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,disp); // print "Temp: " to lcd
//Lcd_Out(2,1,disp); // print "Temp: " to lcd
Lcd_Out(1,13,cel); // print "'C" to lcd
do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
temp = 5.00*temp_res*100.00/1023.00; // convert voltage to degree celcius
//print temperature in 2 decimal point
tempint = temp * 100;
test[0]= (tempint/1000) % 10 + 48; // add 48 to get the ASCII char value
test[1]= (tempint/100) % 10 + 48;
test[3]= (tempint/10) % 10 + 48;
test[4]= tempint % 10 + 48;
Lcd_Out(1,7,test); // display temperature
Delay_ms(500);
} while(1);
// Moving text
}