void time_ini(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //PB5复用为TIM3的通道2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*TIM3局部复用功能开启 在TIM3的局部复用开启时,PB5会被复用为TIM3_CH2*/
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3 , ENABLE); //TIM3部分映射到PB5
/*-------------------------------------------------------------------
TIM3CLK=72MHz 预分频系数Prescaler=2 经过分频 定时器时钟为24MHz
根据公式 通道输出占空比=TIM3_CCR2/(TIM_Period+1),可以得到TIM_Pulse的计数值
捕获/比较寄存器2 TIM3_CCR2= CCR2_Val
-------------------------------------------------------------------*/
TIM3_TimeBaseStructure.TIM_Prescaler = 2; //预分频器TIM3_PSC=3 24M
TIM3_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器向上计数模式 TIM3_CR1[4]=0
TIM3_TimeBaseStructure.TIM_Period =2399; //自动重装载寄存器TIM3_APR 确定频率为10KHz
TIM3_TimeBaseStructure.TIM_ClockDivision = 0x0; //时钟分频因子 TIM3_CR1[9:8]=00
TIM3_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM_TimeBaseInit(TIM3,&TIM3_TimeBaseStructure); //写TIM3各寄存器参数
|