[Help] Using PIC to provide 5V

Discussion in 'Embedded Systems and Microcontrollers' started by VoltVolt, Sep 15, 2013.

  1. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    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:

    Code ( (Unknown Language)):
    1.  
    2. [FONT="Courier New"]#include <htc.h>
    3.  
    4. /* Start of configuration fuses*/
    5. #pragma config IESO=OFF, FOSC=INTIO67,PRICLKEN=OFF,FCMEN =OFF,PLLCFG=ON,BOREN=ON,BORV=250
    6. #pragma config WDTEN=OFF
    7. #pragma config P2BMX=PORTC0,CCP2MX=PORTC1,PBADEN=OFF,CCP3MX=PORTE0,MCLRE=INTMCLR,HFOFST=OFF,T3CMX=PORTC0
    8. #pragma config DEBUG=OFF,STVREN=ON,XINST=OFF,LVP=OFF
    9. #pragma config CP0=OFF,CP1=OFF,CP2=OFF,CP3=OFF
    10. #pragma config CPB=OFF,CPD=OFF
    11. #pragma config WRT0=ON,WRT1=ON,WRT2=ON,WRT3=ON
    12. #pragma config WRTB=ON,WRTC=ON,WRTD=ON
    13. /* end of configuration fuses */
    14.  
    15. void main()
    16. {
    17.     OSCCON=0b11110011;
    18.     OSCCON2=0b00000000;
    19.  
    20.     ANSELA = 0x00;      //Configure all ports as I/O digital
    21.     ANSELB = 0x00;
    22.     ANSELC = 0x00;
    23.     ANSELD = 0x00;
    24.     ANSELE = 0x00;
    25.  
    26.     TRISA=TRISB=TRISC=TRISE=1;      //set all ports as input
    27.  
    28.     LATA = LATB = LATC = LATD = LATE = 0xFF;
    29. }[/FONT]
    30.  
     
    Last edited by a moderator: Sep 15, 2013
  2. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    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
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    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?
     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    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
     
  5. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    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?
     
  6. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    TRISx must be 0x00 for outputs. TRISx=0xFF makes them all inputs. Kapish?
     
  7. VoltVolt

    Thread Starter New Member

    Jan 25, 2013
    12
    0
    Thanks...
    My problem solved... :)
     
  8. joeyd999

    AAC Fanatic!

    Jun 6, 2011
    2,689
    2,750
    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: Sep 16, 2013
Loading...