Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

RS232 Assembly Code - This code works but I don't understand

Status
Not open for further replies.

Spadez

New Member
Hi,

I got help with a section of code but even though it works, I have no idea how. I want to understand exactly what is going on with this code, and see if I can re-code it in a way I can better understand.

It is designed to send the character stored in the working register through RS232 on the pics UART.

Here is the code:
Code:
send:    
		banksel TXREG
		movwf TXREG             ; send data in W 

TransWt bsf STATUS,RP0		; RAM PAGE 1
		banksel TXSTA
WtHere  btfss TXSTA,TRMT        ; (1) transmission is complete if hi
        goto WtHere

        bcf STATUS,RP0          ; RAM PAGE 0
        return
 
Hi,

I got help with a section of code but even though it works, I have no idea how. I want to understand exactly what is going on with this code, and see if I can re-code it in a way I can better understand.

It is designed to send the character stored in the working register through RS232 on the pics UART.

Here is the code:
Code:
send:    
		banksel TXREG
		movwf TXREG             ; send data in W 

TransWt bsf STATUS,RP0		; RAM PAGE 1
		banksel TXSTA
WtHere  btfss TXSTA,TRMT        ; (1) transmission is complete if hi
        goto WtHere

        bcf STATUS,RP0          ; RAM PAGE 0
        return

Shoot, lost my first reply, try again!

Hi Spadez,
The directive banksel selects the page which TXREG is in, then moves what is in W to the TXREG, as you correctly observed.

I'm not sure why they set the RP0 bit in STATUS to select page 1, as the directive banksel TXSTA does this. You could probably remove that first instruction...(bsf STATUS, RP0)

Then the bit TRMT in the TXSTA register is checked. If it is set the next instruction (goto WtHere) is skipped (as the comment points out.) If the bit is clear, then the counter goes back to test the bit. (btfss = Bit Test in File Skip if Set)

Once the transmission is complete (and the TRMT bit has been set by the UART) the program counter goes to clear the RP0 bit and we are back in bank 0. Then return to where left off.

Hope this makes sense for you, if not, just say, and will try it a different way.
 
Thank you very much for the reply. This really does help me to understand it. Im sorry that your first reply got lost, its happened several times to me before to!

It is correct that things like "TXREG" and "TXSTA" are not user defined, and are infact the standard lables built into the PIC?

It where another way to go to that particular bank other than using banksel TXREG?
 
Last edited:
The way to shift banks is either through using the BANKSEL directive or setting/clearing different bits in the STATUS register. Take a look at the data sheet for the model PIC you are using. It will likely show certain configuration of the status register for the different memory banks. That's pretty much what is happening in the "STATUS, RP0" commands.

As BeeBop advised, BANKSEL and status bit changing is redundant. He advised you to delete it, but I would caution you. I remember reading in a data sheet on a higher end 16F PIC that either the "TRANS COMPLETE BIT", "TRANS COMPLETE INTERRUPT FLAG", or "TRANS BUFFER FULL BIT," (Something to that effect) will not change state until a few clock cycles AFTER it actually happens. I don't remember the specifics, but I just wanted to urge you to read the UART/USART section of your PIC's datasheet before you got into trouble. I may be wrong here, but that could be why there were redundant commands there, to give the USART peripheral time to do it's thing with the flags. If this is the case, "NOP" could be substituted for a redundant bank selection command.
 
Last edited:
Hi, yes, wannaB is correct; the data sheet reveals all. You should study the relevant sections for any micro you are using.As he says you can use the RP0 RP1 bits in the STATUS register to change banks, however banksel allows you to do this without thinking of which bank you are in and which you need to go to....
you could redefine these registers using equ, but then your source becomes more difficult to follow, even for you. It is best to use the include file for your PIC and stay with the standard, then when you go back to your assembly file after many months, or years, you won't have to fight to understand it.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top