Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
SagginDragon
Level 2
Level 2
5 likes given 5 questions asked 5 replies posted

 We're basing our code on the examples in the documentation but seem to be unable to get an actual reading off the ADC. Debugging has traced the problem to something about our CheckADC() function.

int main(void) {
    cy_rslt_t result;
    Cy_GPIO_Write(Batt1Out_PORT, Batt1Out_NUM, 1);
    Cy_GPIO_Write(TempPwr_PORT, TempPwr_NUM, 1);
    Cy_GPIO_Write(PayloadCtrl_PORT, PayloadCtrl_NUM, 0);
    int16_t resultADC;
    int16_t resultmV;
    int16_t voltagemVolts;
    uint32_t chan = 0UL;

    Cy_SAR_Init(BusVoltage_HW, &BusVoltage_config);  // ADC channel 0 is designated BusVoltage in config
    Cy_SAR_Enable(BusVoltage_HW);
    Cy_SAR_StartConvert(BusVoltage_HW, CY_SAR_START_CONVERT_CONTINUOUS);

/* snip #if defined block */

    int CheckADC( void ) {
        resultADC = Cy_SAR_GetResult32(BusVoltage_HW, chan);
        resultmV = Cy_SAR_CountsTo_mVolts(BusVoltage_HW, chan, resultADC);
        voltagemVolts = resultmV * 8; // 8:1 voltage divider on the input

        return 0UL;
    }
}

The resultADC expression is returning a constant value of -2048 and as a result we're getting no useful results when CheckADC() is called, despite having a 15 V (ETA: 1.875 V after an 8:1 voltage divider), 0.1 A power supply connected to that terminal.

0 Likes
1 Solution
Andri-setyabudi
Level 5
Level 5
50 likes received 50 sign-ins 25 solutions authored

Hi @SagginDragon , 

Could you please tell us the voltage reference you used ?
If you use the Internal reference as the Vref - source as shown in the pict.below

Andrisetyabudi_0-1689412001899.png

You need to Activate the AREF and call these functions : Cy_SysAnalog_Init and Cy_SysAnalogEnable before activate the SAR_ADC.
Please refer to this document on section 2.3.3.2

 As an additional note, you need to use int32_t type instead of int16_t for resultADC since function Cy_SAR_GetResult32 will return the result data as a signed 32-bit integer.

 

Warm regards,

Andri

View solution in original post

2 Replies
Andri-setyabudi
Level 5
Level 5
50 likes received 50 sign-ins 25 solutions authored

Hi @SagginDragon , 

Could you please tell us the voltage reference you used ?
If you use the Internal reference as the Vref - source as shown in the pict.below

Andrisetyabudi_0-1689412001899.png

You need to Activate the AREF and call these functions : Cy_SysAnalog_Init and Cy_SysAnalogEnable before activate the SAR_ADC.
Please refer to this document on section 2.3.3.2

 As an additional note, you need to use int32_t type instead of int16_t for resultADC since function Cy_SAR_GetResult32 will return the result data as a signed 32-bit integer.

 

Warm regards,

Andri

Nailed it, thanks man.

0 Likes