Continue to Site

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.

Using TRIS and BANKSEL instruction in MPLAB

Status
Not open for further replies.

charles01

Newbie level 5
Newbie level 5
Joined
Feb 15, 2010
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
leicester
Activity points
1,350
hi
guys
i am using tris in programming the PIC16F876 and when i assemble the program, it says the use of "tris" is not recomended even though i can get a hex file
i am using MPLAB IDE v8.43

the line with the code is like this
' tris PORTB'

can i replace it with ' movwf PORTB'
thank you
 

Re: MP ASM warning

I don't know what you think that command does, but it makes little sense, really.


TRIS-X is a register related to the Tri-State latch of the 'X' port. So, TRISB is PORTB's Tri-state latch control. Depending on the value of TRISB, some pints of PORTB are either inputs or outputs.

If you want to use PORTB as an 8-bit output port, you need to:

BANKSEL TRISB ;<-- MPAsm command that gets replaced with the right Bank select sequence.
MOVLW 0x00
MOVWF TRISB


or

CLRF TRISB
 

Re: MP ASM warning

TRIS instruction is used in old PIC models (like 10F series) to load TRIS registers as these old models don't have TRIS registers mapped on their RAM. Although datasheets of new models don't mention them, they are still available but not recommended.

Normally, one should use the method that jumper2high suggested.
 

    charles01

    Points: 2
    Helpful Answer Positive Rating
Re: MP ASM warning

Tagli said:
TRIS instruction is used in old PIC models...

This must have been before 'my time'. :lol:
 

Re: MP ASM warning

thank you guys
i was doing a simple program to switch ON LEDs on port B
this was my first program for PIC16F876 to work
now i am going to improve the program to make it more intersting

Added after 2 hours 12 minutes:

i have done a program which is supposed to cycle a bit parttern and display on PORTB but when i switch ON the citcuit after loading it to the PIC16F876, all LEDs switch ON
i have no errors when i assemble the program in MPLAB IDE v8.43

here is the code
Code:
 ;**********************************************************************
;   This file is a basic code template for object module code         *
;   generation on the PIC16F876.                                      *
;   This code cycles a bit parttern through the LEDs on PORTB.        *       
;                                                                     *
;   THE PROGRAM DISPLAYS LEDs ON PORTB IN SEQUENCE                    *
;**********************************************************************
;                                                                     *
;    Filename:      COUNTER.asm                                       *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author: CHARLES NENGUKE                                          *
;**********************************************************************
;                                                                     *
;    Files required: P16F876.INC                                      *
;**********************************************************************


    list      p=16f876            ; list directive to define processor
    #include <p16f876.inc>        ; processor specific variable definitions
    radix     hex
   __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF   
    
STATUS      EQU      0x03
PORTB       EQU      0x06
SHIFT       EQU		 0x0C
NCOUNT		EQU    	 0x0D
MCOUNT		EQU		 0x0E
C           EQU      0	
;=======================================================================
               org     0x0000
;
start         movlw    0x00        ; load W with 0x00
              banksel    TRISB       ; make PORTB pins outputs
              movwf    PORTB       ; make PORTB lines low
              bcf      STATUS,C    ; clear the carry flag
              clrf     PORTB       ; PORTB lines low
              movlw    b'01001001'      ; initialize counter
			  movwf    SHIFT       ; store the value b'01001001' in SHIFT register
GET_BIT       movf     SHIFT,W     ; move from register SHIFT to W
              movwf    PORTB       ; display output at port b
              call     PAUSE       ;  delay via subroutine PAUSE
              RLF      SHIFT,f     ; rotate the bits in register SHIFT and store the
                                   ; result in SHIFT
              goto     GET_BIT     ;  start all over again

PAUSE         movlw   0xFF         ; M
              movwf   MCOUNT       ; move contents of W to MCOUNT
LOADN         movlw   0x0F         ; N
              movwf   NCOUNT       ; move contents of W to register NCOUNT
DECN          decfsz  MCOUNT,f     ; decrement M
              goto    LOADN        ; repaet operation
              return               ; done
              
;
              end

please help
thank you
 

Re: MP ASM warning

You're not using the "banksel" directive correctly. It doesn't work like the old "tris" instruction. Actualy, it's replaced by these lines:
Code:
bsf STATUS,RP0
bcf STATUS,RP1
These lines enables Bank 1 for addressing, where the TRISB register is mapped. The second line is not needed for switching from Bank 0 to Bank 1 but MPLAB inserts it as it's not able to know what the old bank was.

Then you must load the TRISB register. For your case, simply clearing it with "clrf TRISB" will do. Then you need to switch back to Bank 0 to continue. For your case, "bcf STATUS,RP0" is enough. You can also use "banksel" directive with any register name which is located on Bank 0.
 

Re: MP ASM warning

despite having changed the code as per instruction from my previous post, only one LED switches On constantly until i reset the PIC and then it goes ON again
i do not know where i am going wrong here

i am not well vresed with this MPLAB ASM coding but i want to learn it
 

Re: MP ASM warning

If you post the lastest version of your code, I can inspect it again.
 

Re: MP ASM warning

