Page 1 of 1

External ADC to 18F2553 via SPI

Posted: Mon Feb 11, 2013 5:35 pm
by randomrichard
Hello All
I am trying to build a prototype to read 16bit data from an external ADC (AD977) via the 18F2553's SPI pins and using the SPI legacy component. I have been able to write a FCF to collect 1.5 bytes of data from the PIC's internal 12bit ADC by using the component macro's "readasint" option, convert this to a string as shown in MM's ADC to USB example, and then send it successfully to the internal USB module for onward transmission.

The problem is that the SPI in Flowcode5 for PIC doesn't have the "readasint" or other binary convertor options but only has "SPI_Get_Char" and "SPI_Get_String" options that are not useful in a digital world. I have checked my Flowcode5 for dsPIC and that too has no facility for collecting digital data from an external ADC but only strings and characters. Can anyone advise me if there is a way round this brick wall.
Thanks
Richard Clarke

Re: External ADC to 18F2553 via SPI

Posted: Mon Feb 11, 2013 6:48 pm
by kersing
The SPI component macro's SPI_Get_Char reads one 8 bit byte. SPI_Get_String reads the specified number of bytes and stores them in a String. The names of the macros can be confusing as they could be interpreted to return ASCII characters/strings but they do return byte(s).

To read an integer value (where the most significant bit is transmitted first) there are two options:
- two calls to SPI_Get_Char, store the result of the first call in (byte) b1 and of the second call in (byte) b2, calculation: i = ( b1 << 8 ) + b2
- one call to SPI_Get_String with length 2, store result in String s (length at least 2), calculation: i = ( s[0] << 8 ) + s[1]

In both cases i should be (unsigned) integer.

Re: External ADC to 18F2553 via SPI

Posted: Mon Feb 11, 2013 11:05 pm
by randomrichard
Kersing
Thank you very much for taking the time to give me the answer, and for the suggested manipulation of the received data.
Richard