//使用定时器TIM4的3通道CH3输出占空比为25%的
PWM波#include "stm32f10x.h"
int main(void)
{
// SystemInit();
//配置IO口
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);//IO口使能设置
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM3的CH3连接PB8管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//定时器2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//配置时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period=10000; //1s
TIM_TimeBaseStructure.TIM_Prescaler=7199; //720分频
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//PWM1模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//比较输出使能
TIM_OCInitStructure.TIM_Pulse = 2500; //设置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性高
TIM_OC3Init(TIM4, & TIM_OCInitStructure); //初始化TIM4的CH3通道
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CH3通道CCR3上的预装载寄存器
TIM_ARRPreloadConfig(TIM4, ENABLE);//使能TIM4在CH3通道ARR3上的预装载寄存器
TIM_Cmd(TIM4,ENABLE);//使能定时器4
while(1);
}
//以下是报错函数
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif