Wong Sing Sing
Newbie level 1
Code:
//Header file
#include <p18F4520.h>
#include <delays.h>
#include <adc.h>
#include <system4520.h>
#include <stdio.h>
#include <LCD.h>
#define CHANNEL0 0b10000001 // AN0(ra0)
#define fan PORTBbits.RB4
void read_adc(void);
unsigned short read_temp(void);
unsigned short result;
unsigned short temp,tempA;
void main(void)
{
ADRESH=0; //clear A/D result
ADRESL=0; //clear A/D result
ADCON0=CHANNEL0;
ADCON1=0b11000101; // A/D result right justified, configure RA2 and RA5 as digital I/O
ADCON2 = 0b00000100; // Result Left justified, Manual acquisition time, Fosc/4
ADCON0bits.ADON = 1;
TRISA=0b11011011; //configure PORTA I/O direction
TRISB=0b00000000; //configure PORTB as output
TRISC=0b00000000; //configure PORTC as output
//PR2 = 0b01111100 ;
//T2CON = 0b00000101 ;
//CCP1CON = 0b00001100 ;
//CCP2CON = 0b00111100 ;
//CCPR1L=0;
//CCPR2L=1;
PORTA=0;
PORTB=0;
while(1)
{
LCD_init();
cursor_set(0,0);
PutS("Temp= C ");
while(1) //infinity loop
{
ADCON0=CHANNEL0; //CHANNEL1=0b10001001
cursor_set(0,6);
read_adc();
temp=((read_temp()/255.0)*500);
getchar(0b11011111);
getchar('C');
tempA=temp;
if(tempA>400)
{
fan=1;
}
else if(tempA<400)
{
fan=0;
}
Delay10KTCYx(200);
}
}
}
void read_adc(void)
{
unsigned short i;
unsigned long result_temp=0;
for(i=2000;i>0;i-=1) //looping 2000 times for getting average value
{
ADCON0bits.GO = 1;
while(ADCON0bits.DONE);
result=ADRESH;
result=result<<8; //shift to left for 8 bit
result=result|ADRESL; //10 bit result from ADC
result_temp+=result;
}
result = result_temp/2000; //getting average value
}
unsigned short read_temp(void)
{
unsigned short temp;
temp=result;
return temp;
}
Hi, i'm newbie to this type of programming and i hoping to get some solution or guidance on it.
Above is my coding for this temperature sensor interfacing with p18f4520.
I have a LM35 which i will need to output the temperature value onto a 2x16 LCD.
Im using 18f4520 and making use of its ADC to do the conversion but i have faced the problem of lcd which didn't respond to my sensor, or it is my programming problem please help me on this thank you.