硬件連接:戰(zhàn)艦STM32F103ZET6或其他
實驗?zāi)繕?biāo):通過調(diào)制占空比調(diào)節(jié)多路PWM控制LED的明暗;
#include "pwm.h"
#include "led.h"
//PWM輸出初始化
//arr:自動重裝值
//psc:時鐘預(yù)分頻數(shù)
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE); //使能GPIO外設(shè)時鐘使能
//設(shè)置該引腳為復(fù)用輸出功能,輸出TIM1 CH1的PWM脈沖波形
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1 ; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //設(shè)置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設(shè)置用來作為TIMx時鐘頻率除數(shù)的預(yù)分頻值 不分頻
TIM_TimeBaseStructure.TIM_ClockDivision =0; //設(shè)置時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數(shù)模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時間基數(shù)單位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //選擇定時器模式:TIM脈沖寬度調(diào)制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_Pulse = 300; //設(shè)置待裝入捕獲比較寄存器的脈沖值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根據(jù)TIM_OCInitStruct中指定的參數(shù)初始化外設(shè)TIMx
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 150;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 150;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 150;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM3,ENABLE); //MOE 主輸出使能
TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的預(yù)裝載寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIM1
}
.#include "led.h"
#include "delay.h"
#include "sys.h"
#include "pwm.h"
int main(void)
{
u16 led0pwmval=150;
u16 led1pwmval=150;
u16 led2pwmval=150;
u16 led3pwmval=150;
u8 dir=1;
u16 math=0;
delay_init(); //延時函數(shù)初始化
LED_Init(); //初始化與LED連接的硬件接口
TIM3_PWM_Init(899,0);//不分頻。PWM頻率=72000/(899+1)=80Khz
while(1)
{
delay_ms(10);
math++;
if(math)
{
delay_ms(10);
if(dir)
{led0pwmval++;led1pwmval++;led2pwmval++;led3pwmval++;}
else {led0pwmval--;led1pwmval--;led2pwmval--;led3pwmval--;}
if(led0pwmval>600||led1pwmval>600||led2pwmval>600||led3pwmval>600) dir=0;
if(led0pwmval==0||led1pwmval==0||led2pwmval==0||led3pwmval==0) dir=1;
math=0;
}
TIM_SetCompare1(TIM3,led0pwmval);
TIM_SetCompare2(TIM3,led1pwmval);
TIM_SetCompare3(TIM3,led2pwmval);
TIM_SetCompare4(TIM3,led3pwmval);
}
}
方法二:
void PWM_Configuration(void)//PWM初始化函數(shù)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;//定時器初始化結(jié)構(gòu)
TIM_OCInitTypeDef TIM_OCInitStructure;//通道輸出初始化結(jié)構(gòu)
GPIO_InitTypeDefGPIO_InitStructure;//輸出通道的GPIO管腳結(jié)構(gòu)
//(1)TIM3初始化,時間基部分
TIM_TimeBaseStructure.TIM_Period=(1000-1); //周期0~999
TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //時鐘分頻
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //時鐘分割
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;//向上計數(shù)模式
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //初始化
//TIM_ITConfig(TIM3, TIM_IT_CC4,ENABLE); //打開中斷,中斷需要這行代碼
//(2)TIM3通道初始化 ,CCP部分
TIM_OCStructInit(&TIM_OCInitStructure); //默認(rèn)參數(shù)
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //工作狀態(tài)
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //設(shè)定為輸出,需要PWM輸出才需要這行代碼
TIM_OCInitStructure.TIM_Pulse =250; //占空長度
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; //高電平
TIM_OC4Init(TIM3,&TIM_OCInitStructure); //通道初始化
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //工作狀態(tài)
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //設(shè)定為輸出,需要PWM輸出才需要這行代碼
TIM_OCInitStructure.TIM_Pulse =500; //占空長度
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; //高電平
TIM_OC3Init(TIM3,&TIM_OCInitStructure); //通道初始化
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //工作狀態(tài)
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //設(shè)定為輸出,需要PWM輸出才需要這行代碼
TIM_OCInitStructure.TIM_Pulse =750; //占空長度
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; //高電平
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode =TIM_OCMode_PWM1; //工作狀態(tài)
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //設(shè)定為輸出,需要PWM輸出才需要這行代碼
TIM_OCInitStructure.TIM_Pulse =900; //占空長度