Log in

View Full Version : Trying to learn Assembly



E Kizer
- 28th June 2018, 21:28
I've been using PBP3 for awhile now, but I'm trying to learn simple Assembly routines.
To start, it seemed wise to learn how to just configure a port for digital I/O, and set a few pins HI.
My breadboard has a 16F1518 and I have great control of the pins using PBP3, ie, yes the outputs are all good.
But I must be missing something when doing the same with Assembly.
Page 106 of the 16F1518 manual shows how to configure ANSEL and TRIS for port A and placing their routine within a PBP3 program compiles without issue.

asm

BANKSEL PORTA ;
CLRF PORTA ;Init PORTA
BANKSEL LATA ;Data Latch
BANKSEL ANSELA ;
CLRF ANSELA ;digital I/O
BANKSEL TRISA ;
MOVLW B'11100001' ;Set RA<4:1> as outputs, the rest inputs
MOVWF TRISA
MOVLW B'00011110' ;Set RA<4:1> HI
MOVWF PORTA


endasm

I added the last 2 lines, ie move literal to W and then move W to the file register hoping to see the pins A1-A4 go HI.
But it didn't work. The problem must be with moving the config data to the port.

Does anyone know what I am doing wrong here? I've been searching the web for ideas and it seems I'm doing things correctly by what I'm finding but this routine isn't working.
Any help would be appreciated.

Ed

richard
- 29th June 2018, 00:36
The problem must be with moving the config data to the port.





MOVLW B'00011110' ;Set RA<4:1> H
BANKSEL LATA
MOVWF LATA

mpgmike
- 29th June 2018, 05:21
You did a BANKSEL TRISA and configured that register, then you loaded W and tried to shove it into PORTA register without resetting the Bank Select Register. TRISA is in BANK 1 while PORTA is in BANK 0. Without selecting BANK 0 with "BANKSEL PORTA" you simply wrote over your TRISA; TRISA & PORTA share the same Register address, though in different Banks.

BANKSEL PORTA
MOVLW B'00011110
MOVWF PORTA

mpgmike
- 29th June 2018, 15:50
Got to thinking after posting, I gave you an answer, but didn't share any knowledge.

In the Data Sheet there is a section called 3.0 Memory Organization. On page 23, Table 3.3 shows where SFR are stored; both Bank# and Address#. You should be able to look at that Table and see PORTA located in BANK 0 at address 000Ch. Next row in BANK 1 you see TRISA at address 008Ch, but the same position within the BANK. Next you can see TRISA in the same position in BANK 2.

Using BANKSEL is a shortcut that allows the Compiler to find the proper BANK for you. In the Instruction Summary, Section 24, Table 24-3 lists the Assembly Instructions applicable to this PIC. Towards the bottom under the heading Literal Operations is the command MOVLB. If you know TRISA is in BANK 1 you can:

MOVLB 0x01

Page 272 shows the expanded explanation of MOVLB.

I hope this helps you better understand how the Banking system works (at least in the PIC family).