STM32學(xué)習(xí)記錄19 定時(shí)器觸發(fā)ADC
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);
①頭文件中
#define | TIM_OCMode_PWM1((uint16_t)0x0060) 賦值給TIMx->CCMR1,對比下面的文檔,可以看出是設(shè)置為PWM模式1 |
②頭文件中定義
#define | TIM_OutputState_Enable((uint16_t)0x0001)賦值給TIMx->CCER寄存器 |
③頭文件中定義
#define | TIM_OCPolarity_Low((uint16_t)0x0002)賦值給TIMx->CCER,對照文檔看出 |
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。
關(guān)鍵詞:
STM32學(xué)習(xí)記錄1定時(shí)器觸發(fā)AD
相關(guān)推薦
技術(shù)專區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線
- 開關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車電子
- 轉(zhuǎn)換器
- 電源管理
- 信號(hào)放大器
評論