|
![]() Oscillators/PCA/Timers/SMBus/UART/SPI
![]() f340 SPI Duplex mode
|
| next newest topic | next oldest topic |
| Author | Topic: f340 SPI Duplex mode |
|
hafblud Member |
I am using f340 controller as a master of SPI to receive data from a sensor (ADIS16350). A 16 bit data is trasmitted and a 16 bit value has to be read in the next cycle. I am using the following code to transmit and receive simultaneously. But I cannot receive data correctly. Please rectify my routine. [This message has been edited by hafblud (edited August 10, 2010).] IP: Logged |
|
hafblud Member |
while (1) { NSSMD0 = 0 ; } IP: Logged |
|
hafblud Member |
for ADIS16350 : __ DIN: A1A2A3A4A5A6A7A8D1D2D3D4D5D6D7D8 DOUT: <-------------Prevoius Data ----> IP: Logged |
|
Scotty Member |
Hi hafblud, I am using f340 controller as a master of SPI to receive data from a sensor (ADIS16350). A 16 bit data is trasmitted and a 16 bit value has to be read in the next cycle. I am using the following code to transmit and receive simultaneously. But I cannot receive data correctly. From my understanding of the datasheet, your read sequence is faulty. Go through yourself step-by-step: This cannot work, because the ADIS16350 gives the first part of the answer of the previous command when the master transmits the third byte(or multiples of the third byte) and the second part on the fourth byte. Regards, Scotty IP: Logged |
|
hafblud Member |
Sorry for mentioning so little. I have changed the code and my new code is as follow. Also I am using 4-wire master mode, 1Mbps SCLK. I am now using SPI ISR to collect data as follow. IP: Logged |
|
hafblud Member |
while(1) { NSSMD0 = 0; SPI0DAT = 0x02; while(!txOk); txOk = 0; SPI0DAT = 0x00; // Don't Care void SPI_ISR () interrupt 6 } IP: Logged |
|
hafblud Member |
The above mentioned code should read the Supply Level of the Sensor. And it should give consistent result and correct as per conversion mentioned in the Data sheet. But I am receiving random values at the Master. This is also true for other values that I read from the sensor. I varied the voltage applied to sensor and the data read also changed but that was random as well. IP: Logged |
|
erikm Member |
'random' Does the avarage readings (somewhat) vary in accordance to the input?. Erik IP: Logged |
|
hafblud Member |
by random i mean out of range like : 00 01 CA 65 C0 08 00 08 etc. These values vary in accordance to the input but still that change is random. Is my routine ok or not (Theoretically). Am I missing some interrupt or anything imp ? [This message has been edited by hafblud (edited August 10, 2010).] IP: Logged |
|
Scotty Member |
Hi hafblud, Also I am using 4-wire master mode, 1Mbps SCLK. I have changed the code and my new code is as follow. For example, txOK is what? A bit flag? A redefined SFR bit? A port bit? Is my routine ok or not (Theoretically). Am I missing some interrupt or anything imp ? If I had to communicate interrupt driven with such a chip, my flow would look like this: Steps 1/2 and 3/4 are the made of the same code. I'd use a function with two eight bit parameters -> Command and Data. The function will initiate a SPI transfer by setting the SPI interrupt flag. Regards, Scotty [This message has been edited by Scotty (edited August 10, 2010).] IP: Logged |
|
hafblud Member |
Thank you very much for suggestion. I will try to implement them as well. As far as complete code is concerned , few weeks back you helped me correct the SPI usage. So I took it that my SPI initialization part is correct. Also why 3-wire approach is better than 4-wire in this case : How will Interrupt generated in case of the 3-wire (& 16 bit data). I thought 8 bit completion will generate an interrupt. IP: Logged |
|
Scotty Member |
Hi hafblud, Also why 3-wire approach is better than 4-wire in this case : How will Interrupt generated in case of the 3-wire (& 16 bit data). I thought 8 bit completion will generate an interrupt. Regards, Scotty IP: Logged |
|
hafblud Member |
What is diff between Skipped pin & GPIO ? . . Use of 3-wire and manual /SS also didn't work. It is still reading garbage as well as meaning full data. IP: Logged |
|
erikm Member |
have you configured to SCLK starting high? Erik IP: Logged |
|
Scotty Member |
Hi hafblud, What is diff between Skipped pin & GPIO ? Use of 3-wire and manual /SS also didn't work. It is still reading garbage as well as meaning full data. Erik made a also good hint, which he wouldn't have to do if... there were complete code... Regards, Scotty IP: Logged |
|
erikm Member |
Erik made a also good hint, which he wouldn't have to do if... there were complete code... I would still have made the hint, it gives the OP an opportunity to study the datasheet. Erik IP: Logged |
|
hafblud Member |
#include "C8051F340.h" sbit LED1 = P2^2; // Debug LEDs on The uC Board sbit SCK = P0^0; unsigned char cc; bit transmitted = 0 , ss = 0; void main () NSS = 1; for(k=1;k<18;k++); // Wait for conversion of Data SPI0DAT = 0x0F; NSS = 1;
} void init_s_io_t () PCA0MD &= ~0x40; IP: Logged |
|
hafblud Member |
This is the complete code. I monitored all the signal at the sensor pins. They are as per requirement in the data sheet of sensor except the data out is not OK. IP: Logged |
|
erikm Member |
except the data out is not OK. which DOUT sensor or uC? Erik IP: Logged |
|
hafblud Member |
I am using an oscilloscope to monitor din, dout, /ss & sck of the sensor. The sensor is not transmitting data as expected. e.g. If 0x02 or 0x03 is transmitted to sensor it should return a 16 bit value which give supply of the sensor. IP: Logged |
|
Scotty Member |
Hi hafblud, If 0x02 or 0x03 is transmitted to sensor it should return a 16 bit value which give supply of the sensor. Regards, Scotty [This message has been edited by Scotty (edited August 13, 2010).] IP: Logged |
|
hafblud Member |
Reading supply is just an example to explain my point. Its behavior was similar to all the commands (not sending valid data). I am working on a f340-DK, so i burn the code and reset it every time i have to use. If u are refering to sensor, no i am not resetting it (can it be a problem ?). Anyway I got it right this time . 1) 3-wire master These settings are working perfectly. Thanyou very much for your guidance. [This message has been edited by hafblud (edited August 13, 2010).] IP: Logged |
|
Scotty Member |
Hi hafblud, If u are refering to sensor, no i am not resetting it (can it be a problem ?). Anyway I got it right this time . These settings are working perfectly. Thanyou very much for your guidance. Regards, Scotty IP: Logged |
|
hafblud Member |
that error was one thing but main problem was solved by you guys which was transmission of 16 bits in two cycles. Thanks again Regards IP: Logged |
All times are CT (US) | next newest topic | next oldest topic |
![]() |
|
Have you seen our MCU Knowledge Base?