Title: [HELP] XC peripheral libraries spi implementation Post by: biomed12 on October 14, 2016, 11:08:40 23:08 I couldn't understand the aim of writing/reading address information for spi protocol. I have learned the spi protocol from pic18f4620 datasheet but there is nothing about it. I want to use the protocol with dac and adc device. In these examples, programs just read the char array from other device. When I used the RS-232 serial communication with assembly or CCS C, I was reading first low byte of data and high byte of data. Is the SPI implementation needs the address information like I2C?
codes for master side Code: /********************************************************************* Codes for slave side Code:
Title: Re: [HELP] XC peripheral libraries spi implementation Post by: Gallymimu on October 15, 2016, 03:00:28 03:00 SPI doesn't use addressing at the hardware or protocol layer. SPI uses chip select pins to select or "address" different devices.
The application layer may require addressing (i.e. send a command, indicate an address to read write dummy bytes to get all the data read out). Sorry I don't understand your question in further detail and your code isn't very useful without understanding the implementation of putsSPI and getsSPI. It is definitely not like I2C where there is an explicit address at the protocol layer. to talk to an ADC in polling mode something like this is usually done: send SPI byte wait for transmission complete flag to set read SPI byte (so that the read buffer doesn't overflow but you can throw the byte away) For a multichannel ADC you often do the above 2 or 3 times then: send SPI dummy byte wait for transmission to finish read SPI byte (store it as it's the real ADC value now) repeat above for ??? bytes Title: Re: [HELP] XC peripheral libraries spi implementation Post by: mars01 on October 15, 2016, 01:28:37 13:28 @biomed12
In the code snippets that you've posted, from what I can see, there is a protocol implemented over the SPI. This is not just an SPI driver usage but more: one chip is designated as master, and one (or many more others) as slaves. Maybe, the ones that writed this code wanted to use the same lines for all the slave chips, including the CS. No slave will answer the data request from the master unless it receives "the password". Also, the master knows which slave is answering from the received "password". Title: Re: [HELP] XC peripheral libraries spi implementation Post by: biomed12 on October 15, 2016, 07:38:29 19:38 SPI doesn't use addressing at the hardware or protocol layer. SPI uses chip select pins to select or "address" different devices. The application layer may require addressing (i.e. send a command, indicate an address to read write dummy bytes to get all the data read out). Sorry I don't understand your question in further detail and your code isn't very useful without understanding the implementation of putsSPI and getsSPI. It is definitely not like I2C where there is an explicit address at the protocol layer. to talk to an ADC in polling mode something like this is usually done: send SPI byte wait for transmission complete flag to set read SPI byte (so that the read buffer doesn't overflow but you can throw the byte away) For a multichannel ADC you often do the above 2 or 3 times then: send SPI dummy byte wait for transmission to finish read SPI byte (store it as it's the real ADC value now) repeat above for ??? bytes Dear Gallymimu, Thanks for your advices. I am trying to read adc values from 10-bit adc(MCP3008, Microchip). But, I am always getting bad bytes which are not expected bytes. For example, I put the analog input pin of MCP3008 to the 5v line, I expect 1024(decimal) but always I get wrong bytes. The MCP3008 adc device sending 10-bit adc data and SPI protocol is making 8 bir communication. Is there any issue at reading these datas? Briefly, this program reads adc values from device and sends it via usart to my pc. My code: Code: void main(void) { My custom functions: Code:
Title: Re: [HELP] XC peripheral libraries spi implementation Post by: Gallymimu on October 16, 2016, 11:10:43 23:10 Have you read the data sheet for the ADC?
It's pretty clear how it works in the data sheet. If you read data out of it you will see a bunch of invalid data: data coming back is in a 3 byte format: XXXXXXXX XXXXXXY98 76543210 you need to do what I said before: set CS write 0x01 (start bit) wait for transmit to complete read SPIBUFF (throw data away) write 0xNM (where N is the mode and channel and M is don't care) wait for transmit to complete read SPIBUFF (2 lowest bits are part of your data) write 0xMM (M is don't cares) wait for transmit to complete. read SPIBUFF (lowest 8 bytes of data) unset CS Where X is garbage data Y is a null bit that is always zero and 9-0 are the 10 bits of your ADC measurement. unfortunately your code doesn't make sense. You need to realize you always have to WRITE to SPI (send clocks) in order to READ from SPI. Both actions occur simultaneously. The only asynchronous part is you WRITE to SPIBUFF to start the transmission and you READ from SPIBUFF after the transmission is over to get the data that was clocked in. Title: Re: [HELP] XC peripheral libraries spi implementation Post by: biomed12 on October 17, 2016, 02:08:11 02:08 Dear Gallymimu,
Thanks to you, I realized that I had not understood the concept of spi though I read the datasheet of MCP3008 many times. I have completely understood SPI now. Thanks. Title: Re: [HELP] XC peripheral libraries spi implementation Post by: Gallymimu on October 17, 2016, 04:46:50 04:46 Dear Gallymimu, Thanks to you, I realized that I had not understood the concept of spi though I read the datasheet of MCP3008 many times. I have completely understood SPI now. Thanks. Awesome, glad it helped! |