Jump to content
Sign in to follow this  
Joan

16F873 ADC conversion at RA0

Recommended Posts

Hi all;

 

I want to learn how to do an ADC conversion without interrupts form RA0 with 16F873 uC. Exactly, I want to put 8 less significative bits of ADC(RA0) conversion into PORTB once at second.

 

Anybody have an example?

 

Regards;

 

Joan :D

Share this post


Link to post
Share on other sites

Hi Joan,

 

this is my function to read ADC in polled mode:

 

unsigned char Read_ADC_8(unsigned char ADC_Ch)

{

adcon1 &= ~0x80;/* ADFM left justified */

adcon0 = (ADC_Ch << 3) + 01000001b; /* Selecting ADC channel Enable ADC*/

delay_us(100); /* Aquisition time */

adcon0 |= 0x04;   /* Staring the ADC process */

while(adcon0 & 0x04) {;} /* Wait for conversion complete */

return (adresh);     /* Return 8 MS-Bits of the ADC process */

}

Share this post


Link to post
Share on other sites

Hi Joan,

 

this is my function to read ADC in polled mode:

 

unsigned char Read_ADC_8(unsigned char ADC_Ch)

{

adcon1 &= ~0x80; /* ADFM left justified */

adcon0 = (ADC_Ch << 3) + 01000001b;

   /* Selecting ADC channel Enable ADC*/

delay_us(100);     /* Aquisition time */

adcon0 |= 0x04;   /* Staring the ADC process */

while(adcon0 & 0x04) {;}

            /* Wait for conversion complete */

return (adresh);/* Return 8 MS-Bits of the ADC process */

}  

 

In the main loop it is called like this:

ad_result_0 = (Read_ADC_8(0));

 

Now you just have to store ad_result_0 in portb.

I hope this will help .....

 

Regards

 

Uwe

java script:emoticon(':laugh:')

Share this post


Link to post
Share on other sites

Hi uwe;

I tried with this code ... (I used it with 16F873 uC)

 

///////////////////////////////////////////////////////////////

 

#include

 

unsigned char ad_result_0;

 

char cont;

 

unsigned char Read_ADC_8(unsigned char ADC_Ch)

{

adcon1 &= ~0x80; /* ADFM left justified */

adcon0 = ((ADC_Ch << 3) + 01000001b); /* Selecting ADC channel Enable ADC*/

delay_us(100);     /* Aquisition time */

adcon0 |= 0x04;   /* Staring the ADC process */

while(adcon0 & 0x04) {;} /* Wait for conversion complete */

return (adresh); /* Return 8 MS-Bits of the ADC process */

}  

 

 

 

main()

{

// Hardware Init

trisa=00111111b; // PORTA = Input  [Output=0;Input=1]

trisb=0x00; // PORTB = Output

trisc=0x00; // PORTC = Output

 

adcon1 = 10000000b; // PORTA(RA0...RA5)=ANALOG_IN

// +Vref= Vdd [5V]; -Vref=Vss [GND]

 

adcon0 = 01000000b; // ADC(RA0)

// Clock= 8*Tosc(XX); Channel AN0(XXX);(Go)(X);(-);(ADON) (X)

 

cont=0;

portb=0;

portc=0;

 

while(1)

{

ad_result_0 = (Read_ADC_8(0)); //In the main loop it is called like this

cont++;

portc = cont;

portb=ad_result_0;

 

}

}

//////////////////////////////////////////////////////////////

 

but It doesn't works. When I run the simulator seems that there is an infinite loop at the instruction

 

           while(adcon0 & 0x04) {;}

 

Variable "cont" do not increments and PortB=0 allways.

I think there is something I do wrong but I don't know what.

Somebody knows what's happenning?

 

Regards;

 

Joan.

???

Share this post


Link to post
Share on other sites
Guest Dave

Looks like the ADC simulation may have a problem.

I'm currently investigating.

 

Have you tried this code  on the actual device?

 

Regards

Dave

Share this post


Link to post
Share on other sites

Hi Joan, Hi Dave,

 

I use this function on PIC16F873,  PIC16F876(A) and  PIC16F877

and it works fine.

 

I also tried the code you posted on a 16F876 on the board that came withe ICD1 debugger and it runs.

 

There are 2 differences to your code :

1) i have included the

2) I had to use portb for the AD result because the LED's  are on portb

 

Have you got a bord to test with ?

 

Regards

Uwe

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...