#include "Includes.h"
void InitADC(unsigned char Channel)
{
ANSEL = 0x10; // Clear Pin selection bits
ANSEL = 0x34; // Select Channel
TRISIO = 0x40; // GP3 input, rest all output
TRISIO |= Channel; // Make selected channel pins input
ADCON0 = 0x81; // Turn on the A/D Converter
VRCON = 0x00;
GPIO=0x00; // Shut off the Voltage Reference for Comparator
}
unsigned int GetADCValue(unsigned char Channel)
{
ADCON0 &= 0xf3; // Clear Channel selection bits
switch(Channel)
{
case AN0: ADCON0 |= 0x00; break; // Select GP0 pin as ADC input
case AN1: ADCON0 |= 0x04; break; // Select GP1 pin as ADC input
case AN2: ADCON0 |= 0x08; break; // Select GP2 pin as ADC input
case AN3: ADCON0 |= 0x0c; break; // Select GP4 pin as ADC input
default: return 0; //Return error, wrong channel selected
}
__delay_ms(10); // Time for Acqusition capacitor to charge up and show correct value
GO_nDONE = 1; // Enable Go/Done
while(GO_nDONE); //wait for conversion completion
return ((ADRESH<<8)+ADRESL); // Return 10 bit ADC value
}
void InitADC(unsigned char Channel)
{
ANSEL = 0x10; // Clear Pin selection bits
ANSEL = 0x34; // Select Channel
TRISIO = 0x40; // GP3 input, rest all output
TRISIO |= Channel; // Make selected channel pins input
ADCON0 = 0x81; // Turn on the A/D Converter
VRCON = 0x00;
GPIO=0x00; // Shut off the Voltage Reference for Comparator
}
unsigned int GetADCValue(unsigned char Channel)
{
ADCON0 &= 0xf3; // Clear Channel selection bits
switch(Channel)
{
case AN0: ADCON0 |= 0x00; break; // Select GP0 pin as ADC input
case AN1: ADCON0 |= 0x04; break; // Select GP1 pin as ADC input
case AN2: ADCON0 |= 0x08; break; // Select GP2 pin as ADC input
case AN3: ADCON0 |= 0x0c; break; // Select GP4 pin as ADC input
default: return 0; //Return error, wrong channel selected
}
__delay_ms(10); // Time for Acqusition capacitor to charge up and show correct value
GO_nDONE = 1; // Enable Go/Done
while(GO_nDONE); //wait for conversion completion
return ((ADRESH<<8)+ADRESL); // Return 10 bit ADC value
}
MAIN.C
#include "Includes.h"
// Config word
__CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_ON & MCLRE_OFF & BOREN_ON & CP_OFF & CPD_OFF);
#define LED GP5 //RED LED
#define LED1 GP4 //GREEEN LED
#define BUZZER GP0 //BUZZER
unsigned int ADC_Value,ADC_Value1;
unsigned int ADC_Total, ADC_Total1,ADC_Total2,ADC_Average,ADC_Average1;
unsigned int flag,count,ADC_Average2;
unsigned int ADCValue1,Lowerlimit,Upperlimit;
// Main function
void main()
{
OSCCON=0x61;
InitADC(AN2); // Initialize GP2 as ADC input pin CHANNEL 2
while(1)
{
LED=0;
LED1=0;
count=0;
ADC_Value = GetADCValue(AN2); //taken reading for .2mv to read
__delay_ms(250);
count++;
if((ADC_Value>70) &&(ADC_Value<650) && (count==1))
{
__delay_ms(2000);
ADC_Value = GetADCValue(AN2); // Initialize GP2 as ADC input pin CHANNEL 2
ADC_Average1=ADC_Value;
Upperlimit=ADC_Average1+30;
Lowerlimit=ADC_Average1-30;
LED=1; //Referance Obtained RED LED && BUZZER ON
GP0=1;
__delay_ms(1000);
GP0=0;
LED=0;
count++;
}
if(count==2)
{
while(1)
{
//__delay_ms(2000);
ADCValue1= GetADCValue(AN2); // GET a first ADC value to make calculation
ADC_Average1=ADCValue1; //STORE In A variable
if((ADC_Average1>Upperlimit) ||(ADC_Average1<Lowerlimit))
{
LED=0;
LED1=0;
}
else
{
GP0=1;
__delay_ms(500);
GP0=0;
LED=1;
LED1=1;
}
}
}
}
}
No comments:
Post a Comment