![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | Thread Tools | Display Modes |
| | (permalink) |
| New Member | 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 Code: LEDPORT Equ PORTA ;set constant LEDPORT = 'PORTA' LEDTRIS Equ TRISA ;set constant for TRIS register 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? Thank you. kcn |
| | |
| | (permalink) | |
| Super Moderator | Quote:
| |
| | |
| | (permalink) |
| Experienced Member | kcn, 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 |
| | |
| | (permalink) |
| New Member | 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? |
| | |
| | (permalink) | |
| Super Moderator | Quote:
Code: clrf PortB movlw 0x01 movwf PortB | |
| | |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |