STM32F103ZET6 之 通用定時(shí)器單脈沖模式實(shí)驗(yàn)
掃描二維碼
隨時(shí)隨地手機(jī)看文章
由于前面買(mǎi)的核心板,供電老有問(wèn)題,使得我現(xiàn)在的項(xiàng)目又改用了以前用的F103ZET6微控制器!
1、實(shí)驗(yàn)?zāi)康?/p>
1)產(chǎn)生脈寬任意可調(diào)的單脈沖(在允許的范圍內(nèi))
2、硬件:通用定時(shí)器3、通用定時(shí)器4
3、單脈沖模式介紹
單脈沖模式允許計(jì)數(shù)器響應(yīng)一個(gè)激勵(lì),并在一個(gè)程序可控的延時(shí)之后,產(chǎn)生一個(gè)脈寬可程序控制的脈沖。
可以通過(guò)從模式控制器啟動(dòng)計(jì)數(shù)器,在輸出比較模式或者PWM模式下產(chǎn)生波形。設(shè)置TIMx_CR1 寄存器中的OPM 位將選擇單脈沖模式,這樣可以讓計(jì)數(shù)器自動(dòng)的產(chǎn)生下一個(gè)更新
事情UEV時(shí)停止。
僅當(dāng)比較值與計(jì)數(shù)器的初始值不同時(shí),才能產(chǎn)生一個(gè)脈沖。啟動(dòng)之前(當(dāng)定時(shí)器正在等待觸發(fā)),必須配置如下:
向上計(jì)數(shù)方式:CNT (計(jì)數(shù)器寄存器) < CCRx (比較寄存器)< ARR(自動(dòng)裝載寄存器)
向下計(jì)數(shù)方式:CNT > CCRx。
需要在從TI2輸入腳上檢測(cè)到一個(gè)上升沿開(kāi)始,延遲tDELAY 之后,在OC1上產(chǎn)生一個(gè)長(zhǎng)度為tPULSE 的正脈沖。
具體的可以看參考手冊(cè)。
4、軟件設(shè)計(jì)
/**
******************************************************************************
*@filetimonepulse.c
*@authorCawen
*@versionV1.0
*@date2015-12-22
******************************************************************************
*/
/*Includes------------------------------------------------------------------*/
#include"timonepulse.h"
/*Privatevariables---------------------------------------------------------*/
uint16_tPrescalerValue=0;
/*
*FunctionName:GPIO_Configuration
*Description:ConfiguretheGPIODPins.
*Input:None
*Output:None
*Return:None
*Attention:None
*/
voidGPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/*TIM4_CH1pin(PB.06)configuration*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/*TIM4_CH2pin(PB.07)configuration*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
/*
*FunctionName:TIM4_Configuration
*Description:TIM4configuration:OnePulsemode
TheexternalsignalisconnectedtoTIM4_CH2pin(PB.07),
TheRisingedgeisusedasactiveedge,
TheOnePulsesignalisoutputonTIM4_CH1pin(PB.06)
TheTIM_Pulsedefinesthedelayvalue
The(TIM_Period-TIM_Pulse)definestheOnePulsevalue.
TIM2CLK=SystemCoreClock,wewanttogetTIM2counterclockat24MHz:
-Prescaler=(TIM2CLK/TIM2counterclock)-1
TheAutoreloadvalueis65535(TIM4->ARR),sothemaximumfrequencyvalue
totriggertheTIM4inputis24000000/65535=300Hz.
TheTIM_Pulsedefinesthedelayvalue,thedelayvalueisfixed
to682.6us:
delay=CCR1/TIM4counterclock=682.6us.
The(TIM_Period-TIM_Pulse)definestheOnePulsevalue,
thepulsevalueisfixedto2.048ms:
OnePulsevalue=(TIM_Period-TIM_Pulse)/TIM4counterclock=2.048ms.
*Input:None
*Output:None
*Return:None
*Attention:None
*/
voidTIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_ICInitTypeDefTIM_ICInitStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
/*TIM4andGPIOBclockenable*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
/*Computetheprescalervalue*/
PrescalerValue=(uint16_t)(72000000/10000)-1;
/* Time base configuration */