程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 【stm32】用TIM1產生6路ADC,用CCR4觸發ADC1的注入通道采樣

【stm32】用TIM1產生6路ADC,用CCR4觸發ADC1的注入通道采樣

編輯:關於C語言

      這幾天一直在使用STM32來寫sensorless BLDC的驅動框架,那麼必須會用到TIM1的CCR1/CCR2/CCR3產生的六路互補PWM,以及用CCR4來產生一個中斷,用來在PWM-ON的時候產生中斷進行過零檢測,以及相電流的檢測等。

      這幾天一直在測試PWM,CCR4的中斷,ADC1的采樣觸發+DMA等功能,現在也了解的差不多了,先記錄下來,先看下我的一些設置,TIM1設置:

   
     TIM_TimeBaseStructure.TIM_Period =     TIM_TimeBaseStructure.TIM_Prescaler =      TIM_TimeBaseStructure.TIM_CounterMode =     TIM_TimeBaseStructure.TIM_ClockDivision =      TIM_TimeBaseStructure.TIM_RepetitionCounter =      TIM_TimeBaseInit(TIM1, &     
     
     TIM_BDTRInitStructure.TIM_OSSRState =     TIM_BDTRInitStructure.TIM_OSSIState =     TIM_BDTRInitStructure.TIM_LOCKLevel =     TIM_BDTRInitStructure.TIM_DeadTime =      TIM_BDTRInitStructure.TIM_Break =     TIM_BDTRInitStructure.TIM_BreakPolarity =     TIM_BDTRInitStructure.TIM_AutomaticOutput =     TIM_BDTRConfig(TIM1, &     
     
     TIM_OCInitStructure.TIM_OCMode =     TIM_OCInitStructure.TIM_OutputState =     TIM_OCInitStructure.TIM_OutputNState =     TIM_OCInitStructure.TIM_Pulse =      TIM_OCInitStructure.TIM_OCPolarity =     TIM_OCInitStructure.TIM_OCNPolarity =     TIM_OCInitStructure.TIM_OCIdleState =     TIM_OCInitStructure.TIM_OCNIdleState =     
     TIM_OC1Init(TIM1, &     
     TIM_OCInitStructure.TIM_Pulse =      TIM_OC2Init(TIM1, &     
     TIM_OCInitStructure.TIM_Pulse =      TIM_OC3Init(TIM1, &     
     TIM_OCInitStructure.TIM_OutputState =     TIM_OCInitStructure.TIM_Pulse =      TIM_OCInitStructure.TIM_OCIdleState =     TIM_OC4Init(TIM1, & 
     
    
     
  
     
     TIM_CtrlPWMOutputs(TIM1, ENABLE);

  ADC1設置,這裡采用規則通道+DMA+軟件觸發,注入通道+CCR4事件觸發:

     ADC_InitStructure.ADC_Mode =     ADC_InitStructure.ADC_ScanConvMode =     ADC_InitStructure.ADC_ContinuousConvMode =     ADC_InitStructure.ADC_ExternalTrigConv =     ADC_InitStructure.ADC_DataAlign =     ADC_InitStructure.ADC_NbrOfChannel =      ADC_Init(ADC1, &     
      ADC_RegularChannelConfig(ADC1,ADC_Channel_0,,ADC_SampleTime_239Cycles5);
     ADC_RegularChannelConfig(ADC1,ADC_Channel_1,,ADC_SampleTime_239Cycles5);
     ADC_RegularChannelConfig(ADC1,ADC_Channel_2,,ADC_SampleTime_239Cycles5);
     ADC_RegularChannelConfig(ADC1,ADC_Channel_10 ,     ADC_RegularChannelConfig(ADC1,ADC_Channel_Vrefint,    
     
     ADC_InjectedSequencerLengthConfig(ADC1,      ADC_InjectedChannelConfig(ADC1,ADC_Channel_TempSensor,,ADC_SampleTime_7Cycles5);
     ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_T1_CC4);
      
     ADC_ITConfig(ADC1,ADC_IT_JEOC,ENABLE);
     
   
     
     
  
     ADC_Cmd(ADC1, ENABLE);

  另外我還加了一個注入通道采樣結束的中斷,用來快速處理相電流的一些問題,比如限流、過流的處理。

  測試從發現如果把CCR4的pulse設為0,那麼就不會觸發ADC1的注入采樣,也就不會產生ADC的中斷,只有pluse設置為一定的有效數值,那麼adc注入通道的采樣才會被觸發。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved