hey guys i need to collect the ADC value from the AN channel and if that value is grater than some other constant than an LED should be turned ON... here is my code plx i heed your help urgent....
Rich (BB code):
#include<htc.h>
#include<pic.h>
#define _XTAL_FREQ 20000000
void ADC_Init()
{
ADCON0 = 0x41; //ADC Module Turned ON and Clock is selected
ADCON1 = 0xC0; //All pins as Analog Input
//With reference voltages VDD and VSS
}
unsigned int ADC_Read(unsigned char channel)
{
if(channel > 7) //If Invalid channel selected
return 0; //Return 0
ADCON0 &= 0xC5; //Clearing the Channel Selection Bits
ADCON0 |= channel<<3; //Setting the required Bits
__delay_ms(2); //Acquisition time to charge hold capacitor
GO_nDONE = 1; //Initializes A/D Conversion
while(GO_nDONE); //Wait for A/D Conversion to complete
return ((ADRESH<<8)+ADRESL); //Returns Result
}
void main()
{
unsigned int a;
PORTB=0x00;
TRISB = 0x00; //PORTB as output
TRISC = 0x00; //PORTC as output
TRISA = 0xFF; //PORTA as input
ADC_Init(); //Initializes ADC Module
do
{
a = ADC_Read(0); //Reading Analog Channel 0
if (a>0100){
RB0=1;
}
__delay_ms(30); //Delay
}while(1); //Infinite Loop
}
Last edited by a moderator: