系统时钟24M
void TIM3_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//引脚复用配置 PA6 用作TIM3_CH1
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);
//引脚配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//TIM3时钟使能
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//TIM3中断配置
/*
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);*/
//定时时间计算:((1+TIM_Prescaler)/48M)*(1+TIM_Period)
TIM_DeInit(TIM3); //复位定时器
TIM_TimeBaseStructure.TIM_Period=1000; //定时器溢出值
TIM_TimeBaseStructure.TIM_Prescaler=24-1; //时钟预分频
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // 时钟分割
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //初始化定时器的值
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);//使能预装载寄存器
TIM_ARRPreloadConfig(TIM3, ENABLE);//使能重装载寄存器
//TIM_ClearFlag(TIM3,TIM_FLAG_Update); //清除定时器中断标志
//TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//中断使能
TIM_Cmd(TIM3, ENABLE);//定时器使能
}
void TIM3_WaveOutput(uint32_t freq)
{
uint32_t PeriodPFM;
if(freq==0)
{
TIM_SetCompare1(TIM3,0);
return ;
}
PeriodPFM=1000000/freq;
TIM_SetAutoreload(TIM3,PeriodPFM);
TIM_SetCompare1(TIM3,PeriodPFM/2);
TIM_Cmd(TIM3,ENABLE);
}
|
|