ADC 16f877 code beginner help

Status
Not open for further replies.

maria258

Member level 2
Joined
Feb 10, 2011
Messages
42
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,288
Activity points
1,577
i am new using microcontrollers. at the moment im starting using 16f877 and need to start by programming the adc (10 bit). all i did for now is the code shown below. is it enough to read data? or does it need alteration?
i am using mplab to compile and proteus for schematics.
thanks
maria



#include <htc.h>
#include <pic.h>

unsigned int data;

void main(void)
{
//configuring input/outputs
PORTA = 0x00;
TRISA = 0xFF;
TRISB = 0x00;

//configuring ADC
ADCON0=0b00000001 //setting ADON on
ADCON1=0b10000000 //ADFM is set right


while(1)
{
ADCON0|= GODONE; //start conversion
{
break
}

if (ADRESH == 1)
{
data = ADRESL + 256;
}
else if (ADRESH == 2)
{
data = ADRESL + 512;
}
else if (ADRESH == 3)
{
data = ADRESL + 768;
}
else if (ADRESH == 0)
{
data = ADRESL;
}
}
}
 

Hi,
You can change this bit of code:
Code:
if (ADRESH == 1)
{
data = ADRESL + 256;
}
else if (ADRESH == 2)
{
data = ADRESL + 512;
}
else if (ADRESH == 3)
{
data = ADRESL + 768;
}
else if (ADRESH == 0)
{
data = ADRESL;
}
to
Code:
data = (ADRESH<<8)+ADRESL;

Reading data is all you need. Alteration depends on your application and how the data must be processed.

In your sample code, you might want to output the ADC value to some port(s) and display the value using LEDs to understand what value it is holding. Or you could send it via hyperterminal or display on LCD or 7-segment, but I think the LED solution would be easiest at this point.

Hope this helps.
Tahmid.
 
Why do you do 8 instead of all the long code that i wrote?

i cant understand that part?
the remaining is good then?
 
<<8 means *256
So, when ADRESH shows 1, it adds 256 to ADRESL, 512 to ADRESL when ADRESH contains 2 and 768 to ADRESL when ADRESH contains 3. Basically, they are both the same thing.

Hope this helps.
Tahmid.
 
ok thank you very much for your help. i appreciate you spent some time to help me.
 

Just to clarify things, << means bit shift left. So, << 8 is bit shift left 8 times, ie, multiply by (2^8). So <<2 means multiply by 4.
The same way >> means bit shift right. So >>8 means bit shift right 8 times, ie, divide by (2^8)

Hope this helps.
Tahmid.
 
Last edited:
yea yea thanks for your help
 

hi..
i am also using 16f887a,but i am not getting the output...
please help me
my code is:
#include<pic.h>
void serial()
{
SPBRG=25;
SYNC=0;
BRGH=1;
SPEN=1;
TXEN=1;
}
int ADCvalue = 0;
void main(void)
{
serial();
TRISA =0x0FF;
TRISD=0x00;
TRISE=0x00;
ADCON0 = 0x05;
ADCON1 = 0x0CE;
while(1)
{
PORTA=0X01;
PORTD=0X00;
PORTE=0X03;
ADON=1;
GODONE=1;
while(GODONE!=0);
ADCvalue = (ADRESH*256) + ADRESL;
if(ADCvalue >= 204)
{ TXREG = '1';
while(TXIF==0);
}
else
{
TXREG = '1';
while(TXIF==0);
TXREG = 'L';
while(TXIF==0);
}
if(ADCvalue >= 409)
{
TXREG = '2';
while(TXIF==0);
}
else
{
TXREG = '2';
while(TXIF==0);
TXREG = 'L';
while(TXIF==0);
}
if(ADCvalue >= 614)
{
TXREG = '3';

while(TXIF==0);
}
else
{ TXREG = '3';
while(TXIF==0);
TXREG = 'L';
while(TXIF==0);
}
if(ADCvalue >= 819)
{
TXREG = '4';
while(TXIF==0);
}
else
{
TXREG = '4';
while(TXIF==0);
TXREG = 'L';
while(TXIF==0);
}
if(ADCvalue >= 1023)
{ TXREG = '5';
while(TXIF==0);
}
else
{
TXREG = '5';
while(TXIF==0);
TXREG = 'L';
while(TXIF==0);
}
ADON=0;
}
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…