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);
}
}
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);
}
}