SDCC PIC16f737 odd problem

Thread Starter

TrevorP

Joined Dec 8, 2006
55
I'm writing the code for a microcontroller to control an ROV for a school competition and I've come into and interesting problem.

I'm getting serial data from a laptop (or another chip in my case) and the packets are sending fine. So then I want to set ports based on that data.

Anyway I've got a integer called 'packet' it contains: ##000000 00000000 00000000 10000101

Now I've got the following two lines of code:
Rich (BB code):
PORTB = ((packet & 0x00C0) >> 6); /* Turn on the correct direction */
PORTB |= ((packet & 0x000F) << 2);
What's weird is that the 2nd bit of port B is not being set (seeing as the light doesn't turn on...but the 5th and 3rd bit pins do turn on as expected. If I comment out the second line the the first line works as expected.

Also the generated assembly for those two lines are as follows:

Rich (BB code):
;	.line	97; "receive.c"	PORTB = ((packet & 0x00C0) >> 6); /* Turn on the correct direction */
	MOVLW	0xc0
	ANDWF	r0x1001,W
	MOVWF	r0x1002
	CLRF	r0x1003
	SWAPF	r0x1002,W
	ANDLW	0x0f
	MOVWF	r0x1004
	SWAPF	r0x1003,W
	MOVWF	r0x1005
	ANDLW	0xf0
	IORWF	r0x1004,F
	XORWF	r0x1005,F
	MOVLW	0xf0
	BTFSC	r0x1005,3
	IORWF	r0x1005,F
;shiftRight_Left2ResultLit:6080: shCount=1, size=2, sign=1, same=1, offr=0
	BCF	STATUS,0
	BTFSC	r0x1005,7
	BSF	STATUS,0
	RRF	r0x1005,F
	RRF	r0x1004,F
;shiftRight_Left2ResultLit:6080: shCount=1, size=2, sign=1, same=1, offr=0
	BCF	STATUS,0
	BTFSC	r0x1005,7
	BSF	STATUS,0
	RRF	r0x1005,F
	RRF	r0x1004,F
	MOVF	r0x1004,W
	BANKSEL	_PORTB
	MOVWF	_PORTB
;	.line	105; "receive.c"	PORTB = ((packet & 0x000F) << 2) | PORTB;
	MOVLW	0x0f
	BANKSEL	r0x1001
	ANDWF	r0x1001,F
	CLRF	r0x1000
	MOVF	r0x1001,W
	MOVWF	r0x1002
	BCF	STATUS,0
	RLF	r0x1002,W
	MOVWF	r0x1001
	BCF	STATUS,0
	RLF	r0x1001,F
	BANKSEL	_PORTB
	MOVF	_PORTB,W
	BANKSEL	r0x1002
	MOVWF	r0x1002
	IORWF	r0x1001,W
	BANKSEL	_PORTB
	MOVWF	_PORTB
	GOTO	_00105_DS_
If you might know why this is happening or a good solution it would be greatly appreciated. (Also the tristate for Port B has been entirely set to output and there is nothing special about those pins).

Thanks,

Trevor
 

thatoneguy

Joined Feb 19, 2009
6,359
Can you simulate the input of those 4 bytes in a debugger to make sure the logic statements are doing what you are expecting them to do?
 

Thread Starter

TrevorP

Joined Dec 8, 2006
55
I could but it would be a bit difficult. I seemed to have found a solution though.

If I do:
Rich (BB code):
char portb;

portb = ((packet & 0xC0) >> 6);
portb |= ...;

PORTB = portb;
It solves the problem. It seems that the compiler makes code that doesn't like accessing the value on PORTB.
 

thatoneguy

Joined Feb 19, 2009
6,359
Which compiler are you using?

Please post the ASM listing for what the second version creates compared to the first.

In the first listing, what is the define for _PORTB? where banksel _PORTB and movwf _PORTB are referencing something other than PORTB.
 
Top