# Problem with bank selection in 16f877A

Status
Not open for further replies.

#### anhnha

##### Full Member level 6
Hi all,
I am have a problem when i insert a assembly in c code in ccs.this is my code:
Code:
......#asm
MOVLW    0x00
MOVWF    TRISB
MOVLW    0x18
MOVWF    TRISC
BSF PORTC, 0
BCF 0x03, 5
MOVF 0x4C, 0
SUBLW 1
BTFSC 0x03,2
MOVLW 0xF9
MOVWF PORTB
#endasm
in this i want to set trisb and trisc state but when i watch a list file i find that:
Code:
[CODE]
.................... #asm
.................... MOVLW 0x00
017F: MOVLW 00
.................... MOVWF TRISB
0180: BSF 03.5
0181: MOVWF 06
.................... MOVLW 0x18
0182: MOVLW 18
.................... MOVWF TRISC
0183: MOVWF 07
.................... BSF PORTC, 0
0184: BCF 03.5
0185: BSF 07.0
.................... BCF 0x03, 5
0186: BCF 03.5
.................... MOVF 0x4C, 0
0187: MOVF 4C,W
.................... SUBLW 1
0188: SUBLW 01
.................... BTFSC 0x03,2
0189: BTFSC 03.2
.................... MOVLW 0xF9
018A: MOVLW F9
.................... MOVWF PORTB
018B: MOVWF 06
.................... #endasm[/CODE]
I can not understand MOVWF 06 instruction I think it has to be a MOVWF 86 instruction because 06 is portb address and 86 is trisb address?
Thank you.

Last edited:

#### betwixt

##### Super Moderator
Staff member
You are both right and wrong.

The way the internal registers are arranged is in 'banks' and the address your program provides is only applicable to the currently selected bank unless you use indirect addressing. I'm not sure if CCS allows the use of 'banksel' instructions so what you have to do is set the bits in the STATUS register to select bank 1 at the start of your code, this will let you access the TRISx registers, then set the bits back to zero to reach page 0 before the BSF instruction so you access the PORTx registers.

This looks like stand-alone code, is there a good reason for using CCS rather than MPLAB or MPLABX?

Brian.

anhnha

### anhnha

Points: 2
Helpful Answer Positive Rating

#### anhnha

##### Full Member level 6
Thanks for reply.
I have a project and my program is in c code, therefore i used ccs.In ccs Ii don't need to used banksel,if i used it it will give a error.This compiler auto select bank depend on the instruction.I had use :
bsf status, rp0
bcf status, rp1 and so on to choose bank but nothing different!
however when i run code in proteus it is work!

#### betwixt

##### Super Moderator
Staff member
I use a different compiler but I would be surprised if CCS auto selects the bank inside a #asm section as you are generating the code yourself. The asm code is used exactly as you enter it so no instructions to change banks would or should be added to it.

You could always code it like this:
Code:
TRISB =0;
TRISC = 0x18;
PORTC |= 1;
STATUS &= 0xDF;
if (<variable in 0x4c> -1 == 0) PORTB = 0xF9;  //put the real variable name in < >
else PORTB = <variable in 0x4C> - 1;

In which case the compiler WOULD be able to add the bank selection bits. You can probably use register and bit names in the #asm code to make it more readable but I'm not familiar with CCS 'C' so I can't help in that respect.

Brian.

anhnha

### anhnha

Points: 2
Helpful Answer Positive Rating
Status
Not open for further replies.

Replies
1
Views
1K
Replies
12
Views
2K
Replies
3
Views
339
Replies
1
Views
1K
Replies
3
Views
897