I am using a 16F628A to go through Nigel's excellent tutorials;
in particular, I am in his second tutorial.
When I compile his code into hex and program it into the chip,
it works absolutely fine... but I am having trouble understanding
the bank selection.
In Tutorial 2.1, the start of his code is the following
Code:
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
movlw 0x07
movwf CMCON ;turn comparators off (make it like a 16F84)
bsf STATUS, RP0 ;select bank 1
movlw b'11110000' ;set PortA 4 inputs, 4 outputs
movwf LEDTRIS
bcf STATUS, RP0 ;select bank 0
clrf LEDPORT ;set all outputs low
BTW, earlier in the .asm file, Nigel had defined
Code:
LEDPORT Equ PORTA ;set constant LEDPORT = 'PORTA'
LEDTRIS Equ TRISA ;set constant for TRIS register
When Nigel does the BSF STATUS, RP0, how come he doesn't also do a BCF STATUS, RP1?
I'm confused as to how he knows that he is selecting bank 1... couldn't
you also be selecting bank 3 depending upon the values in the register
STATUS when the chip powers up?
I'm confused as to how he knows that he is selecting bank 1... couldn't
you also be selecting bank 3 depending upon the values in the register
STATUS when the chip powers up?
Because a PIC always powers up in bank 0, if you wanted to you could add the extra lines, it wouldn't do any harm (except for using a little space) - but it's not required.
New and old programmers alike can get into trouble with bank selection... Basically, you need to make sure you're in the correct bank for every instruction that accesses a GPR file register (General Purpose Register or RAM) or SFR file register (Special Function Register)... I've been programming awhile but I still find it helpful to track the current bank at the end of each comment line... It makes it much easier for me to verify correct banking when I'm debugging programs...
Code:
org 0x0000 ;org sets the origin, 0x0000 for the 16F628,
;this is where the program starts running
movlw 0x07 ; |B0
movwf CMCON ;turn comparators off (make it like a 16F84) |B0
bsf STATUS, RP0 ;select bank 1 |B1
movlw b'11110000' ;set PortA 4 inputs, 4 outputs |B1
movwf LEDTRIS ; |B1
bcf STATUS,RP0 ;select bank 0 |B0
clrf LEDPORT ;set all outputs low |B0
as noticed, it is a common practise to clear registers and for this eg ledport too when switching back to bank 0, any reason why so?is this a must or a good prging practise?
as noticed, it is a common practise to clear registers and for this eg ledport too when switching back to bank 0, any reason why so?is this a must or a good prging practise?
It depends on what you are doing, if you are specifically writing a value to the registers and ports than there's no point in clearing them. What possible advantage would the following have?.
Code:
clrf PortB
movlw 0x01
movwf PortB
However, if you're not specifically writing values to them it's probably a good idea to clear them - but in any case, it's not a good idea to assume they may be clear if you don't specifically do it!.