Newbie question about switching banks

Status
Not open for further replies.

parts-man73

New Member
You'll have to excuse my "newness" I recently decided to try PICs and teach myself assembly language. I'm not new to microcontrollers in general. I am very active on other forums dedicated to different Microcontrollers. I realize there are "New" people joining forums every day, and they often ask the same "new guy" questions, and sometimes the veteran forum members get tired of answering the same FAQs over and over. If this is such a question, I apologize in advance.

I have never have done assembly. I chose a PIC16F628A to start on. and I'm using MPLAB to build HEX files from my assembly code. One error I continuously get is...

"Register in operand not in bank 0. Ensure that bank bits are correct."

when I use the following code

bsf 03h,5
movlw 00h
movwf 86h
bcf 03h,5

when searching for the cause of this error, I stumbled upon this line in many example code samples...

ERRORLEVEL 0, -302 ;suppress bank selection messages

I never see an explaination as to why this line is included.
So is this a "bug" in MPlab, and there's nothing wrong with my code? or am I doing something wrong that needs to be corrected?

Thank you in advance,
Brian
 
ERRORLEVEL 0, -302 ;suppress bank selection messages
That line shuts off the following warning:
Register in operand not in bank 0. Ensure that bank bits are correct.
The above is a warning, not an error. It tells you to check your bank bits whether you already did or not. Since it can be a bit annoying to be nagged constantly about something you already know you did right, you can suppress the warning.
 
Thank you for the quick reply. As I hate being nagged. I'll turn that off.

I understand now that it's not saying there IS an error, but more like "this kind of operation is were mistakes are often made so pay attention" and I can see why most turn it off. Too bad there wasn't a way to turn it off via an Options menu in MPLAB instead of having to include that line in every program.
 
You need to be writing your code a bit differently. First thing is that it should always have an include file and you should be using the proper equates from the include file. At the top of every asm program you write should be this line:
Code:
	include <P16F628A.INC>
Navigate to (typically) c:\program files\microchip\mpasm suite, open the p16f628a.inc file and have a look (there's an inc file for every model of PIC - use the appropriate one for your PIC). Get in the habit of using those equate symbols instead of the actual hex numbers in your code. Makes the code much more readable and less prone to errors. You'll spend much less time trying to remember what the numbers are and be able to tell at a glance what registers you're manipulating.

Another trick to make things easier to read is to use the banksel directive to generate the bank select code, rather than diddling the bits yourself. Here's an example of what it looks like in a snip of some 16F886 code I recently wrote:
Code:
	list	p=16F886
	include	<p16F886.inc>
	.
	.
	.
	banksel	OSCCON		;bank 1
	movlw	b'01110001'	;8MHz internal clock
	movwf	OSCCON
	movlw	b'00011111'
	movwf	TRISA
	movlw	b'11000000'
	movwf	TRISB
	movlw	b'11011011'
	movwf	TRISC
	movlw	b'10000000'	;config ADC voltage ref and data justification
	movwf	ADCON1	
	banksel	ANSEL		;bank 3
	movlw	b'00000001'
	movwf	ANSEL
	banksel	PORTC		;bank 0
	movlw	b'00000001'	;enable ADC
	movwf	ADCON0

Using the above suggestions, this:
Code:
	bsf	03h,5
	movlw	00h
	movwf	86h
	bcf	03h,5
would look like this:
Code:
	banksel	TRISB	;bank 1
	movlw	0x00
	movwf	TRISB
	banksel	PORTA	;bank 0
or better yet:
Code:
	banksel	TRISB	;bank 1
	clrf	TRISB
	banksel	PORTA	;bank 0
Much clearer what's going on now, right? You'll especially appreciate it when you come back to this code six months later when it's not fresh in your mind anymore. (I changed two lines into one by using clrf instead of the two lines you used.)

You're using the h suffix (00h) for hex, like a Motorola programmer. That's fine too, but most PIC programmers use a 0x prefix (0x00) for hex. Took me a while to get used to that. Either one is fine. If your assembler's radix is set correctly you don't need any prefix or suffix at all, though I still do it just to be absolutely clear. I believe the default radix for MPLAB is hex.
 
Last edited:
Oh! I should point out one little gotcha that MPLAB has.

I'd like to be able to do this:
Code:
init	banksel	OSCCON		;bank 1
	movlw	b'01110001'	;8MHz internal clock
	movwf	OSCCON
and of course I tried it, but MPLAB will give you an illegal label error or something like that. It refuses to allow labels on directive lines. What you have to do to get around it is this:
Code:
init
	banksel	OSCCON		;bank 1
	movlw	b'01110001'	;8MHz internal clock
	movwf	OSCCON
 
Last edited:
Thank you again for the excellent pointers. I just printed the contents of P16F628A.INC so I can study it and know what's contained within. Quite a difference from the high level languages I'm used to, but I think I'm getting the hang of it. I now wish I had tried this a long time ago, it's really not as hard to code as I had feared (but all I've done so far is blink some LEDs)

Brian
 
parts-man73 said:
I just printed the contents of P16F628A.INC so I can study it and know what's contained within.
Everything in that inc file is the same as in the datasheet. Never mind printing the inc file. Print the datasheet! It's much more useful.

Quite a difference from the high level languages I'm used to, but I think I'm getting the hang of it. I now wish I had tried this a long time ago, it's really not as hard to code as I had feared
Yes, people seem to have an irrational fear of assembly. It's not so tough. And it gets easier the longer you use it and build up a collection of code you can re-use (exactly like libraries in C). After a while you'll find yourself grabbing large sections of new programs from older code. No sense writing routine stuff all over again. Just copy/paste and rework any small changes needed.

You'll also find yourself trying to make those sections of code more generic and "portable", so they can more easily be re-used. You can also put them in macros and build your own include file with all the macros in it.
 
Whoa, just reading through the datasheet for the 628A as you suggested and noticed something.


Does this mean that if running at 4mhz, it will execute 4 million (excluding 2 cycle instructions) instructions per second? I briefly tried a PIC16F84a before deciding upon the 628a as my focus. The 84A executed 1 instruction per every 4 clock cycles, thus only performing 1 million instructions on a 4mhz clock.

Brian
 
The fancy PICs like most 18F and higher have PLL clock multipliers so lots of fun. The 18F4550 can take a 4MHz crystal and bump it up to 48MHz for itself and 96MHz for its USB peripheral.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…