0

I am trying to configure a PIC24FJ256GB412 to use the SPI interface with an ADC module(ADS114S08). And now, I can view data by oscilloscope(by measuring the SDI pin). But the SPI1BUFL didn't receive correct data(always received 0x01). Below is the relevant code that I am using to configure the SPI.

uint8_t    DATA_S[1000]; // SPI data buffer for Receiving
uint8_t    DATA1;        // SPI data buffer for Receiving
uint16_t   DATA_counter=0;

void System_Initial(void)
{
    //Setup IO
    TRISDbits.TRISD0  = 0; //SCLK
    TRISDbits.TRISD5  = 0; //ADC RESET
    TRISDbits.TRISD10 = 0; //ADC START
    TRISDbits.TRISD11 = 1; //ADC DRDY
    TRISFbits.TRISF3  = 1; //SDI
    TRISFbits.TRISF4  = 0; //SDO

    ANSELDbits.ANSELD0  = 0; //Digital pins
    ANSELDbits.ANSELD5  = 0;
    ANSELDbits.ANSELD10 = 0;
    ANSELDbits.ANSELD11 = 0;
    ANSELFbits.ANSELF3  = 0;
    ANSELFbits.ANSELF4  = 0;
    
    //Setup SPI
    //MCU operates in 16MHz, ADC internal clk 4MHz
    SPI1BRGL    = 1;      //Baud rate = 4MHz
    SPI1CON1L   = 0x8020; //8bit mode //SCLK enable //Master Mode
    SPI1CON1H   = 0x2000; //AUDEN=0
    SPI1CON2L   = 0x0007; //8bit data

    //Setup PPS    
    __builtin_write_OSCCONL(OSCCON & 0xBF);
    _RP11R  = _RPOUT_SCK1OUT; // RP11 -> SCK1.
    RPINR20bits.SDI1R = 16;   // RP16 -> SDI1.
    _RP10R  = _RPOUT_SDO1;    // RP10 -> SDO1.
    __builtin_write_OSCCONL(OSCCON | 0x40);
    
}

//SPI1 - Set ADS114S08
void SPI1_SetADC()
{
    //set PGA
    //Input MUX Register, address = 02h
    //Gain setting Register, address = 03h
    SPI1BUFL = 0x42; //WREG at 02h
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x01; //Two bytes
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x01; //02h //AINP = AIN0, AINN = AIN1
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x0B; //03h //PGA enabled, Gain = 011 =8
    while( SPI1STATLbits.SPIBUSY==1);
    
    //set Mode
    //Data Rate Register, address = 04h
    //Reference Control Register, address = 05h
    SPI1BUFL = 0x44; //WREG at 04h
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x01; //Two bytes
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x14; //04h //Continuous conversion mode //0100 : 20 SPS
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x30; //05h
    while( SPI1STATLbits.SPIBUSY==1);
   
    //set Excitation Current Sources
    //Excitation Current Register1(IDACMAG), address = 06h
    //Excitation Current Register2(IDACMUX), address = 07h
    SPI1BUFL = 0x46; //WREG at 06h
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x01; //Two bytes
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0x05; //06h //IDAC = 0101 = 500uA
    while( SPI1STATLbits.SPIBUSY==1);
    SPI1BUFL = 0xF0; //07h //use IDAC1 => AIN0
    while( SPI1STATLbits.SPIBUSY==1);
}

//SPI1 - RREG
void SPI1_RREG()
{ 
    SPI1BUFL = 0x20; //send RREG Command //read 00h
    while( SPI1STATLbits.SPIBUSY==1);
    
    SPI1BUFL = 0x00; //1 byte
    while( SPI1STATLbits.SPIBUSY==1);

    SPI1BUFL = 0x0000; //send DUMMY data
    while( SPI1STATLbits.SPIBUSY==1); //Wait Data
    uint8_t register1=  SPI1BUFL; //get data
    DATA1  = register1;
    DATA_S[DATA_counter%1000] = register1;
}

int main(void)
{
    System_Initial();

    LATDbits.LATD10 = 0; //ADC START=0
    LATDbits.LATD5 = 0;  //ADC RESET=0 //Enter RESET Status
    
    uint32_t loop = 0;
    loop = 4*4;         //MCU 16Mhz, ADC internal clk 4Mhz
    while (loop--);     //wait 4clk => ADS114S08 tw(RSL)
    LATDbits.LATD5 = 1; //ADC RESET low -> high //Enter Standby Mode
    
    loop=4096*4;
    while (loop--);     //wait 4096clk => ADS114S08 td(RSSC)
    
    SPI1_SetADC();      //set ADC Configuration
    LATDbits.LATD10 = 1; //START high -> Start Conversion

    //main while
    while(1)
    {
        DATA_counter++;
        SPI1_RREG();
    }
}

bold italic

As mentioned above, I can see data on SDI pin by oscilloscope. enter image description here And another data test. enter image description here

By the waveform that we can see on the oscilloscope. The ADC module probably works the right way.

