/* *****************************************************************************
; *
; Filename: *
; Date: *
; File Version: 001 *
; *
; Author: Jason Lopez *
; Company: AtomSoft *
; *
;***************************************************************************** */
#include <p18F4620.h>
#include <delays.h>
#include <string.h>
#include "lcd.h"
#pragma config WDT = OFF, LVP = OFF, OSC = HS, XINST = OFF
/************************************
Prototypes
*************************************/
void main(void);
unsigned char DECtoBCD(unsigned char DEC);
unsigned char DECtoASCII(unsigned char DEC,char part);
/************************************
Variables and Defines
*************************************/
#define ADCPin TRISAbits.TRISA0 // RA0/AN0 TRIS (DATA DIRECTION REGISTER)
unsigned int result; //Result Variable
unsigned int DegC; //Result in Celsius
unsigned int DegF; //Result in Fahrenheit
unsigned char lcdtmp[10];
/************************************
Main
*************************************/
void main(void){
ADCPin = 1; //AN0 = Input
ADCON0 = 0x00; //BIT 7:6 Always 00, BIT 5:2 is 0000 for Channel 0 (AN0),
//BIT 1:0 is 00 (A/D Idle and Module is off) until we are done setting up
ADCON1 = 0x0E; //BIT 7:6 Always 00, BIT 5 is 0 for VSS, BIT 4 is 0 for VDD
//BIT 3:0 is 1110 for all digital expect AN0. We need it analog so we can convert it.
ADCON2 = 0b10001101; //BIT 7 is 0 for Left Justified, BIT 6 Always 0, BIT 5:3 001 for 2 TAD (A/D Acquisition Time)
//BIT 2:0 is 100 for FOSC/4 for A/D Conversion Clock
ADCON0bits.ADON = 1; //Turn on the A/D module
LCD_Init();
DelayMS(100);
LCD_LINE(1);
LCD_STR((unsigned rom char*)" AtomSoftTech ");
LCD_LINE(2);
LCD_STR((unsigned rom char*)"Temp: ");
while(1){
ADCON0bits.GO = 1; //Start the conversion
while(ADCON0bits.DONE); //Wait until it’s done
result = ADRESH; //Load ADRESH into result
result <<= 8; //Shift over Result 8 bits to the left
result |= ADRESL; //OR in our LOW byte to finish off out INT
DegC = (result*5)/10;
DegC--;
DegF = DegC * 9;
DegF/=5;
DegF+=32;
lcdtmp[0] = DECtoASCII(DegC,'h');
lcdtmp[1] = DECtoASCII(DegC,'l');
lcdtmp[2] = 'C';
lcdtmp[3] = '-';
lcdtmp[4] = DECtoASCII(DegF,'h');
lcdtmp[5] = DECtoASCII(DegF,'l');
lcdtmp[6] = 'F';
lcdtmp[7] = lcdtmp[8] = lcdtmp[9] = ' ';
LCD_LINE(2);
LCD_STR((unsigned rom char*)"Temp: ");
LCD_STR2(lcdtmp);
DelayMS(1000);
}
}
unsigned char DECtoASCII(unsigned char DEC,char part){
unsigned char temp;
unsigned char myData;
temp = DECtoBCD(DEC);
if(part == 'l')
myData = temp & 0x0F;
if(part == 'h')
myData = (temp >> 4) & 0x0F;
myData += 0x30;
return myData;
}
unsigned char DECtoBCD(unsigned char DEC){
unsigned char temph;
unsigned char templ;
unsigned char myData;
templ = DEC % 10;
temph = DEC / 10;
myData = (temph << 4) | templ;
return myData; //Return myData
}