写一个STM32F103C8T6 的pwm程序,占空比为0.2%,频率1HZ
时间: 2023-08-24 21:05:40 浏览: 89
好的,以下是使用STM32F103C8T6的PWM程序,占空比为0.2%,频率为1HZ的代码:
```c
#include "stm32f10x.h"
void delay_ms(uint32_t ms)
{
for(uint32_t i=0; i<ms; i++)
{
for(uint32_t j=0; j<7200; j++);
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_Period = 9999;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 20; // 0.2% duty cycle
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
}
int main(void)
{
GPIO_Configuration();
TIM_Configuration();
while(1)
{
// do nothing
}
}
```
这个程序通过TIM3通道1产生PWM信号,占空比为0.2%,频率为1HZ。在GPIOA1引脚上输出PWM信号。在main函数中,使用两个函数分别进行GPIO和TIM的配置。之后进入一个无限循环,程序会一直输出PWM信号。为了让程序保持运行,我们在循环中添加了一个什么也不做的代码。
阅读全文