#include "stm32f10x.h"
/* RCC时钟配置 */
void RCC_config(void)
{
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(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOA时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM1, ENABLE);
/* 将GPIOA时钟设置为默认参数 */
GPIO_DeInit(GPIOA);
/* 复用推挽输出 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
/* 第7、8引脚 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
/* 输出频率50MHz */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* 初始化GPIOA 7、8 */
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/* 定时器配置 */
void TIMER_config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
/* 允许TIM1的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* 将定时器1寄存器设为初始值 */
TIM_DeInit(TIM1);
/* 设置定时器1由内部时钟 */
TIM_InternalClockConfig(TIM1);
/* 预分频值 */
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;
/* 循环次数 */
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
/* 初始化定时器1 */
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* PWM设置 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
/* 设置死区 */
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 0xFF;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
/* 开启定时器1 */
TIM_Cmd(TIM1, ENABLE);
/* PWM输出 */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/* 毫秒延时 */
void delay_ms(uint16_t time)
{
uint16_t i = 0;
while(time--)
{
i = 12000;
while(i--);
}
}
int main(void)
{
/* 时钟配置 */
RCC_config();
/* GPIO配置 */
GPIO_config();
/* 定时器2配置 */
TIMER_config();
while(1)
{
delay_ms(1000);
}
}
上一篇:STM32之CAN总线原理
下一篇:STM32之SPI从机DMA例程
推荐阅读最新更新时间:2024-03-16 16:25
设计资源 培训 开发板 精华推荐
- 安卓13要大改 疯狂杀后台问题说再见
- 利亚德:Micro LED2022年产能将达到1600KK/月
- 如何校正示波器?示波器探头校正方波
- 几维通信基于比科奇PC802 PHY SoC的5G NR 一体化小基站通过运营商测试
- 我国研发出首个室温超快氢负离子导体
- 大联大世平集团推出基于onsemi产品的高集成准谐振反激式电源转换器方案
- 【STM32H7教程】第38章 STM32H7的LPTIM低功耗定时器应用之超时唤醒
- e络盟达成新分销合作,开售Grayhill系列精密开关
- 仅利用太阳能,人造树叶可制成清洁液体燃料
- Software République安全智能的电动汽车充电站亮相2022年巴黎车展