On spi you read and write at the same time, the write function returns the data received while it was writing.
So to read a value you first write the address you want to read and then you write something (anything, it doesn't matter normally) that is the same length as the data you expect to get back. 0 is a nice safe value to write.
As for the actual syntax used, the |= is in there because the code is reading a value longer than 1 byte and needs to combine multiple reads into a single value.
value |= spi.write()
is shorthand for
value = value | spi.write()
every time that is called in the code it is guaranteed that the least significant 8 bits of value will be 0 which means it's effectively the same as saying
value = value + spi.write()
If you look at the code the line afterwards is normally
value = value << 8;
which then shifts value to the left 8 bits ensuring that the least significant 8 bits are again 0.
In terms of end result
value = 0;
value |= _spi.write(0x00);
value = value << 8;
value |= _spi.write(0x00);
value = value << 8;
value |= _spi.write(0x00);
Is identical to
value = 0;
value = value + _spi.write(0x00);
value = value * 256;
value = value + _spi.write(0x00);
value = value * 256;
value = value + _spi.write(0x00);
They both read 3 bytes and combine the result into a single number.
So why do it the first way?
Because bitwise operators like | and << are just about the fastest operations possible on a CPU. Additions and multiplies can be significantly slower.
Dear all,
I am trying to connect a Direct Digital Synthesizer via the SPI interface to the mbed controller. Since I never worked with electronic boards/ circuits and C it is a bit difficult for me to find the mistakes.
I found the code from Remy for the AD 9951 (http://mbed.org/forum/helloworld/topic/1664/?page=1#comment-8348) and modified it a bit. I use it in the form posted below.
The datasheet of the AD 9954 DDS is here http://www.analog.com/static/imported-files/data_sheets/AD9954.pdf I connected the pins as follows
I get no output on the oscilloscope and in the Tera Term it just reads out c2= 0 FTWO = 0
I really don't know what is going wrong. Maybe one of you can give me a tip?
#
#
#
#
AD9954.h#
#
#
#
#
###
#
#
#
# main
#
#
#
#
#
##