1. 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.
    Dismiss Notice

Setting Special Function Registers in PIC18F26K20 and XC8 C

Discussion in 'Microcontrollers' started by Flyback, Jun 14, 2017.

  1. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    1,990
    Likes:
    34
    Hello,
    We are using a PIC18F26K20 in our simple DALI dimmable lamp.
    Our software engineer used XC8 C in MPLAB X IDE.

    His code does not have any mention of the ANSEL, ANSELH, INTCON or INTCON3 registers.
    Is this allright?
    We want RA1 and RA2 as analog inputs (ADC ). All the other pins we want as Digital I/O.
    He also said he didn’t need to set the INTCON or INTCON3 registers because he wasn’t using them. (I thought you haD to disable interrupts in the INTCON register if you are not using them?…GIE=0;)

    One of the PCBs is working and some work intermittently, most don’t work.

    PIC18F26K20 Datasheet:
    http://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdf
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Nope.. Disabled by default
    If only port a are adc inputs, then the PBADEN bit in the config reg will shut off portb..

    Looks like you're looking for a fall guy!!
     
    • Like Like x 1
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    If you don't turn off analogue then any read of a port will return zero. So, bsf PORTA,7 will clear all bits except bit 7. Best to always turn off analogue when not needed.

    Mike.
     
    • Like Like x 1
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    1,990
    Likes:
    34

    ..Thanks, some PORTA are ADC inputs, but most aren't, how can the micro know which are and which aren't if we don't tell it via the ANSEL register?
    Thanks, so do you agree that we need to use the ANSEL register to turn analog off on PORTS RA0, RA3, RA5. (since they are Digital I/O.....RA1 and RA2 are used as ADC)

    PBADEN (in CONFIG) concerns setting ports to either analog or digital on reset....our micro does not get reset in our code...so surely PBADEN is not relevant to us?
     
    Last edited: Jun 14, 2017
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,022
    Likes:
    317
    Location:
    Brisbane Australia
    If port A is only used as analogue input and digital output then it will work fine as long as no rmw instructions are executed (No bsf bcf iorf andf etc with destination file). The pic always does a reset at power up hence how the config registers are always relevant.

    Mike.
     
    • Like Like x 1
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Switches portb to digital.. period.. Once set you cannot use portb for analogue until you unset this fuse
     
    • Like Like x 1
  8. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    1,990
    Likes:
    34
    Thanks, and if PBADEN=OFF, then there is no default setting of the ANSEL register, is this right?..and we have to thereby set the ANSEL explicitly ourselves?
    Thanks, how do we know if the compiler puts RMW commands in there or not?

    .....Surely to avoid this potential badness we must set ANSEL and ANSELH explicitly?
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,168
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    PBADEN is only for the ANSELH... You are correct! ANSEL still needs setting
     
    • Like Like x 1
  10. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    1,990
    Likes:
    34

    The software guy tells us that we don’t have to set ANSEL register up because it defaults to analog on power up. And since we only have either analog inputs or Digital outputs on PORTA, there is no reason for us to bother setting up ANSEL..thats what he says.

    All we have to do he says, is do the TRISA command for the PORTA and that’s it

    Is he right?
     
  11. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    1,990
    Likes:
    34
    Page 136 of the PIC18F26K20 datasheet tells us this about ANSEL….

    So supposing the entirety of PORTA is set up as analog…then apparently you can also just clear its TRISA bit and then happily use it as an Digital output….but what does the Read-Modify-Write Mean?

    Ive searched it on google but I cant imagine why anybody would do that…or is it something that goes on deep in the bowels of the microcontroller without the programmer knowing it even happened?

    PIC18F26K20 datasheet:
    http://ww1.microchip.com/downloads/en/DeviceDoc/41303G.pdf
     
  12. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,901
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    Here is a nice discussion by "ric", a frequent contributor to the Microchip forums: http://picforum.ric323.com/viewtopic.php?f=38&t=12

    He explains it better than I could. It is not a question of "why would anybody do that." Many of the common instructions (e.g., bsf, bcf) are RMW instructions. MOVF and MOVWF are notable exceptions. Newer chips have output latches for writing to the ports avoid it. The problem is greatest when a port pin is connected to a capacitative load, as the slows transitions.

    John
     
    • Like Like x 1
  13. Flyback

    Flyback Well-Known Member

    Joined:
    Jan 5, 2007
    Messages:
    1,990
    Likes:
    34
    Thanks Jpanhalt, i read your kind offering....it seems like if a PIC has LAT registers then RMW problems are less likely but still not avoided completely.
    I must admit i am grateful Jpanhalt, because your article there is the first time i have had any inkling of understanding of what RMW is....its about the changing voltages in there , and it being read before the voltage had chance to change.

    I am now thinking we need to use some sort of emulator and step through our code....the non working ones just dim to 75% and wont respond to DALI dimming commands.
     
  14. jpanhalt

    jpanhalt Well-Known Member Most Helpful Member

    Joined:
    Jun 21, 2006
    Messages:
    5,901
    Likes:
    502
    Location:
    Cleveland, OH, USA
    ONLINE
    If you just remember to "read a port and write to latch" (source: Microchip forums), it will solve most RMW problems.

    John
     
    • Like Like x 1

Share This Page