MPLAB problem

Status
Not open for further replies.

islinux

New Member
Hi
I'm trying to run this program from the C18 examples which sends value to PortB
the program is as follows

Code:
#include <p18cxxx.h>

#pragma config WDT = OFF

void main (void)
{
  /* Make all bits on the Port B (LEDs) output bits.
   * If bit is cleared, then the bit is an output bit.
   */
  TRISB = 0;

  /* Reset the LEDs */
  PORTB = 0;

  /* Light the LEDs */
  PORTB = 0x5A;

  while (1)
    ;
}
but the value read from PortB not as expected
i tried to change the written value but here the results:
0x5A --> 0x40
0xFF --> 0xE0
0xAA--> 0xA0

i can't understand why the output looks like that?

I'm using MPLAB IDE with c18
 
What PIC are you using?

Did you remember to set the port to digital. Some are analog by default.
 
Last edited:
Looks like only the upper 3 bits of the port are enabled for general purpose IO.
The reason is that the peripherals on the PIC are still enabled. Two examples are the ADC and the analog comparators. What PIC are you using?
 
As suggested, you have to disable the analog functions on PORTB pins 0 to 4, otherwise those pins are always read as '0'.
Set the ADCON1 register to 0x0F
 
All the Microchip datasheets have an example on how to initialize each port. This is for the 18F1320 PORTB. Even if you can not read PIC assembler you can get a good idea of which ports you need to setup.

Code:
CLRF PORTB        ; Initialize PORTB by
                  ; clearing output
                  ; data latches
CLRF LATB          ; Alternate method
                  ; to clear output
                  ; data latches
MOVLW 0x70      ; Set RB0, RB1, RB4 as
MOVWF ADCON1 ; digital I/O pins
MOVLW 0xCF      ; Value used to
                  ; initialize data
                  ; direction
MOVWF TRISB    ; Set RB<3:0> as inputs
                  ; RB<5:4> as outputs
                  ; RB<7:6> as inputs
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…