Apollo 3 plus ADC for Timer3A sample

  • ADC配置變量

    //*****************************************************************************
    //
    // ADC Configuration
    //
    //*****************************************************************************
    const static am_hal_adc_config_t g_sADC_Cfg =
    {
        //
        // Select the ADC Clock source.
        //
        .eClock = AM_HAL_ADC_CLKSEL_HFRC_DIV2,
    
        //
        // Polarity
        //
        .ePolarity = AM_HAL_ADC_TRIGPOL_RISING,
    
        //
        // Select the ADC trigger source using a trigger source macro.
        //
        .eTrigger = AM_HAL_ADC_TRIGSEL_SOFTWARE,
    
        //
        // Select the ADC reference voltage.
        //
        .eReference = AM_HAL_ADC_REFSEL_INT_1P5,
        .eClockMode = AM_HAL_ADC_CLKMODE_LOW_POWER,
    
        //
        // Choose the power mode for the ADC's idle state.
        //
        .ePowerMode = AM_HAL_ADC_LPMODE1,
    
        //
        // Enable repeating samples using Timer3A.
        //
        .eRepeat = AM_HAL_ADC_REPEATING_SCAN
    };

  eClock ADC時鐘源選擇

//
// ADC clock selection.
//
typedef enum
{
    AM_HAL_ADC_CLKSEL_OFF,
    AM_HAL_ADC_CLKSEL_HFRC,
    AM_HAL_ADC_CLKSEL_HFRC_DIV2
} am_hal_adc_clksel_e;

ePolarity ADC觸發極性選擇

//
// ADC trigger polarity
//
typedef enum
{
    AM_HAL_ADC_TRIGPOL_RISING,
    AM_HAL_ADC_TRIGPOL_FALLING
} am_hal_adc_trigpol_e;

eTrigger ADC觸發源選擇

//
// ADC trigger selection
//
typedef enum
{
    AM_HAL_ADC_TRIGSEL_EXT0,
    AM_HAL_ADC_TRIGSEL_EXT1,
    AM_HAL_ADC_TRIGSEL_EXT2,
    AM_HAL_ADC_TRIGSEL_EXT3,
    AM_HAL_ADC_TRIGSEL_VCOMP,
    AM_HAL_ADC_TRIGSEL_SOFTWARE = 7
} am_hal_adc_trigsel_e;

eReference ADC參考電壓選擇

//
// ADC reference selection.
//
typedef enum
{
    AM_HAL_ADC_REFSEL_INT_2P0,
    AM_HAL_ADC_REFSEL_INT_1P5,
    AM_HAL_ADC_REFSEL_EXT_2P0,
    AM_HAL_ADC_REFSEL_EXT_1P5
} am_hal_adc_refsel_e;

eClockMode ADC時鐘模式選擇


// ADC clock mode selection.
//
typedef enum
{
    AM_HAL_ADC_CLKMODE_LOW_POWER,   // Disable the clock between scans for LPMODE0.
                                    // Set LPCKMODE to 0x1 while configuring the ADC.
    AM_HAL_ADC_CLKMODE_LOW_LATENCY  // Low Latency Clock Mode. When set, HFRC and the
                                    // adc_clk will remain on while in functioning in LPMODE0.
} am_hal_adc_clkmode_e;

ePowerMode ADC低功耗模工選擇


// ADC low-power mode selection.
//
typedef enum
{
    AM_HAL_ADC_LPMODE0,  // Low Latency Clock Mode. When set, HFRC and the adc_clk
                         // will remain on while in functioning in LPMODE0.
    AM_HAL_ADC_LPMODE1   // Powers down all circuity and clocks associated with the
                         // ADC until the next trigger event. Between scans, the reference
                         // buffer requires up to 50us of delay from a scan trigger event
                         // before the conversion will commence while operating in this mode.
} am_hal_adc_lpmode_e;

