• 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.

Setting Special Function Registers in PIC18F26K20 and XC8 C

Status
Not open for further replies.

Flyback

Well-Known Member
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
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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;)
Nope.. Disabled by default
His code does not have any mention of the ANSEL, ANSELH, INTCON or INTCON3 registers.
Is this allright?
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!!
 

Pommie

Well-Known Member
Most Helpful Member
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.
 

Flyback

Well-Known Member
If only port a are adc inputs, then the PBADEN bit in the config reg will shut off portb..
..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?
Best to always turn off analogue when not needed.
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)

If only port a are adc inputs, then the PBADEN bit in the config reg will shut off portb..
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:

Pommie

Well-Known Member
Most Helpful Member
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.
 

Flyback

Well-Known Member
PBADEN in CONFIG Switches portb to digital.. period.. Once set you cannot use portb for analogue until you unset this fuse
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?
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).
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?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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?
PBADEN is only for the ANSELH... You are correct! ANSEL still needs setting
 

Flyback

Well-Known Member
You are correct! ANSEL still needs setting

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?
 

Flyback

Well-Known Member
Page 136 of the PIC18F26K20 datasheet tells us this about ANSEL….

The state of the ANSx bits has no affect on digital
output functions. A pin with the associated TRISx bit
clear and ANSx bit set will still operate as a digital
output but the input mode will be analog. This can
cause unexpected behavior when performing readmodify-
write operations on the affected port.
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
 

jpanhalt

Well-Known Member
Most Helpful Member
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
 

Flyback

Well-Known Member
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.
 

jpanhalt

Well-Known Member
Most Helpful Member
If you just remember to "read a port and write to latch" (source: Microchip forums), it will solve most RMW problems.

John
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top