/*************INCLUDES****************
**************************************/
#include <p18f4520.h>
#include <delays.h>
#include <stdio.h>
#include <adc.h>
#include <math.h>
#include <pwm.h>
#include <timers.h>
#include "XLCD/xlcd.h"
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF
#pragma config PBADEN = ON
#define PWM1_DIR TRISCbits.TRISC1
#define PWM1_OUT LATCbits.LATC1
#define PWM2_DIR TRISCbits.TRISC2
#define PWM2_OUT LATCbits.LATC2
/*************FUNCTION PROTOTYPES**************
**********************************************/
void ReadTemp (void);
int TempCi;
volatile int temp;
char Duty_cycle;
/*********************************************/
#pragma interrupt CCP2_ISR
void CCP2_ISR()
{
if(PWM1_OUT == 1) //Will be 1 if we are at end of pulse
{
PWM1_OUT = 0;
CCPR1 = 5000 - temp; //Off time
}
else
{
PWM1_OUT = 1;
CCPR1 = temp; //On time
}
if(Duty_cycle == 0)
{
PWM1_OUT = 0;
}
if(Duty_cycle == 100)
{
PWM1_OUT = 1;
}
PIR1bits.CCP1IF=0; //Clear int flag
return;
}
void main (void)
{
//Open ADC CH7 for thermistor readings
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_4_TAD,
ADC_CH7 & ADC_INT_OFF & ADC_REF_VDD_VSS, ADC_7ANA);
//Open ADC CH11 for pressure readings
OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_4_TAD,
ADC_CH11 & ADC_INT_OFF & ADC_REF_VDD_VSS, ADC_11ANA);
ADCON1 = 0b0001110;
OSCCON=0b01100000; //Osc=4MHz
//OSCCON = 0xF2; //configure for internal oscillator, 8MHz
PWM1_DIR = 0; //Make bit 0 output
PWM1_OUT = 0; //Output off
CCP1CON = 0b00001011; //Special event trigger
CCPR1 = 2500; //Set CCP initial value
PIR1bits.TMR1IF = 0; //Clear the interrupt flag
T1CON = 0b10100001; //Timer 1 on with Pre = 4
PIE1bits.CCP1IE=1; //Enable CCP1 interrupt
INTCONbits.PEIE=1; //Enable peripheral interrupts
INTCONbits.GIE=1; //Enable glogal interrupts
PWM1_OUT = 1; //Turn on output
XLCDInit(); //Initialize the LCD display
XLCDClear(); //Clear the LCD display
while (1)
{
for(char dc = 0; dc < 100; dc++)
{
Duty_cycle = dc;
temp = (Duty_cycle * 50);
//__delay_ms(100);
}
for(char dc = 99; dc > 0; dc--)
{
Duty_cycle = dc;
temp = (Duty_cycle * 50);
//__delay_ms(100);
}
ReadTemp ();
}
CloseADC(); //Close the ADC
}
void ReadTemp (void)
{
/*************Temperature Coefficients****************
-Thermistor temperature coefficients
******************************************************/
const float coeffA = 1.12503e-3;
const float coeffB = 2.35058e-4;
const float coeffC = 7.85661e-8;
char line[16]; //16row LCD Disp
float Rt, TempC, ADCVal;
SetChanADC(ADC_CH7);
XLCDDelay4ms ();
ConvertADC(); // Start conversion
while( BusyADC() ); // Wait for completion
XLCDDelay4ms ();
ADCVal = (float)ReadADC(); // Read ADC
Rt = ((1024.0/ADCVal)-1)*10000.0; //10000 is the value of R21 on PCB
XLCDDelay4ms ();
TempC = (1/(coeffA+(coeffB*(log(Rt)))+(coeffC * (exp(log(Rt)),3))))-273.15;
XLCDDelay4ms ();
XLCDL1home();
XLCDPutRomString(" TEMP :: ");
TempCi = (int)TempC;
if (TempCi > 0)
{
sprintf(line,"%d",TempCi);
XLCDPutRamString(line);
}
else
XLCDPutRomString("ERR");
XLCDPut(223); //Send degree symbol
XLCDPut('C');
XLCDPutRomString(" ");
}