problem reading temperature value using a pic16f917

Thread Starter

andyforeverest

Joined Nov 3, 2009
1
Hello all,

I have a Picdem Mechatronics board with a pic16f917. I am using HI-TEC compiler and trying to read temperature value. I get all sorts of wrong info displayed on the LCD.

I am including the C software here.

Any help will be appreciated.

Andrei


#include <htc.h>
#include "lcd.h"

__CONFIG(CP_OFF & CPD_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTOSCIO & MCLRE_ON & FCMEN_OFF & IESO_OFF);

#define _XTAL_FREQ 8000000UL

unsigned short readTemp()
{
unsigned char j;
unsigned short tempADC;

ADCON0 = 0b00000001;
tempADC = 0;

for(j=0; j<5; j++)
{
GO=1;
while(nDONE) ;
tempADC = tempADC + ADRESH;
__delay_us(40);
}//for
tempADC = tempADC/5;
tempADC = (tempADC/2) - 50;
return tempADC;

}

void main (void)
{
unsigned short temp;
unsigned char ones, tens;

OSCCON = 0x70; // 8MHz operation
TRISA0 = 1; //pin is input
TRISD7 = TRISD6 = 0;//pins are output
OPTION_REG = 0x87;
ANSEL = 0b00000001;//AN0

ADCON0 = 0b00000001;//
ADCON1 = 0b01010000;//

CMCON0 = 0x07;//comparatoare off

lcd_init();
lcd_clear();

while (1)
{
temp = readTemp();

if ((temp<10) && (temp>=1))
{
ones=(unsigned char)temp;
RD6 = RD7 = 0;
}

else if ((temp <20) && (temp>=10))
{
tens = 1;
ones = (unsigned char)(temp - 10);
RD6 = 0;
RD7 = 0;
}

else if ((temp <30) && (temp >=20))
{
tens = 2;
ones = (unsigned char)(temp - 20);
RD6 = 0;
RD7 = 1;
}

else if ((temp <40) && (temp >=30))
{
tens = 3;
ones = (unsigned char)(temp - 30);
RD7 = 1;
RD6 = 0;
}

else if ((temp <50) && (temp >=40))
{
tens = 4;
ones = (unsigned char)(temp - 40);
RD6 = 1;
RD7 = 1;
}

lcd_d1(ones);
lcd_d2(tens);

}
}
 

MrChips

Joined Oct 2, 2009
30,823
Put your code listing in between {code} and {/code} tags.
Replace the { } braces above with [ ] braces.

Then indent your procedures and program structures for easier reading.
Next, try to break your main( ) code into functional modules and test and debug each module one at a time.

Rich (BB code):
#include <htc.h> 
#include "lcd.h" 

__CONFIG(CP_OFF & CPD_OFF & BOREN_OFF & PWRTE_ON &  WDTE_OFF & FOSC_INTOSCIO & MCLRE_ON & FCMEN_OFF &  IESO_OFF); 

#define _XTAL_FREQ 8000000UL 

unsigned short readTemp() 
{    
unsigned char j; 
unsigned short tempADC; 

ADCON0 = 0b00000001; 
tempADC = 0; 

for(j=0; j<5; j++) 
{ 
GO=1; 
while(nDONE) ; 
tempADC = tempADC + ADRESH; 
__delay_us(40); 
}//for 
tempADC = tempADC/5; 
tempADC = (tempADC/2) - 50; 
return tempADC; 

} 

void main (void) 
{ 
unsigned short temp; 
unsigned char ones, tens; 

  OSCCON = 0x70; // 8MHz operation  
  TRISA0 = 1; //pin is input 
  TRISD7 = TRISD6 = 0;//pins are output 
  OPTION_REG = 0x87; 
  ANSEL = 0b00000001;//AN0 
   
  ADCON0 = 0b00000001;// 
  ADCON1 = 0b01010000;// 
   
  CMCON0 = 0x07;//comparatoare off 
   
  lcd_init(); 
  lcd_clear(); 
   
  while (1) 
    { 
temp = readTemp(); 

if ((temp<10) && (temp>=1)) 
{ 
ones=(unsigned char)temp; 
RD6 = RD7 = 0; 
} 
  
else if ((temp <20) && (temp>=10)) 
{ 
tens = 1; 
ones = (unsigned char)(temp - 10); 
RD6 = 0; 
RD7 = 0; 
} 

else if ((temp <30) && (temp >=20)) 
{ 
tens = 2; 
ones = (unsigned char)(temp - 20); 
RD6 = 0; 
RD7 = 1; 
} 

else if ((temp <40) && (temp >=30)) 
{ 
tens = 3; 
ones = (unsigned char)(temp - 30); 
RD7 = 1; 
RD6 = 0; 
} 

else if ((temp <50) && (temp >=40)) 
{ 
tens = 4; 
ones = (unsigned char)(temp - 40); 
RD6 = 1; 
RD7 = 1; 
}    

lcd_d1(ones);  
lcd_d2(tens); 
  
   } 
}
 

Markd77

Joined Sep 7, 2009
2,806
The section with tempADC:
If ADRESH is 8 bit then its maximum value is 255. You then divide by 10, subtract 50 then repeat. I'm pretty sure that's going to give some unusual numbers.
 
Last edited:
Top