eRepeat ADC週期採樣與單次採樣選擇 

//
// ADC repetition selection.
//
typedef enum
{
    AM_HAL_ADC_SINGLE_SCAN,
    AM_HAL_ADC_REPEATING_SCAN
} am_hal_adc_repeat_e;
  •  Timer配置

am_hal_ctimer_config_t g_sTimer3 =
{
    // do not link A and B together to make a long 32-bit counter.
    .ui32Link = 0,

    // Set up timer 3A to drive the ADC
    .ui32TimerAConfig = (AM_HAL_CTIMER_FN_PWM_REPEAT |
     AM_HAL_CTIMER_LFRC_32HZ),

    // Timer 3B is not used in this example.
    .ui32TimerBConfig =0,
};

 ui32Link Timer連接配置

      Set to 1 to operate this timer as a 32-bit timer instead of two 16-bit timers.

ui32TimerAConfig TimerA配置

    Configuration options for TIMERA

ui32TimerBConfig TimerB配置

     Configuration options for TIMERB

初時化ADC功能

uint32_t am_hal_adc_initialize(uint32_t ui32Module, void **ppHandle)

  1.         參數ui32Module只能爲0,超出會初時化失敗
  2.         參數void **ppHandle指向一個處理句柄指針的指針

  ADC電源配置

uint32_t am_hal_adc_power_control(void *pHandle,am_hal_sysctrl_power_state_e ePowerState,bool bRetainState)

  1. 參數void *pHandle爲處理句柄指針
  2. 參數am_hal_sysctrl_power_state_e ePowerState爲電源狀態
//
// Definition of Global Power State enumeration
//
//*****************************************************************************
typedef enum
{
  AM_HAL_SYSCTRL_WAKE,
  AM_HAL_SYSCTRL_NORMALSLEEP,
  AM_HAL_SYSCTRL_DEEPSLEEP
} am_hal_sysctrl_power_state_e;

3.    參數bool bRetainState爲是否開啓保存及恢復狀態值

         flag (if true) to save/restore peripheral state upon power state change.

ADC配置

     uint32_t am_hal_adc_configure(void *pHandle, am_hal_adc_config_t *psConfig)

  1. 參數void *pHandle爲處理句柄指針
  2. 參數am_hal_adc_config_t *psConfig爲指向ADC配置信息的指針

ADC slot配置

  uint32_t am_hal_adc_configure_slot(void *pHandle, uint32_t ui32SlotNumber,am_hal_adc_slot_config_t *pSlotConfig)

  1. 參數void *pHandle爲處理句柄指針
  2. 參數uint32_t ui32SlotNumber爲ADC slot,取值爲0~7
  3. 參數am_hal_adc_slot_config_t *pSlotConfig指向ADC slot配置
//*****************************************************************************
//
//! @brief Configuration structure for the ADC slot.
//
//*****************************************************************************
typedef struct
{
    //! Select the number of measurements to average
    am_hal_adc_meas_avg_e         eMeasToAvg;

    //! Select the precision mode
    am_hal_adc_slot_prec_e        ePrecisionMode;

    //! Select the channel
    am_hal_adc_slot_chan_e        eChannel;

    //! Select window comparison mode
    bool                          bWindowCompare;

    //! Enable the slot
    bool                          bEnabled;

} am_hal_adc_slot_config_t;

使能ADC功能

    uint32_t am_hal_adc_enable(void *pHandle)

參數void *pHandle爲處理句柄指針

timer時鐘產生控制

    uint32_t am_hal_clkgen_control(am_hal_clkgen_control_e eControl, void *pArgs)

  1. 參數am_hal_clkgen_control_e eControl爲時鐘控制
