您好,歡迎進(jìn)入深圳市穎特新科技有限公司官方網(wǎng)站!
今天我來(lái)講講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ā)測(cè)試 void SYS_Init(void) //選擇時(shí)鐘,選擇模塊,配置引腳 { /*---------------------------------------------------------------------------------------------------------*/ /* Init System Clock */ /*---------------------------------------------------------------------------------------------------------*/ /* Enable Internal RC 22.1184MHz clock */ CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); //選擇內(nèi)部RC時(shí)鐘 /* 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ù)位,和打開(kāi)串口模塊 { /* 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ā)測(cè)試。 */ /*---------------------------------------------------------------------------------------------------------*/ void ADC_PWMTrigTest_SingleOpMode()//這個(gè)得好好講講了 說(shuō)明PWM是如何激發(fā)ADC進(jìn)行讀取的。 { 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 打開(kāi)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.在啟動(dòng)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)個(gè)系統(tǒng)時(shí)鐘周期 /****************************************************************************************************************************************************** /* /** * @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引腳啟動(dòng) * - \ref ADC_ADCR_TRGS_PWM :A/D conversion is started by PWM.//A / D轉(zhuǎn)換由PWM啟動(dòng) * @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對(duì)齊類(lèi)型*/ PWM_SET_ALIGNED_TYPE(PWM0, PWM_CH_0_MASK, PWM_CENTER_ALIGNED);//中心對(duì)齊的類(lèi)型 /****************************************************************************************************************************************************** /** * @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 時(shí)鐘預(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指定通道的時(shí)鐘預(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.這個(gè)新設(shè)置將在下一個(gè)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來(lái)觸發(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ù)通道計(jì)數(shù)器匹配零點(diǎn) #define PWM_TRIGGER_ADC_EVEN_PERIOD_POINT (1UL) /*!< PWM trigger ADC while counter of even channel matches period point */當(dāng)偶數(shù)通道計(jì)數(shù)器匹配周期點(diǎn)時(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ù)通道計(jì)數(shù)器匹配零點(diǎn)或周期點(diǎn) #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ù)通道計(jì)數(shù)器匹配時(shí)PWM觸發(fā)ADC計(jì)數(shù)到比較器點(diǎn) #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ù)通道計(jì)數(shù)器的匹配下降計(jì)數(shù)到比較點(diǎn) #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ù)通道計(jì)數(shù)器匹配到比較器點(diǎn)數(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ù)通道計(jì)數(shù)器下降計(jì)數(shù)到比較點(diǎn) * @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已啟用通道的組合。 每一位對(duì)應(yīng)一個(gè)通道 * Bit 0 represents channel 0, bit 1 represents channel 1... * @param[in] u32ZeroLevel output level at zero point, valid values are: u32ZeroLevel輸出電平在零點(diǎn),有效值是: * - \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比較點(diǎn)的輸出電平,有效值為: * - \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: 周期(中心)點(diǎn)的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比較下來(lái)的輸出級(jí)別,有效值是: * - \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. 該功能用于通過(guò)清除通道使能位立即停止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. 返回用戶(hù)指定的中斷標(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)位的組合。 每個(gè)位對(duì)應(yīng)一個(gè)中斷狀態(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無(wú) */
#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); }
看了這么多,有些亂了吧,不要著急,后面是對(duì)這個(gè)程序的詳細(xì)說(shuō)明,做技術(shù)要有耐心,細(xì)心
這個(gè)程序是說(shuō),選定PWM如何觸發(fā)ADC的取值,在示波器上觀(guān)察波形,在串口上觀(guān)察ADC讀取的數(shù)據(jù), PWM選取了通道0,ADC選取了通道2,也就是說(shuō)ADC通道2讀取到的數(shù)值,通過(guò)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)換器支持三種操作模式:?jiǎn)我?single),單周期掃描(single-cycle scan)和連續(xù)掃描模式(continuous scan mode)。A/D轉(zhuǎn)換器可由軟件、PWM、BPWM觸發(fā)器和外部STADC管腳啟動(dòng)轉(zhuǎn)換。
輸入模式分為單端輸入和差分輸入模式。
差分輸入方式:差分輸入的是將兩個(gè)輸入端的差值作為信號(hào),這樣可以免去一些誤差,比如輸入一個(gè)1V的信號(hào)電源有偏差,比實(shí)際輸入要大0.1.就可以用差分輸入1V和2V,一減就把兩端共有的那0.1誤差剪掉了。單端輸入無(wú)法去除這類(lèi)誤差。 一個(gè)差分信號(hào)是用一個(gè)數(shù)值來(lái)表示兩個(gè)物理量之間的差異。從嚴(yán)格意義上來(lái)講,所有電壓信號(hào)都是差分的,因?yàn)橐粋(gè)電壓只能是相對(duì)于另一個(gè)電壓而言的。 在某些系統(tǒng)里,系統(tǒng)'地'被用作電壓基準(zhǔn)點(diǎn)。當(dāng)'地'當(dāng)作電壓測(cè)量基準(zhǔn)時(shí),這種信號(hào)規(guī)劃被稱(chēng)之為單端的。使用該術(shù)語(yǔ)是因?yàn)樾盘?hào)是用單個(gè)導(dǎo)體上的電壓來(lái)表示的。 另一方面,一個(gè)差分信號(hào)作用在兩個(gè)導(dǎo)體上。信號(hào)值是兩個(gè)導(dǎo)體間的電壓差。盡管不是非常必要,這兩個(gè)電壓的平均值還是會(huì)經(jīng)常保持一致。 這里有一個(gè)網(wǎng)友問(wèn)的關(guān)于TI的一個(gè)關(guān)于ADC的問(wèn)題,或許有助于了解一下ADC的輸入模式(單端或差分):
硬件觸發(fā)源
1.外部STADC引腳啟動(dòng)
2.由PWM啟動(dòng)
硬件觸發(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ù)的四個(gè)參數(shù): 1.輸出電平在零點(diǎn)
2.比較點(diǎn)的輸出電平, 3.周期(中心)點(diǎn)的輸出電平, 4.比較下來(lái)的輸出級(jí)別, 關(guān)于零點(diǎn)事件與周期時(shí)間,你是否明白了?還有就是比較器事件
采樣一個(gè)信號(hào),以前直接放大送給ADC。TI很多ADC(只關(guān)注16bit的系列)的模擬信號(hào)輸入端都是AINP/AINN,即支持差分輸入。我的問(wèn)題是:
1:這個(gè)差分輸入和和單端輸入在本質(zhì) 上到底有什么區(qū)別? 因?yàn),ADC采集的信號(hào)說(shuō)到底是AINP - AINN,不管單端還是差分,采集的信號(hào)都是這兩個(gè)pad的差值。
差分信號(hào)是 AINP - AINN
單端信號(hào)是 AIN - REFN
2:將單端信號(hào)接在ADC的差分輸入接口上可以用嗎?可以
3:如果將差分放大器輸出的差分信號(hào),接在只支持單端信號(hào)輸入的ADC,會(huì)正常工作嗎?不會(huì),結(jié)果會(huì)出現(xiàn)錯(cuò)誤
PWM中斷觸發(fā)ADC采樣和ADC中斷是一個(gè)東西嗎?
首先要區(qū)分adc在這里扮演的角色,pwm中斷出發(fā)adc采樣是去讀取某個(gè)參數(shù),而adc中斷是在某個(gè)參數(shù)達(dá)到一定限制時(shí)發(fā)出一個(gè)中斷。性質(zhì)不一樣的哩。
PWM觸發(fā)ADC啟動(dòng)轉(zhuǎn)換有什么用
典型應(yīng)用是電機(jī)控制里面的相電流控制,pwm打開(kāi)之后ADC對(duì)相電流同步采樣,然后做電流PID反饋環(huán)
簡(jiǎn)單地說(shuō),補(bǔ)碼就是反碼加1。 計(jì)算機(jī)中為什么要使用補(bǔ)碼呢? 主要原因:1、使用補(bǔ)碼,可以將符號(hào)位和其它位統(tǒng)一處理;同時(shí),減法也可按加法來(lái)處理。另外,兩個(gè)用補(bǔ) 碼表示的數(shù)相加時(shí),如果最高位(符號(hào)位)有進(jìn)位,則進(jìn)位被舍棄。 2、補(bǔ)碼與原碼的轉(zhuǎn)換過(guò)程幾乎是相同的。 (1)正數(shù)的補(bǔ)碼 與原碼相同。 【例1】+9的補(bǔ)碼是00001001。(備注:這個(gè)+9的補(bǔ)碼說(shuō)的是用8位的2進(jìn)制來(lái)表示補(bǔ)碼的,補(bǔ)碼表示方式很多,還有16位2進(jìn)制補(bǔ)碼表示形式,以及32位2進(jìn)制補(bǔ)碼表示形式等。) (2)負(fù)數(shù)的補(bǔ)碼 符號(hào)位為1,其余位為該數(shù)絕對(duì)值的原碼按位取反;然后整個(gè)數(shù)加1。 同一個(gè)數(shù)字在不同的補(bǔ)碼表示形式里頭,是不同的。比方說(shuō)-15的補(bǔ)碼,在8位2進(jìn)制里頭是11110001,然而在16位2進(jìn)制補(bǔ)碼表示的情況下,就成了1111111111110001。在這篇補(bǔ)碼概述里頭涉及的補(bǔ)碼轉(zhuǎn)換默認(rèn)了把一個(gè)數(shù)轉(zhuǎn)換成8位2進(jìn)制的補(bǔ)碼形式,每一種補(bǔ)碼表示形式都只能表示有限的數(shù)字。 【例2】求-7的補(bǔ)碼。 因?yàn)榻o定數(shù)是負(fù)數(shù),則符號(hào)位為“1”。 后七位:+7的原碼(0000111)→按位取反(1111000)→加1(1111001) 所以-7的補(bǔ)碼是11111001。 已知一個(gè)數(shù)的補(bǔ)碼,求原碼的操作分兩種情況: 。1)如果補(bǔ)碼的符號(hào)位為“0”,表示是一個(gè)正數(shù),其原碼就是補(bǔ)碼。 。2)如果補(bǔ)碼的符號(hào)位為“1”,表示是一個(gè)負(fù)數(shù),那么求給定的這個(gè)補(bǔ)碼的補(bǔ)碼就是要求的原碼。 另一種方法求負(fù)數(shù)的補(bǔ)碼如下: 例如:求-15的補(bǔ)碼 第一步:+15:00001111 第二步:逐位取反(1變成0,0變成1),然后在末尾加1。 11110001 再舉一個(gè)例子驗(yàn)證下:求-64的補(bǔ)碼 +64:01000000 11000000 【例3】已知一個(gè)補(bǔ)碼為11111001,則原碼是10000111(-7)。 因?yàn)榉?hào)位為“1”,表示是一個(gè)負(fù)數(shù),所以該位不變,仍為“1”。 其余七位1111001取反后為0000110; 再加1,所以是10000111。
掃碼關(guān)注我們
傳真:0755-82591176
郵箱:vicky@yingtexin.net
地址:深圳市龍華區(qū)民治街道民治大道973萬(wàn)眾潤(rùn)豐創(chuàng)業(yè)園A棟2樓A08