But... Let's see the MPLAB IDE side. enter image description here I can't get the right data(SPI1BUF = 0x0001). I have tried changing baudrate lower. But the result is no different. Is there a parameter I'm missing? Thanks for your help.

------------------------------------------0530-----------------------------------------------

Hi Craig, I have tried the code.

SPI1BUFL = 0x20; //send RREG Command //read 00h
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty

SPI1BUFL = 0x00; //set 1 byte
while(SPI1STATLbits.SPITBF); // wait until the transmit buffer is empty

uint8_t register1=0;
SPI1BUFL = 0x00; //send DUMMY data
while(!SPI1STATLbits.SPIRBF); //hang until data is received
register1 =  SPI1BUFL; //get data

The results were no difference. And I recorded it.

Oscilloscope: enter image description here

IDE: enter image description here
enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
  • Are you sure you're using the correct status bits? SPI is 4 wire, full duplex. I would expect a different status bit for Tx and Rx data, etc. You're using `SPIBUSY` but other examples use `SPITBF` and `SPIRBF`. From a web search on: `SPI1STATLbits`, amongst others see: https://www.microchip.com/forums/m1096047.aspx and https://www.thisisant.com/forum/viewthread/6948 – Craig Estey May 27 '22 at 22:18
  • Hi Craig, thank you very much for your reply, you reminded me of an important idea, but I don't have a circuit board to test at the moment. If I do I'll let you know the results. – John Jhong May 28 '22 at 04:08
  • Hi @Craig , I have tried some code testing. Please see the record at question. – John Jhong May 30 '22 at 11:25
  • Just a guess, based on some of the web search results. They talk about "enhanced mode". If it's on, `SPIRBF` is only valid after 8 bytes have been received [device has a H/W FIFO?]. Turning it off makes it work. Also, you could try different dummy values to see if the readback on `SPI1BUFL` tracks the value (e.g. if so, it's just giving back what you wrote to it--not what you want). – Craig Estey May 30 '22 at 18:58
  • Also, it can be more about how the _device_ responds, so from a search on: `ADS114S08 SPI` we have: https://e2e.ti.com/support/data-converters-group/data-converters/f/data-converters-forum/874470/ads114s08-can-not-read-temperature-over-spi-from-ads114s08 – Craig Estey May 30 '22 at 19:01
  • Hi @Craig , I already set the SPI1CON1L bit 0=0 (Enhanced Buffer Enable bit, 0 = Enhanced Buffer mode is disabled). Also tried the different dummy data, like 0x0A, 0xFF... And the SDI was the same results(0x00). – John Jhong May 31 '22 at 02:17
  • https://e2e.ti.com/support/data-converters-group/data-converters/f/data-converters-forum/874470/ads114s08-can-not-read-temperature-over-spi-from-ads114s08 referenced it. I also already set the Global chop disable. It's the same results whether this bit set 1 or 0. – John Jhong May 31 '22 at 02:40
  • https://www.microchip.com/forums/m1035784.aspx There, OP has some code that got fixed in the forum. It uses `__builtin_write_OSCCONL(OSCCON | 0x40);` and sets up some more things. Different baud rate (`BRG`). See the `SPI_TXRX` function. It spins on `!SPITBE` and then `!SPIRBF` It seems to always write a byte and read a byte, but you do: write, write, read. Also, `LATBbits`? In `SPI1_SetADC`, you're doing a lot of inline spin/write. I'd put that into a function or macro to tidy up, similar to link's `SPI_TXRX` – Craig Estey May 31 '22 at 14:31
  • This may not matter depending upon the initial state, but the link does spin_on_bit,write_data but you do write_data,spin_on_bit – Craig Estey May 31 '22 at 14:41
  • Hi @Craig, all the above I have tried and the result without any progress... – John Jhong Jun 05 '22 at 14:14
  • So I tried this. I add counter into the while like this... – John Jhong Jun 05 '22 at 14:14
  • while( !SPI1STATLbits.SPIRBF) { SPI_loop++; } //hang until data is received – John Jhong Jun 05 '22 at 14:14
  • The counter 「SPI_loop」 is always zero...seems like SPI read registers never work. – John Jhong Jun 05 '22 at 14:17
  • Thank you all. I solved the problem by https://www.microchip.com/forums/m1208598.aspx – John Jhong Jun 07 '22 at 16:09
  • Please do not edit solution announcements into the question or title. See [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) You can also create your own answer, and even accept it, if your solution is not yet covered by an existing answer. See [Can I answer my own question?](https://stackoverflow.com/help/self-answer) – jmoerdyk Jun 07 '22 at 16:19
  • Hi @jmoedyk . Ok I’ll add answer later. Thanks for your reply. – John Jhong Jun 07 '22 at 23:43

1 Answers1

0

The SPI should clear SPI1BUFL every time as you transmit data. So I try the code like this.

    uint8_t SPI_EXCH(uint8_t data)
    {
       SPI1BUFL = data; // write to buffer for TX
       while(!SPI1STATLbits.SPIRBF); // wait for transfer to complete
       return SPI1BUFL; // read the received value
    }
    

And every time you try to do an SPI exchange, use the function above.