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.

ADC采集电源电压问题



如题。我的代码如下

uint16 adc;

HalAdcSetReference( HAL_ADC_REF_125V );

adc = HalAdcRead( HAL_ADC_CHN_VDD3, HAL_ADC_RESOLUTION_10 );

float volt = (adc*1.25*3)/512;

请问老师上面的代码正确么?我用的usb供电,为何显示的电压为3.6v?

  • /*********************************************************************
     * @fn      battMeasure
     *
     * @brief   Measure the battery level with the ADC and return
     *          it as a percentage 0-100%.
     *
     * @return  Battery level.
     */
    static uint8 battMeasure( void )
    {
      uint16 adc;
      uint8 percent;
    
      /**
       * Battery level conversion from ADC to a percentage:
       *
       * The maximum ADC value for the battery voltage level is 511 for a
       * 10-bit conversion.  The ADC value is references vs. 1.25v and
       * this maximum value corresponds to a voltage of 3.75v.
       *
       * For a coin cell battery 3.0v = 100%.  The minimum operating
       * voltage of the CC2540 is 2.0v so 2.0v = 0%.
       *
       * To convert a voltage to an ADC value use:
       *
       *   (v/3)/1.25 * 511 = adc
       *
       * 3.0v = 409 ADC
       * 2.0v = 273 ADC
       *
       * We need to map ADC values from 409-273 to 100%-0%.
       *
       * Normalize the ADC values to zero:
       *
       * 409 - 273 = 136
       *
       * And convert ADC range to percentage range:
       *
       * percent/adc = 100/136 = 25/34
       *
       * Resulting in the final equation, with round:
       *
       * percent = ((adc - 273) * 25) + 33 / 34
       */
    
      // Call measurement setup callback
      if (battServiceSetupCB != NULL)
      {
        battServiceSetupCB();
      }
    
      // Configure ADC and perform a read
      HalAdcSetReference( HAL_ADC_REF_125V );
      adc = HalAdcRead( battServiceAdcCh, HAL_ADC_RESOLUTION_10 );
    
      // Call measurement teardown callback
      if (battServiceTeardownCB != NULL)
      {
        battServiceTeardownCB();
      }
    
      if (adc >= battMaxLevel)
      {
        percent = 100;
      }
      else if (adc <= battMinLevel)
      {
        percent = 0;
      }
      else
      {
        if (battServiceCalcCB != NULL)
        {
          percent = battServiceCalcCB(adc);
        }
        else
        {
          uint16 range =  battMaxLevel - battMinLevel + 1;
    
          // optional if you want to keep it even, otherwise just take floor of divide
          // range += (range & 1);
          range >>= 2; // divide by 4
    
          percent = (uint8) ((((adc - battMinLevel) * 25) + (range - 1)) / range);
        }
      }
    
      return percent;
    }

    请参考TI协议栈里面的Batt Profile