本帖最后由 ddllxxrr 于 2021-2-6 09:28 编辑
我用STM32F030C8T6我用定时器延时,SYSTICK想给别的用比如OS,反正留用。
我在初使化的时候我已经把APB时钟2分频别的地方要用。
if (HSEStatus == (uint32_t)0x01)
{
/* Enable Prefetch Buffer and set Flash Latency */
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
/* HCLK = SYSCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1;
/* PCLK = HCLK */
RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE_DIV2; // //24MHZ
/* PLL configuration = HSE * 6 = 48 MHz */
RCC->CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL));
RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLMULL6);
/* Enable PLL */
RCC->CR |= RCC_CR_PLLON;
我的延时程序如下:
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Compute the prescaler value */
PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1; //1 u second
TIM_TimeBaseStructure.TIM_Prescaler = 23;//PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_Cmd(TIM3, DISABLE);
}
void TimeDelay1us(uint16_t Times)
{
TIM3->CR1 |= TIM_CR1_CEN; //启动定时器
while(Times--)
{
while((TIM3->SR & TIM_FLAG_Update) == RESET);
TIM3->SR &= ~TIM_FLAG_Update;
}
TIM3->CR1 &= ~TIM_CR1_CEN; //关闭定时器
}
void Delay(uint16_t Times1)
{
uint16_t dd,count;
for(count=0;count<Times1;count++)
{
for(dd=1;dd<1000;dd++)
TimeDelay1us(1);
}
}
void delay_us(uint16_t Times2)
{
uint16_t count;
for(count=0;count<Times2;count++)
{
TimeDelay1us(1);
}
}
void delay_ms(uint16_t Times3)
{
uint16_t count;
for(count=0;count<Times3;count++)
Delay(1);
}
我运行了一下,怎么将4秒,而我定时是1秒。而且好像我改变参数时间并不变求解。
|