写一段STM32F103RCT6使用外部12MHZ晶振,系统时钟为72M,定时器3定时10MS的程序
时间: 2024-05-30 15:15:38 浏览: 109
#include "stm32f10x.h"
int main(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON); //启动外部高速晶振
while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); //等待外部高速晶振稳定
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6); //设置PLL时钟源和倍频系数
RCC_PLLCmd(ENABLE); //启动PLL
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //等待PLL稳定
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //将PLL时钟作为系统时钟
SystemCoreClockUpdate(); //更新系统时钟频率
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 720 - 1; //计数器重载值,10ms
TIM_TimeBaseStructure.TIM_Prescaler = 1000 - 1; //定时器分频系数,72MHz/1000
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化定时器3
TIM_Cmd(TIM3, ENABLE); //启动定时器3
while (1) {
if (TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == SET) { //判断定时器3是否更新
TIM_ClearFlag(TIM3, TIM_FLAG_Update); //清除定时器3更新标志
//定时器3定时10ms的任务
}
}
}
阅读全文