本帖最后由 禁基的矮子 于 2022-9-6 13:39 编辑
这是我根据官方例程TIMTrigger_AutoInjection将规则通道取消后的测试代码- #include "misc.h"
- #include "n32g031_adc.h"
- #include "n32g031_gpio.h"
- #include "n32g031_rcc.h"
- #include "n32g031_tim.h"
- ADC_InitType ADC_InitStructure;
- TIM_TimeBaseInitType TIM_TimeBaseStructure;
- OCInitType TIM_OCInitStructure;
- __IO uint16_t ADC_InjectedConvertedValueTab[32];
- __IO uint32_t Index;
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- int main(void)
- {
- /* System clocks configuration ---------------------------------------------*/
- RCC_Configuration();
- /* NVIC configuration ------------------------------------------------------*/
- NVIC_Configuration();
- /* GPIO configuration ------------------------------------------------------*/
- GPIO_Configuration();
- /* TIM1 configuration ------------------------------------------------------*/
- /* Time Base configuration */
- TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.Period = 3000 - 1; // 0xFF;
- TIM_TimeBaseStructure.Prescaler = 0;
- TIM_TimeBaseStructure.ClkDiv = 0x0;
- TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
- TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure);
- /* TIM1 channel1 configuration in PWM mode */
- TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;
- TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
- TIM_OCInitStructure.Pulse = 2400 - 1;
- TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;
- TIM_InitOc4(TIM1, &TIM_OCInitStructure);
- /* ADC configuration ------------------------------------------------------*/
- ADC_InitStructure.MultiChEn = ENABLE;
- ADC_InitStructure.ContinueConvEn = DISABLE;
- ADC_InitStructure.ExtTrigSelect = ADC_EXT_TRIGCONV_NONE;
- ADC_InitStructure.DatAlign = ADC_DAT_ALIGN_R;
- ADC_InitStructure.ChsNumber = 1;
- ADC_Init(ADC, &ADC_InitStructure);
- /* Set injected sequencer length */
- ADC_ConfigInjectedSequencerLength(ADC, 2);
- /* ADC injected channel Configuration */
- ADC_ConfigInjectedChannel(ADC, ADC_CH_0_PA0, 1, ADC_SAMP_TIME_6CYCLES5);
- ADC_ConfigInjectedChannel(ADC, ADC_CH_1_PA1, 2, ADC_SAMP_TIME_6CYCLES5);
- /* ADC injected external trigger configuration */
- ADC_ConfigExternalTrigInjectedConv(ADC, ADC_EXT_TRIG_INJ_CONV_T1_CC4);
- /* Enable automatic injected conversion start after regular one */
- ADC_EnableAutoInjectedConv(ADC, DISABLE);
- /* Enable JEOC interrupt */
- ADC_ConfigInt(ADC, ADC_INT_JENDC, ENABLE);
- /* Enable ADC external trigger */
- ADC_EnableExternalTrigInjectedConv(ADC, ENABLE);
- /* Enable ADC */
- ADC_Enable(ADC, ENABLE);
- /*wait ADC is ready to use*/
- while (!ADC_GetFlagStatusNew(ADC, ADC_FLAG_RDY))
- ;
- /*wait ADC is powered on*/
- while (ADC_GetFlagStatusNew(ADC, ADC_FLAG_PD_RDY))
- ;
- /* TIM1 counter enable */
- TIM_Enable(TIM1, ENABLE);
- /* TIM1 main Output Enable */
- TIM_EnableCtrlPwmOutputs(TIM1, ENABLE);
- while (1) {
- }
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Configures the different system clocks.
- */
- void RCC_Configuration(void)
- {
- ErrorStatus HSIStartUpStatus;
- /* Enable peripheral clocks ------------------------------------------------*/
- /* Enable TIM1 clocks */
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM1, ENABLE);
- /* Enable GPIO clocks */
- RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB, ENABLE);
- /* Enable ADC clocks */
- RCC_EnableAHBPeriphClk(RCC_AHB_PERIPH_ADC, ENABLE);
- /* RCC_ADCHCLK_DIV16*/
- ADC_ConfigClk(ADC_CTRL3_CKMOD_AHB, RCC_ADCHCLK_DIV3);
- /* enable ADC1M clock */
- RCC_EnableHsi(ENABLE);
- /* Wait til1 HSI is ready*/
- HSIStartUpStatus = RCC_WaitHsiStable();
- if (HSIStartUpStatus == SUCCESS) {
- }
- else {
- /* If HSI fails to start-up, the application will have wrong clock configuration. User can add here some code to
- * deal with this error*/
- /* Go to infinital 1oop*/
- while (1) {
- }
- }
- RCC_ConfigAdc1mClk(RCC_ADC1MCLK_SRC_HSI, RCC_ADC1MCLK_DIV8);
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Configures the different GPIO ports.
- */
- void GPIO_Configuration(void)
- {
- GPIO_InitType GPIO_InitStructure;
- GPIO_InitStruct(&GPIO_InitStructure);
- /* Configure TIM1_CH4(PA11) as alternate function push-pull */
- GPIO_InitStructure.Pin = GPIO_PIN_11;
- GPIO_InitStructure.GPIO_Current = GPIO_DC_LOW;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP;
- GPIO_InitStructure.GPIO_Alternate = GPIO_AF2_TIM1;
- GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
- /* Configure PB.07 as output push-pull */
- GPIO_InitStructure.Pin = GPIO_PIN_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure);
- /* Configure PA.00 and PA.01 as analog input */
- GPIO_InitStructure.Pin = GPIO_PIN_0 | GPIO_PIN_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_MODE_ANALOG;
- GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
- }
- /**
- * @brief Configures NVIC and Vector Table base location.
- */
- void NVIC_Configuration(void)
- {
- NVIC_InitType NVIC_InitStructure;
- /* Configure and enable ADC interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- /**
- * @brief This function handles ADC global interrupts requests.
- */
- void ADC_IRQHandler(void)
- {
- /* Set PA.06 pin */
- GPIOB->PBSC = GPIO_PIN_7;
- /* Get injected channel11 converted value */
- if (Index >= sizeof(ADC_InjectedConvertedValueTab) / sizeof(ADC_InjectedConvertedValueTab[0])) {
- Index = 0;
- }
- ADC_InjectedConvertedValueTab[Index++] = ADC_GetInjectedConversionDat(ADC, ADC_INJ_CH_1);
- ADC_InjectedConvertedValueTab[Index++] = ADC_GetInjectedConversionDat(ADC, ADC_INJ_CH_2);
- /* Clear ADC JEOC pending interrupt bit */
- ADC_ClearIntPendingBit(ADC, ADC_INT_JENDC);
- /* Reset PA.06 pin */
- GPIOB->PBC = GPIO_PIN_7;
- }
|