ADC collecting the ADC value from the AN channel.

Thread Starter

mrasheed

Joined Apr 15, 2014
5
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:

ErnieM

Joined Apr 24, 2011
8,377
You need to add something to turn the LED off when that is desirable.

But first you need to put the ADC_Read() insire a loop that repeats. "do..." only runs once, then smacks into an infinite while(1) loop.

So delete the "do" and put the while there instead.

And for all that is holy never ever post code without code tags.
 

Thread Starter

mrasheed

Joined Apr 15, 2014
5
thanx for the reply..
did that but still does not work... im very new to this can anyone plx help me out

Rich (BB code):
while(1){
ADC_value = GetADCValue(0); //Reading Analog Channel 0


if (ADC_value>Temp35){
RB0=1;
}
else
RB0=0;
__delay_ms(30); //Delay
}
 

ErnieM

Joined Apr 24, 2011
8,377
Unless it's a typo, you are missing an open brace after "else". And the corresponding close brace.
That's not going to matter... the else sets the output low, then either way a delay is encountered. That seems (perhaps accidentally) correct as the two paths from the if can both be 1 statement long.

did that but still does not work
You need an explanation with much more detail than "does not work"

Does it compile? Does it load? Does the LED seem to work? Do you have a schematic to post?

Currently we even have no idea which PIC you use. The more detail you share the easier it is to see the problem(s).
 

fernan82

Joined Apr 19, 2014
26
you can try changing
Rich (BB code):
 return ((ADRESH<<8)+ADRESL;
to:
Rich (BB code):
 return ((((unsigned int)ADRESH)<<8)|ADRESL;
and:
Rich (BB code):
 RB0 = 1;
to

Rich (BB code):
 LATB |= 1;
and see what happens. BTW, your do loop was correct, the only different that makes is when the condition gets checked and since it's always true it makes no difference.
 
Top