芯片是stm8s103f3p6,fmaster选取LSI,LSI_EN enable
奇怪的问题,本意是 tim4 1分频,arr 128,那么每次溢出1ms,但是奇怪的是,有时候时间是一倍,有时候程序在enableInterrupts就死掉了,请问是哪儿出错了呢
unsigned long elapseTime = 0;
void DelayMS(unsigned int ms) {
unsigned long lmttime = elapseTime + ms;
while(lmttime >= elapseTime);
return;
}
int main( void )
{
CLK_DeInit();
CLK_LSICmd(ENABLE);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_MANUAL, CLK_SOURCE_LSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
while(CLK_GetFlagStatus(CLK_FLAG_LSIRDY)==RESET);
CLK_ClockSwitchCmd(ENABLE);
while(CLK_GetSYSCLKSource() != CLK_SOURCE_LSI){
}
TIM4_DeInit();
TIM4_TimeBaseInit(TIM4_PRESCALER_1, 128);
TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
TIM4_SetCounter(0x00);
TIM4_Cmd(ENABLE);
GPIO_DeInit(GPIOB);
GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_OD_LOW_SLOW);
GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
enableInterrupts();
while (1) {
DelayMS(2000);
GPIO_WriteReverse(GPIOB, GPIO_PIN_5);
}
return 0;
}
INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
{
elapseTime++;
TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
}
|