Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 12th March 2005, 07:21 AM   (permalink)
kcn
New Member
kcn is on a distinguished road
Default Question about bank selection, 16F628A, Nigel's tutorial

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?

Thank you.

kcn
kcn is offline   Reply With Quote
Old 12th March 2005, 08:10 AM   (permalink)
Super Moderator
 
Nigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to behold
Default Re: Question about bank selection, 16F628A, Nigel's tutorial

Quote:
Originally Posted by kcn
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Old 12th March 2005, 09:47 AM   (permalink)
Experienced Member
Mike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to allMike, K8LH is a name known to all
Default

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
Best wishes... Regards, Mike
Mike, K8LH is online now   Reply With Quote
Old 12th March 2005, 03:22 PM   (permalink)
New Member
ryan_ryan is on a distinguished road
Default

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?
ryan_ryan is offline   Reply With Quote
Old 13th March 2005, 06:23 AM   (permalink)
Super Moderator
 
Nigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to behold
Default

Quote:
Originally Posted by ryan_ryan
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!.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes




All times are GMT. The time now is 02:47 AM.


Electronic Circuits  |  Radio Controlled
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.