#include "stm32f10x.h"
/* RCC时钟配置 */
void RCC_config()
{
ErrorStatus HSEStartUpStatus;
/* RCC寄存器设置为默认配置 */
RCC_DeInit();
/* 打开外部高速时钟 */
RCC_HSEConfig(RCC_HSE_ON);
/* 等待外部高速时钟稳定 */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* 设置HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* 设置PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* 设置PCLK1 = HCLK / 2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
// /* 设置FLASH代码延时 */
// FLASH_SetLatency(FLASH_Latency_2);
// /* 使能预取址缓存 */
// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* 设置PLL时钟源为HSE倍频9 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* 使能PLL */
RCC_PLLCmd(ENABLE);
/* 等待PLL稳定 */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* 设置PLL为系统时钟源 */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* 等待系统时钟源切换到PLL */
while(RCC_GetSYSCLKSource() != 0x08);
}
}
/* GPIO配置 */
void GPIO_config()
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOA时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
/* 将GPIOA时钟设置为默认参数 */
GPIO_DeInit(GPIOA);
/* 复用推挽输出 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
/* 第13引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
/* 输出频率50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 初始化GPIOA_0 */
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/* 定时器配置 */
void TIMER_config()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* 允许TIM2的时钟 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* 将定时器2寄存器设为初始值 */
TIM_DeInit(TIM2);
/* 设置定时器2由内部时钟 */
TIM_InternalClockConfig(TIM2);
/* 预分频值 */
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
/* 时钟分割 */
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
/* 向上计数 */
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
/* 自动重载值 */
TIM_TimeBaseStructure.TIM_Period = 1000 - 1;
/* 初始化定时器2 */
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);
/* 清除中断标志 */
TIM_ClearFlag(TIM2, TIM_IT_CC1);
/* 开启TIM2的中断 */
TIM_ITConfig(TIM2, TIM_IT_CC1, ENABLE);
/* 开启定时器2 */
TIM_Cmd(TIM2, ENABLE);
}
/* 中断配置 */
void NVIC_config()
{
NVIC_InitTypeDef NVIC_InitStructure;
/* 选择中断分组1 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
/* 选择TIM2的中断通道 */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
/* 抢占式中断优先级设置为0 */
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
/* 响应式中断优先级设置为0 */
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
/* 使能中断 */
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
/* 中断初始化 */
NVIC_Init(&NVIC_InitStructure);
}
/* 毫秒延时 */
void delay_ms(uint16_t time)
{
uint16_t i = 0;
while(time--)
{
i = 12000;
while(i--);
}
}
/* 定时器2中断向量 */
void TIM2_IRQHandler(void)
{
uint16_t capture = 0;
if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_CC1);
/* 清除中断标志 */
TIM_ClearFlag(TIM2, TIM_IT_CC1);
capture = TIM_GetCapture1(TIM2);
TIM_SetCompare1(TIM2, 500 - capture);
}
}
int main()
{
/* 时钟配置 */
RCC_config();
/* GPIO配置 */
GPIO_config();
/* 中断配置*/
NVIC_config();
/* 定时器2配置 */
TIMER_config();
while(1)
{
delay_ms(1000);
}
}
上一篇:STM32定时器----Toggle模式实现2路pwm移相
下一篇:STM32定时器----多通道PWM捕获
推荐阅读最新更新时间:2024-03-16 16:07