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.

Test and branch code

Status
Not open for further replies.

AGCB

Member
Would someone look at this branching code and tell me if there is something wrong.
What I'm trying to do is receive a message from another PIC via USART and then branch accodingly. I have simplified my actual code so you can understand it w/o the whole program. I'm using 18 series chips for both send and receive.
Code:
usart_in	
	
	movf	rcreg,w		;get info into wreg	
	
	cpfslt	number		;if number (0xf9) is less than wreg,(I.E. W is greater)
				;	 skip next instruction
	bra	set_A
			
	sublw	0xfa		;if wreg is 0xfa		
	btfsc	status,z	;
	bra	Set_B		;yes, Z is set	
	
	
	sublw	0xfb		;if wreg is 0xfb		
	btfsc	status,z	;
	bra	Set_C		;yes, Z is set

		;other tests for 0xfc-0xff

return

I have reserved 0xfa-ff for instructions. Everything below 0xfa will be displayed as a 3 digit decimal. It works until I add the FB test, then seems to reset or go back to the front of program. I have tried both using interrupts and just polling the receive flag RCIF. The USART part works fine.
 
Last edited:
If you have already subtracted 0xfa from W then to check for 0xfb only requires you to subtract 1 more. Alternatively, reload W before the second subtract.

Mike.
 
If you have already subtracted 0xfa from WMike.

You know, as I was getting into bed last night, I thought of the fact that W may be changed before the 2nd test, and your reply and my looking at the datasheet this morning confirmed that.
Thanks Mike
 
Just one more confirmation of my understanding. On the instructions CPFSEQ/GT/LT the contents of the wreg is not changed by these instructions. Am I right?
 
So are you branching to Set_A if RCREG <= 0xF9 and the other sets for 0xFA through 0xFF? Something like this?

Code:
test    movf    RCREG,W         ; get RX data
        sublw   0xF9            ; data >= 0xFA?
        skpnc                   ; yes, skip, else
        bra     Set_A           ; branch ( rcreg <= 0xF9)
        infsnz  WREG,W          ; 0xFA? no, skip, else
        bra     Set_B           ;
        infsnz  WREG,W          ; 0xFB? no, skip, else
        bra     Set_C           ;
        infsnz  WREG,W          ; 0xFC? no, skip, else
        bra     Set_D           ;
 
Last edited:
So are you branching to Set_A if RCREG <= 0xF9 and the other sets for 0xFA through 0xFF? Something like this?

Code:
test    movf    RCREG,W         ; get RX data
        sublw   0xF9            ; data >= 0xFA?
        skpnc                   ; yes, skip, else
        bra     Set_A           ; branch ( rcreg <= 0xF9)
        infsnz  WREG,W          ; 0xFA? no, skip, else
        bra     Set_B           ;
        infsnz  WREG,W          ; 0xFB? no, skip, else
        bra     Set_C           ;
        infsnz  WREG,W          ; 0xFC? no, skip, else
        bra     Set_D           ;

I believe your code and mine accomplish the same, provided I reload W before each test as POMMIE said. Short answer, Yes. That part of my code has been corrected but still have a problem. I will start a new thread as the title of this one doesn't apply. See 'Computed GOTO jumps back one byte"
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top