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.

关于CC2530 ADC采样

我遇到一个奇葩问题,我的程序是这样的  P0_1 = 1; P0_5 = 0; powerSrcVoltage = HalAdcRead (HAL_ADC_CHANNEL_2, HAL_ADC_RESOLUTION_8);//只要加ADC采样这句话 P0_1, P0_5的配置好像就被AD的配置覆盖了,输出值都变成0了,如果把ADC注释掉就没问题,请问这怎么回事
  • 应该没影响啊。建议提供下相关的代码。

     HalAdcRead (HAL_ADC_CHANNEL_2, HAL_ADC_RESOLUTION_8)的函数如下:

    uint16 HalAdcRead (uint8 channel, uint8 resolution)
    {
      int16  reading = 0;
    
    #if (HAL_ADC == TRUE)
    
      uint8   i, resbits;
      uint8   adctemp;
      volatile  uint8 tmp;
      uint8  adcChannel = 1;
      uint8  reference;
    
      /* store the previously set reference voltage selection */
      reference = ADCCON3 & HAL_ADC_REF_BITS;
    
      /*
      * If Analog input channel is AIN0..AIN7, make sure corresponing P0 I/O pin is enabled.  The code
      * does NOT disable the pin at the end of this function.  I think it is better to leave the pin
      * enabled because the results will be more accurate.  Because of the inherent capacitance on the
      * pin, it takes time for the voltage on the pin to charge up to its steady-state level.  If
      * HalAdcRead() has to turn on the pin for every conversion, the results may show a lower voltage
      * than actuality because the pin did not have time to fully charge.
      */
      if (channel < 8)
      {
        for (i=0; i < channel; i++)
        {
          adcChannel <<= 1;
        }
      }
    
      /* Enable channel */
      ADCCFG |= adcChannel;
    
      /* Convert resolution to decimation rate */
      switch (resolution)
      {
        case HAL_ADC_RESOLUTION_8:
          resbits = HAL_ADC_DEC_064;
          break;
        case HAL_ADC_RESOLUTION_10:
          resbits = HAL_ADC_DEC_128;
          break;
        case HAL_ADC_RESOLUTION_12:
          resbits = HAL_ADC_DEC_256;
          break;
        case HAL_ADC_RESOLUTION_14:
        default:
          resbits = HAL_ADC_DEC_512;
          break;
      }
    
      /* read ADCL,ADCH to clear EOC */
      tmp = ADCL;
      tmp = ADCH;
    
      /* Setup Sample */
      adctemp = ADCCON3;
      adctemp &= ~(HAL_ADC_CHN_BITS | HAL_ADC_DEC_BITS | HAL_ADC_REF_BITS);
      adctemp |= channel | resbits | (reference);
    
      /* writing to this register starts the extra conversion */
      ADCCON3 = adctemp;
    
      /* Wait for the conversion to be done */
      while (!(ADCCON1 & HAL_ADC_EOC));
    
      /* Disable channel after done conversion */
      ADCCFG &= (adcChannel ^ 0xFF);
    
      /* Read the result */
      reading = (int16) (ADCL);
      reading |= (int16) (ADCH << 8);
    
      /* Treat small negative as 0 */
      if (reading < 0)
        reading = 0;
    
      switch (resolution)
      {
        case HAL_ADC_RESOLUTION_8:
          reading >>= 8;
          break;
        case HAL_ADC_RESOLUTION_10:
          reading >>= 6;
          break;
        case HAL_ADC_RESOLUTION_12:
          reading >>= 4;
          break;
        case HAL_ADC_RESOLUTION_14:
        default:
          reading >>= 2;
        break;
      }
    #else
      // unused arguments
      (void) channel;
      (void) resolution;
    #endif
    
      return ((uint16)reading);
    }