#include"adc.h"
void adcconvert(unsigned int g)
{
unsigned char temp=0,i=0;
unsigned int x=10000;
if(g==0)
lcddata(temp+0x30);
else
{
while(x)
{
temp=g/x;
if((temp!=0)&&(i==0))
i++;
if((temp!=0)||(i!=0))
lcddata(temp+0x30);
g-=(x*temp);
x/=10;
}
}
}
unsigned int adc(unsigned char chn_sel)
{
unsigned char x,y,adcon_bc;
auto int s=0;
if(chn_sel == 0)
{
CHS2 = 0; CHS1 = 0; CHS0 = 0;
}
else if(chn_sel == 1)
{
CHS2 = 0; CHS1 = 0; CHS0 = 1;
}
else if(chn_sel == 2)
{
CHS2 = 0; CHS1 = 1; CHS0 = 0;
}
else if(chn_sel == 3)
{
CHS2 = 0; CHS1 = 1; CHS0 = 1;
}
else if(chn_sel == 4)
{
CHS2 = 1; CHS1 = 0; CHS0 = 0;
}
else if(chn_sel == 5)
{
CHS2 = 1; CHS1 = 0; CHS0 = 1;
}
else if(chn_sel == 6)
{
CHS2 = 1; CHS1 = 1; CHS0 = 0;
}
else if(chn_sel == 7)
{
CHS2 = 1; CHS1 = 1; CHS0 = 1;
}
ADON=1;
delay(100);
ADGO=1;
while(ADGO==1);
x=ADRESL;
y=ADRESH;
s+=y;s<<=8;
s+=x;
delay(100);
ADON=0;
return s;
}
unsigned int adc_getvalue(unsigned char chn_sel)
{
unsigned char i=0;
unsigned int avg = 0;
for(;i<10;i++)
{
avg += adc(chn_sel);
}
return (avg/10);
}
void adc_init()
{
TRISA = 0xFF; // set PORTA as input
/* uncoment the below code for all analog sensors*/
// TRISE = 0x07; // set RE0 (AN5), RE0 (AN6) and RE2(AN7) as input
// ADCON1=0x80; //For configure all ADC Channels
/*************************************************/
CMCON = CMCON | 7; // Turn off comparators
ADCON0=0x40;
// ADCON1=0x8E; //For configure one sensor and make others Digital
ADCON1=0x82; //For configure First five sensor and last three Digital
}
void adcconvert(unsigned int g)
{
unsigned char temp=0,i=0;
unsigned int x=10000;
if(g==0)
lcddata(temp+0x30);
else
{
while(x)
{
temp=g/x;
if((temp!=0)&&(i==0))
i++;
if((temp!=0)||(i!=0))
lcddata(temp+0x30);
g-=(x*temp);
x/=10;
}
}
}
unsigned int adc(unsigned char chn_sel)
{
unsigned char x,y,adcon_bc;
auto int s=0;
if(chn_sel == 0)
{
CHS2 = 0; CHS1 = 0; CHS0 = 0;
}
else if(chn_sel == 1)
{
CHS2 = 0; CHS1 = 0; CHS0 = 1;
}
else if(chn_sel == 2)
{
CHS2 = 0; CHS1 = 1; CHS0 = 0;
}
else if(chn_sel == 3)
{
CHS2 = 0; CHS1 = 1; CHS0 = 1;
}
else if(chn_sel == 4)
{
CHS2 = 1; CHS1 = 0; CHS0 = 0;
}
else if(chn_sel == 5)
{
CHS2 = 1; CHS1 = 0; CHS0 = 1;
}
else if(chn_sel == 6)
{
CHS2 = 1; CHS1 = 1; CHS0 = 0;
}
else if(chn_sel == 7)
{
CHS2 = 1; CHS1 = 1; CHS0 = 1;
}
ADON=1;
delay(100);
ADGO=1;
while(ADGO==1);
x=ADRESL;
y=ADRESH;
s+=y;s<<=8;
s+=x;
delay(100);
ADON=0;
return s;
}
unsigned int adc_getvalue(unsigned char chn_sel)
{
unsigned char i=0;
unsigned int avg = 0;
for(;i<10;i++)
{
avg += adc(chn_sel);
}
return (avg/10);
}
void adc_init()
{
TRISA = 0xFF; // set PORTA as input
/* uncoment the below code for all analog sensors*/
// TRISE = 0x07; // set RE0 (AN5), RE0 (AN6) and RE2(AN7) as input
// ADCON1=0x80; //For configure all ADC Channels
/*************************************************/
CMCON = CMCON | 7; // Turn off comparators
ADCON0=0x40;
// ADCON1=0x8E; //For configure one sensor and make others Digital
ADCON1=0x82; //For configure First five sensor and last three Digital
}
Comments
Post a Comment