打印
[STM32F3]

STM32F303 定时器触发ADC

[复制链接]
4891|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
amanmax|  楼主 | 2016-12-14 17:07 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
求教定时器触发ADC,采用TRGO或TRGO2能触发,但用CC1至CC4都不行,CC1至CC4都能产生中断,不知哪里还需要设置
沙发
zhuotuzi| | 2016-12-14 17:21 | 只看该作者
本帖最后由 zhuotuzi 于 2016-12-14 17:23 编辑

从定时器框图可以看出,TRGO为TIMx的一个输出.不过这个输出是内部使用的.
使用TRGO触发ADC是可以的.具体你可以参考一下<<STM32参考手册>>V10第278页,使用一个定时器作为另一个定时器的预分频器的方法.


void myADC_Init()
{
                 ADC_InitTypeDef ADC_InitStruct;
                 GPIO_InitTypeDef GPIO_InitStruct;
                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 ,ENABLE);
                 RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  
                //ADC1_Init
                 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0;
                 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
                 GPIO_Init(GPIOA,&GPIO_InitStruct);
                  
                ADC_DeInit(ADC1);
                 ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;
                 ADC_InitStruct.ADC_NbrOfChannel = 1;
                 ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;
                 ADC_InitStruct.ADC_ScanConvMode = DISABLE;
                 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO;
                 ADC_InitStruct.ADC_DataAlign =ADC_DataAlign_Right;
                 ADC_Init(ADC1,&ADC_InitStruct);
         
                ADC_ExternalTrigConvCmd(ADC1,ENABLE);
                 //ENABLE ADC1
                 ADC_RegularChannelConfig(ADC1,0,1,ADC_SampleTime_1Cycles5);
         
                ADC_DMACmd(ADC1,ENABLE);
                 ADC_Cmd(ADC1,ENABLE);
                  
                ADC_ResetCalibration(ADC1);
                 while(ADC_GetResetCalibrationStatus(ADC1));
                 ADC_StartCalibration(ADC1);
                 while(ADC_GetCalibrationStatus(ADC1));
         
}
  
void TIM3_Init()
{
                 TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
                 NVIC_InitTypeDef NVIC_InitStruct;
         
                RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
                  
                TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
                 TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
                 TIM_TimeBaseInitStruct.TIM_Period = 72000000/SampleRate;
                 TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
                 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
                  
                TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Update); //选择TRGO触发源为计时器更新时间
         
                TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
                 NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
                 NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
                 NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3;
                 NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                 NVIC_Init(&NVIC_InitStruct);
                 TIM_Cmd(TIM3,ENABLE);
         
}

使用特权

评论回复
板凳
amanmax|  楼主 | 2016-12-14 17:44 | 只看该作者
zhuotuzi 发表于 2016-12-14 17:21
从定时器框图可以看出,TRGO为TIMx的一个输出.不过这个输出是内部使用的.
使用TRGO触发ADC是可以的.具体你可 ...

谢谢你,现在是stm32F1 F2 F4 都能触发CC1 至 CC4, STM32F303 把ADC_ExternalTrigConv_T3_TRGO 改为 CC1 或 CC4 不能触发, ADC_ExternalTrigConv_T3_TRGO是可以的, 用了标准库与HAL库都不行

使用特权

评论回复
地板
zhuotuzi| | 2016-12-14 17:45 | 只看该作者
amanmax 发表于 2016-12-14 17:44
谢谢你,现在是stm32F1 F2 F4 都能触发CC1 至 CC4, STM32F303 把ADC_ExternalTrigConv_T3_TRGO 改为 CC1  ...

你可以看一下库函数的说明部分,里面有没有提到这个。

使用特权

评论回复
5
amanmax|  楼主 | 2016-12-14 17:51 | 只看该作者
看过了,搞了我3天了,不知谁有用过ADC注入时用CC4触发能成功的,提供资料感激不尽!

使用特权

评论回复
6
amanmax|  楼主 | 2016-12-16 18:54 | 只看该作者
经过测试,TIM2,3,4,的CC1,CC2,CC3,CC4,都能触发ADC,唯有TIM1,TIM8的高级定时器没法用CC4触发ADC,TGRO能触发,CC4能产生中断,下面配置是用TRGO触发成功的,谁能帮我看看?

void test_Time1_adc(void)
{
ADC_InitTypeDef   ADC_InitStructure;
ADC_CommonInitTypeDef  ADC_CommonInitStructure;       
__IO uint16_t  ADC1ConvertedValue = 0, ADC1ConvertedVoltage = 0, calibration_value = 0;
        u32 i;
  NVIC_InitTypeDef NVIC_InitStructure;
       
/* Configure the ADC clock */  
RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div2);
/* Enable the  DMA1¡¢GPIOA ¡¢GPIOB and ADC1 Clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_ADC12, ENABLE);
       
/* Configure ADC1 */
ADC_StructInit(&ADC_InitStructure);
   
