0

I am trying to set RB0 - 7 as digital inputs on my PIC16F818 microcontroller. In order to do that I used the following config:

void config() {
    //cmcon = 0x07;     // Sets all IO pins to digital 
    trisa = 0xFF;       // Set porta as inputs
    trisb = 0x00;       // Set portb as outputs
    adcon1 = 0x07;  // For PIC16F84A comment the line above and uncomment this statement
    portb = 0x00;       // Reset value of portb (portb = 0)
    _CONFIG  & _CP_OFF & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _MCLR_OFF & _INTRC_IO & _WRT_ENABLE_OFF  & _PWRTE_OFF;

}

I test this setting by mapping my porta (8 switch block) to portb (8 bit led block), switches 0 - 4 are working correctly, but switches 5-7 are not responding, however bits are set internally by some other PIC functions and output: (b5)0 (b6)0 (b7)1.

Alex2452
  • 324
  • 1
  • 10

2 Answers2

1

This is a common issue people have when using Microchip controllers. There are many places on stackexchange where the mantra is: "write to LATx, read from PORTx"

PORTx gives you the current state of the port.

LATX is the output latch.

During initialization, you should set up the values of TRISx, ANSELx (analogue selection), CNPUx/CNPDx (pull up/down) for every port.

EBlake
  • 735
  • 7
  • 14
0

I'd recommend to use Latch for outputting data to a port.

So use "latb" instead of "portb" in your case.

Note that "latb" could be defined differently, depending on the compiler you are using.

Please let me know if it solved the problem.

Also, For more information about the difference between LATCH and PORT use, refer to this link

Difference between PORT and LATCH on PIC

In this case he asked about PIC18F family, but the answer that is given there is very good for general pic programming (no matter what family it is), so you would benefit from it as well.

Community
  • 1
  • 1