ok, thank you
here is the new code
Code:
 ;**********************************************************************
;   This file is a basic code template for object module code         *
;   generation on the PIC16F876.                                      *
;   This code cycles a bit parttern through the LEDs on PORTB.        *       
;                                                                     *
;   THE PROGRAM DISPLAYS LEDs ON PORTB IN SEQUENCE                    *
;**********************************************************************
;                                                                     *
;    Filename:      COUNTER.asm                                       *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author: CHARLES NENGUKE                                          *
;**********************************************************************
;                                                                     *
;    Files required: P16F876.INC                                      *
;**********************************************************************


    list      p=16f876            ; list directive to define processor
    #include <p16f876.inc>        ; processor specific variable definitions
    radix     hex
   __CONFIG _CP_OFF & _WDT_ON & _BODEN_ON & _PWRTE_ON & _XT_OSC & _WRT_ENABLE_ON & _LVP_ON & _DEBUG_OFF & _CPD_OFF   
    
STATUS      EQU      0x03
PORTB       EQU      0x06
SHIFT       EQU		 0x0C
NCOUNT		EQU    	 0x0D
MCOUNT		EQU		 0x0E
C           EQU      0	
;=======================================================================
               org     0x0000
;
start         bsf STATUS,RP0
			  bcf STATUS,RP1
              movlw    0x00        ; load W with 0x00
              banksel  86h       ; make PORTB pins outputs
              movwf    86h       ; make PORTB lines low
              bcf      STATUS,C    ; clear the carry flag
              clrf     PORTB       ; PORTB lines low
              movlw    b'01001001'      ; initialize counter
			  movwf    SHIFT       ; store the value b'01001001' in SHIFT register
GET_BIT       movf     SHIFT,W     ; move from register SHIFT to W
              movwf    PORTB       ; display output at port b
              call     PAUSE       ;  delay via subroutine PAUSE
              RLF      SHIFT,f     ; rotate the bits in register SHIFT and store the
                                   ; result in SHIFT
              goto     GET_BIT     ;  start all over again

PAUSE         movlw   0xFF        ; M
              movwf   MCOUNT       ; move contents of W to MCOUNT
LOADN         movlw   0xFF        ; N
              movwf   NCOUNT       ; move contents of W to register NCOUNT
DECN          decfsz  MCOUNT,f     ; decrement M
              goto    LOADN        ; repaet operation
              return               ; done
              
;
              end
 

Re: MP ASM warning

It seems you still don't get the idea of bank switching and this code doesn't include the changes that I have advised in my previous posts.

You must first study the memory structure of the PIC from its datasheet. As you can see there, the data memory is divided into 4 banks. The TRISB register that you are trying to access is located on Bank 1 (some registers exist more than one bank), and you always start with Bank 0. So, you need a bank switching by setting or resetting RP0 and RP1 bits in the STATUS register.

"banksel" directive is an easy way to switching banks in a way that I have explained before. So, to make TRISB = 0, you must use these lines:
Code:
banksel TRISB   ;switches to Bank 1
clrf TRISB   ;clears TRISB, thus making them outputs
banksel PORTB   ;switches to Bank 0

or the same effect can be achieved with these lines:
Code:
bsf STATUS,RP0   ;switches to Bank 1
clrf TRISB   ;clears TRISB, thus making them outputs
bcf STATUS,RP0   ;switches to Bank 0
 

Re: MP ASM warning

thanks Tagli, i have managed to makre it run
i changed SHIFT EQU 0x0C to 0xFF although i get some massages of not being in bank 0 when i assemble the progrm
next i changed the bit b'01001001' to b'00000001' and i can see the LEDs switching ON in this order from B0 ....B7
B0-B1-B0-B1-B2-B4-B5-B7
and then back to B0
for some reason B3 on my PIC16F876 does not work at all even with other programs

so for the above program B6 is being skipped and God knows why

anyway i have achieved a result
now i want to do bit manipulation from one port and display at another port

thank you very much for the support you are giving
 

Re: MP ASM warning

Sorry, I missed the "SHIFT EQU 0x0C" error as I focused on the bank error. It is because h'0C' is the address of the PIR1 register. Some bits in this register are defined as read-only, so you can't change them. Normally, you must inspect the memory map and avoid placing your general use data on these special function registers (SFR).

To be able to use B3, you need to disable low voltage programming (LVP). When LVP is enabled, it allows you to program the PIC without the need of 13 volt programming voltage. 5 volt on B3 forces PIC to enter the programming mode. Normally you don't need LVP, as the programmer devices are designed to be able to provide 13 volt programming voltage. LVP option is controlled by configuration bits. You can disable it in your programming software, but including it at the beginning of your code is a better idea.
Code:
__CONFIG _LVP_OFF
You already have it in your code as I see. Just change ON with OFF.

Just ignore that bank warning messages. MPLAB thinks that you may forget to change the bank, so it gives warnings. You can get rid of them placing this line at the beginning of your program:
Code:
errorlevel -302
 

Re: MP ASM warning

thanks a million, i am now working on my next program and see if it is going to work
once again thank you:D
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top