* File: Temprature.c
* Author: TOSHIBA
*
* Created on May 5, 2013, 3:54 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <adc.h>
#include<delays.h>
#include<string.h>
#include<p18f452.h>
#pragma config WDT=OFF,OSC=HS,OSCS=OFF,BORV=45,PWRT=ON,BOR=ON
#define rs PORTCbits.RC5
#define rw PORTCbits.RC6
#define en PORTCbits.RC7
#define sdata PORTD
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void adc_con(int);
void adc_con1(int);
void adc_init(void);
void Print_string(char *A);
void Delay_ms(int);
char HR[]="Heart Rate";
char NO[]="PRESS BUTTON";
float LDR[10],avg_output=0,temp;
unsigned int i=0;
/*
*
*
*/
void main(void)
{
TRISA=0x0f; // Configure RA3 as input pin
TRISB=0; // Configure Port B as output port
TRISC=0;
TRISD=0;
// Set cursor position to 1st line, 1st column
TRISBbits.RB4=1;
TRISBbits.RB5=1;
TRISBbits.RB6=1;
adc_init(); //ADC Initialization
while(PORTBbits.RB5==1)
{
lcdcmd(0x38); // Configure the LCD in 8-bit mode, 2 line and 5x7 font
lcdcmd(0x0C); // Display On and Cursor Off
lcdcmd(0x01); // Clear display screen
lcdcmd(0x06); // Increment cursor
lcdcmd(0x80);
Print_string(HR);
lcdcmd(0x87);
ADCON0=0xD1;
ADRES=0;
temp=0;
for(i=0;i<10;i++)
{
ADCON0bits.GO=1; // Start A/D
while(ADCON0bits.DONE==1); // Wait
LDR[i]=ADRES ; //Store data
Delay_ms(5);
temp=temp+LDR[i];
}
avg_output=temp/10;
avg_output=(1024-avg_output); // Take average
adc_con(avg_output);
}
while(PORTBbits.RB5==0){
lcdcmd(0x38); // Configure the LCD in 8-bit mode, 2 line and 5x7 font
lcdcmd(0x0C); // Display On and Cursor Off
lcdcmd(0x01); // Clear display screen
lcdcmd(0x06); // Increment cursor
lcdcmd(0x80);
Print_string(NO);
lcdcmd(0x87);
Delay_ms(1000);
}
}
void lcdcmd(unsigned char cmdout)
{
sdata=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
Delay_ms(20);
en=0;
}
void Delay_ms(int d)
{
int i,j;
for(i=0;i<d;i++)
{
for(j=0;j<d;j++)
{
}
}
}
void adc_init()
{
ADCON1=0xC0; // Make RA0/AN0 ,RA1,RA2,RA3,RA4,RA5,RA6 and RA7 ANALOG
// Select Channel0 & ADC off
// ADCON2=0x85; // Fosc/32 clock option
ADCON0=0xD1; // AN3... clock drived from internal RC Enable ADC
}
void adc_con( int adc_out)
{
int adc_out1;
int i=0;
char position=0xC3;
for(i=0;i<=3;i++)
{
adc_out1=adc_out%10; // To exract the unit position digit
adc_out=adc_out/10;
lcdcmd(position);
lcddata(48+adc_out1); // Convert into its corresponding ASCII
position--;
}
Delay_ms(100);
}
void adc_con1( int adc)
{
int adc_out2;
int i=0;
char position=0xCF;
for(i=0;i<=3;i++)
{
adc_out2=adc%10; // To exract the unit position digit
adc=adc/10;
lcdcmd(position);
lcddata(48+adc_out2); // Convert into its corresponding ASCII
position--;
}
}
void lcddata(unsigned char dataout)
{
sdata=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
Delay_ms(30);
en=0;
}
void Print_string(char *a)
{
int i;
for(i=0;(*(a+i))!='\0';i++)
{
lcddata(*(a+i));
}
}