futz Active Member Jul 22, 2008 #1 EDIT: Never mind! I thought about it some more and realized I had my clock polarity wrong. Changed it and she works perfect! I wrote this little function to send a byte out the SPI, copying the way I used to do it in assembler (which worked fine). Code: void spi_out(unsigned char digit) { unsigned char junk; sspbuf=digit; while(!sspstat.BF); junk=sspbuf; } But it doesn't work. The byte in the receiving shift register appears to be shifted by one bit, and of course my 7-seg displays junk. If I remove the sspstat.BF test, it displays correctly, but doesn't work right (hard to explain). The generated assembly code looks fine: Code: 52: void spi_out(unsigned char digit) 53: { 54: unsigned char junk; 55: sspbuf=digit; 008A 501F MOVF 0x1f, W, ACCESS 008C 6EC9 MOVWF 0xfc9, ACCESS 56: while(!sspstat.BF); 008E A0C7 BTFSS 0xfc7, 0, ACCESS 0090 D7FE BRA 0x8e 57: junk=sspbuf; 0092 50C9 MOVF 0xfc9, W, ACCESS 0094 6E20 MOVWF 0x20, ACCESS 58: } 0096 0012 RETURN 0 Here's the (working) asm code I ported from: Code: spisend movwf SSPBUF BANKSEL SSPSTAT spiloop btfss SSPSTAT,BF goto spiloop BANKSEL SSPBUF movf SSPBUF,W return Last edited: Jul 22, 2008
EDIT: Never mind! I thought about it some more and realized I had my clock polarity wrong. Changed it and she works perfect! I wrote this little function to send a byte out the SPI, copying the way I used to do it in assembler (which worked fine). Code: void spi_out(unsigned char digit) { unsigned char junk; sspbuf=digit; while(!sspstat.BF); junk=sspbuf; } But it doesn't work. The byte in the receiving shift register appears to be shifted by one bit, and of course my 7-seg displays junk. If I remove the sspstat.BF test, it displays correctly, but doesn't work right (hard to explain). The generated assembly code looks fine: Code: 52: void spi_out(unsigned char digit) 53: { 54: unsigned char junk; 55: sspbuf=digit; 008A 501F MOVF 0x1f, W, ACCESS 008C 6EC9 MOVWF 0xfc9, ACCESS 56: while(!sspstat.BF); 008E A0C7 BTFSS 0xfc7, 0, ACCESS 0090 D7FE BRA 0x8e 57: junk=sspbuf; 0092 50C9 MOVF 0xfc9, W, ACCESS 0094 6E20 MOVWF 0x20, ACCESS 58: } 0096 0012 RETURN 0 Here's the (working) asm code I ported from: Code: spisend movwf SSPBUF BANKSEL SSPSTAT spiloop btfss SSPSTAT,BF goto spiloop BANKSEL SSPBUF movf SSPBUF,W return