SPI PIC18F458 interfacing. Problem with reading data...

Status
Not open for further replies.

Chaitanya Varma D

Newbie level 4
Joined
Sep 21, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
74
HI there,
any help would be appreciated..

I am trying to read from the AD0831 ADC through the SPI inteface in proteus lite sim . It seems that I have set the correct flags and every thing but the ADC doesnot communiccate with the micocontroller at all.

This is my configuration:
SCK is RC3
SDI is RC4
SS is RA5.

I think the algorithm for reading from an external device is :
1. first make the SSbar low and the ADC conversion starts and then,
2. and then make a loop such that the SSPSTATbits.BF is set.
3. and then move the content of SSPBUF to another register
4. clear SSPBUF and
5. make SSBAR high....

this is the algorithm i am following to implement the SPI read....
I donot know where am I going wrong...
Please let me know if I have to make any modification in the circuit..

attached in the text file is the code ...
and the file extensions foe datasheet.......


Thanks in advance....

View attachment SPI_READ_CODE.txt

and the datasheet links are.....
https://ww1.microchip.com/downloads/en/devicedoc/41159d.pdf
https://www.ti.com/lit/ds/symlink/adc0831-n.pdf
 

Where to start: that MCU is ancient; is there any reason you are using that chip and not a more up to date one?
Do you understand the basics of how an SPI peripheral works? The mechanics of how to use the MSSP peripheral in SPI mode are fairly well explained in the data sheet but you need to know some basics of SPI exchanges. For a start, there are two 'modes' of operations - master and slave. The master (which your PIC is - NOT the ADC chip; step 2 on page 15 of the Ti data sheet is very clear about this) will generate the SCK signal only while an exchange is in progress. An exchange is initiated when you write a value into the SSPBUF register.
The typical way to use a SPI mater is:
Code:
// drop the \SS\ line to select the slave device
SSPBUF = valueToSend;    // Initiate an exchange by sending something
while( !SSPSTATSbits.BF);  // Wait until the exchange is complete
valueReceived = SSPBUF;  // Save the value that you have received
// Raise the \SS\ line to deselect the slave device
Within the MCU, you need to make sure the oscillator is set up correctly and the port pins are set to input or output correctly. You therefore need to SCK, SDO and whatever line you want for the \SS\ signal to be outputs, and the SDI line to be an input. As for the oscillator, the ADC chip has a clock rate between 10KHz and 400KHz (the Fclk parameter at the top of the table on page 6) and so you need to know the system clock speed (generated by the crystal or external clock or, in your case, the RC components), the PLL configuration and hence the Fosc frequency. From this you can work out the required division to be set in the SSPCON1bits.SSPM.
Now the value you need to send to the slave depends on what the slave is expecting. In your case you will need to read the Ti data sheet, and Figure 18 and "The Digital Interface" section on page 15 seem to be relevant.
Now, looking at the Ti data sheet, you need to deliver at least 12 clock pulses per conversion. This is a little tricky for the PIC as it will only work in 8-bit increments. Therefore you need to see if you can exchange 2 8-bit values (you do this by repeating the code between lowering and raising the \SS\ line) have have the ADC accept it; if it MUST have 12 clock pulses the you will need to "bit-bang" the SPI interface and not use the PIC hardware - that will make things a lot harder for your code but it is not impossible and there are a number of examples of how to do this on the internet).
In your configuration section, you don't need to set anything to the default settings - in this case just about all of the protection bits will be the way you want them if you leave them alone.
However, I strongly recommend that you DON'T enable the BOR until you have everything working correctly; otherwise any flicker on the power lines will have you chasing down problems that don;t exist in your code.
Enough for now
Susan
 
Also, the device has both PORT and LAT registers. The LAT registers are there to get around the "read-modify-write" problem (there are many descriptions and solutions on the Internet) so the golden rule is "read from the PORT, write to the LAT".
This especially applies when you are manipulating individual bits as the code of the Microchip devices (except the PIC32 families) tends to read the whole register, do what it needs to with the bit and then write the register back again. The bit manipulation instructions do not work on the SFRs "in place"
Susan
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…