This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
When I use the code below, I get the expected recieved data. However, when I change the SPICHAR bits in the SPICCR register from 16 bit to 8 bit, I get values that are multiplied by a factor of 256. Why is that happening?
void spi_init() { SpiaRegs.SPICTL.all =0x0006; // Enable master mode, normal phase, enable talk, and SPI int disabled. SpiaRegs.SPIBRR =0x007F; // SPI Baud Rate = LSPCLK/128 SpiaRegs.SPICCR.all =0x009F; // 16-bit char bits, SPI loopback mode enabled, Relinquish SPI from Reset SpiaRegs.SPIPRI.bit.FREE = 1; // Set so breakpoints don't disturb xmission SpiaRegs.SPIFFTX.all=0xE040; SpiaRegs.SPIFFRX.all=0x2041; SpiaRegs.SPIFFCT.all=0x0; }
The code below is what I use to test the SPI communication.
Void MotorPositionSaveTask() { unsigned int MotorPosition = 0; unsigned int i = 0; while(1) { while(SpiaRegs.SPIFFTX.bit.TXFFST); // Wait until the FIFO Buffer is empty and the chip select line is deactivated SpiaRegs.SPITXBUF = 0x06; // Set write enable latch (WREN) while(SpiaRegs.SPIFFTX.bit.TXFFST); // Wait until the FIFO Buffer is empty and the chip select line is deactivated SpiaRegs.SPITXBUF = 0x02; // Write memory data (WRITE) while(SpiaRegs.SPIFFTX.bit.TXFFST >= 4); // Wait until the FIFO Buffer is clear to send while keeping the chip select line is activate SpiaRegs.SPITXBUF = 0x00; // Memory address of write location (MSB) while(SpiaRegs.SPIFFTX.bit.TXFFST >= 4); // Wait until the FIFO Buffer is clear to send while keeping the chip select line is activate SpiaRegs.SPITXBUF = MotorPosition++&0x00FF; // Data to Write (LSB) for(i=0;i<4;i++) rdata[i] = SpiaRegs.SPIRXBUF; // Read data (MSB) Task_sleep(1000); } }
Hi Andrew,
The SPI receive register is a shift register where the MSB is shifted out first. If you use 8-bit data configuration, you have shift the data up by 8 bits. If you use 16-bit values then there is no shifting required.
So is it transmitted not shifted by 8 bits and only my reciever needs to shift the message by 8 bits or does it get transmitted shifted out and the device recieving the message has to shift the bits by 8?
When you transmit an 8-bit character, you need to shift it << by 8 bits. The received data does not need to be shifted.