This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ADS1255: ads1255 with pic microcontroller using spi

Part Number: ADS1255
Other Parts Discussed in Thread: ADS1256

Hi

I am developing a driver for ads1255 to read analog data using spi with pic18f46k80 microcontroller

i have followed the sample code from internet but not working

And also i have read datasheet still not able to get

So need a sample code using spi with ads1255 to microcontroller

Thanks

 R Sridhar

  • Hi Sridhar,

    Here is a link to some examples of sample code from e2e. This will be your best place to start: https://e2e.ti.com/support/data-converters/f/73/t/571671?ADS1256-Sample-code-for-ADS1256-

    -Bryan

  • Hi

    I have checked your code and it is very helpful

    I am currently writing a baud rate from uC to ads1255 module, it accepts and setting the baud rate

    I am also checking my baud rate by reading register, it is working only when pickit-4 is connected that too only if i click disconnect then connect

    So i think some issue in initialization

    So can you give sample initializations for ads1255 module 

    In my code i commented 

    I will update my code for reference

    void ads_1255_channel_function(void)

    {

    //   SPI_WriteByte(ADS125X_CMD_RESET);DelayMicro(100);

    ADS125X_DRDY_Wait();

       SPI_WriteByte(ADS125X_CMD_SDATAC);DelayMicro(100);   

                  spiTx[0] = (ADS125X_CMD_WREG|ADS125X_REG_MUX);// 1st command byte

                  spiTx[1] = 0;// 2nd command byte = bytes to be read -1

       spiTx[2] = (ADS125X_MUXP_AIN4|ADS125X_MUXN_AIN0);

       SPI_WriteByte(spiTx[0]);DelayMicro(1);

       SPI_WriteByte(spiTx[1]);DelayMicro(1);

       SPI_WriteByte(spiTx[2]);DelayMicro(10);

    }

    void ads_1255_channel_read_function(void)

    {

       ADS125X_DRDY_Wait();

       SPI_WriteByte(ADS125X_CMD_SYNC);DelayMicro(10);

       ADS125X_DRDY_Wait();

       SPI_WriteByte(ADS125X_CMD_WAKEUP);DelayMicro(40);  

        spiTx[0] = ADS125X_CMD_RDATA; // 1st command byte

        spiTx[1] = 0;// 2nd command byte = bytes to be read -1

       ADS125X_DRDY_Wait();

       SPI_WriteByte(spiTx[0]);DelayMicro(10);

       ADS125X_DRDY_Wait();

       SPI_WriteByte(spiTx[1]);DelayMicro(10);

       data_rate_u8=SPI_ReadByte();DelayMicro(10);

       sprintf(ext_int_str,"%05x",data_rate_u8);

       LedDisplay(ext_int_str,0,0);Delay1sec(2);

    }

    Thanks

    R Sridhar

  • Hi 

    now my code is working and i can able to read data in polling method

    but the value is fluctuation upto 5000 counts when reading all 24 bits

    i want repeatability and how to do that?

    Thanks

    R Sridhar

  • Hi Sridhar,

    To get the best repeatability from a delta-sigma ADC, it helps to sample as slow as possible. This allows more averaging, which reduces noise.

    Please also make sure your signal chain is as noise-free as possible. For example, if you use a 24-bit ADC but have a very noisy amplifier in front of the ADC, the system performance will not be very good even though you are using a high-resolution ADC.

    -Bryan

  • Hi

    Now my ads1255 module is working with ads1255 

    I have error in reading spi data 

    my data mode is 8 bit but i am reading as 24 bit mode, so i am unable to read correct adc value 

    I am updating my code for reference to help others

    // reference voltage
    #define ADS125X_VREF (2.5f)
    #define ADS125X_OSC_FREQ (7680000)
    /*
    const defaults = {
    clkinFrequency: 7680000,
    spiFrequency: 976563,
    spiMode: 1,
    vRef: 2.5,
    };
    */
    // ADS1256 Register
    #define ADS125X_REG_STATUS 0x00
    #define ADS125X_REG_MUX 0x01
    #define ADS125X_REG_ADCON 0x02
    #define ADS125X_REG_DRATE 0x03
    #define ADS125X_REG_IO 0x04
    #define ADS125X_REG_OFC0 0x05
    #define ADS125X_REG_OFC1 0x06
    #define ADS125X_REG_OFC2 0x07
    #define ADS125X_REG_FSC0 0x08
    #define ADS125X_REG_FSC1 0x09
    #define ADS125X_REG_FSC2 0x0A
    // ADS1256 Command
    // Datasheet p. 34 / Table 24
    // All of the commands are stand-alone
    // except for the register reads and writes (RREG, WREG)
    // which require a second command byte plus data
    #define ADS125X_CMD_WAKEUP 0x00
    #define ADS125X_CMD_RDATA 0x01
    #define ADS125X_CMD_RDATAC 0x03
    #define ADS125X_CMD_SDATAC 0x0f
    #define ADS125X_CMD_RREG 0x10
    #define ADS125X_CMD_WREG 0x50
    #define ADS125X_CMD_SELFCAL 0xF0
    #define ADS125X_CMD_SELFOCAL 0xF1
    #define ADS125X_CMD_SELFGCAL 0xF2
    #define ADS125X_CMD_SYSOCAL 0xF3
    #define ADS125X_CMD_SYSGCAL 0xF4
    #define ADS125X_CMD_SYNC 0xFC
    #define ADS125X_CMD_STANDBY 0xFD
    #define ADS125X_CMD_RESET 0xFE
    #define ADS125X_BUFON 0x02
    #define ADS125X_BUFOFF 0x00
    #define ADS125X_CLKOUT_OFF 0x00
    #define ADS125X_CLKOUT_1 0x20
    #define ADS125X_CLKOUT_HALF 0x40
    #define ADS125X_CLKOUT_QUARTER 0x60
    #define ADS125X_RDATA 0x01 /* Read Data */
    #define ADS125X_RDATAC 0x03 /* Read Data Continuously */
    #define ADS125X_SDATAC 0x0F /* Stop Read Data Continuously */
    #define ADS125X_RREG 0x10 /* Read from REG */
    #define ADS125X_WREG 0x50 /* Write to REG */
    #define ADS125X_SELFCAL 0xF0 /* Offset and Gain Self-Calibration */
    #define ADS125X_SELFOCAL 0xF1 /* Offset Self-Calibration */
    #define ADS125X_SELFGCAL 0xF2 /* Gain Self-Calibration */
    #define ADS125X_SYSOCAL 0xF3 /* System Offset Calibration */
    #define ADS125X_SYSGCAL 0xF4 /* System Gain Calibration */
    #define ADS125X_SYNC 0xFC /* Synchronize the A/D Conversion */
    #define ADS125X_STANDBY 0xFD /* Begin Standby Mode */
    #define ADS125X_RESET 0xFE /* Reset to Power-Up Values */
    #define ADS125X_WAKEUP 0xFF /* Completes SYNC and Exits Standby Mode */
    // multiplexer codes
    #define ADS125X_MUXP_AIN0 0x00
    #define ADS125X_MUXP_AIN1 0x10
    #define ADS125X_MUXP_AIN2 0x20
    #define ADS125X_MUXP_AIN3 0x30
    #define ADS125X_MUXP_AIN4 0x40
    #define ADS125X_MUXP_AIN5 0x50
    #define ADS125X_MUXP_AIN6 0x60
    #define ADS125X_MUXP_AIN7 0x70
    #define ADS125X_MUXP_AINCOM 0x80
    #define ADS125X_MUXN_AIN0 0x00
    #define ADS125X_MUXN_AIN1 0x01
    #define ADS125X_MUXN_AIN2 0x02
    #define ADS125X_MUXN_AIN3 0x03
    #define ADS125X_MUXN_AIN4 0x04
    #define ADS125X_MUXN_AIN5 0x05
    #define ADS125X_MUXN_AIN6 0x06
    #define ADS125X_MUXN_AIN7 0x07
    #define ADS125X_MUXN_AINCOM 0x08
    // gain codes
    #define ADS125X_PGA1 0x00
    #define ADS125X_PGA2 0x01
    #define ADS125X_PGA4 0x02
    #define ADS125X_PGA8 0x03
    #define ADS125X_PGA16 0x04
    #define ADS125X_PGA32 0x05
    #define ADS125X_PGA64 0x06
    // data rate codes
    /** @note: Data Rate vary depending on crystal frequency.
    * Data rates listed below assumes the crystal frequency is 7.68Mhz
    * for other frequency consult the datasheet.
    */
    #define ADS125X_DRATE_30000SPS 0xF0
    #define ADS125X_DRATE_15000SPS 0xE0
    #define ADS125X_DRATE_7500SPS 0xD0
    #define ADS125X_DRATE_3750SPS 0xC0
    #define ADS125X_DRATE_2000SPS 0xB0
    #define ADS125X_DRATE_1000SPS 0xA1
    #define ADS125X_DRATE_500SPS 0x92
    #define ADS125X_DRATE_100SPS 0x82
    #define ADS125X_DRATE_60SPS 0x72
    #define ADS125X_DRATE_50SPS 0x63
    #define ADS125X_DRATE_30SPS 0x53
    #define ADS125X_DRATE_25SPS 0x43
    #define ADS125X_DRATE_15SPS 0x33
    #define ADS125X_DRATE_10SPS 0x23
    #define ADS125X_DRATE_5SPS 0x13
    #define ADS125X_DRATE_2_5SPS 0x03

    char dummy=0;
    /**
    * @brief waits for DRDY pin to go low
    * @param *ads pointer to ads handle
    */
    uint8_t ADS125X_DRDY_Wait(){
    while(Read_ADC_DRDY_Func());
    // while(!Read_ADC_DRDY_Func());
    // while(Read_ADC_DRDY_Func());
    return 0;
    }

    //#include "Menu.h" //If included automatically remove it to avoid errors
    /* MAIN APPLICATION */
    static long i = 0, j;
    unsigned long ext_int_u32;
    char ext_int_str[10],val[10];
    unsigned char abc=0xAA;
    extern void SPI_WriteByte(uint8_t byte);
    extern uint8_t SPI_ReadByte(void);
    uint8_t spiTx[10],spiRx[10],data_rate_u8;
    unsigned char a[3];
    long total,avg_total;
    int ADC_Array[10];
    long adc_val_fil=0;
    int front = 0;
    int rear = -1;
    int itemCount = 0,TempCnt;
    extern long ADC_Moving_Avg;
    extern long WeightADC;
    extern long WeightADCFilterCnt;
    extern long Avg_ADC_Buff[100];
    extern long Avg_ADC_Cnt;
    void RemoveData()
    { if(itemCount == (unsigned int)ADC_Moving_Avg)
    {itemCount--;}
    else
    {}
    }
    long MOV_AVG_DataInsert(long data)
    { int l=0;
    long sampling_adc=0;
    long final_adc_data=0;
    // if the adc buffer reaches the maximum limit this function decrement the
    // buffer variable
    RemoveData();
    // insert the new adc data into buffer
    if(itemCount != (unsigned int)ADC_Moving_Avg)
    { if(rear == ((unsigned int)ADC_Moving_Avg)-1)
    {rear = -1;}
    ADC_Array[++rear] = data;
    itemCount++;
    }
    //read the value in the buffer
    for(l=0;l<itemCount;l++)
    {sampling_adc = sampling_adc + ADC_Array[l];}
    final_adc_data= sampling_adc/itemCount;
    return final_adc_data;
    }
    void ClearAdcbuffer()
    { rear=-1;
    itemCount=0;
    memset(ADC_Array,0,10);
    }
    long Average_Filter_Func(void)
    {
    long Value1 =0;
    for(TempCnt=0;TempCnt<ADC_Moving_Avg;TempCnt++) // 1 to 99 entry
    {Value1 += Avg_ADC_Buff[TempCnt];}
    Value1 /= ADC_Moving_Avg;
    return(Value1);
    }
    void ADC_Filter_Func(long Value)
    {
    WeightADC += Value;
    WeightADCFilterCnt++;
    //ADC_Moving_Avg=5;
    if(WeightADCFilterCnt >= 10) // 1 to 9
    {
    Avg_ADC_Buff[Avg_ADC_Cnt++] = WeightADC / (long)10;
    if(Avg_ADC_Cnt >= ADC_Moving_Avg)
    Avg_ADC_Cnt = 0;
    WeightADC = WeightADCFilterCnt = 0;
    }
    }
    void ads_1255_status_function(void)
    {
    spiTx[0] = (ADS125X_CMD_WREG|ADS125X_REG_STATUS);// 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    spiTx[2] = 0x06;//buffer enabled & auto calib enabled
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(1);
    SPI_WriteByte(spiTx[2]);DelayMicro(10);
    }
    void ads_1255_status_read_function(void)
    {
    spiTx[0] = (ADS125X_CMD_RREG|ADS125X_REG_STATUS); // 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(10);
    data_rate_u8=SPI_ReadByte();DelayMicro(10);
    sprintf(ext_int_str,"%05x",data_rate_u8);
    LedDisplay(ext_int_str,0,0);Delay1sec(2);
    }
    void ads_1255_adcon_function(void)
    {
    spiTx[0] = (ADS125X_CMD_WREG|ADS125X_REG_ADCON);// 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    spiTx[2] = 0x47;//clkin/2,gain64
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(1);
    SPI_WriteByte(spiTx[2]);DelayMicro(10);
    }
    void ads_1255_adcon_read_function(void)
    {
    spiTx[0] = (ADS125X_CMD_RREG|ADS125X_REG_ADCON);// 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(10);
    data_rate_u8=SPI_ReadByte();DelayMicro(10);
    sprintf(ext_int_str,"%05x",data_rate_u8);
    LedDisplay(ext_int_str,0,0);Delay1sec(2);
    }
    void ads_1255_data_rate_function(void)
    {
    spiTx[0] = (ADS125X_CMD_WREG|ADS125X_REG_DRATE);// 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    spiTx[2] = 0xA1;//1000SPS//500->0x92
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(1);
    SPI_WriteByte(spiTx[2]);DelayMicro(10);
    }
    void ads_1255_data_rate_read_function(void)
    {
    spiRx[0] = (ADS125X_CMD_RREG|ADS125X_REG_DRATE); // 1st command byte
    spiRx[1] = 0;// 2nd command byte = bytes to be read -1
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiRx[0]);DelayMicro(1);
    SPI_WriteByte(spiRx[1]);DelayMicro(10);
    data_rate_u8=SPI_ReadByte();DelayMicro(10);
    sprintf(ext_int_str,"%05x",data_rate_u8);
    LedDisplay(ext_int_str,0,0);Delay1sec(2);
    }
    void ads_1255_channel_function(void)
    {
    ADS125X_DRDY_Wait();
    spiTx[0] = (ADS125X_CMD_WREG|ADS125X_REG_MUX);// 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    spiTx[2] = 0x01;
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(1);
    SPI_WriteByte(spiTx[2]);DelayMicro(10);
    }
    void ads_1255_channel_read_function(void)
    {
    spiTx[0] = (ADS125X_CMD_RREG|ADS125X_REG_MUX);; // 1st command byte
    spiTx[1] = 0;// 2nd command byte = bytes to be read -1
    ADS125X_DRDY_Wait();
    SPI_WriteByte(spiTx[0]);DelayMicro(1);
    SPI_WriteByte(spiTx[1]);DelayMicro(10);
    data_rate_u8=SPI_ReadByte();DelayMicro(10);
    sprintf(ext_int_str,"%05x",data_rate_u8);
    LedDisplay(ext_int_str,0,0);Delay1sec(2);
    }
    void ads_1255_self_cal_function(void)
    {
    ADS125X_DRDY_Wait();
    SPI_WriteByte(ADS125X_SYSOCAL);DelayMicro(10);
    ADS125X_DRDY_Wait();
    SPI_WriteByte(ADS125X_SYSGCAL);DelayMicro(10);
    ADS125X_DRDY_Wait();
    SPI_WriteByte(ADS125X_SELFOCAL);DelayMicro(10);
    ADS125X_DRDY_Wait();
    SPI_WriteByte(ADS125X_SELFGCAL);DelayMicro(10);
    ADS125X_DRDY_Wait();
    SPI_WriteByte(ADS125X_SELFCAL);DelayMicro(10);
    }
    void ads_1255_read_function(void)
    {
    ads_1255_channel_function();
    ads_1255_data_rate_function();
    ads_1255_data_rate_read_function();
    ads_1255_self_cal_function();
    ads_1255_adcon_function();
    }
    unsigned long shift_8_bit_value_to_24_bit_value(char arr[])
    {
    unsigned long Adcdata=0;
    unsigned char ADC_Byte=0;
    for(ADC_Byte=0;ADC_Byte<3;ADC_Byte++)
    {
    Adcdata = (Adcdata|arr[2-ADC_Byte]);
    Adcdata <<= 8;
    }
    return Adcdata;
    }
    void ads_1255_read_adc_function(void)
    {
    memset(spiRx,0,sizeof(spiRx));
    ADS125X_DRDY_Wait();
    // SPI_WriteByte(ADS125X_CMD_SYNC);DelayMicro(10);
    // while(!Read_ADC_DRDY_Func())DelayMicro(1);
    // SPI_WriteByte(ADS125X_CMD_WAKEUP);DelayMicro(40);
    spiRx[0] = (ADS125X_CMD_RDATA); // 1st command byte
    spiRx[1] = 0;// 2nd command byte = bytes to be read -1
    spiRx[2] = 0;// 3rd command byte = bytes to be read -1
    spiRx[3] = 0;// 4th command byte = bytes to be read -1
    // spiRx[4] = 0;// 4th command byte = bytes to be read -1
    total=0;
    SPI_WriteByte(spiRx[0]);DelayMicro(8);
    SPI_WriteByte(spiRx[1]);DelayMicro(1); a[2]=SPI_ReadByte();DelayMicro(1);
    SPI_WriteByte(spiRx[2]);DelayMicro(1);a[1]=SPI_ReadByte();DelayMicro(1);
    SPI_WriteByte(spiRx[3]);DelayMicro(1);a[0]=SPI_ReadByte();DelayMicro(1);
    total=shift_8_bit_value_to_24_bit_value(&a[0]);
    // avg_total=MOV_AVG_DataInsert(total);
    AdcVal=(total>>15); // Getting Raw Adc value actual value for full range is 1000000
    ADC_Moving_Avg=100;
    // Setup.ADCFilter=2;
    Wadccnt++; //ADC FILTER FUNCTION
    WeightAdc += AdcVal;
    if(Wadccnt >= Setup.ADCFilter) //trial
    {
    Wadccnt = 0;
    AdcVal = 0;
    AdcVal = WeightAdc / Setup.ADCFilter;//prev
    AdcBuff[AdcCounter++] = AdcVal;
    if(AdcCounter >= 100)//prev
    if(AdcCounter >= ADC_Moving_Avg)//trial
    AdcCounter = 0;
    WeightAdc = 0;
    AdcIntr=1;
    }
    }


    void INT1_ISR(void)
    {
    if(ext_int_u32>=100)
    {ext_int_u32=0;
    // EXT_INT1_InterruptDisable();
    }
    else
    {ext_int_u32++;}
    ads_1255_read_adc_function();
    //***User Area End->code***
    EXT_INT1_InterruptFlagClear();
    // Callback function gets called everytime this ISR executes
    INT1_CallBack();
    }


    void SPI_Initialize(void)
    {
    // SPI setup
    TRISCbits.TRISC3=0;//sck
    TRISCbits.TRISC4=1;//sdi
    TRISCbits.TRISC5=0;//sdo
    SSPSTAT = 0xC0;
    SSPCON1 = 0x01;
    SSPCON2 = 0x00;
    SSPCON1bits.SSPEN = 1;
    SSPADD = 0x00;
    // PIE1bits.SSPIE = 1;
    SSPCON1bits.CKP=0;
    SPI_SetInterruptHandler(SPI_Isr);
    }

    Thanks

    R Sridhar