0

I used PIC16F877 and my purpose is to choose CHANNEL 4 to display the analogue input AN4 value on PortD leds. The approximate value is about 1V. I wrote a code and however, no matter how I ran my code, there're not reaction with the GPIO monitor. By the way, do I write ReadADC1() in the while(1){} loop? I tried that, but there's no help. Thanks.

#include <xc.h>
#define LEDs    PORTD   
#include "prologue.c"
unsigned char ReadADC1(void) {
    ADCON0 |= 0b00000010;
    while ( (ADCON0 & 0b00000010) );
    return ADRESH;
}

main ()
{
//  declare variables if any required
    
    TRISA= 0B00100000;   
    ANSEL=0B00010000;
    
    ADCON0 = 0b11010001;
    ADCON1 = 0b10000000; 
    
    LEDs=ReadADC1();

    //*** your code for initialisation if required

    //*** end of your initialisation

    //***  your code for the superloop
    while (1) {

    }
    
    //*** end of the superloop
} 

There's no reaction with the GPIO pins monitor. I restarted the IDE many times.

1 Answer 1

0

By default all ports are configured as an input. If you want want use a port as an output you had to change the configuration:

TRISD = 0x00;

Another issue: There is no ANSEL register in this controller, you had to do the selection (digital or analog input) with ADCON1register.

Sign up to request clarification or add additional context in comments.

5 Comments

Hello, ANSEL I used is to select the analogue input. Could you explain the selection with ADCON1 further? Thanks.
I've checked that. My code ADCON1 = 0b10000000 has set the analogue input.
Thats right, You don't need ANSEL
This question was asked on the Microchip forum here, as the OP never confirmed they were actually using the ancient PIC16F877 controller the thread has gone dormant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.