如果知道频率是100hz,自动重装载值和预分频系数是怎么算出来的呢?
void TIM6_Configuration(unsigned int TIM_Period,unsigned int TIM_Prescaler)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
TIM_TimeBaseStruct.TIM_Period=1799;//初值
TIM_TimeBaseStruct.TIM_Prescaler=9;//预分频
TIM_TimeBaseStruct.TIM_ClockDivision=0;
TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;//向上
TIM_TimeBaseInit(TIM6,&TIM_TimeBaseStruct);
// TIM_ClearFlag(TIM6,TIM_IT_Update);
TIM_ITConfig(TIM6,TIM_IT_Update,ENABLE);
// TIM_Cmd(TIM6,ENABLE);
}
void NVIC_Tim6_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = TIM6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
* Input Clk 72MHz
*/
GLOBAL void AdcTIM1Init(u32 freq)
{
u32 Period,Prescaler;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
Prescaler = 72;
Period = 72000000l/Prescaler/freq;
/* Time Base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_Prescaler = Prescaler - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0x00;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* TIM1 channel1 configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period/2;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, DISABLE);
}
这是我使用定时中断触发AD采集的定时器配置
假设你的主时钟为72MHz 要得到100Hz的频率
可以配置为
TIM_TimeBaseStruct.TIM_Period= 10000 -1;//初值
TIM_TimeBaseStruct.TIM_Prescaler=72 - 1;//预分频
100 = 72000000L/72/10000;
根据的参数计算 36000000/(9+1)/(1799+1)=2000Hz
所以你中断事件为1/2000=0.5mS中断一次
要配置为100Hz 只要将1799改为35999
预分频系数那里为什么要做加1处理? 我一直不明白这个表达方式。
一周热门 更多>