//使用定時(shí)器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口使能設(shè)置
GPIO_InitTypeDef GPIO_InitStructure; //定義結(jié)構(gòu)體
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM3的CH3連接PB8管腳
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//定時(shí)器2
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);//配置時(shí)鐘
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;//向上計(jì)數(shù)
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; //設(shè)置占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出比較極性高
TIM_OC3Init(TIM4, & TIM_OCInitStructure); //初始化TIM4的CH3通道
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //使能TIM4在CH3通道CCR3上的預(yù)裝載寄存器
TIM_ARRPreloadConfig(TIM4, ENABLE);//使能TIM4在CH3通道ARR3上的預(yù)裝載寄存器
TIM_Cmd(TIM4,ENABLE);//使能定時(shí)器4
while(1);
}
//以下是報(bào)錯(cuò)函數(shù)
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
while (1)
{
}
}
#endif
評(píng)論