/* Calibration procedure */
ADC_VoltageRegulatorCmd(ADC1, ENABLE);
/* Insert delay equal to 10ms */
       
        ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single);
ADC_StartCalibration(ADC1);
   
while(ADC_GetCalibrationStatus(ADC1) != RESET );
calibration_value = ADC_GetCalibrationValue(ADC1);

/* Configure the ADC1 in continuous mode */  
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;                                                                    
ADC_CommonInitStructure.ADC_Clock = ADC_Clock_AsynClkMode;                  
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;            
ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular;                  
ADC_CommonInitStructure.ADC_TwoSamplingDelay = 5;      
ADC_CommonInit(ADC1, &ADC_CommonInitStructure);
   
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Disable;
ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_7;     //TIM4_CC4 event        
ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable;   
ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable;   
ADC_InitStructure.ADC_NbrOfRegChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
   
/* ADC1 regular channel1 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 1, ADC_SampleTime_181Cycles5);

  ADC_InjectedStructInit(&ADC1_Injected);
       
        ADC1_Injected.ADC_ExternalTrigInjecConvEvent=ADC_ExternalTrigInjecConvEvent_0;  //TIM1_TRGO
//        ADC1_Injected.ADC_ExternalTrigInjecConvEvent=ADC_ExternalTrigInjecConvEvent_1;  //TIM1_CC4
        ADC1_Injected.ADC_ExternalTrigInjecEventEdge=ADC_ExternalTrigInjecEventEdge_RisingEdge;
        ADC1_Injected.ADC_NbrOfInjecChannel=1;
        ADC1_Injected.ADC_InjecSequence1=3;
        ADC_InjectedInit(ADC1,&ADC1_Injected);
  ADC_InjectedChannelConfig(ADC1, 3,1,ADC_SampleTime_7Cycles5);
  //ADC1->JSQR

  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
        TIM1_Conf();

/* wait for ADRDY */
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY));

  ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
  ADC_ClearFlag(ADC1, ADC_FLAG_JEOC);
        ADC_StartInjectedConversion(ADC1);

                while(1){
    /* Test JEOC flag */
    while(ADC_GetFlagStatus(ADC1, ADC_FLAG_JEOC) == RESET);   
    /* Get ADC1 converted data */
    ADC1ConvertedValue =ADC_GetInjectedConversionValue(ADC1,ADC_InjectedChannel_1);
    ADC_ClearFlag(ADC1, ADC_FLAG_JEOC);
        }
       
}

void TIM1_Conf(void)  
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

/* TIM1 clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
       
  TIM_DeInit(TIM1);
  TIM_TimeBaseStructInit(&TIM1_TimeBaseStructure);
  /* Time Base configuration */
  TIM1_TimeBaseStructure.TIM_Prescaler = 0x0;
  TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;
  TIM1_TimeBaseStructure.TIM_Period = 0x800;
  TIM1_TimeBaseStructure.TIM_ClockDivision = 0;
  
  // Initial condition is REP=0 to set the UPDATE only on the underflow
  TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0;
  TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseStructure);
  
  TIM_OCStructInit(&TIM1_OCInitStructure);
  /* Channel 1, 2,3 in PWM mode */
  TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;                  
  TIM1_OCInitStructure.TIM_Pulse = 0x400; //dummy value
  TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;         
  TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;         
  
  TIM_OC1Init(TIM1, &TIM1_OCInitStructure);
  TIM_OC2Init(TIM1, &TIM1_OCInitStructure);
  TIM_OC3Init(TIM1, &TIM1_OCInitStructure);
  
  TIM_OCStructInit(&TIM1_OCInitStructure);
  /* Channel 4 Configuration in OC */
  TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;  
  TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;                  
  TIM1_OCInitStructure.TIM_Pulse = 0x400;
  
  TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM1_OCInitStructure.TIM_OCNPolarity =TIM_OCNPolarity_Low;         
  TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  TIM1_OCInitStructure.TIM_OCNIdleState = LOW_SIDE_POLARITY;            
  
  TIM_OC4Init(TIM1, &TIM1_OCInitStructure);
  
  /* Enables the TIM1 Preload on CC1 Register */
  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
  /* Enables the TIM1 Preload on CC2 Register */
  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
  /* Enables the TIM1 Preload on CC3 Register */
  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
  /* Enables the TIM1 Preload on CC4 Register */
  TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);

  TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
  TIM_SelectOutputTrigger2(TIM1, TIM_TRGO2Source_Reset);
        TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Reset);

  TIM_Cmd(TIM1, ENABLE);
  // Resynch to have the Update evend during Undeflow
  TIM_GenerateEvent(TIM1, TIM_EventSource_Update);
  // Clear Update Flag
  TIM_ClearFlag(TIM1, TIM_FLAG_Update);
  
  TIM_ITConfig(TIM1, TIM_IT_CC4, ENABLE);
       
       
}

使用特权

评论回复
7
solo电| | 2019-6-1 14:55 | 只看该作者
弱弱的问一句,TIM1->CCR4可以触发了吗?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

5

帖子

0

粉丝