SPI Hardware 16Bit

Status
Not open for further replies.

Suraj143

Active Member
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;
                 }
 
Clear the correct CS, then you send the high byte, then the low byte, then set CS
 
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;
                 }
 
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.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…