關(guān)于stm32的定時(shí)器主從模式概念
以下程序是從官方的修改而來(lái)
修改為寄存器版(主定時(shí)器:TIM2;從定時(shí)器:TIM3,省略了TIM4):
/*從模式:TIM2主模式,TIM3從模式*/
void TIM_Parallel_Synchro(void)
{
/* System Clocks Configuration */
RCC->APB2ENR|=1<<2; //使能PORTA時(shí)鐘
RCC->APB1ENR|=1<<0; //TIM2時(shí)鐘使能
RCC->APB1ENR|=1<<1; //TIM3時(shí)鐘使能
/* GPIO Configuration */
GPIOA->CRL&=0XFFFFFFF0;//PA0輸出
GPIOA->CRL|=0X0000000B;//50Hz,復(fù)用功能輸出
GPIOA->ODR|=1<<0; //PA0上拉
GPIOA->CRL&=0XF0FFFFFF;//PA6輸出
GPIOA->CRL|=0X0B000000;//50Hz,復(fù)用功能輸出
GPIOA->ODR|=1<<6; //PA6上拉
/* Timebaseconfiguration */
TIM2->ARR=255;//設(shè)定計(jì)數(shù)器自動(dòng)重裝值
TIM2->PSC=0 ;//預(yù)分頻器分頻
TIM2->CR1 &=~(3<<8);// 選擇時(shí)鐘分頻
TIM2->CR1 &=~(3<<5);// 選擇計(jì)數(shù)模式
TIM3->ARR=9; //設(shè)定計(jì)數(shù)器自動(dòng)重裝值
TIM3->PSC=0 ;//預(yù)分頻器分頻
TIM3->CR1 &=~(3<<8);// 選擇時(shí)鐘分頻
TIM3->CR1 &=~(3<<5);// 選擇計(jì)數(shù)模式
/* Master Configuration in PWM1 Mode */
TIM2->CCMR1|=6<<4; //輸出比較模式
TIM2->CCER |=1<<0; //OC1 輸出使能
TIM2->CCR1 =64; //捕獲比較寄存器(占空比)
TIM2->CCER &=~(1<<1); //OC1 輸出極性
/* Select the Master Slave Mode */
TIM2->SMCR|=1<<7;//選擇主從模式
/* Master Mode selection */
TIM2->CR2 |=2<<4;// 主模式選擇
/* Slaves Configuration: PWM1 Mode */
TIM3->CCMR1|=6<<4; //輸出比較模式
TIM3->CCER |=1<<0; //OC1 輸出使能
TIM3->CCR1 =3; //捕獲比較寄存器(占空比)
TIM3->CCER &=~(1<<1); //OC1 輸出極性
/* Slave Mode selection: TIM3 */
TIM3->SMCR|=5<<0;//從模式選擇
TIM3->SMCR|=1<<4;//觸發(fā)選擇
/* TIM enable counter */
TIM3->CR1|=0x01; //CEN=1,使能定時(shí)器
TIM2->CR1|=0x01; //CEN=1,使能定時(shí)器
}
仿真結(jié)果:
但是仿真結(jié)果并不是庫(kù)函數(shù)注釋中描述的那樣
The TIMxCLK is fixed to 72 MHz, the TIM2 counter clock is 72 MHz.
The Master Timer TIM2 is running at 281.250 KHz and the duty cycle is equal to 25%
The TIM3 is running:
- At (TIM2 frequency)/ (TIM3 period + 1) = 28.125 KHz and a duty cycle
equal to TIM3_CCR1/(TIM3_ARR + 1) = 30%
如果修改:TIM3->SMCR|=5<<0;//從模式選擇
為:TIM3->SMCR|=7<<0;//從模式選擇
仿真結(jié)果與庫(kù)函數(shù)描述相同。
評(píng)論