Jump to content
Sign in to follow this  

16F877 ADC

Recommended Posts



Like in many other cases of life it is simple when you know how to. I embarked on an identical mission two nights ago and managed to get it to work today.


See my P2C program below.


Program Test876;


#pragma CLOCK_FREQ 4000000              // Change to match xtal frequency





 ADCon0@0x1F,ADCon1@0x9F,              // ADC registers




 Disable_Interrupt('GIE');             // Initialization of registers

 Set_Tris_A(11111111b);                // Analogue input on PortA_01

 Set_Tris_B(00000110b);                // Input switches selecting ADC ch. and H/L, PortB_21

 Set_Tris_C(00000000b);                // ADC result is displayed on PortC


 // ADC Setup --- see datasheet page 113 -> for more information ----------------------------------------

                                       // Configure ADC module

 //Set_Bit(STATUS,'RP0');                // Set Data Mem Bank 1

 ADCon1:=10000000b;                    // Result left or right justified (ADCon1_7)

                                       // Analogue pins/voltage ref and digital I/O (ADCon1_3210)

                                       // ADCon1_654 not used


 //Clear_Bit(STATUS,'RP0');              // Set Data Mem Band 0                        

 ADCon0:=01000001b;                    // ADC conversion clock (ADCon0_76)

                                       // ADC input channel (ADCon0_543)

                                       // Turn ADC on (ADCon0_0)

                                       // ADCon0_1 not used

 While 1 do


   If Input_Pin_port_B(2)=0 then

     Clear_Bit(ADCon0,3);              // Select RA0


     Set_Bit(ADCon0,3);                // Select RA1


   Set_bit(ADCon0,2);                  // Start ADC conversion, ADCon0_2 = 1


   While ((ADCon0 and 4)=4) do nop;    // ADC conversion complete ones ADCon0_2 = 0


   If Input_pin_port_B(1)=0 then       // Lower part of result is default


     //Set_Bit(Status,'RP0');            // Set Data Mem Bank 1

     Output_Port_C(ADResL);            // Show low order result on PortC

     //Clear_Bit(Status,'RP0');          // Set Data Mem Bank 0



     Output_Port_C(ADResH);            // Show high order result on PortC








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.

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.

Sign in to follow this  

  • Create New...