Brandon: Here's some SPI code I wrote for the PIC16F877. Hope it works for you. If you have any questions, let me know. Sam.... "May the roof over your head never leak until it rains gold." ("`-''-/").___..--''"`-._ `6_ 6 ) `-. ( ).`-.__.`) (_Y_.)' ._ ) `._ `. ``-..-' _..`--'_..-_/ /--'_.' ,' (il),-'' (li),' ((!.-' // ***************************************************************** // spi.c // // Author: Sam R. Linder // // // ***************************************************************** // external functions // external global variables // internal functions unsigned char ReadByteSPI(void); void WriteByteSPI(unsigned char writebyte); // global variables unsigned char rdataval; unsigned char wdataval; unsigned char loopcount; //***************** // Bit Declarations //***************** // SPIPORT = Port-C // SPICLK_OUT = pin-3 // SPIDATA_IN = pin-4 // SPIDATA_OUT = pin-5 #define SCLK SPIPORT,SPICLK_OUT #define DATAIN SPIPORT,SPIDATA_IN #define DATAOUT SPIPORT,SPIDATA_OUT unsigned char ReadByteSPI(void) { #asm CLRF STATUS // select bank-0 clrf _rdataval movlw 08 // set loop count movwf _loopcount // save it bcf STATUS,C // clear Carry R0: bsf SCLK // set SCLK high bcf SCLK // set SCLK low bcf STATUS,C // clear Carry btfsc SPIPORT,SPIDATA_IN bsf STATUS,C // set Carry rlf _rdataval,f // rotate Carry bit into memory location decfsz _loopcount,f // decrement loop count, skip next line if zero (we're done) goto R0 // else loop back to read next data bit #endasm return (rdataval); } // end of ReadByteSPI() void WriteByteSPI(unsigned char dataval) { #asm clrf STATUS // select bank-0 movwf _wdataval // save data byte to write movlw 08 // set loop count movwf _loopcount // save it bcf STATUS,C // clear Carry W0: rlf _wdataval,f // rotate msb of _zdataval into Carry bcf DATAOUT // assume bit = 0 (set DATAOUT low) btfsc STATUS,C // test Carry bit (skip next line if Carry = 0) bsf DATAOUT // set DATAOUT high bsf SCLK // set SCLK high bcf SCLK // then low decfsz _loopcount,f // decrement loop count, skip next line if zero (we're done) goto W0 // else loop back to output next data bit #endasm } // end of WriteByteSPI() // end of module spi.c -----Original Message----- From: Brandon Fosdick [mailto:bfoz@GLUE.UMD.EDU] Sent: Sunday, August 05, 2001 2:03 PM To: PICLIST@MITVMA.MIT.EDU Subject: [PIC]: Need SPI code for 16F877 As I've mentioned before, I'm trying to use the MCP2510 in a project. The SPI module on the 16F877 is driving me crazy. Does anybody have code for it that I can use? Or even just some helpful hints? I'm about ready to forget the hardware module and just bit-bang it, but I didn't see any code on piclist for that either. A quick search of google didn't provide any usefull answers. I think I can TX a byte by just loading it into SSPBUF and then waiting for it to leave. But I have no idea how to get the thing to receive a byte. I keep getting the last byte that I wrote to SSPBUF. -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics