您好,歡迎進入深圳市穎特新科技有限公司官方網(wǎng)站!
今天我來講講PWM觸發(fā)ADC的例程
/**************************************************************************** * @file main.c * @version V2.0
* $Revision: 5 $ * $Date: 14/06/30 4:51p $ * @brief Demonstrate how to trigger ADC by PWM. * @note * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved. * ******************************************************************************/ #include <stdio.h> #include "NUC131.h" #define PLL_CLOCK 50000000 /*---------------------------------------------------------------------------------------------------------*/ /* Define Function Prototypes */ /*---------------------------------------------------------------------------------------------------------*/ void SYS_Init(void); //系統(tǒng)初始化 void UART0_Init(void); //串口初始化 void ADC_PWMTrigTest_SingleOpMode(void); //ADC硬件觸發(fā)測試 void SYS_Init(void) //選擇時鐘,選擇模塊,配置引腳 { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); //選擇內(nèi)部RC時鐘 /* Waiting for Internal RC clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */ CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));//分頻 /* Enable external XTAL 12MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); /* Waiting for external XTAL clock ready */ CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); /* Set core clock as PLL_CLOCK from PLL */ CLK_SetCoreClock(PLL_CLOCK); /* Enable UART module clock */ CLK_EnableModuleClock(UART0_MODULE); /* Enable ADC module clock */ CLK_EnableModuleClock(ADC_MODULE); /* Enable PWM0 module clock */ CLK_EnableModuleClock(PWM0_MODULE); /* Select UART module clock source */ CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); /* Select PWM01 module clock source */ CLK_SetModuleClock(PWM0_MODULE, CLK_CLKSEL3_PWM0_S_PCLK, 0); /* ADC clock source is 22.1184MHz, set divider to 7, ADC clock is 22.1184/7 MHz */ CLK_SetModuleClock(ADC_MODULE, CLK_CLKSEL1_ADC_S_HIRC, CLK_CLKDIV_ADC(7)); /*---------------------------------------------------------------------------------------------------------*/ /* Init I/O Multi-function */ /*---------------------------------------------------------------------------------------------------------*/ /* Set GPB multi-function pins for UART0 RXD and TXD */ SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);//配置串口引腳 SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD; /* Disable the GPA0 - GPA3 digital input path to avoid the leakage current. */ GPIO_DISABLE_DIGITAL_PATH(PA, 0xF);//禁用GPA0 - GPA3數(shù)字輸入路徑以避免泄漏電流 /* Configure the GPA0 - GPA3 ADC analog input pins */ SYS->GPA_MFP &= ~(SYS_GPA_MFP_PA0_Msk | SYS_GPA_MFP_PA1_Msk | SYS_GPA_MFP_PA2_Msk | SYS_GPA_MFP_PA3_Msk) ;//配置GPA //0至PA3位為ADC模擬輸入引腳 SYS->GPA_MFP |= SYS_GPA_MFP_PA0_ADC0 | SYS_GPA_MFP_PA1_ADC1 | SYS_GPA_MFP_PA2_ADC2 | SYS_GPA_MFP_PA3_ADC3 ; //引腳復(fù)用 /* Configure the PA12 as PWM0 output pin */ SYS->GPA_MFP = (SYS->GPA_MFP & (~SYS_GPA_MFP_PA12_Msk));//配置PWM0的引腳 SYS->GPA_MFP |= SYS_GPA_MFP_PA12_PWM0_CH0;//PWM0的0通道 } /*---------------------------------------------------------------------------------------------------------*/ /* Init UART */ /*---------------------------------------------------------------------------------------------------------*/ void UART0_Init()//復(fù)位,和打開串口模塊 { /* Reset IP */ SYS_ResetModule(UART0_RST); /* Configure UART0 and set UART0 Baudrate */ UART_Open(UART0, 115200); } /*---------------------------------------------------------------------------------------------------------*/ /* Function: ADC_PWMTrigTest_SingleOpMode */ /* */ /* Parameters: */ /* None. */ /* */ /* Returns: */ /* None. */ /* */ /* Description: */ /* ADC hardware trigger test. ADC硬件觸發(fā)測試。 */ /*---------------------------------------------------------------------------------------------------------*/ void ADC_PWMTrigTest_SingleOpMode()//這個得好好講講了 說明PWM是如何激發(fā)ADC進行讀取的。 { printf("\n<<< PWM trigger test (Single mode) >>>\n"); /* Set the ADC operation mode as single, input mode as single-end and enable the analog input channel 2 */ ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, 0x1 << 2); //將ADC操作模式設(shè)置為單一模式,輸入模式為單端,并啟用模擬輸入通道2 打開ADC通道 /* Power on ADC module */ ADC_POWER_ON(ADC); //#define ADC_POWER_ON(adc) ((adc)->ADCR |= ADC_ADCR_ADEN_Msk) //Before starting A/D conversion function, ADEN bit (ADCR[0]) should be set to 1.在啟動A / D轉(zhuǎn)換功能之前,應(yīng)將ADEN位(ADCR [0])設(shè)置為1。 /* Configure the hardware trigger condition and enable hardware trigger; PWM trigger delay: (4*10) system clock cycles*/ ADC_EnableHWTrigger(ADC, ADC_ADCR_TRGS_PWM, 0);//配置硬件觸發(fā)條件并啟用硬件觸發(fā); PWM觸發(fā)延遲:(4 * 10)個系統(tǒng)時鐘周期 /****************************************************************************************************************************************************** /* /** * @brief Configure the hardware trigger condition and enable hardware trigger.配置硬件觸發(fā)條件并啟用硬件觸發(fā) * @param[in] adc The pointer of the specified ADC module.adc指定ADC模塊的指針。 * @param[in] u32Source Decides the hardware trigger source. Valid values are: u32Source決定硬件觸發(fā)源。 * - \ref ADC_ADCR_TRGS_STADC :A/D conversion is started by external STADC pin.A / D轉(zhuǎn)換由外部STADC引腳啟動 * - \ref ADC_ADCR_TRGS_PWM :A/D conversion is started by PWM.//A / D轉(zhuǎn)換由PWM啟動 * @param[in] u32Param ADC trigger by external pin, this parameter is used to set trigger condition. Valid values are://u32Param ADC由外部引腳觸發(fā), //該參數(shù)用于設(shè)置觸發(fā)條件。 * - \ref ADC_ADCR_TRGCOND_LOW_LEVEL :STADC Low level active. * - \ref ADC_ADCR_TRGCOND_HIGH_LEVEL :STADC High level active. * - \ref ADC_ADCR_TRGCOND_FALLING_EDGE :STADC Falling edge active. * - \ref ADC_ADCR_TRGCOND_RISING_EDGE :STADC Rising edge active. * @return None * @details Software should disable TRGEN (ADCR[8]) and ADST (ADCR[11]) before change TRGS(ADCR[5:4]). */ void ADC_EnableHWTrigger(ADC_T *adc, uint32_t u32Source, uint32_t u32Param) { ADC->ADCR &= ~(ADC_ADCR_TRGS_Msk | ADC_ADCR_TRGCOND_Msk | ADC_ADCR_TRGEN_Msk); ADC->ADCR |= u32Source | u32Param | ADC_ADCR_TRGEN_Msk; return; } *******************************************************************************************************************************************************/ /* Clear the A/D interrupt flag for safe */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT);//清除A / D中斷標(biāo)志以確保安全 /* Center-aligned type 該宏設(shè)置PWM對齊類型*/ PWM_SET_ALIGNED_TYPE(PWM0, PWM_CH_0_MASK, PWM_CENTER_ALIGNED);//中心對齊的類型 /****************************************************************************************************************************************************** /** * @brief This macro set the PWM aligned type * @param[in] pwm The pointer of the specified PWM module * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. Every two channels share the same setting. * Bit 0 represents channel 0, bit 1 represents channel 1... * @param[in] u32AlignedType PWM aligned type, valid values are: * - \ref PWM_EDGE_ALIGNED * - \ref PWM_CENTER_ALIGNED * @return None * @details This macro is used to set the PWM aligned type of specified channel(s). * \hideinitializer */ #define PWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \ do{ \ int i; \ for(i = 0; i < 6; i++) { \ if((u32ChannelMask) & (1 << i)) \ (pwm)->CTL1 = (((pwm)->CTL1 & ~(3UL << ((i >> 1) << 2))) | ((u32AlignedType) << ((i >> 1) << 2))); \ } \ }while(0)02 *************************************************************************************************************************************************************/ /* Clock prescaler 時鐘預(yù)分頻器*/ PWM_SET_PRESCALER(PWM0, 0, 1);//該宏設(shè)置所選通道的預(yù)分頻器 /******************************************************************************************************************************************************** /** * @brief This macro set the prescaler of the selected channel * @param[in] pwm The pointer of the specified PWM module * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5 PWM通道 * @param[in] u32Prescaler Clock prescaler of specified channel. Valid values are between 1 ~ 0xFFF u32Prescaler指定通道的時鐘預(yù)分頻器。 // 有效值在1?0xFFF之間 * @return None * @details This macro is used to set the prescaler of specified channel. * @note Every even channel N, and channel (N + 1) share a prescaler. So if channel 0 prescaler changed, * channel 1 will also be affected. * \hideinitializer */ #define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler) (*(__IO uint32_t *) (&((pwm)->CLKPSC0_1) + ((u32ChannelNum) >> 1)) = (u32Prescaler)) ***********************************************************************************************************************************************************************/ /* PWM counter value */ /* PWM frequency = PWM clock source/(clock prescaler setting + 1)/(CNR+1) */ PWM_SET_CNR(PWM0, 0, 5);//該宏設(shè)置所選通道的周期 /**********************************************************************************************************************************************************
/**
* @brief This macro set the period of the selected channel
* @param[in] pwm The pointer of the specified PWM module
* @param[in] u32ChannelNum PWM channel number. Valid values are between 0, 2, 4. Every two channels share the same setting.
* @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF
* @return None
* @details This macro is used to set the period of specified channel.
* @note This new setting will take effect on next PWM period.
* @note PWM counter will stop if period length set to 0.
* \hideinitializer
*/
#define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR) ((pwm)->PERIOD[(((u32ChannelNum) >> 1) << 1)] = (u32CNR))
/** * @brief This macro set the comparator of the selected channel * @param[in] pwm The pointer of the specified PWM module * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5 * @param[in] u32CMR Comparator of specified channel. Valid values are between 0~0xFFFF * @return None * @details This macro is used to set the comparator of specified channel. * @note This new setting will take effect on next PWM period. * \hideinitializer */ #define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) ((pwm)->CMPDAT[(u32ChannelNum)] = (u32CMR)) **********************************************************************************************************************************************************/ /* PWM compare value */ PWM_SET_CMR(PWM0, 0, 1); /******************************************************************************************************************************************************* /** * @brief This macro set the comparator of the selected channel 該宏設(shè)置所選通道的比較器 * @param[in] pwm The pointer of the specified PWM module * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5 * @param[in] u32CMR Comparator of specified channel. Valid values are between 0~0xFFFF 指定通道的u32CMR比較器。 有效值在0?0xFFFF之間 * @return None * @details This macro is used to set the comparator of specified channel.該宏用于設(shè)置指定通道的比較器。 * @note This new setting will take effect on next PWM period.這個新設(shè)置將在下一個PWM周期生效。 * \hideinitializer */ #define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) ((pwm)->CMPDAT[(u32ChannelNum)] = (u32CMR)) ***************************************************************************************************************************************************************/ /* Enable PWM0 to trigger ADC */ PWM_EnableADCTrigger(PWM0, 0, PWM_TRIGGER_ADC_EVEN_PERIOD_POINT);//使能PWM來觸發(fā)ADC /****************************************************************************************************************************************************** /** * @brief Enable selected channel to trigger ADC * @param[in] pwm The pointer of the specified PWM module * - PWM0 : PWM Group 0 * - PWM1 : PWM Group 1 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5 * @param[in] u32Condition The condition to trigger ADC. Combination of following conditions: * - \ref PWM_TRIGGER_ADC_EVEN_ZERO_POINT * - \ref PWM_TRIGGER_ADC_EVEN_PERIOD_POINT * - \ref PWM_TRIGGER_ADC_EVEN_ZERO_OR_PERIOD_POINT * - \ref PWM_TRIGGER_ADC_EVEN_COMPARE_UP_COUNT_POINT * - \ref PWM_TRIGGER_ADC_EVEN_COMPARE_DOWN_COUNT_POINT * - \ref PWM_TRIGGER_ADC_ODD_COMPARE_UP_COUNT_POINT * - \ref PWM_TRIGGER_ADC_ODD_COMPARE_DOWN_COUNT_POINT #define PWM_TRIGGER_ADC_EVEN_ZERO_POINT (0UL) /*!< PWM trigger ADC while counter of even channel matches zero point */PWM觸發(fā)ADC,偶數(shù)通道計數(shù)器匹配零點 #define PWM_TRIGGER_ADC_EVEN_PERIOD_POINT (1UL) /*!< PWM trigger ADC while counter of even channel matches period point */當(dāng)偶數(shù)通道計數(shù)器匹配周期點時,PWM觸發(fā)ADC #define PWM_TRIGGER_ADC_EVEN_ZERO_OR_PERIOD_POINT (2UL) /*!< PWM trigger ADC while counter of even channel matches zero or period point */PWM觸發(fā)ADC,偶數(shù)通道計數(shù)器匹配零點或周期點 #define PWM_TRIGGER_ADC_EVEN_COMPARE_UP_COUNT_POINT (3UL) /*!< PWM trigger ADC while counter of even channel matches up count to comparator point */當(dāng)偶數(shù)通道計數(shù)器匹配時PWM觸發(fā)ADC計數(shù)到比較器點 #define PWM_TRIGGER_ADC_EVEN_COMPARE_DOWN_COUNT_POINT (4UL) /*!< PWM trigger ADC while counter of even channel matches down count to comparator point */PWM觸發(fā)ADC,而偶數(shù)通道計數(shù)器的匹配下降計數(shù)到比較點 #define PWM_TRIGGER_ADC_ODD_COMPARE_UP_COUNT_POINT (8UL) /*!< PWM trigger ADC while counter of odd channel matches up count to comparator point */PWM觸發(fā)ADC將奇數(shù)通道計數(shù)器匹配到比較器點數(shù) #define PWM_TRIGGER_ADC_ODD_COMPARE_DOWN_COUNT_POINT (9UL) /*!< PWM trigger ADC while counter of odd channel matches down count to comparator point */PWM觸發(fā)ADC將奇數(shù)通道計數(shù)器下降計數(shù)到比較點 * @return None * @details This function is used to enable selected channel to trigger ADC. */ void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition) { if(u32ChannelNum < 4) { (pwm)->ADCTS0 &= ~((PWM_ADCTS0_TRGSEL0_Msk) << (u32ChannelNum * 8)); (pwm)->ADCTS0 |= ((PWM_ADCTS0_TRGEN0_Msk | u32Condition) << (u32ChannelNum * 8)); } else { (pwm)->ADCTS1 &= ~((PWM_ADCTS1_TRGSEL4_Msk) << ((u32ChannelNum - 4) * 8)); (pwm)->ADCTS1 |= ((PWM_ADCTS1_TRGEN4_Msk | u32Condition) << ((u32ChannelNum - 4) * 8)); } } *************************************************************************************************************************************************************/ /* PWM0 pin output enabled */ PWM_SET_OUTPUT_LEVEL(PWM0, PWM_CH_0_MASK, PWM_OUTPUT_HIGH, PWM_OUTPUT_NOTHING, PWM_OUTPUT_LOW, PWM_OUTPUT_NOTHING); /******************************************************************************************************************************************************* /** * @brief Set output level at zero, compare up, period(center) and compare down of specified channel(s) 將輸出電平設(shè)置為零,比較上升,指定通道的周期(中心)和比較下降 * @param[in] pwm The pointer of the specified PWM module // * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel u32ChannelMask已啟用通道的組合。 每一位對應(yīng)一個通道 * Bit 0 represents channel 0, bit 1 represents channel 1... * @param[in] u32ZeroLevel output level at zero point, valid values are: u32ZeroLevel輸出電平在零點,有效值是: * - \ref PWM_OUTPUT_NOTHING * - \ref PWM_OUTPUT_LOW * - \ref PWM_OUTPUT_HIGH * - \ref PWM_OUTPUT_TOGGLE * @param[in] u32CmpUpLevel output level at compare up point, valid values are: u32CmpUpLevel比較點的輸出電平,有效值為: * - \ref PWM_OUTPUT_NOTHING * - \ref PWM_OUTPUT_LOW * - \ref PWM_OUTPUT_HIGH * - \ref PWM_OUTPUT_TOGGLE * @param[in] u32PeriodLevel output level at period(center) point, valid values are: 周期(中心)點的u32PeriodLevel輸出電平,有效值為: * - \ref PWM_OUTPUT_NOTHING * - \ref PWM_OUTPUT_LOW * - \ref PWM_OUTPUT_HIGH * - \ref PWM_OUTPUT_TOGGLE * @param[in] u32CmpDownLevel output level at compare down point, valid values are: @param [in] u32CmpDownLevel比較下來的輸出級別,有效值是: * - \ref PWM_OUTPUT_NOTHING * - \ref PWM_OUTPUT_LOW * - \ref PWM_OUTPUT_HIGH * - \ref PWM_OUTPUT_TOGGLE * @return None * @details This macro is used to Set output level at zero, compare up, period(center) and compare down of specified channel(s). * \hideinitializer */ #define PWM_SET_OUTPUT_LEVEL(pwm, u32ChannelMask, u32ZeroLevel, u32CmpUpLevel, u32PeriodLevel, u32CmpDownLevel) \ do{ \ int i; \ for(i = 0; i < 6; i++) { \ if((u32ChannelMask) & (1 << i)) { \ (pwm)->WGCTL0 = (((pwm)->WGCTL0 & ~(3UL << (2 * i))) | ((u32ZeroLevel) << (2 * i))); \ (pwm)->WGCTL0 = (((pwm)->WGCTL0 & ~(3UL << (PWM_WGCTL0_PRDPCTLn_Pos + (2 * i)))) | \ ((u32PeriodLevel) << (PWM_WGCTL0_PRDPCTLn_Pos + (2 * i)))); \ (pwm)->WGCTL1 = (((pwm)->WGCTL1 & ~(3UL << (2 * i))) | ((u32CmpUpLevel) << (2 * i))); \ (pwm)->WGCTL1 = (((pwm)->WGCTL1 & ~(3UL << (PWM_WGCTL1_CMPDCTLn_Pos + (2 * i)))) | \ ((u32CmpDownLevel) << (PWM_WGCTL1_CMPDCTLn_Pos + (2 * i)))); \ } \ } \ }while(0) **********************************************************************************************************************************************************************************************************/ PWM_EnableOutput(PWM0, PWM_CH_0_MASK); /***************************************************************************************************************************************************** /* Start PWM module */ PWM_Start(PWM0, PWM_CH_0_MASK); /* wait for one cycle */ while(PWM_GetPeriodIntFlag(PWM0, 0) == 0); while(PWM_GetZeroIntFlag(PWM0, 0) == 0); PWM_ClearPeriodIntFlag(PWM0, 0); PWM_ClearZeroIntFlag(PWM0, 0); /******************************************************************************************************************************************************** /** * @brief Get period interrupt of selected channel 獲取選定通道的周期中斷 * @param[in] pwm The pointer of the specified PWM module * - PWM0 : PWM Group 0 * - PWM1 : PWM Group 1 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5. Every two channels share the same setting. * @return Period interrupt flag of specified channel * @retval 0 Period interrupt did not occur * @retval 1 Period interrupt occurred * @details This function is used to get period interrupt of selected channel. */ uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) { return (((pwm)->INTSTS0 & (PWM_INTSTS0_PIF0_Msk << ((u32ChannelNum >> 1) << 1))) ? 1 : 0); } /** * @brief Get zero interrupt of selected channel 獲取所選通道的零中斷 * @param[in] pwm The pointer of the specified PWM module * - PWM0 : PWM Group 0 * - PWM1 : PWM Group 1 * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~5. Every two channels share the same setting. * @return zero interrupt flag of specified channel * @retval 0 zero interrupt did not occur * @retval 1 zero interrupt occurred * @details This function is used to get zero interrupt of selected channel. */ uint32_t PWM_GetZeroIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) { return (((pwm)->INTSTS0 & (PWM_INTSTS0_ZIF0_Msk << ((u32ChannelNum >> 1) << 1))) ? 1 : 0); } /** * @brief Stop PWM generation immediately by clear channel enable bit * @param[in] pwm The pointer of the specified PWM module * - PWM0 : PWM Group 0 * - PWM1 : PWM Group 1 * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. * Bit 0 is channel 0, bit 1 is channel 1... * @return None * @details This function is used to stop PWM generation immediately by clear channel enable bit. 該功能用于通過清除通道使能位立即停止PWM的產(chǎn)生。 */ void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask) { uint32_t i; for(i = 0; i < PWM_CHANNEL_NUM; i ++) { if(u32ChannelMask & (1 << i)) { (pwm)->CNTEN &= ~(1UL << ((i >> 1) << 1)); } } } /** * @brief Return the user-specified interrupt flags. 返回用戶指定的中斷標(biāo)志。 * @param[in] adc The pointer of the specified ADC module. * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status.//u32Mask以下中斷狀態(tài)位的組合。 每個位對應(yīng)一個中斷狀態(tài) * Valid values are: * - \ref ADC_ADF_INT :Convert complete interrupt flag. * - \ref ADC_CMP0_INT :Comparator 0 interrupt flag. * - \ref ADC_CMP1_INT :Comparator 1 interrupt flag. * @return User specified interrupt flags. * @details Get the status of the ADC interrupt flag. */ #define ADC_GET_INT_FLAG(adc, u32Mask) ((adc)->ADSR & (u32Mask)) **************************************************************************************************************************************************************/ /* Stop PWM generation */ PWM_ForceStop(PWM0, PWM_CH_0_MASK); /* Wait conversion done */ while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT)); /* Clear the ADC interrupt flag */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); printf("Channel 2: 0x%X\n", ADC_GET_CONVERSION_DATA(ADC, 2));//ADC獲取轉(zhuǎn)換數(shù)據(jù) /* Disable ADC */ ADC_POWER_DOWN(ADC); /********************************************************************************************************************************************************* ///** *提示關(guān)閉ADC模塊。 * @param [in] adc指定ADC模塊的指針。 * @返回?zé)o * @details禁用A / D轉(zhuǎn)換器模擬電路以節(jié)省功耗。 * @note無 */
#define ADC_POWER_DOWN(adc)((adc) - > ADCR&=?ADC_ADCR_ADEN_Msk)
***********************************************************************************************************************************************************/
/*---------------------------------------------------------------------------------------------------------*/ /* MAIN function */ /*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); /* Init System, IP clock and multi-function I/O */ SYS_Init(); /* Lock protected registers */ SYS_LockReg(); /* Init UART0 for printf */ UART0_Init(); /*---------------------------------------------------------------------------------------------------------*/ /* SAMPLE CODE */ /*---------------------------------------------------------------------------------------------------------*/ printf("\nSystem clock rate: %d Hz", SystemCoreClock); /* ADC hardware trigger test */ ADC_PWMTrigTest_SingleOpMode(); /* Disable ADC module */ ADC_Close(ADC); /* Disable ADC IP clock */ CLK_DisableModuleClock(ADC_MODULE); /* Disable External Interrupt */ NVIC_DisableIRQ(ADC_IRQn); printf("\nExit ADC sample code\n"); while(1); }
看了這么多,有些亂了吧,不要著急,后面是對這個程序的詳細說明,做技術(shù)要有耐心,細心
這個程序是說,選定PWM如何觸發(fā)ADC的取值,在示波器上觀察波形,在串口上觀察ADC讀取的數(shù)據(jù), PWM選取了通道0,ADC選取了通道2,也就是說ADC通道2讀取到的數(shù)值,通過PWM0觸發(fā)了ADC的取值。
void ADC_PWMTrigTest_SingleOpMode() { printf("\n<<< PWM trigger test (Single mode) >>>\n"); /* Set the ADC operation mode as single, input mode as single-end and enable the analog input channel 2 */ ADC_Open(ADC, ADC_ADCR_DIFFEN_SINGLE_END, ADC_ADCR_ADMD_SINGLE, 0x1 << 2);//操作模式選為單段輸入模式,輸入模式位單一模式 /* Power on ADC module */ ADC_POWER_ON(ADC); /* Configure the hardware trigger condition and enable hardware trigger; PWM trigger delay: (4*10) system clock cycles*/ ADC_EnableHWTrigger(ADC, ADC_ADCR_TRGS_PWM, 0);//硬件觸發(fā)條件 /* Clear the A/D interrupt flag for safe */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); /* Center-aligned type */ PWM_SET_ALIGNED_TYPE(PWM0, PWM_CH_4_MASK, PWM_CENTER_ALIGNED); /* Clock prescaler */ PWM_SET_PRESCALER(PWM0, 0, 1); /* PWM counter value */ /* PWM frequency = PWM clock source/(clock prescaler setting + 1)/(CNR+1) */ PWM_SET_CNR(PWM0, 0, 5); /* PWM compare value */ PWM_SET_CMR(PWM0, 0, 1); /* Enable PWM0 to trigger ADC */ PWM_EnableADCTrigger(PWM0, 0, PWM_TRIGGER_ADC_EVEN_PERIOD_POINT); /* PWM0 pin output enabled */ PWM_SET_OUTPUT_LEVEL(PWM0, PWM_CH_0_MASK, PWM_OUTPUT_HIGH, PWM_OUTPUT_NOTHING, PWM_OUTPUT_LOW, PWM_OUTPUT_NOTHING); PWM_EnableOutput(PWM0, PWM_CH_0_MASK); /* Start PWM module */ PWM_Start(PWM0, PWM_CH_0_MASK); /* wait for one cycle */ while(PWM_GetPeriodIntFlag(PWM0, 0) == 0); while(PWM_GetZeroIntFlag(PWM0, 0) == 0); PWM_ClearPeriodIntFlag(PWM0, 0); PWM_ClearZeroIntFlag(PWM0, 0); /* Stop PWM generation */ PWM_ForceStop(PWM0, PWM_CH_0_MASK); /* Wait conversion done */ while(!ADC_GET_INT_FLAG(ADC, ADC_ADF_INT)); /* Clear the ADC interrupt flag */ ADC_CLR_INT_FLAG(ADC, ADC_ADF_INT); printf("Channel 2: 0x%X\n", ADC_GET_CONVERSION_DATA(ADC, 2)); /* Disable ADC */ ADC_POWER_DOWN(ADC); while(1); }
PWM0通道發(fā)出波形,ADC2通道讀取數(shù)值。
A/D轉(zhuǎn)換器支持三種操作模式:單一(single),單周期掃描(single-cycle scan)和連續(xù)掃描模式(continuous scan mode)。A/D轉(zhuǎn)換器可由軟件、PWM、BPWM觸發(fā)器和外部STADC管腳啟動轉(zhuǎn)換。
輸入模式分為單端輸入和差分輸入模式。
差分輸入方式:差分輸入的是將兩個輸入端的差值作為信號,這樣可以免去一些誤差,比如輸入一個1V的信號電源有偏差,比實際輸入要大0.1.就可以用差分輸入1V和2V,一減就把兩端共有的那0.1誤差剪掉了。單端輸入無法去除這類誤差。 一個差分信號是用一個數(shù)值來表示兩個物理量之間的差異。從嚴(yán)格意義上來講,所有電壓信號都是差分的,因為一個電壓只能是相對于另一個電壓而言的。 在某些系統(tǒng)里,系統(tǒng)'地'被用作電壓基準(zhǔn)點。當(dāng)'地'當(dāng)作電壓測量基準(zhǔn)時,這種信號規(guī)劃被稱之為單端的。使用該術(shù)語是因為信號是用單個導(dǎo)體上的電壓來表示的。 另一方面,一個差分信號作用在兩個導(dǎo)體上。信號值是兩個導(dǎo)體間的電壓差。盡管不是非常必要,這兩個電壓的平均值還是會經(jīng)常保持一致。 這里有一個網(wǎng)友問的關(guān)于TI的一個關(guān)于ADC的問題,或許有助于了解一下ADC的輸入模式(單端或差分):
硬件觸發(fā)源
1.外部STADC引腳啟動
2.由PWM啟動
硬件觸發(fā)條件分為
1.ADC_ADCR_TRGCOND_LOW_LEVEL
2.ADC_ADCR_TRGCOND_HIGH_LEVEL
3.ADC_ADCR_TRGCOND_FALLING_EDGE
4.ADC_ADCR_TRGCOND_RISING_EDGE
PWM_SET_OUTPUT_LEVEL函數(shù)的四個參數(shù): 1.輸出電平在零點
2.比較點的輸出電平, 3.周期(中心)點的輸出電平, 4.比較下來的輸出級別, 關(guān)于零點事件與周期時間,你是否明白了?還有就是比較器事件
采樣一個信號,以前直接放大送給ADC。TI很多ADC(只關(guān)注16bit的系列)的模擬信號輸入端都是AINP/AINN,即支持差分輸入。我的問題是:
1:這個差分輸入和和單端輸入在本質(zhì) 上到底有什么區(qū)別? 因為,ADC采集的信號說到底是AINP - AINN,不管單端還是差分,采集的信號都是這兩個pad的差值。
差分信號是 AINP - AINN
單端信號是 AIN - REFN
2:將單端信號接在ADC的差分輸入接口上可以用嗎?可以
3:如果將差分放大器輸出的差分信號,接在只支持單端信號輸入的ADC,會正常工作嗎?不會,結(jié)果會出現(xiàn)錯誤
PWM中斷觸發(fā)ADC采樣和ADC中斷是一個東西嗎?
首先要區(qū)分adc在這里扮演的角色,pwm中斷出發(fā)adc采樣是去讀取某個參數(shù),而adc中斷是在某個參數(shù)達到一定限制時發(fā)出一個中斷。性質(zhì)不一樣的哩。
PWM觸發(fā)ADC啟動轉(zhuǎn)換有什么用
典型應(yīng)用是電機控制里面的相電流控制,pwm打開之后ADC對相電流同步采樣,然后做電流PID反饋環(huán)
簡單地說,補碼就是反碼加1。 計算機中為什么要使用補碼呢? 主要原因:1、使用補碼,可以將符號位和其它位統(tǒng)一處理;同時,減法也可按加法來處理。另外,兩個用補 碼表示的數(shù)相加時,如果最高位(符號位)有進位,則進位被舍棄。 2、補碼與原碼的轉(zhuǎn)換過程幾乎是相同的。 (1)正數(shù)的補碼 與原碼相同。 【例1】+9的補碼是00001001。(備注:這個+9的補碼說的是用8位的2進制來表示補碼的,補碼表示方式很多,還有16位2進制補碼表示形式,以及32位2進制補碼表示形式等。) (2)負數(shù)的補碼 符號位為1,其余位為該數(shù)絕對值的原碼按位取反;然后整個數(shù)加1。 同一個數(shù)字在不同的補碼表示形式里頭,是不同的。比方說-15的補碼,在8位2進制里頭是11110001,然而在16位2進制補碼表示的情況下,就成了1111111111110001。在這篇補碼概述里頭涉及的補碼轉(zhuǎn)換默認了把一個數(shù)轉(zhuǎn)換成8位2進制的補碼形式,每一種補碼表示形式都只能表示有限的數(shù)字。 【例2】求-7的補碼。 因為給定數(shù)是負數(shù),則符號位為“1”。 后七位:+7的原碼(0000111)→按位取反(1111000)→加1(1111001) 所以-7的補碼是11111001。 已知一個數(shù)的補碼,求原碼的操作分兩種情況: ?。?)如果補碼的符號位為“0”,表示是一個正數(shù),其原碼就是補碼。 ?。?)如果補碼的符號位為“1”,表示是一個負數(shù),那么求給定的這個補碼的補碼就是要求的原碼。 另一種方法求負數(shù)的補碼如下: 例如:求-15的補碼 第一步:+15:00001111 第二步:逐位取反(1變成0,0變成1),然后在末尾加1。 11110001 再舉一個例子驗證下:求-64的補碼 +64:01000000 11000000 【例3】已知一個補碼為11111001,則原碼是10000111(-7)。 因為符號位為“1”,表示是一個負數(shù),所以該位不變,仍為“1”。 其余七位1111001取反后為0000110; 再加1,所以是10000111。
掃碼關(guān)注我們
傳真:0755-82591176
郵箱:vicky@yingtexin.net
地址:深圳市龍華區(qū)民治街道民治大道973萬眾潤豐創(chuàng)業(yè)園A棟2樓A08