Hot!PORT RA4 OUTPUT of PIC18F46K22

Author
Laslite
New Users
  • Total Posts : 42
  • Reward points : 0
  • Joined: 2016/02/26 23:45:03
  • Location: 0
  • Status: offline
2018/01/13 03:39:34 (permalink)
0

PORT RA4 OUTPUT of PIC18F46K22

Hello,
I´m using the PIC18F46K22 and want to config the RA4 port as output.
What do I have to disable or change, that the RA4 works only as output.
Can anybody help me?

This is my init code:

//*********************************************************************************************************
#pragma config PLLCFG=ON,FOSC=INTIO67,PRICLKEN=OFF,IESO=OFF,FCMEN=OFF,PWRTEN=ON,BOREN=SBORDIS,PBADEN=OFF,MCLRE=EXTMCLR
#pragma config STVREN=ON,WDTEN=SWON,WDTPS=32768,LVP=OFF,DEBUG=OFF,CP0=ON,CP1=ON,CP2=ON,CP3=ON,CPB=ON,CPD=ON
#pragma config WRT0=OFF,WRT1=OFF,WRT2=OFF,WRT3=OFF,WRTB=OFF,WRTC=OFF,WRTD=OFF
#pragma config EBTR0=OFF,EBTR1=OFF,EBTR2=OFF,EBTR3=OFF,EBTRB=OFF
//*********************************************************************************************************
  OSCCON  = 0b01110000;                 //Interner 16MHz Oszilator mit 4xPLL => 64MHz    (0b01110000;)
  OSCTUNE = 0b01000000;                 //Interner 16MHz Oszilator mit 4xPLL => 64MHz    (0b01000000;)

  ADCON0= 0b00000000;                   //ADC disabled
  ADCON1= 0b00000000;                   //ADC-REF = +5V,GND
  PORTA = 0b00100000;                    //PORTA
  TRISA = 0b00000001;                    //PORTA
  ANSELAbits.ANSA0 = 1;
 
  PMD1 = 0xFF;
  SRCON0 = 0;
  SRCON1 = 0;
  PWM1CON = 0;
  CCP2CON = 0;

  CCP5CON = 0x08;
  CCPR5L = 0;
  CCPR5H = 0;
  CCPTMRS1bits.C5TSEL = 0;

  PORTB = 0b00000000;                    //PORTB
  TRISB = 0b00000000;                    //PORTB

  PORTC = 0b00011000;                    //PORTC
  TRISC = 0b10011000;                    //PORTC
  ANSELCbits.ANSC5 = 0;
  ANSELCbits.ANSC6 = 0;
  ANSELCbits.ANSC7 = 0;

  PORTD = 0b00000000;                    //PORTD
  TRISD = 0b00000000;                    //PORTD

  PORTE = 0b00000011;                    //PORTE
  TRISE = 0b00000000;                    //PORTE

  WDTCONbits.SWDTEN = 1;    //Now start Watchdog-Timer
  T0CON   = 0b00000000;        //TO = off, 16 BIT MODE, clock from CPU, Prescaler (PSA) = on, Prescaler 1:2 (000)
  INTCON  = 0b11100000;        //Enable TIMER0 Interrupt (GIE freigegeben, PEIE freigegeben, Timer0 freigegeben)
  TMR0H = 0xA0;                     //Timer0 Reload
  TMR0L = 0x40;             
  T0CONbits.TMR0ON = 1;        //Now start Timer0


#1

