//我初学。。。
//这个能用就是有误差测8KHZ的时候开始出现1HZ的误差到1MHZ的时候差50HZ
//请问有办法处理吗,还是我配置出错了
u32 TIM2_PWM_val=0;
/********配置定时器5输入捕获通道PA0**************/
void TIME2_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructrue;//定义GPIO结构体
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);//使能GPIOA时钟
GPIO_InitStructrue.GPIO_Pin = GPIO_Pin_0;//GPIOA0
GPIO_InitStructrue.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructrue.GPIO_PuPd = GPIO_PuPd_DOWN;//下拉
GPIO_Init(GPIOA,&GPIO_InitStructrue);//初始化PA0
}
/*********************定时器5基本配置******************/
void TIME2_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;//定义定时器5初始化结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIME2_GPIO_Init();//*****************************调用GPIO初始化
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM2);//PA0复用为定时器
TIM_TimeBaseInitStruct.TIM_Period = 0xffffFFFF;//自动重装载值
TIM_TimeBaseInitStruct.TIM_Prescaler = 0x0;//定时器分频
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);//初始化定时器2
/*********下面五行我配的云里雾里的****************/
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_External1);
TIM_SelectInputTrigger(TIM2,TIM_TS_TI1FP1);
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Reset);
TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Disable);
TIM_TIxExternalClockConfig(TIM2,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_BothEdge,0);//双边触发发送的时候/2
/***********************************************/
TIM_SetCounter(TIM2,0);//将CNT寄存器清零
TIM_Cmd(TIM2,ENABLE);
}
/**********配置定时器3定时一秒钟********/
void TIME3_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitStruct.TIM_Period = arr;
TIM_TimeBaseInitStruct.TIM_Prescaler = psc;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd (TIM3,ENABLE);
}
/**********配置定时器3中断**************/
void TIM3_NVIC(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0x02;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0x03;
NVIC_Init(&NVIC_InitStruct);
}
/***********定时器3中断服务函数*******************/
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus (TIM3,TIM_IT_Update) == SET)
{
TIM2_PWM_val = TIM_GetCounter(TIM2);
TIM_SetCounter(TIM2,0);
TIM_ClearITPendingBit (TIM3,TIM_IT_Update);
}
}
一周热门 更多>