S Suraj143 Active Member Oct 31, 2016 #1 I use PIC 16F88 Hardware SPI.I want to write a 16bit value.How to do it? SPI_Write(0x07E4); This is the code for 8bits.I need to convert it to 16bit. Code: unsigned char SPI_Write(unsigned char val) { SSPSTAT.BF=0; SSPBUF = val; while(SSPSTAT.BF==0); return SSPBUF; }
I use PIC 16F88 Hardware SPI.I want to write a 16bit value.How to do it? SPI_Write(0x07E4); This is the code for 8bits.I need to convert it to 16bit. Code: unsigned char SPI_Write(unsigned char val) { SSPSTAT.BF=0; SSPBUF = val; while(SSPSTAT.BF==0); return SSPBUF; }
Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member Oct 31, 2016 #2 Clear the correct CS, then you send the high byte, then the low byte, then set CS
S Suraj143 Active Member Oct 31, 2016 #3 You mean like this? Code: CS = 0; SPI_Write(High_Byte); SPI_Write(LOW_Byte); CS = 1; unsigned char SPI_Write(unsigned char val) { SSPSTAT.BF=0; SSPBUF = val; while(SSPSTAT.BF==0); return SSPBUF; }
You mean like this? Code: CS = 0; SPI_Write(High_Byte); SPI_Write(LOW_Byte); CS = 1; unsigned char SPI_Write(unsigned char val) { SSPSTAT.BF=0; SSPBUF = val; while(SSPSTAT.BF==0); return SSPBUF; }
Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member Oct 31, 2016 #4 Yep.... That's how I do it..
Jon Wilder Active Member Oct 31, 2016 #5 Or you can do - Code: unsigned int writeSpi(unsigned int data){ PIR1bits.SSPIF = 0; SSPBUF = (data / 256) & 0x00FF; while(!PIR1bits.SSPIF); unsigned int returndata = SSPBUF * 256; PIR1bits.SSPIF = 0; SSPBUF = data & 0x00FF; while(!PIR1bits.SSPIF); returndata += SSPBUF; return returndata; } This allows you to pass the complete 16-bit value to the procedure in a single procedure call. The procedure will return a 16-bit value.
Or you can do - Code: unsigned int writeSpi(unsigned int data){ PIR1bits.SSPIF = 0; SSPBUF = (data / 256) & 0x00FF; while(!PIR1bits.SSPIF); unsigned int returndata = SSPBUF * 256; PIR1bits.SSPIF = 0; SSPBUF = data & 0x00FF; while(!PIR1bits.SSPIF); returndata += SSPBUF; return returndata; } This allows you to pass the complete 16-bit value to the procedure in a single procedure call. The procedure will return a 16-bit value.