STM32F4 定时器触发 三ADC 规则+注入

2019-07-14 19:02发布

想要使用高级定时器相互触发,同时用定时器触发三个ADC的多路规则通道和注入通道,可是写好程序后ADC总是未被触发看了几遍也找不到问题,请教一下大家
  1. ADC配置:
  2.         ADC_CommonInitStructure.ADC_Mode = ADC_TripleMode_RegSimult;                 //adc三重模式
  3.         ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  4.         ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1;
  5.         ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  6.         ADC_CommonInit(&ADC_CommonInitStructure);

  7.         // Channel-specific settings
  8.         ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;      //ADC分辨率双模式  12位精度
  9.         ADC_InitStructure.ADC_ScanConvMode = ENABLE;   //扫描模式
  10.         ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  11.         ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Falling;     //选择外部触发方式 下降沿触发
  12.         ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T8_CC1;             //选择TIM8_CC1触发规则组开始转换的外部事件
  13.         ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;     //数据右对齐
  14.         ADC_InitStructure.ADC_NbrOfConversion = 4;       //指定使用规则通道组完成转换的次数   采样通道数

  15.         ADC_Init(ADC1, &ADC_InitStructure);
  16.         ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;  //不使用外部触发  (多ADC模式中,外部事件触发必须仅触发主ADC,从ADC设置为软件触发)
  17.         ADC_InitStructure.ADC_ExternalTrigConv = 0;
  18.         ADC_Init(ADC2, &ADC_InitStructure);
  19.         ADC_Init(ADC3, &ADC_InitStructure);

  20.         // Enable DMA request after last transfer (Multi-ADC mode)  多ADC模式下 使能 源数据变化时开启DMA传输
  21.         ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE);

  22.         // Injected channels for current measurement at end of cycle
  23.         ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_T1_CC4);
  24.         ADC_ExternalTrigInjectedConvConfig(ADC2, ADC_ExternalTrigInjecConv_T8_CC2);
  25.         ADC_ExternalTrigInjectedConvEdgeConfig(ADC1, ADC_ExternalTrigInjecConvEdge_Falling);
  26.         ADC_ExternalTrigInjectedConvEdgeConfig(ADC2, ADC_ExternalTrigInjecConvEdge_Falling);
  27.         ADC_InjectedSequencerLengthConfig(ADC1, 2);   //设置注入通道长度  1-4
  28.         ADC_InjectedSequencerLengthConfig(ADC2, 2);

  29.     ADC_Channels();
  30.    
  31.         // Interrupt
  32.         ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);    //使能ADC的JEOC中断源   注入转换中断屏蔽结束
  33.    
  34.     ADC_AutoInjectedConvCmd(ADC1, ENABLE);
  35.     ADC_AutoInjectedConvCmd(ADC2, ENABLE);
  36.    
  37.     //Enable ADC_DMA
  38.     ADC_DMACmd(ADC1, ENABLE);
  39.     ADC_DMACmd(ADC2, ENABLE);
  40.     ADC_DMACmd(ADC3, ENABLE);

  41.         // Enable ADC1
  42.         ADC_Cmd(ADC1, ENABLE);

  43.         // Enable ADC2
  44.         ADC_Cmd(ADC2, ENABLE);

  45.         // Enable ADC3
  46.         ADC_Cmd(ADC3, ENABLE);
复制代码
  1. 定时器配置
  2. void TIM1_Config(void)
  3. {
  4.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  5.         TIM_OCInitTypeDef  TIM_OCInitStructure;
  6.    
  7.     TIM_DeInit(TIM1);
  8.         TIM1->CNT = 0;

  9.         // TIM1 clock enable
  10.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

  11.         // Time Base configuration
  12.         TIM_TimeBaseStructure.TIM_Prescaler = 8399;
  13.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  14.         TIM_TimeBaseStructure.TIM_Period = 9999;
  15.         TIM_TimeBaseStructure.TIM_ClockDivision = 0;    //时钟分割
  16.         TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

  17.         TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  18.         // Channel 1, 2 and 3 Configuration in PWM mode
  19.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  20.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  21.         TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  22.         TIM_OCInitStructure.TIM_Pulse = TIM1->ARR / 2;
  23.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;      //输出比较极性 高
  24.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;  //互补输出极性 高
  25.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;        //空闲状态期间输出比较引脚状态
  26.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
  27.         TIM_OC4Init(TIM1, &TIM_OCInitStructure);

  28.         TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);

  29.         TIM_CCPreloadControl(TIM1, ENABLE);
  30.         TIM_ARRPreloadConfig(TIM1, ENABLE);

  31.         // TIM1 Master and TIM8 slave
  32.         TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
  33.         TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
  34.    
  35.     TIM_CCxCmd(TIM1, TIM_Channel_4, TIM_CCx_Enable);

  36.         // Enable TIM1 and TIM8
  37.         TIM_Cmd(TIM1, ENABLE);

  38.         // Main Output Enable
  39.         TIM_CtrlPWMOutputs(TIM1, ENABLE);   //设置TIM1 的PWM输出使能

  40. }

  41. void TIM8_Config(void)
  42. {
  43.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  44.         TIM_OCInitTypeDef  TIM_OCInitStructure;
  45.    
  46.     TIM_DeInit(TIM8);
  47.     TIM8->CNT = 0;
  48.    
  49.         // ------------- Timer8 for ADC sampling ------------- //
  50.         // Time Base configuration
  51.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

  52.         TIM_TimeBaseStructure.TIM_Prescaler = 8399;
  53.         TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  54.         TIM_TimeBaseStructure.TIM_Period = 9999;
  55.         TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  56.         TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  57.         TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

  58.         TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  59.         TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  60.         TIM_OCInitStructure.TIM_Pulse = TIM8->ARR / 2;
  61.         TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  62.         TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  63.         TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
  64.         TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;
  65.         TIM_OC1Init(TIM8, &TIM_OCInitStructure);
  66.         TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
  67.         TIM_OC2Init(TIM8, &TIM_OCInitStructure);
  68.         TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);

  69.         TIM_ARRPreloadConfig(TIM8, ENABLE);
  70.         TIM_CCPreloadControl(TIM8, ENABLE);

  71.         TIM_CtrlPWMOutputs(TIM8, ENABLE);
  72.    
  73.     TIM_SelectInputTrigger(TIM8, TIM_TS_ITR0);              //选择输入触发源   ITR0
  74.         TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Reset);        //计数器从模式选择   所选的触发信号上升沿重新初始化
  75.    
  76.     TIM_ITRxExternalClockConfig(TIM8, TIM_TS_ITR0);      //选择ITR0为触发源提供时钟

  77.     TIM_CCxCmd(TIM8, TIM_Channel_1, TIM_CCx_Enable);
  78.     TIM_CCxCmd(TIM8, TIM_Channel_2, TIM_CCx_Enable);
  79.    
  80.     TIM_ITConfig(TIM8, TIM_IT_CC1, ENABLE);
  81.     TIM_ITConfig(TIM8, TIM_IT_CC2, ENABLE);
  82.    
  83.     TIM_Cmd(TIM8, ENABLE);
  84. }

复制代码


0条回答

一周热门 更多>