Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic16f84 Bank 0 Bank 1 and control manipulation

Status
Not open for further replies.

dunn

Full Member level 3
Joined
Apr 12, 2001
Messages
155
Helped
22
Reputation
44
Reaction score
7
Trophy points
1,298
Activity points
1,284
pic16f84 Bank 0 Bank 1

Hi i am trying to learn PIC programming.

Please could i get some help.

I read that:
1) pic16f84 Bank 1 is used for Control.
2) pic16f84 Bank 0 is used for Manipulation.

How can i tell if an instruction (for example MOVLW) is a Control or Manipulation?

Thanks.
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Re: pic16f84 Bank 0 Bank 1

Hi,

The 'Banks' can be a difficult thing to understand when you are just starting.

Will try and explain clearly.

First, the ' Banks' are really just RAM memory.

You, the User can use certain parts of the RAM for your storing your values in whats generally refered to as Registers.
Similarly the Pic system itself needs to use the RAM for its own Registers.

In the Pic16F84 the RAM is split in to two distinct area, Bank0 and Bank1.
Both banks are used by the System and can be accessed by the User , but at specific locations only.
See Page 13 of the data sheet - this shows the Systems registers and the Users Registers refered to as ' general purpose '

Although the above is not too difficult it is when you start to code that the problems begin.

Whenever you write or read to either your User registers or the Systems register like PortA, you must ensure that the Bank Select Bit is set accordingly.

This is done by using special bits in the Status register.

An example would be :-

Code:
bcf    STATUS, RP0   	  ;  Select Bank 0
bcf    PORTB              ;  clear Port B
movlw  b'00000000'		  ;  value to make PortB digital Output
bsf    STATUS,RP0         ;  select Bank 1
movwf  TRISB      		  ;  Set TRISB to Output
bcf    STATUS,RP0   	   ;  Set to Bank 0

That example just used the system registers, for you User registers it exactly the same, but before you can use them you must tell the program the name of your Registers and thier location.



Code:
cblock 0x0C 	; define users registers at location 0x0C BANK 0

userone
usertwo

etc etc

endc


	
	movlw 	0xFF		   ;   load W with hex 0xFF
	bcf		STATUS,RP0	;  SET TO BANK 0 if not already in  there
	movwf	 userone		;  store  0xFF in register userone
 

    dunn

    Points: 2
    Helpful Answer Positive Rating

dunn

Full Member level 3
Joined
Apr 12, 2001
Messages
155
Helped
22
Reputation
44
Reaction score
7
Trophy points
1,298
Activity points
1,284
Re: pic16f84 Bank 0 Bank 1

Hi

Thank you for the reply.

I understand your example (I think):
bcf STATUS, RP0 ; Select Bank 0
bcf PORTB ; clear Port B
movlw b'00000000' ; value to make PortB digital Output
bsf STATUS,RP0 ; select Bank 1
movwf TRISB ; Set TRISB to Output
bcf STATUS,RP0 ; Set to Bank 0

In the tutoral I am reading it says:

Line 1 BSF 03h,5 ;GO to Bank 1
Line 2 MOVLW 06h ; Put 00110 into W
Line 3 MOVWF 85h ; Move 00110 onto TRISA
Line 4 BCF 03h,5 ; Come back to Bank 0

I do not understand why, in Line 1, it goes to Bank 1 when Bank 1 is Control.
I would have thought Line 1 would go to Bank 0 as MOV (MOVLW 06h and MOVWF 85h) is used to manipulate data.

I think it should be:
Line 1 BSF 03h,5 ;GO to Bank 0
Line 2 MOVLW 06h ; Put 00110 into W (put is manipulating data ?)
Line 3 BSF 03h,5 ;GO to Bank 1 (ready to Control Port A)
Line 4 MOVWF 85h ; Move 00110 onto TRISA
Line 5 BCF 03h,5 ; Come back to Bank 0

Thanks for any help
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Re: pic16f84 Bank 0 Bank 1

Hi,

Ok, your tutorial code is also correct - however I think your tutorial must be wriiten by a 1980s college tutor - please search the web for some of the many good and understandable assembler tutorials that are out there.
Don't just go the first one you find, have a good look and go with the one that seems to make most sense to you.

Here is a good list - just hope EDA don't mind me referencing ET https://www.electro-tech-online.com...ewcomers-please-read-pic-regarded-upd-0xd.htm

Going back to your code;-

In the pic16F84 data sheet look at the schematic of the chip on page 8 and you will see that W is a unit on its own and not part of the system registers in the banks. so anything you do to W is not dependant on the banks.

Now look at the memory map on page 13 showing the system registers - first the STATUS register and then the TRIS register - notice the hex address at the side of them.

Now look at your tutors code, unlike mine instead of naming the STATUS register he has addressed it directly in the hex value.
He is doing the same with the TRIS register.
This makes the code very difficult to follow and is so unnecessary.

Lastly look at page 13, the status register and bits 5 & 6 for setting the Banks.


So to look at your code, and to correct it -

Line 1 BSF 03h,5 ;GO to Bank 0

To select Bank 0 you must 'clear ' BCF STATUS, RP0 - makes RP0 = 0


Line 2 MOVLW 06h ; Put 00110 into W (put is manipulating data ?)

