Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Newbie question on Pic18F25k20 - Setting PORTA,B as digital output

Status
Not open for further replies.

hbrault

New Member
Hi all,
I feel pretty silly: I can't turn PORTA and PORTB to all digital output on a Pic25K20!
What did I overlook?
I am new to using C on a Pic, so I thought the problem was C related, therefore I also wrote the program in ASM. Unfortunately the problem remains the same.

Code:
#include <p18cxxx.h>

void main (void)
{
    // Disable all Analog inputs
    ANSEL = 0;
    ANSELH = 0;

    // PORTS A,B and C are all Outputs
    TRISA = 0;
    TRISB = 0;
    TRISC = 0;
    // Set All the bits
    PORTA = 0xFF;   // PORTA is set to 0xD0  instead of 0xFF, LATA too
    PORTB = 0xFF;   // PORTB is set to 0xF6  instead of 0xFF and LATB to 0xE0 
    PORTC = 0xFF;   // Yeah! PORTC and LATC both get 0xFF
    while (1) {}
}

and now for the ASM version (identical code, identical problem)

Code:
    list      p=18f25K20          ; list directive to define processor
    #include <p18F25k20.inc>      ; processor specific variable definitions

ResetCode    org    0x000
    goto    Main

HighInteruptCode    ORG     0x008   ; High Interrupt vector address
    retfie FAST                     ; return from interrupt

LowInteruptCode    ORG     0x0018   ; Low interrupts vector address
    retfie                          ; return from interrupt

Main
    ; Disable all Analog input
    clrf    ANSEL
    clrf    ANSELH

    ;PORTS A,B and C are all Outputs
    clrf    TRISA
    clrf    TRISB
    clrf    TRISC

    ; Set All the bits
    setf    PORTA   ; PORTA is set to 0xD0, LATA too
    setf    PORTB   ; PORTB is set to 0xF6 and LATB to 0xE0
    setf    PORTC   ; Yeah! PORTC and LATC get 0xFF

LoopHere
    bra     LoopHere
    END     ; end of program

I looked at the values of some other related SFR (the ones I could think of):
ADCON0 = 0x00 (ADC is disabled)
ADCON1 = 0x00 (Vref- and Vref+ not used)
CCP1CON = 0x00 (Capture/Compare/PWM off)
CCP2CON = 0x00 (ditto)

I am now running out of ideas, any suggestion will be welcome.

Thanks
 
These newer 18fxxKxx devices have switched some of the standard SFR's around. So clr CM1CON0 and CM2CON0 to get PortA up and running. On PortB its a config thing now, so set PBADEN=OFF to get that to digital output.
 
CM1CON0 and CM2CON0 But the data sheet doesn't say you need to do that for portA

But it looks like they forgot that LOL
 
Last edited:
Huh, both the CMxCON0 and the CCPxCON registers are zero on reset, so that's probably not it. The O.P.'s register read says the analog pins are active PortA = 0xD0 or 0b11010000, so why doesn't clrf ANSEL and clrf ANSELH work? With the larger K22 devices it is ANSELA and ANSELB.

EDIT: I know when writing to a Port you should use LATA=0xFF, not sure if it affects this example though.
 
Last edited:
This is the only thing I see wrong you set tiris first then ANSEL

Code:
#include <p18cxxx.h>
 
void main (void)
{
    // Disable all Analog inputs
    TRISA = 0;
    TRISB = 0;
    TRISC = 0;
    ANSEL = 0;
   
 
    // PORTS A,B and C are all Outputs
   
    // Set All the bits
    PORTA = 0xFF;   // PORTA is set to 0xD0  instead of 0xFF, LATA too
    PORTB = 0xFF;   // PORTB is set to 0xF6  instead of 0xFF and LATB to 0xE0 
    PORTC = 0xFF;   // Yeah! PORTC and LATC both get 0xFF
    while (1) {}
}
 
Last edited:
First, thank you all for your quick replies.
Second, I am a schmuck. I wasted your time and I apologize.

The chip works just fine (at least as expected on something this trivial).
The bug was in the MPLAB simulator, I was using an older version: 8.30, I updated to v8.70 and tada!

And by the way nickelflippr, you are right: there is no need to clear the CMxCON0 and CCPxCON registers: they are 0 on reset.
 
Status
Not open for further replies.
Back
Top