[Help] Using PIC to provide 5V

Thread Starter

VoltVolt

Joined Jan 25, 2013
12
I'm using PIC18F45K22
My circuit is just connected to my 5 ICSP Header at PICKIT2

Problem: Suppose to get 5V from all pins(except MCLR and VSS)?

My code:

Rich (BB code):
#include <htc.h>

/* Start of configuration fuses*/
#pragma config IESO=OFF, FOSC=INTIO67,PRICLKEN=OFF,FCMEN =OFF,PLLCFG=ON,BOREN=ON,BORV=250
#pragma config WDTEN=OFF
#pragma config P2BMX=PORTC0,CCP2MX=PORTC1,PBADEN=OFF,CCP3MX=PORTE0,MCLRE=INTMCLR,HFOFST=OFF,T3CMX=PORTC0
#pragma config DEBUG=OFF,STVREN=ON,XINST=OFF,LVP=OFF
#pragma config CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF
#pragma config CPB=OFF,CPD=OFF
#pragma config WRT0=ON,WRT1=ON,WRT2=ON,WRT3=ON
#pragma config WRTB=ON,WRTC=ON,WRTD=ON
/* end of configuration fuses */

void main()
{
    OSCCON=0b11110011;
    OSCCON2=0b00000000;

    ANSELA = 0x00;      //Configure all ports as I/O digital
    ANSELB = 0x00;
    ANSELC = 0x00;
    ANSELD = 0x00;
    ANSELE = 0x00;

    TRISA=TRISB=TRISC=TRISE=1;      //set all ports as input

    LATA = LATB = LATC = LATD = LATE = 0xFF;
}
 
Last edited by a moderator:

absf

Joined Dec 29, 2010
1,968
How to get 5V if all the ports are set to input? They would be all in high impedance state. Try set them to outputs and see what happens.

Allen
 

John P

Joined Oct 14, 2008
2,026
By saying TRISA = 1, what you're actually doing is setting the TRIS values to 0b00000001. They're 8-bit bytes, not a single bit each!

That seems to be a mistake, but it should be setting all the pins as outputs, except bit 0 in each port. I'm not sure how the use of the latches works. Is that equivalent to writing directly to the ports on processors without latches?
 

JohnInTX

Joined Jun 26, 2012
4,787
I'm not sure how the use of the latches works. Is that equivalent to writing directly to the ports on processors without latches?
In the 18Fxxx, writing to either PORT or LAT does the same thing. r-m-w e.g. bsf to an IO should always be done on LAT.

If the code runs, it should output 1 on the ports.
Rather than exiting main(), there should be a while(1) at the end of the IO init.

The OP should examine the config bits in MPLAB after building to be sure that the various pragmas have set up the oscillator to work in the circuit along with other configs. I don't see INT067 in the datasheet or the .h file for the PIC.

Be sure the PICKIT is set to supply Vdd if the board doesn't. Verify Vdd / Gnd /MCLR=1
 

Thread Starter

VoltVolt

Joined Jan 25, 2013
12
Thank you for the advises...
I have changed ''TRISA=TRISB=TRISC=TRISE=0xFF"
Now suppose to get all pins 5V,
but now left PORTD(RA0~RA7) couldn't get 5V...
Why?
 

joeyd999

Joined Jun 6, 2011
5,283
In the 18Fxxx, writing to either PORT or LAT does the same thing...
Does not!

Writing to LAT reads the latch first, not the port. Subsequently, both instructions then write to the latch.

This has a significant impact if some of the port pins are set to hi-z prior to the write instruction.

Yet another issue arises with two or more successive writes to PORT if one or more of the port pins is heavily capacitively loaded. In this case, the port pin may not rise (or fall) to the correct value prior to being read again.

Writing to LAT resolves both these issues.

EDIT: Sorry! Misread your post. Yes, a simple write is the same for LAT or PORT. R-M-W instructions are different, as you said. They should address the LAT not the PORT, for the reasons I stated. My bad.
 
Last edited:
Top