14 Replies Related Threads

    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/13 03:59:25 (permalink)
    0
    I see you are using CCP5 in capture mode.
    Does that mean you are trying to manually trigger the capture?
    (Yes, this should be possible, I just want to confirm that is your aim.)
     Edit. Ignore the above, I was off by one bit.
     
    What test have to done to determine if this is working or not?
    It's hard to comment further when you only show a small part of your code. There's many more details we can only guess, like if you are correctly handling the WDT, seeing as you are enabling it...
     
    post edited by qɥb - 2018/01/13 04:22:06

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #2
    mbrowning
    Just a Member
    • Total Posts : 1092
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/13 04:13:26 (permalink)
    +1 (1)
    CCP5CON=0x08 sets RA4 as the compare output. Set to 0x0a would enable compare mode but leave RA4 as gpio

    Go Navy! Beat Army!
    #3
    Aussie Susan
    Super Member
    • Total Posts : 3331
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/14 18:17:22 (permalink)
    0
    Side issue but do not specify any setting for the DEBUG config item. This is controlled by the IDE depending on whether you are doing a debug or release compile/build.
    Susan
    #4
    Laslite
    New Users
    • Total Posts : 42
    • Reward points : 0
    • Joined: 2016/02/26 23:45:03
    • Location: 0
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/15 10:57:07 (permalink)
    0
    Thanks for your replys and help,

    I changed the CCP5CON = 0x08; to CCP5CON = 0x0a; but nothing happens.
    I start den WDT with WDTCONbits.SWDTEN = 1;    //Now start Watchdog-Timer
    The WDT runs continiously and I uptdate it with _asm clrwdt _endasm in the main while(1)
    The LED at RA4 is pulled up to 5V with 560 Ohm and it only flashes with a few milliseconds when I switch on the RA4.
    Do the functions of RA4 (C1OUT, SRQ, CCP5 and T0CKI) are blocking the port?
    Do I have to disable them and how?


    #5
    mbrowning
    Just a Member
    • Total Posts : 1092
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/15 12:56:45 (permalink)
    0
    Possibly some RMW problem. You're using PORT registers to write to in the bit of code you showed. If you are using PORTA (maybe PORTAbits.PORTA4) to control the LED, then maybe a read-modify write problem. change PORTx to LATx for all writes.
     
    It seems that you are disabling the alternate RA4 functions, so I can't think of what else might be the problem.

    Go Navy! Beat Army!
    #6
    Laslite
    New Users
    • Total Posts : 42
    • Reward points : 0
    • Joined: 2016/02/26 23:45:03
    • Location: 0
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/16 09:13:22 (permalink)
    0
    Sorry, I didn´t send the information about the definition of RA4 port:     #define LED LATAbits.LATA4
    If I compile with MPLAB and start it, the LED still flashes for one time when I set the port.

    Today I found a solution, but only when the PIC runs without MPLAP.
    =======================
    In ADCON1 I set bit7 to 1
    ADCON1= 0b10000000;
    =======================

    It is a solution for the moment. I prefer the complete function with MPLAB.
    I´m sure that there is still somthing which blocks the port.
    If someone has any ideas I would be very happy Smile: Smile

    Thanks
    #7
    jack@kksound
    code tags!
    • Total Posts : 2761
    • Reward points : 0
    • Joined: 2014/05/14 10:03:19
    • Location: 0
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/16 12:23:57 (permalink)
    0
    ADCON1 bit 7 controls the selction of the Special Event Trigger for the A/D converter, it sets the A/D trigger to the CTMU instead of the CCP5 module. Sounds like you need to disable the special event trigger in the CCP5 module: see CCP5CON register CCP5M control bits <3:0>
    #8
    PStechPaul
    Super Member
    • Total Posts : 2041
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/16 13:22:58 (permalink)
    0
    You may need to clear the ANSELA register and then set the bit for ANSA0:
     
    ANSELAbits.ANSA0 = 1;


     
    #9
    Aussie Susan
    Super Member
    • Total Posts : 3331
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/16 20:18:31 (permalink)
    +2 (2)
    Laslite
     
    Today I found a solution, but only when the PIC runs without MPLAP.

    If by running without the MPLABx you mean that you are running a 'release' mode build and not when you are running a 'debug' build then the explanation is as follows.
    When you use 'release' mode, the program is built so that it will run when you power up the device/
    However in 'debug' mode, additional code is added to your app that is run when the device is reset. This additional code - called the 'debug kernel' - is what talks between your deice and the MPLABx IDE to actually control the debugging process.
    One peculiarity of the 'debug kernel' is that it will always set all pins with analog capability to 'digital' mode, whereas in 'release' mode the MCU will go to the normal reset state of 'analog'.
    The bottom line is that you should ALWAYS set the analog-capable pins to the desired state during the initialisation phase of your app.
    Susan
    #10
    SpokaneNexus
    Super Member
    • Total Posts : 386
    • Reward points : 0
    • Joined: 2013/02/05 14:53:25
    • Location: 0
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/17 11:13:34 (permalink)
    0
    PStechPaulYou may need to clear the ANSELA register and then set the bit for ANSA0

    My first thought as well, but RA4 on that part doesn't have an analog function. So much for that easy fix, darn it.
     
    lasliteDo the functions of RA4 (C1OUT, SRQ, CCP5 and T0CKI) are blocking the port?

    When any of those peripherals are enabled, yes - they will "take over" the pin. But most peripherals are disabled by default at reset so unless you're intentionally enabling them (as you do with CCP5) they shouldn't cause you problems. As noted by others, CCP5 optionally uses the same pin as RA4 so you must be certain to use a mode for CCP5 that leaves the pin as standard I/O (or choose a different capture/compare peripheral, this particular MCU has quite a few).
    #11
    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/17 11:54:27 (permalink)
    0
    Laslite
    Sorry, I didn´t send the information about the definition of RA4 port:     #define LED LATAbits.LATA4

    What about the OTHER pins on PORTA?
    Are you referring to all of them via the LATA port?
    Just writing to any one of them via PORTA rather than LATA can cause mysterious problems.
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #12
    Laslite
    New Users
    • Total Posts : 42
    • Reward points : 0
    • Joined: 2016/02/26 23:45:03
    • Location: 0
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/22 00:13:48 (permalink)
    0
    I´m using the ports RA1 - RA5 as outputs and they are defined like follows:

    #define LED1 LATAbits.LATA1
    #define LED2 LATAbits.LATA2
    #define LED3 LATAbits.LATA3
    #define LED4 LATAbits.LATA4
    #define LED5 LATAbits.LATA4
    #13
    qɥb
    Monolothic Member
    • Total Posts : 3329
    • Reward points : 0
    • Joined: 2017/09/09 05:07:30
    • Location: Jupiter
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/22 00:20:35 (permalink)
    0
    and nothing else in your code tries to write to PORTAbits?
     

    This forum is mis-configured so it only works correctly if you access it via https protocol.
    The Microchip website links to it using http protocol. Will they ever catch on?
    PicForum "it just works"
    #14
    Laslite
    New Users
    • Total Posts : 42
    • Reward points : 0
    • Joined: 2016/02/26 23:45:03
    • Location: 0
    • Status: offline
    Re: PORT RA4 OUTPUT of PIC18F46K22 2018/01/22 00:22:52 (permalink)
    0
    No, nothing else
    #15
    Jump to:
    © 2018 APG vNext Commercial Version 4.5