This just loads a hex value into W , W does not care which bank you are in


Line 3 BSF 03h,5 ;GO to Bank 1 (ready to Control Port A)

Now you select Bank 1 with 'SET' BSF STATUS,RP0 - makes RP0 = 1

Line 4 MOVWF 85h ; Move 00110 onto TRISA

You then move the data in W to TRISA register in bank1 movwf TRISA

Line 5 BCF 03h,5 ; Come back to Bank 0

Finally you return to Bank0 if you have no other instructions for bank1



If you cannot follow that don't give up, take a day off and look around for a better tutorial, typically starting with flashing a led, don't worry if you do not understand the code examples, just build the circuit and run them, things will soon fall into place and become a lot easier to understand.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,859
Helped
4,854
Reputation
9,726
Reaction score
4,640
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
126,484
Re: pic16f84 Bank 0 Bank 1

I think I should add that if you are using MPLAB as the assembler, Microchip added a feature to help you select the correct bank. All you have to do is use the 'banksel' instruction before the register you want to access.

For example:
If you want to make Port B an output and put 10101010 on its pins you need to use bank 1 to access the TRIS register and bank 0 to access the data register. Instead of setting and resetting the bits in the STATUS register, which becomes confusing and easy to lose track of, use:

banksel TRISB
clrf TRISB

banksel PORTB
movlw B'10101010'
movwf PORTB

Ir makes it much easier to read. As a beginner, it is probably wise to over use the banksel instruction where the bank isn't actually changing but it does no harm and only loses two instructions. I use it more often than necessary to get programs working then remove the instances where the correct bank was already selected. Note that banksel is not a processor instruction, it is an assembler instruction that generates two processor instructions automatically, one for each bank selection bit. You will find banksel particularly useful when you use larger PICs which have more register banks as it controls both bank bits, not just the one for banks 0 and 1.

Brian.
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Re: pic16f84 Bank 0 Bank 1

Hi Brian,


Thanks for adding the Banksel - something I could have done with when I used to program the 16Fs.

Just so glad I moved to the 18F with no paging to worry about and up to 384 bytes of ram available before you need to change Banks ( bank 0 access 128 + bank 1 256 )
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,859
Helped
4,854
Reputation
9,726
Reaction score
4,640
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
126,484
Re: pic16f84 Bank 0 Bank 1

I also now use 18F46J11 in most applications, usually with masses of unused memory but they cost so little it doesn't worry me. If only that chip had on-board EEPROM. The funny thing is, I also use lots of 10F202s and they are too small to need banks selection bits!

Brian.
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Re: pic16f84 Bank 0 Bank 1

Hi Brian,

If only that chip had on-board EEPROM.

Without going off subject too much - always wondered why the 18 J chips dropped EEprom ? cost, voltage, parallel port ? - although have used the 18LF4620 which has plenty of memory plus low voltage. and still in dip form.
 

dunn

Full Member level 3
Joined
Apr 12, 2001
Messages
155
Helped
22
Reputation
44
Reaction score
7
Trophy points
1,298
Activity points
1,284
Re: pic16f84 Bank 0 Bank 1

Thanks to all involved.

Please could anybody help me out by letting me know if there is a book or.... where I could look at sample code (preferably with comments) written in assembly) for the 16F84/16F690 so that I could learn from it.

In the end I want to learn how to program the PIC using C.
First assembly then C. Is this the correct way to learn (or should I just learn how to program in C from the start)?

I ultimately want to program the PIC 16F690 using C.
I thought I would learn how to program 16F84 first because it looked more easier then 16F690.

Thanks for all the replys.
 

betwixt

Super Moderator
Staff member
Joined
Jul 4, 2009
Messages
14,859
Helped
4,854
Reputation
9,726
Reaction score
4,640
Trophy points
1,393
Location
Aberdyfi, West Wales, UK
Activity points
126,484
Re: pic16f84 Bank 0 Bank 1

I come from an electronic background so I would say learn assembly language first then move on to C. The reason being that assembly language gives far more insight to how the PIC works and helps you understand its strengths and shortfalls. C is easier but in many ways less efficient because it does things in a 'general purpose' way rather than being optimized to your particular application.

You should also consider that C passes parameters on a stack, either the real hardware one or one created by the compiler, this uses quite a lot of memory to implement. For that reason, C is more restricted on devices that have small memories, in fact many C compilers do not support small PIC devices at all.

As for ease of use, I wouldn't say the F84 is easier to use than the F690, it depends on the application. Sometimes it is much simpler to use a built in peripheral than emulate it in software on device without it.

There are lots of code examples in the MPLAB help files, the Microchip web site and on the PICLIST web site. A Google search "+16F84A +asm" turned up 8,400 references a moment ago so there should be plenty of examples out there.

Brian.
 

wp100

Advanced Member level 5
Joined
May 15, 2009
Messages
3,056
Helped
883
Reputation
1,781
Reaction score
736
Trophy points
113
Location
Prime Meridian
Activity points
0
Re: pic16f84 Bank 0 Bank 1

Hi,

Thats good advice from Brian, but don't just settle for reading books, download MPlab and run your code on it , even if its only the 4 lines you are currently using.
You can use the SIMulator function in single step where you can see how each of your intructions are proceeding and move into the various registers

Have also sent you a PM.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top