CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC18F66K40 SPI2 Communication Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Mrinmoy Dey



Joined: 23 Jan 2018
Posts: 44

View user's profile Send private message

PIC18F66K40 SPI2 Communication Problem
PostPosted: Mon Apr 03, 2023 1:50 am     Reply with quote

Hi,
I am using PIC18F66K40 in one of my project to communicate with a peripheral ADC module ADS131M04 over SPI.
After setting up initial SPI communication(Here I have used SPI2 port of the controller) from firmware I have reset the device at very start and as per peripheral device's datasheet it should return 0xFF24 as reset confirmation command.

When I am capturing the data in oscilloscope, It looks perfect as per clock and SDI sequence which I have attached below.
[img]https://ibb.co/dKn07Yz[/img]
In the image yellow line is SCL and green line is SDI.

But when I have received data in register the 24-bit or 3bytes received as
byte 1 = 01111111
byte 2 = 10010010
byte 3 = 00000000
Though the peripheral device command length is 16-bit but its default spi word length is 24-bit; so last byte is padding byte.

Here the problem is MSB of the 1st byte is zero and then each byte shifted by 1bit right but this is not reflected in oscilloscope.

I can't figure out any probable reason of it. I am using CCS version 5.083.
If anyone having any idea about that will be helpful.

Code:


void ADS131M0x_SPI_write_read(unsigned int8 *data_in,unsigned int8 *out_data,unsigned int8 len)
{
    unsigned int8 wr_byte = 0;
   
    // length is in byte.
    while(len != 0)
    {
        wr_byte = *out_data;
        *data_in = spi_read2(wr_byte);       
       
        out_data++;
        data_in++;
        len--;
    }   
   
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 03, 2023 2:14 am     Reply with quote

Post your SPI setup line.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Mon Apr 03, 2023 2:46 am     Reply with quote

Yes.
Being one bit shifted, suggests you are using the wrong SPI mode.
Mode 1 I think is needed for this chip, so SPI_L_TO_H in the setup.

However I have to say, why make things complex for yourself?.
Just use #USE_SPI, select mode1, and bits=24. Let the compiler do
the work for you. Work with int32 values.

However, you could also set the WLENGTH parameter to give 32bits
and do everything with 32bit transfers.

What it replies with will depend on what you are sending in out_data.
You do understand, that the reply, is in the frame _following_ the command.
So you actually need to send the command as one operation, then
clock a second frame for the reply.
The first byte back, when a new command is sent, is the response to
the last command sent.
I'd honestly configure the chip to 16bit mode. Otherwise you are wasting
space and time. Each 'frame' is normally six words long. So working
with 16bit word length, twelve bytes. With 24bit word length, eighteen
bytes.
Mrinmoy Dey



Joined: 23 Jan 2018
Posts: 44

View user's profile Send private message

PostPosted: Mon Apr 03, 2023 6:27 am     Reply with quote

My spi2 setup code is -
Code:


void InitSPI2(void)
{
   
    /* Initialize MSSP; SPI2 of the controller selecting Mode of SPI communication,
     SPI clock polarity and phase and SPI communication speed. Here as per peripheral device's data-sheet CPOL = 0;CPHA = 1*/               
   
    setup_spi2(SPI_MASTER | SPI_SCK_IDLE_LOW | SPI_CLK_DIV_16 | SPI_XMIT_L_TO_H | SPI_SAMPLE_AT_MIDDLE);

    // Make The chip select PIN Idle high.
    output_high(PIN_ADS131M0x_SPI_SS);
}





@Ttelmah
we want to utilize full resolution of the peripheral ADC module of 24-bit for our application. so truncate to 16-bit will affect the ADC output data quality.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Mon Apr 03, 2023 10:43 am     Reply with quote

OK.
So use '#use SPI', and 24bit mode.
A lot simpler.
Your setup is wrong. That selects mode0. You need:

setup_spi2(SPI_MASTER | SPI_CLK_DIV_16 | SPI_L_TO_H);

Do you really want to clock that slow?.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Tue Apr 04, 2023 1:58 am     Reply with quote

A couple of comments.
You do realise that running at 3.3v (which you need to be to talk to this
ADC), you would be slightly better to use the LF chip, rather than the F.
Draws a little less power, and has lower voltages for things like RAM
data retention.
It is just slightly better optimised for operation at the lower voltage.

Are you using the SYNC/RESET pin?. Otherwise you have to do the
repeat read sequence to clear the FIFO when starting a transfer.
Mrinmoy Dey



Joined: 23 Jan 2018
Posts: 44

View user's profile Send private message

PostPosted: Wed Apr 05, 2023 1:54 am     Reply with quote

@Ttelmah
According to your recommended spi setup function the communication is happening perfectly right now and without any bit shift. Thank you very much.

Now other points on the same topic -
1. Initially I am using lower clock speed for testing. Once all set I will enhance it. Although for this case controller side clock freq is 20MHz.

2. Yes, we are using SYNC/RESET pin. so synchronization will be proper and clearing FIFO will be easier.

Thank you once again. Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group