//
// Control operations.
//
typedef enum
{
    AM_HAL_CLKGEN_CONTROL_SYSCLK_MAX,
    AM_HAL_CLKGEN_CONTROL_XTAL_START,
    AM_HAL_CLKGEN_CONTROL_LFRC_START,
    AM_HAL_CLKGEN_CONTROL_XTAL_STOP,
    AM_HAL_CLKGEN_CONTROL_LFRC_STOP,
    AM_HAL_CLKGEN_CONTROL_SYSCLK_DIV2,
    AM_HAL_CLKGEN_CONTROL_RTC_SEL_XTAL,
    AM_HAL_CLKGEN_CONTROL_RTC_SEL_LFRC,
    AM_HAL_CLKGEN_CONTROL_HFADJ_ENABLE,
    AM_HAL_CLKGEN_CONTROL_HFADJ_DISABLE,
} am_hal_clkgen_control_e;

2.  參數void *pArgs指向時鐘調整控制參數,如果爲NULL則使用默認的值

清除Timer標誌

void am_hal_ctimer_clear(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment)

  1. 參數uint32_t ui32TimerNumber定時器序號,從裏取3
  2. 參數uint32_t ui32TimerSegment定時器段
#define AM_HAL_CTIMER_TIMERA                0x0000FFFF
#define AM_HAL_CTIMER_TIMERB                0xFFFF0000
#define AM_HAL_CTIMER_BOTH                  0xFFFFFFFF

定時器配置

void am_hal_ctimer_config(uint32_t ui32TimerNumber,am_hal_ctimer_config_t *psConfig)

  1. 參數uint32_t ui32TimerNumber爲定時器序號,這裏取3
  2. 參數am_hal_ctimer_config_t *psConfig指向定時器配置信息

配置定時器週期

