NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » How do I send number values on SPI?

February 06, 2012
by sask55
sask55's Avatar

This should be simple.

I am attempting to send a series of higher precision large number values from a slave to the master on the SPI connection. The values I have are held as int32_t type variables in the slave but will eventually end as Double type variables after some necessary mathematical calculations are done using their values. The values can be sent and read at the master as ether int32 or double type numbers. The problem I am having is trying to represent ether one of these number type variables as a string of one byte variables. Or; to but It another way how do I send these multiple byte variables numbers as a sequence of one byte transitions over the SPI? I will require the values to be held as numbers again on the master after they are received completely.

February 06, 2012
by pcbolt
pcbolt's Avatar

@sask55

Will something like this work:

int32_t data_out, data_in;
int8_t temp, i;

for (i = 0; i < 4; i++){
  temp = (int8_t) (data_out >> (8 * i));
  spi_transmit(temp);
}

data_in = 0;
for (i = 0; i < 4; i++){
  spi_receive(temp);
  data_in += (temp << (8 * i));
}
February 07, 2012
by sask55
sask55's Avatar

Yes I think that may work.

I should just shift in increments of 8 to break up or construct the larger variables.

The data stream is originating from digital calliper outputs. This data is read by the slave MCUs as a continuing stream of 24 bit serial outputs. I was reading the calliper output into an int32_t variable. I can see that it may make more sense to read the calliper output into sets of three uint8 variables. I should then be able to send the sets over the SPI and fill the 24 LSB of an int32 variable for each reading. I could then do the math required to interpret the readings coming from all three of the slaves in the master MCU.

Thanks.

Post a Reply

Please log in to post a reply.

Did you know that one NerdKits customer controlled a laser pointer with his computer using a microcontroller? Learn more...