cpicspiadcpic24

Havig trouble with SPI in PIC24FJ256GB412


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


Solution

  • 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.