STM32F103C8T6设置系统时钟为72MHz,定时器3和定时器2输出440HzPWM标准库代码
时间: 2024-04-21 12:23:52 浏览: 192
STM32F103C8实现的SVPWM变频,V/F控制,电压随频率改变,通过模拟量调频,PWM频率4kHz,HAL库编写
5星 · 资源好评率100%
以下是使用标准库代码在STM32F103C8T6上设置系统时钟为72MHz,并且配置定时器3和定时器2输出440Hz PWM信号的示例代码:
```c
#include "stm32f10x.h"
void SystemClock_Config(void);
void TIM3_Config(void);
void TIM2_Config(void);
int main(void)
{
// 设置系统时钟为72MHz
SystemClock_Config();
// 初始化定时器3和定时器2
TIM3_Config();
TIM2_Config();
// 启动定时器3和定时器2
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM2, ENABLE);
while (1)
{
// 主循环
}
}
void SystemClock_Config(void)
{
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
if (RCC_WaitForHSEStartUp() == SUCCESS)
{
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while (RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
SystemCoreClockUpdate();
}
void TIM3_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 使能定时器3和GPIOB的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// 配置GPIOB的引脚为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 设置定时器3的预分频值为72,计数频率为1MHz
TIM_TimeBaseStructure.TIM_Period = 1000000 / 440 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// 配置定时器3通道1的输出比较模式为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
// 设置定时器3通道1的占空比为50%
TIM_SetCompare1(TIM3, (1000000 / 440 - 1) / 2);
// 使能定时器3
TIM_Cmd(TIM3, ENABLE);
}
void TIM2_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
// 使能定时器2和GPIOA的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// 配置GPIOA的引脚为复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 设置定时器2的预分频值为72,计数频率为1MHz
TIM_TimeBaseStructure.TIM_Period = 1000000 / 440 - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
// 配置定时器2通道1的输出比较模式为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
// 设置定时器2通道1的占空比为50%
TIM_SetCompare1(TIM2, (1000000 / 440 - 1) / 2);
// 使能定时器2
TIM_Cmd(TIM2, ENABLE);
}
```
这段代码首先配置系统时钟为72MHz,然后初始化定时器3和定时器2,使其输出440Hz的PWM信号。你可以根据需要修改频率和占空比。注意,使用定时器3和定时器2输出PWM信号时,需要分别配置不同的引脚和定时器。
阅读全文