手上这个STM32F0-DISCO的板子,目前正在用TIM16定时10ms 中断去查询TIM1和TIM3的CNT值
用来计算编码器反馈的的电机转速;但是,遇到个很简单的问题(但我找了一天了,没找到原因);
问题是:
我使能TIM16的 跟新中断,程序会根本进不来住循环。然后,我硬件仿真进去看,发现卡在TIM_ITConfig(TIM16,TIM_IT_Update,ENABLE);
但是,我单步仿真,又能通过这句,也能进入主循环,就是没有进入更新中断;
我始终觉得的代码写的没问题(这还写错,还得了);
然后,我就怀疑是不是我的it.c文件有问题;然后找到官方的DEMO里面的IT.C文件替换,也一样;
void TIM3_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 999;
TIM_TimeBaseInitStructure.TIM_Prescaler = 479;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPriority = 3;//priority :0>1>2>3
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM3,ENABLE);
}
void TIM3_IRQHadler(void)
{
if(TIM_GetFlagStatus(TIM3,TIM_IT_Update)!=RESET)
{
LED3H;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
//TIM1_CNT = Read_Encoder(TIM1);//读取TIM1的计数值
//TIM3_CNT = Read_Encoder(TIM3);//读取TIM3的计数值
}
LED3L;
}
下面是主函数:
int main()
{
LED_Init();
//TB66_Init();
//Encoder_TIM_Init();//TIM1,TIM3的编码器模式
//Encoder_GPIO_Init();
TIM3_Init(); // TIM16用于定时读取TIM1 TIM3的CNT值,用于判断转速与转向
while(1)
{
LED4H;
delay_ms(400);
LED4L;
delay_ms(400);
}
}
STM32F0x_Template.zip
(6.44 MB, 下载次数: 59)
2016-12-13 19:50 上传
点击文件名下载附件
首先,我的代码没问题,
有一处致命错误:
void TIM3_IRQHadler(void)
这里中断服务函数应该是 TIM3_IRQHandler;导致,中断就没有服务函数,就会卡在中断里面
要是万一怕写错了,可以在.s的启动文件中找到
血泪!!!!!
为什么,单步仿真又能通过?
{
uint16_t PrescalerValue = 0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable the TIM3 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 6000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 7999;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//1ms更新一次
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* Prescaler configuration */
TIM_PrescalerConfig(TIM3, PrescalerValue, TIM_PSCReloadMode_Immediate);
/* TIM Interrupts enable */
TIM_ITConfig(TIM3,TIM_IT_Update, ENABLE);
/* TIM3 enable counter */
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_IRQHandler(void)
{
unsigned int Vol;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}
}
试试我这个,我这个正常好用的。
一周热门 更多>