void am_hal_ctimer_period_set(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment,uint32_t ui32Period, uint32_t ui32OnTime)

  1. 參數(uint32_t ui32TimerNumber爲定時器序號,這裏取3
  2. 參數uint32_t ui32TimerSegment爲定時器段
#define AM_HAL_CTIMER_TIMERA                0x0000FFFF
#define AM_HAL_CTIMER_TIMERB                0xFFFF0000
#define AM_HAL_CTIMER_BOTH                  0xFFFFFFFF

3.    參數uint32_t ui32Period爲定時器週期

4. 參數uint32_t ui32OnTime:set the number of clocks where the output signal is high

使能timer觸發ADC

void am_hal_ctimer_adc_trigger_enable(void) 

啓動定時器

void am_hal_ctimer_start(uint32_t ui32TimerNumber, uint32_t ui32TimerSegment)

  1. 參數uint32_t ui32TimerNumber爲定時器序號,這裏取3
  2. 參數uint32_t ui32TimerSegment爲定時器段
#define AM_HAL_CTIMER_TIMERA                0x0000FFFF
#define AM_HAL_CTIMER_TIMERB                0xFFFF0000
#define AM_HAL_CTIMER_BOTH                  0xFFFFFFFF
  • 以測量VBATT和TEMP爲例 

//*****************************************************************************
//
//! @file hello_world.c
//!
//! @brief A simple "Hello World" example.
//!
//! Purpose: This example prints a "Hello World" message with some device info.
//!
//! Printing takes place over the ITM at 1M Baud.
//!
//
//*****************************************************************************

//*****************************************************************************
//
// Copyright (c) 2019, Ambiq Micro
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
// 
// Third party software included in this distribution is subject to the
// additional license terms as defined in the /docs/licenses directory.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// This is part of revision v2.3.1-167-ge65d79147 of the AmbiqSuite Development Package.
//
//*****************************************************************************

#include "am_mcu_apollo.h"
#include "am_bsp.h"
#include "am_util.h"


const static am_hal_adc_config_t g_sADC_Cfg =
{
	.eClock = AM_HAL_ADC_CLKSEL_HFRC_DIV2,
	.ePolarity = AM_HAL_ADC_TRIGPOL_RISING,
	.eTrigger = AM_HAL_ADC_TRIGSEL_SOFTWARE,
	.eReference = AM_HAL_ADC_REFSEL_INT_1P5,
	.eClockMode = AM_HAL_ADC_CLKMODE_LOW_POWER,
	.ePowerMode = AM_HAL_ADC_LPMODE1,
	.eRepeat = AM_HAL_ADC_REPEATING_SCAN
};


am_hal_ctimer_config_t g_sTimer3 =
{
	.ui32Link = 0,
	.ui32TimerAConfig = (AM_HAL_CTIMER_FN_PWM_REPEAT | AM_HAL_CTIMER_LFRC_32HZ),
	.ui32TimerBConfig = 0
	
};

static void *g_ADCHandle;
uint16_t g_ui16ADCTEMP_code;
uint32_t g_ui32SampleCount;
uint16_t g_ui16ADCVDD_code;

void adc_init(void)
{
    am_hal_adc_slot_config_t sSlotCfg;

    //
    // Initialize the ADC and get the handle.
    //
    if ( AM_HAL_STATUS_SUCCESS != am_hal_adc_initialize(0, &g_ADCHandle) )
    {
        am_util_stdio_printf("Error - reservation of the ADC instance failed.\n");
    }

    //
    // Power on the ADC.
    //
    if (AM_HAL_STATUS_SUCCESS != am_hal_adc_power_control(g_ADCHandle,
                                                          AM_HAL_SYSCTRL_WAKE,
                                                          false) )
    {
        am_util_stdio_printf("Error - ADC power on failed.\n");
    }

    //
    // Configure the ADC.
    //
    if ( am_hal_adc_configure(g_ADCHandle, (am_hal_adc_config_t*)&g_sADC_Cfg) != AM_HAL_STATUS_SUCCESS )
    {
        am_util_stdio_printf("Error - configuring ADC failed.\n");
    }

    sSlotCfg.bEnabled       = false;
    sSlotCfg.bWindowCompare = false;
    sSlotCfg.eChannel       = AM_HAL_ADC_SLOT_CHSEL_SE0;    // 0
    sSlotCfg.eMeasToAvg     = AM_HAL_ADC_SLOT_AVG_1;        // 0
    sSlotCfg.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;        // 0

    am_hal_adc_configure_slot(g_ADCHandle, 0, &sSlotCfg);   // Unused slot
    am_hal_adc_configure_slot(g_ADCHandle, 1, &sSlotCfg);   // Unused slot
    am_hal_adc_configure_slot(g_ADCHandle, 2, &sSlotCfg);   // Unused slot
    am_hal_adc_configure_slot(g_ADCHandle, 3, &sSlotCfg);   // Unused slot
    am_hal_adc_configure_slot(g_ADCHandle, 4, &sSlotCfg);   // Unused slot
    am_hal_adc_configure_slot(g_ADCHandle, 6, &sSlotCfg);   // Unused slot

    sSlotCfg.bEnabled       = true;
    sSlotCfg.bWindowCompare = true;
    sSlotCfg.eChannel       = AM_HAL_ADC_SLOT_CHSEL_BATT;
    sSlotCfg.eMeasToAvg     = AM_HAL_ADC_SLOT_AVG_1;
    sSlotCfg.ePrecisionMode = AM_HAL_ADC_SLOT_14BIT;
    am_hal_adc_configure_slot(g_ADCHandle, 5, &sSlotCfg);   // BATT

    sSlotCfg.bEnabled       = true;
    sSlotCfg.bWindowCompare = true;
    sSlotCfg.eChannel       = AM_HAL_ADC_SLOT_CHSEL_TEMP;
    sSlotCfg.eMeasToAvg     = AM_HAL_ADC_SLOT_AVG_1;
    sSlotCfg.ePrecisionMode = AM_HAL_ADC_SLOT_10BIT;
    am_hal_adc_configure_slot(g_ADCHandle, 7, &sSlotCfg);   // TEMP

    //
    // Enable the ADC.
    //
    am_hal_adc_enable(g_ADCHandle);
}


static void timer_init(void)
{
//
// Only CTIMER 3 supports the ADC.
//
#define TIMERNUM    3
    uint32_t ui32Period = 2000; // Set for 2 second (2000ms) period

    //
    // LFRC has to be turned on for this example because we are running this
    // timer off of the LFRC.
    //
    am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_LFRC_START, 0);

    //
    // Set up timer 3A so start by clearing it.
    //
    am_hal_ctimer_clear(TIMERNUM, AM_HAL_CTIMER_TIMERA);

    //
    // Configure the timer to count 32Hz LFRC clocks but don't start it yet.
    //
    am_hal_ctimer_config(TIMERNUM, &g_sTimer3);

    //
    // Compute CMPR value needed for desired period based on a 32HZ clock.
    //
    ui32Period = ui32Period * 32 / 1000;
    am_hal_ctimer_period_set(TIMERNUM, AM_HAL_CTIMER_TIMERA,
                             ui32Period, (ui32Period >> 1));

    //
    // Set up timer 3A as the trigger source for the ADC.
    //
    am_hal_ctimer_adc_trigger_enable();


    //
    // Start timer 3A.
    //
    am_hal_ctimer_start(TIMERNUM, AM_HAL_CTIMER_TIMERA);
} // time




