大家好!
我今天想用
STM32F103C8T6的
tiM1作为1KHz定时器,但是,注意到该定时器的中断频率不正确。请大家帮助一下啊,先谢谢啦
代码如下:
#include "TIM1.h"
/*
* 函数名:TIM1_Mode_Config
* 描述 :TIM1 输出PWM信号初始化,只要调用这个函数
* 输入 :无
* 输出 :无
* 调用 :外部调用
*/
void TIM1_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
NVIC_TIM1_Configuration();
// TIM1_GPIO_Config();
TIM1_Configuration();
}
void NVIC_TIM1_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//打开TIM1的全局中断,在中断中判断是更新中断还是捕捉中断
/* Enable the TIM1 global Interrupt */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 4;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*
* 函数名:TIM1_GPIO_Config
* 描述 :配置TIM1复用输出PWM时用到的I/O
* 输入 :无
* 输出 :无
* 调用 :内部调用
*/
void TIM1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
/*转速输入,捕捉输入模式 用PA0配置成 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*
GPIO_StructInit(&GPIO_InitStructure);
//转速输入,捕捉输入模式 用PA0配置成
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);*/
}
//转速输入捕捉,
void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//TIM_ICInitTypeDef TIM_ICInitStructure;
/* Time Base configuration */
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Prescaler = 7200-1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 10000-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
*/
// TIM_TIxExternalClockConfig(TIM1,TIM_TIxExternalCLK1Source_TI1,TIM_ICPolarity_Rising,0);//设置为外部输入
//TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
// TIM_SetCounter(TIM1, 0); // 清零计数器CNT
TIM_ClearFlag(TIM1, TIM_FLAG_Update); /* 清除溢出中断标志 */
TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE);
//TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1, ENABLE); // 使能TIM4重载寄存器ARR
TIM_Cmd(TIM1,ENABLE);//使能计数
}
void TIM1_Configuration1(void) //??????????
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//??TIMER1?????
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0x00;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // Time base configuration
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);
TIM_SetCounter(TIM1, 0); // 清零计数器CNT
TIM_Cmd(TIM1, ENABLE);
}
一周热门 更多>