//********************************************************************************
//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:Humidity sensor(HSM 20G) interfacing with pic 18f
//Platform: pic18f4520
//Software:MPLAB
//********************************************************************************
#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
unsigned int Humidity = 0;
long adc_rd = 0;
void Get_Humidity();
unsigned int x,y,z,x1;
unsigned int c=0x100;
unsigned char k;
float p,tmpr,f;
char hum[] = "RH: %";
#define LCD PORTB
#define RS PORTDbits.RD5
#define RW PORTDbits.RD6
#define EN PORTDbits.RD7
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char );
void main()
{
unsigned char i;
TRISA=0x03; //all input
TRISD=0x00;
TRISB=0x00;
ADCON1 = 0x0A; //Analog an0
ADCON2=0x9C;
// LCD Initialization...
cmd(0x01); //Clear Lcd
cmd(0x0c);
cmd(0x80);
cmd(0x06);
cmd(0x38);
i=0;
cmd(0x80);
while(hum[i]!='\0')
{
dat(hum[i]);
i++;
Delay_ms(10);
}
while(1)
{
Get_Humidity();
}
}
void cmd(unsigned char k)
{
LCD=k;
RS=0;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void dat(unsigned char k)
{
LCD=k;
RS=1;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void Delay_ms(unsigned char p)
{
unsigned int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<200;j++);
}
}
void Get_Humidity()
{
int ii;
float voltage = 00.00;
ADCON0=0x0c; // Select Channel 3
ADCON0bits.ADON = 1;
ADCON0bits.GO=1;
while(ADCON0bits.GO==1);
adc_rd = 0; //Clear Previous Data
for(ii=0;ii<20;ii++)
{
ADCON0bits.ADON = 1;
ADCON0bits.GO=1;
while(ADCON0bits.GO==1); // Select Channel 2
x=ADRESH;
y=ADRESL;
z=(x*c)+y;
f=(z*(float)4.8875);
x1=(int)f;
adc_rd += x1; //Take sample and add
}
adc_rd /= 20; // Get the average data
voltage = adc_rd*0.004883; // 5/1023
Humidity = (int)((3.71 * voltage * voltage * voltage) - (20.65 * voltage * voltage) + (64.81 * voltage) - 27.44);
hum[3] = Humidity/10 + 48;
hum[4] = Humidity%10 +48;
cmd(0x83);
dat(hum[3]);
dat(hum[4]);
}
Simulation Result:
//Author : Palak Patel(M.Tech)
//Contact No:9173211683
//Title:Humidity sensor(HSM 20G) interfacing with pic 18f
//Platform: pic18f4520
//Software:MPLAB
//********************************************************************************
#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
unsigned int Humidity = 0;
long adc_rd = 0;
void Get_Humidity();
unsigned int x,y,z,x1;
unsigned int c=0x100;
unsigned char k;
float p,tmpr,f;
char hum[] = "RH: %";
#define LCD PORTB
#define RS PORTDbits.RD5
#define RW PORTDbits.RD6
#define EN PORTDbits.RD7
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char );
void main()
{
unsigned char i;
TRISA=0x03; //all input
TRISD=0x00;
TRISB=0x00;
ADCON1 = 0x0A; //Analog an0
ADCON2=0x9C;
// LCD Initialization...
cmd(0x01); //Clear Lcd
cmd(0x0c);
cmd(0x80);
cmd(0x06);
cmd(0x38);
i=0;
cmd(0x80);
while(hum[i]!='\0')
{
dat(hum[i]);
i++;
Delay_ms(10);
}
while(1)
{
Get_Humidity();
}
}
void cmd(unsigned char k)
{
LCD=k;
RS=0;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void dat(unsigned char k)
{
LCD=k;
RS=1;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void Delay_ms(unsigned char p)
{
unsigned int i,j;
for(i=0;i<p;i++)
{
for(j=0;j<200;j++);
}
}
void Get_Humidity()
{
int ii;
float voltage = 00.00;
ADCON0=0x0c; // Select Channel 3
ADCON0bits.ADON = 1;
ADCON0bits.GO=1;
while(ADCON0bits.GO==1);
adc_rd = 0; //Clear Previous Data
for(ii=0;ii<20;ii++)
{
ADCON0bits.ADON = 1;
ADCON0bits.GO=1;
while(ADCON0bits.GO==1); // Select Channel 2
x=ADRESH;
y=ADRESL;
z=(x*c)+y;
f=(z*(float)4.8875);
x1=(int)f;
adc_rd += x1; //Take sample and add
}
adc_rd /= 20; // Get the average data
voltage = adc_rd*0.004883; // 5/1023
Humidity = (int)((3.71 * voltage * voltage * voltage) - (20.65 * voltage * voltage) + (64.81 * voltage) - 27.44);
hum[3] = Humidity/10 + 48;
hum[4] = Humidity%10 +48;
cmd(0x83);
dat(hum[3]);
dat(hum[4]);
}
No comments:
Post a Comment