//
// ADC code for voltage divider from ADC ISR to base level.
//


void am_adc_isr(void)
{
    uint32_t ui32IntStatus;

    //
    // Clear timer 3 interrupt.
    //
    am_hal_adc_interrupt_status(g_ADCHandle, &ui32IntStatus, true);
    am_hal_adc_interrupt_clear(g_ADCHandle, ui32IntStatus);

    //
    // Toggle LED 3.
    //


     float fADCTempVolts;
    float fADCTempDegreesC;

    float fTrims[4];

    uint32_t ui32NumSamples = 1;
    am_hal_adc_sample_t sSample;

    //
    // Go get the sample.
    //

    //
    // Emtpy the FIFO, we'll just look at the last one read.
		am_util_stdio_printf("FIFO COUNT=%d\n",AM_HAL_ADC_FIFO_COUNT(ADC->FIFO));
    
    while ( AM_HAL_ADC_FIFO_COUNT(ADC->FIFO) )
    {
      ui32NumSamples = 1;
      am_hal_adc_samples_read(g_ADCHandle, true, NULL, &ui32NumSamples, &sSample);
      
      //
      // Determine which slot it came from?
      //
      if (sSample.ui32Slot == 5 )
      {
        //
        // The returned ADC sample is for the battery voltage divider.
        //
        g_ui16ADCVDD_code = AM_HAL_ADC_FIFO_SAMPLE(sSample.ui32Sample);
      
      }
      else
      {
        //
        // The returned ADC sample is for the temperature sensor.
        // We need the integer part in the low 16-bits.
        //
        g_ui16ADCTEMP_code = sSample.ui32Sample & 0xFFC0;
      }
    }

    //
    // Signal interrupt arrival to base level.
    //
    g_ui32SampleCount++;
}



//*****************************************************************************
//
// Main
//
//*****************************************************************************
int main(void)
{
    am_util_id_t sIdDevice;
    uint32_t ui32StrBuf;
    //
    // Set the clock frequency.
    //
    am_hal_clkgen_control(AM_HAL_CLKGEN_CONTROL_SYSCLK_MAX, 0);

    //
    // Set the default cache configuration
    //
    am_hal_cachectrl_config(&am_hal_cachectrl_defaults);
    am_hal_cachectrl_enable();

    //
    // Configure the board for low power operation.
    //
    am_bsp_low_power_init();

    //
    // Initialize the printf interface for ITM output
    //
    am_bsp_itm_printf_enable();

    //
    // Print the banner.
    //
    am_util_stdio_terminal_clear();
    am_util_stdio_printf("Hello World!\n\n");

    //
    // Print the device info.
    //
    am_util_id_device(&sIdDevice);
    am_util_stdio_printf("Vendor Name: %s\n", sIdDevice.pui8VendorName);
    am_util_stdio_printf("Device type: %s\n", sIdDevice.pui8DeviceName);


    am_util_stdio_printf("Qualified: %s\n",
                         sIdDevice.sMcuCtrlDevice.ui32Qualified ?
                         "Yes" : "No");

    am_util_stdio_printf("Device Info:\n"
                         "\tPart number: 0x%08X\n"
                         "\tChip ID0:    0x%08X\n"
                         "\tChip ID1:    0x%08X\n"
                         "\tRevision:    0x%08X (Rev%c%c)\n",
                         sIdDevice.sMcuCtrlDevice.ui32ChipPN,
                         sIdDevice.sMcuCtrlDevice.ui32ChipID0,
                         sIdDevice.sMcuCtrlDevice.ui32ChipID1,
                         sIdDevice.sMcuCtrlDevice.ui32ChipRev,
                         sIdDevice.ui8ChipRevMaj, sIdDevice.ui8ChipRevMin );

    //
    // If not a multiple of 1024 bytes, append a plus sign to the KB.
    //
    ui32StrBuf = ( sIdDevice.sMcuCtrlDevice.ui32FlashSize % 1024 ) ? '+' : 0;
    am_util_stdio_printf("\tFlash size:  %7d (%d KB%s)\n",
                         sIdDevice.sMcuCtrlDevice.ui32FlashSize,
                         sIdDevice.sMcuCtrlDevice.ui32FlashSize / 1024,
                         &ui32StrBuf);

    ui32StrBuf = ( sIdDevice.sMcuCtrlDevice.ui32SRAMSize % 1024 ) ? '+' : 0;
    am_util_stdio_printf("\tSRAM size:   %7d (%d KB%s)\n\n",
                         sIdDevice.sMcuCtrlDevice.ui32SRAMSize,
                         sIdDevice.sMcuCtrlDevice.ui32SRAMSize / 1024,
                         &ui32StrBuf);
    //
    // Print the compiler version.
    //
    am_util_stdio_printf("App Compiler:    %s\n", COMPILER_VERSION);
#if defined(AM_PART_APOLLO3)  || defined(AM_PART_APOLLO3P)
    am_util_stdio_printf("HAL Compiler:    %s\n", g_ui8HALcompiler);
    am_util_stdio_printf("HAL SDK version: %d.%d.%d\n",
                         g_ui32HALversion.s.Major,
                         g_ui32HALversion.s.Minor,
                         g_ui32HALversion.s.Revision);
    am_util_stdio_printf("HAL compiled with %s-style registers\n",
                         g_ui32HALversion.s.bAMREGS ? "AM_REG" : "CMSIS");

    am_hal_security_info_t secInfo;
    char sINFO[32];
    uint32_t ui32Status;
    ui32Status = am_hal_security_get_info(&secInfo);
    if (ui32Status == AM_HAL_STATUS_SUCCESS)
    {
        if ( secInfo.bInfo0Valid )
        {
            am_util_stdio_sprintf(sINFO, "INFO0 valid, ver 0x%X", secInfo.info0Version);
        }
        else
        {
            am_util_stdio_sprintf(sINFO, "INFO0 invalid");
        }

        am_util_stdio_printf("SBL ver: 0x%x - 0x%x, %s\n",
            secInfo.sblVersion, secInfo.sblVersionAddInfo, sINFO);
    }
    else
    {
        am_util_stdio_printf("am_hal_security_get_info failed 0x%X\n", ui32Status);
    }
#endif // AM_PART_APOLLO3



    //
    // We are done printing.
    // Disable debug printf messages on ITM.
    //
//    am_bsp_debug_printf_disable();

		
		am_hal_sysctrl_fpu_enable();
    am_hal_sysctrl_fpu_stacking_enable(true);

    //
    // Initialize the ADC.
    //
    adc_init();

    //
    // Initialize CTIMER 3A to trigger the ADC every 0.5 seconds.
    //
    timer_init();
		
		
		 NVIC_EnableIRQ(ADC_IRQn);
    am_hal_interrupt_master_enable();

    //
    // Enable the ADC interrupts in the ADC.
    //
    am_hal_adc_interrupt_enable(g_ADCHandle, AM_HAL_ADC_INT_WCINC       |
                                             AM_HAL_ADC_INT_WCEXC       |
                                             AM_HAL_ADC_INT_FIFOOVR2    |
                                             AM_HAL_ADC_INT_FIFOOVR1    |
                                             AM_HAL_ADC_INT_SCNCMP      |
                                             AM_HAL_ADC_INT_CNVCMP);

    //
    // Reset the sample count which will be incremented by the ISR.
    //
    g_ui32SampleCount = 0;

    //
    // Kick Start Timer 3 with an ADC software trigger in REPEAT used.
    //
    am_hal_adc_sw_trigger(g_ADCHandle);

    //
    // Track buffer depth for progress messages.
		
    //
		 
    int32_t i32BaseLevelCount = g_ui32SampleCount;
		
		 const float fReferenceVoltage = 1.5;
		 float fVBATT;
		 uint32_t ui32Retval;
		  float fADCTempVolts;
    float fADCTempDegreesC;
		 float fTrims[4];
		  float fTempF;
    //
    // Loop forever while sleeping.
    //
    while (1)
    {
			
			 if (g_ui32SampleCount > i32BaseLevelCount)
        {
            i32BaseLevelCount = g_ui32SampleCount;

            //
            // Compute the voltage divider output.
            //
            fVBATT = ((float)g_ui16ADCVDD_code) * 3.0f * fReferenceVoltage / (1024.0f / 64.0f);

            //
            // Print the voltage divider output.
            //
            am_util_stdio_printf("VBATT = <%.3f> (0x%04X) ",
                                 fVBATT, g_ui16ADCVDD_code);

            //
            // Convert and scale the temperature.
            // Temperatures are in Fahrenheit range -40 to 225 degrees.
            // Voltage range is 0.825V to 1.283V
            // First get the ADC voltage corresponding to temperature.
            //
            fADCTempVolts = ((float)g_ui16ADCTEMP_code) * fReferenceVoltage / (1024.0f * 64.0f);

            //
            // Now call the HAL routine to convert volts to degrees Celsius.
            //
            float fVT[3];
            fVT[0] = fADCTempVolts;
            fVT[1] = 0.0f;
            fVT[2] = -123.456;
//          fADCTempDegreesC = am_hal_adc_volts_to_celsius(fADCTempVolts);
            ui32Retval = am_hal_adc_control(g_ADCHandle, AM_HAL_ADC_REQ_TEMP_CELSIUS_GET, fVT);
            if ( ui32Retval == AM_HAL_STATUS_SUCCESS )
            {
                fADCTempDegreesC = fVT[1];  // Get the temperature

                //
                // print the temperature value in Celsius.
                //
                am_util_stdio_printf("TEMP = %.2f C (0x%04X) ",
                                     fADCTempDegreesC, g_ui16ADCTEMP_code);

                //
                // Print the temperature value in Fahrenheit.
                //
                fTempF = (fADCTempDegreesC * (180.0f / 100.0f)) + 32.0f;
                am_util_stdio_printf(" %.2f F", fTempF);
            }
            else
            {
                am_util_stdio_printf("Error: am_haL_adc_control returned %d\n", ui32Retval);

            }

            //
            // Use button 0 to turn on or off the battery load resistor.
            //

            am_util_stdio_printf("\n");

        } 
        //
        // Go to Deep Sleep.
        //
        am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章