色婷婷AⅤ一区二区三区|亚洲精品第一国产综合亚AV|久久精品官方网视频|日本28视频香蕉

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32學(xué)習(xí)記錄19 定時(shí)器觸發(fā)ADC

          STM32學(xué)習(xí)記錄19 定時(shí)器觸發(fā)ADC

          作者: 時(shí)間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
          關(guān)于定時(shí)器出發(fā)ADC的問題一直比較迷惑,看各種資料講的也很少。這里就那官方的例程來仔細(xì)分析一下吧。
          1:下面為官方例程,
          int main(void)
          {
          #ifdef DEBUG
          debug();
          #endif
          /* System clocks configuration ---------------------------------------------*/
          RCC_Configuration();
          /* NVIC configuration ------------------------------------------------------*/
          NVIC_Configuration();
          /* GPIO configuration ------------------------------------------------------*/
          GPIO_Configuration();
          /* TIM1 configuration ------------------------------------------------------*/
          /* Time Base configuration */
          TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
          TIM_TimeBaseStructure.TIM_Period = 0xFF;
          TIM_TimeBaseStructure.TIM_Prescaler = 0x4;
          TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
          TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
          /* TIM1 channel1 configuration in PWM mode */
          TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
          TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
          TIM_OCInitStructure.TIM_Pulse = 0x7F;
          TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
          TIM_OC1Init(TIM1, &TIM_OCInitStructure);
          /* DMA1 Channel1 Configuration ----------------------------------------------*/
          DMA_DeInit(DMA1_Channel1);
          DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
          DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_RegularConvertedValueTab;
          DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
          DMA_InitStructure.DMA_BufferSize = 32;
          DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
          DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
          DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
          DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
          DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
          DMA_InitStructure.DMA_Priority = DMA_Priority_High;
          DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
          DMA_Init(DMA1_Channel1, &DMA_InitStructure);
          /* Enable DMA1 channel1 */
          DMA_Cmd(DMA1_Channel1, ENABLE);
          /* ADC1 configuration ------------------------------------------------------*/
          ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
          ADC_InitStructure.ADC_ScanConvMode = DISABLE;
          ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
          ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
          ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
          ADC_InitStructure.ADC_NbrOfChannel = 1;
          ADC_Init(ADC1, &ADC_InitStructure);
          /* ADC1 regular channel14 configuration */
          ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5);
          /* Set injected sequencer length */
          ADC_InjectedSequencerLengthConfig(ADC1, 1);
          /* ADC1 injected channel Configuration */
          ADC_InjectedChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5);
          /* ADC1 injected external trigger configuration */
          ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None);
          /* Enable automatic injected conversion start after regular one */
          ADC_AutoInjectedConvCmd(ADC1, ENABLE);
          /* Enable ADC1 DMA */
          ADC_DMACmd(ADC1, ENABLE);
          /* Enable ADC1 external trigger */
          ADC_ExternalTrigConvCmd(ADC1, ENABLE);
          /* Enable JEOC interupt */
          ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE);
          /* Enable ADC1 */
          ADC_Cmd(ADC1, ENABLE);
          /* Enable ADC1 reset calibaration register */
          ADC_ResetCalibration(ADC1);
          /* Check the end of ADC1 reset calibration register */
          while(ADC_GetResetCalibrationStatus(ADC1));
          /* Start ADC1 calibaration */
          ADC_StartCalibration(ADC1);
          /* Check the end of ADC1 calibration */
          while(ADC_GetCalibrationStatus(ADC1));
          /* TIM1 counter enable */
          TIM_Cmd(TIM1, ENABLE);
          /* TIM1 main Output Enable */
          TIM_CtrlPWMOutputs(TIM1, ENABLE);
          /* Test on channel1 transfer complete flag */
          while(!DMA_GetFlagStatus(DMA1_FLAG_TC1));
          /* Clear channel1 transfer complete flag */
          DMA_ClearFlag(DMA1_FLAG_TC1);
          /* TIM1 counter disable */
          TIM_Cmd(TIM1, DISABLE);
          while (1)
          {
          }
          }
          2:關(guān)于AD的DMA暫時(shí)不介紹,主要介紹定時(shí)器和ADC的關(guān)聯(lián),下面是例程中兩者的關(guān)鍵函數(shù)
          /* TIM1 configuration ------------------------------------------------------*/
          /* Time Base configuration */
          TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
          TIM_TimeBaseStructure.TIM_Period = 0xFF;
          TIM_TimeBaseStructure.TIM_Prescaler = 0x4;
          TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
          TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
          /* TIM1 channel1 configuration in PWM mode */
          TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
          TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
          TIM_OCInitStructure.TIM_Pulse = 0x7F;
          TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
          TIM_OC1Init(TIM1, &TIM_OCInitStructure);
          。
          。
          /* Enable ADC1 */
          ADC_Cmd(ADC1, ENABLE);
          。
          /* TIM1 counter enable */
          TIM_Cmd(TIM1, ENABLE);
          /* TIM1 main Output Enable */
          TIM_CtrlPWMOutputs(TIM1, ENABLE);
          。
          。
          /* TIM1 counter disable */
          TIM_Cmd(TIM1, DISABLE);

          下面分條解釋,解釋之前需要了解一下ADC外部觸發(fā)的相關(guān)知識(shí):
          2.1首先ADC的外部觸發(fā)必須是上升沿才可以啟動(dòng)轉(zhuǎn)換。
          那么如何得到上升沿呢?需要看一下定時(shí)器的內(nèi)容
          2.2定時(shí)器中有這么一段話:輸出部分產(chǎn)生一個(gè)中間波形OCxRef(高有效)作為基準(zhǔn),鏈的末端決定最終輸出信號(hào)的極性。 也就是說OCxREF只是一個(gè)中間信號(hào),我們關(guān)心的是最終的信號(hào)。
          那么最后一句話中最終信號(hào)的極性如何確定呢?繼續(xù)往下看:
          2.3末端最終信號(hào)的極性確定
          從圖中可以看到中中間參看信號(hào)OC1REF經(jīng)過TIM1_CCER_CCIE TIM1_CCER_CCIP最終決定OC1的輸出極性??匆幌聰?shù)據(jù)手冊,輸出模式中貌似只有PWM模式可以滿足2.1中提到的,可以產(chǎn)生一個(gè)上升沿來觸發(fā)AD轉(zhuǎn)換。
          現(xiàn)在可以解釋代碼了,結(jié)合官網(wǎng)的源文件和寄存器來說明一下會(huì)更加的深刻吧!
          3 代碼解釋
          3.1 定時(shí)器設(shè)定為PWM模式
          /* TIM1 configuration ------------------------------------------------------*/
          /* Time Base configuration */
          TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
          TIM_TimeBaseStructure.TIM_Period = 0xFF;
          TIM_TimeBaseStructure.TIM_Prescaler = 0x4;
          TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
          TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
          TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);//上面的代碼就不需要解釋了,主要就是設(shè)置定時(shí)器的周期,需要注意的是定時(shí)器設(shè)置為向上計(jì)數(shù)模式。下面的代碼逐條解釋
          /* TIM1 channel1 configuration in PWM mode */
          ①TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //TIM1 脈沖寬度調(diào)制模式 1
          ②TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能輸出比較狀態(tài)
          TIM_OCInitStructure.TIM_Pulse = 0x7F;
          ③TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM1 輸出比較極性低
          TIM_OC1Init(TIM1, &TIM_OCInitStructure);
          ①頭文件中
          #defineTIM_OCMode_PWM1((uint16_t)0x0060) 賦值給TIMx->CCMR1,對比下面的文檔,可以看出是設(shè)置為PWM模式1


          ②頭文件中定義
          #defineTIM_OutputState_Enable((uint16_t)0x0001)賦值給TIMx->CCER寄存器
          ,對照文檔看出是使能OC1的輸出。
          ③頭文件中定義
          #defineTIM_OCPolarity_Low((uint16_t)0x0002)賦值給TIMx->CCER,對照文檔看出
          OC1是低電平有效,這一點(diǎn)非常重要,說明在最后的輸出端口是低電平有效的,
          TIMx_ARR:自動(dòng)裝載寄存器, 確定PWM的頻率,
          TIM_CCRx:捕獲/比較寄存器,確定只占空比
          TIMx_CNT:計(jì)數(shù)器寄存器。
          也就是說當(dāng)CNT
          3.2 定時(shí)器和PWM打開
          /* TIM1 counter enable */
          TIM_Cmd(TIM1, ENABLE);
          /* TIM1 main Output Enable */
          TIM_CtrlPWMOutputs(TIM1, ENABLE);
          。
          。
          。
          /* TIM1 counter disable */
          TIM_Cmd(TIM1, DISABLE);
          上面代碼相對簡單,就是打開定時(shí)器,使能PWM輸出。需要注意的是一定要有TIM_CtrlPWMOutputs(TIM1, ENABLE);函數(shù)的調(diào)用。其中就是使能了TIMx_BDTR_MOE。


          評論


          技術(shù)專區(qū)

          關(guān)閉