| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- /******************************************************************************
- * @brief providing APIs for configuring ADC module (ADC).
- *
- *******************************************************************************
- *
- * provide APIs for configuring ADC module (ADC)
- ******************************************************************************/
- #include "common.h"
- #include "adc.h"
- /******************************************************************************
- * Local function
- ******************************************************************************/
- ADC_CallbackType ADC_Callback[1] = {NULL};
- /******************************************************************************
- * Local variables
- ******************************************************************************/
- /******************************************************************************
- * Local function prototypes
- ******************************************************************************/
- /******************************************************************************
- * define ADC APIs
- *
- *//*! @addtogroup adc_api_list
- * @{
- *******************************************************************************/
- /*****************************************************************************//**
- *
- * @brief initialize ADC module.
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] pADC_Config point to ADC configuration structure.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_Init(ADC_Type *pADC, ADC_ConfigTypePtr pADC_Config)
- {
- if( pADC == ADC)
- {
- SIM->SCGC |= SIM_SCGC_ADC_MASK;
- }
- /* set clock cource for ADC */
- ADC_SelectClock(pADC,pADC_Config->u8ClockSource);
- /* set clock divide */
- ADC_SelectClockDivide(pADC,pADC_Config->u8ClockDiv);
- /* set ADC mode */
- ADC_SetMode(pADC,pADC_Config->u8Mode);
- /* set FIFO level */
- ADC_SetFifoLevel(pADC,pADC_Config->u8FiFoLevel);
- /* set pin control */
- pADC->APCTL1 = pADC_Config->u16PinControl;
- if( pADC_Config->sSetting.bCompareEn )
- {
- ADC_CompareEnable(pADC);
- }
-
- if( pADC_Config->sSetting.bCompareGreaterEn )
- {
- ADC_CompareGreaterFunction(pADC);
- }
-
- if( pADC_Config->sSetting.bContinuousEn )
- {
- ADC_ContinuousConversion(pADC);
- }
-
- if( pADC_Config->sSetting.bCompareAndEn )
- {
- ADC_CompareFifoAnd(pADC);
- }
-
- if( pADC_Config->sSetting.bFiFoScanModeEn )
- {
- ADC_FifoScanModeEnable(pADC);
- }
-
- if( pADC_Config->sSetting.bHardwareTriggerEn )
- {
- ADC_SetHardwareTrigger(pADC);
- }
- if( pADC_Config->sSetting.bIntEn )
- {
- ADC_IntEnable(pADC);
- NVIC_EnableIRQ( ADC0_IRQn );
- }
- if( pADC_Config->sSetting.bLongSampleEn )
- {
- ADC_SetLongSample(pADC);
- }
- if( pADC_Config->sSetting.bLowPowerEn )
- {
- ADC_SetLowPower(pADC);
- }
- #if !defined(CPU_NV32)
- if( pADC_Config->sSetting.bHTRGMEn )
- {
- ADC_HardwareTriggerMultiple(pADC);
- }
- else
- {
- ADC_HardwareTriggerSingle(pADC);
- }
- if( pADC_Config->sSetting.bHTRGMASKEn )
- {
- ADC_HardwareTriggerMaskEnable(pADC);
- }
- else
- {
- ADC_HardwareTriggerMaskDisable(pADC);
- }
- if( pADC_Config->sSetting.bHTRGMASKSEL )
- {
- ADC_HardwareTriggerMaskAuto(pADC);
- }
- else
- {
- ADC_HardwareTriggerMaskNonAuto(pADC);
- }
- #endif
- }
- /*****************************************************************************//*!
- *
- * @brief disable ADC module.
- *
- * @param[in] pADC point to ADC module type.
- *
- * @return none.
- *
- * @ Pass/ Fail criteria: none.
- *****************************************************************************/
- void ADC_DeInit( ADC_Type *pADC )
- {
- ADC_SetChannel(pADC,ADC_CHANNEL_DISABLE);
- SIM->SCGC &= ~SIM_SCGC_ADC_MASK;
- }
- /*****************************************************************************//*!
- *
- * @brief start a conversion and get conversion result
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8Channel adc channel to conversion.
- *
- * @return ADC conversion result.
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- unsigned int ADC_PollRead( ADC_Type *pADC, uint8_t u8Channel )
- {
- ADC_SetChannel(pADC,u8Channel);
- while( !ADC_IsCOCOFlag(pADC) );
- return ADC_ReadResultReg(pADC);
- }
- /*****************************************************************************//*!
- *
- * @brief install ADC call back function.
- *
- * @param[in] pADC_CallBack point to address of adc call back function.
- *
- * @return none.
- *
- * @ Pass/ Fail criteria: none.
- *****************************************************************************/
- void ADC_SetCallBack(ADC_CallbackType pADC_CallBack)
- {
- ADC_Callback[0] = pADC_CallBack;
- }
- /*****************************************************************************//*!
- *
- * @brief set ADC channel.
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8Channel adc channel to conversion.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_SetChannel( ADC_Type *pADC, uint8_t u8Channel )
- {
- uint32_t u32temp;
- u32temp = pADC->SC1;
- u32temp &= ~ADC_SC1_ADCH_MASK;
- pADC->SC1 = u32temp|ADC_SC1_ADCH(u8Channel);
- }
- /*****************************************************************************//*!
- *
- * @brief Voltage Reference Selection.
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8Vref adc reference voltage selection.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_VrefSelect( ADC_Type *pADC, uint8_t u8Vref )
- {
- uint32_t u32Temp;
- u32Temp = pADC->SC2;
- u32Temp &= ~ADC_SC2_REFSEL_MASK;
- pADC->SC2 = u32Temp|ADC_SC2_REFSEL(u8Vref);
- }
- /*****************************************************************************//*!
- *
- * @brief select clock divide
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8Div Clock Divide Select.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_SelectClockDivide( ADC_Type *pADC, uint8_t u8Div )
- {
- uint32_t u32Temp;
- u32Temp = pADC->SC3;
- u32Temp &= ~ADC_SC3_ADIV_MASK;
- pADC->SC3 = u32Temp|ADC_SC3_ADIV(u8Div);
- }
- /*****************************************************************************//*!
- *
- * @brief set ADC mode.
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8Mode Conversion Mode Selection.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_SetMode( ADC_Type *pADC, uint8_t u8Mode )
- {
- uint32_t u32Temp;
- u32Temp = pADC->SC3;
- u32Temp &= ~ADC_SC3_MODE_MASK;
- pADC->SC3 = u32Temp|ADC_SC3_MODE(u8Mode);
- }
- /*****************************************************************************//*!
- *
- * @brief Input Clock Select.
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8Clock Input Clock Select.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_SelectClock( ADC_Type *pADC, uint8_t u8Clock )
- {
- uint32_t u32Temp;
- u32Temp = pADC->SC3;
- u32Temp &= ~ADC_SC3_ADICLK_MASK;
- pADC->SC3 = u32Temp|ADC_SC3_ADICLK(u8Clock);
- }
- /*****************************************************************************//*!
- *
- * @brief FIFO Depth enables
- *
- * @param[in] pADC point to ADC module type.
- * @param[in] u8FifoLevel set FIFO level.
- *
- * @return none
- *
- * @ Pass/ Fail criteria: none
- *****************************************************************************/
- void ADC_SetFifoLevel( ADC_Type *pADC, uint8_t u8FifoLevel )
- {
- uint32_t u32Temp;
- u32Temp = pADC->SC4;
- u32Temp &= ~ADC_SC4_AFDEP_MASK;
- pADC->SC4 = u32Temp|ADC_SC4_AFDEP(u8FifoLevel);
- }
- /*! @} End of adc_api_list */
- /*****************************************************************************//*!
- *
- * @brief ADC interrupt service routine.
- *
- * @param none.
- *
- * @return none.
- *
- * @ Pass/ Fail criteria: none.
- *****************************************************************************/
- void ADC_Isr(void)
- {
- // printf("input any character to start a new conversion!\n");
- if( ADC_Callback[0] )
- {
- ADC_Callback[0]();
- }
- }
|