stm32 12路pwm舵機控制器
阿新 • • 發佈:2019-02-03
用於以pwm訊號控制模擬舵機
使用
//@stm32f103 72mhz
PWM_Init(1000-1,1440-1);//開12路pwm 50hz,arr=1000
RATIO_0(150);//設定位置,對模擬舵機CCR:100右極限,200左極限
h檔案
#ifndef __pwm_H
#define __pwm_H
#include "sys.h"
/*
使用
PWM_Init(1000-1,1440-1);//開12路pwm 50hz,arr=1000,對模擬舵機
CCR:100右極限,200左極限 RATIO_0(150);//...
*/
#define RATIO_0(RATIO) TIM_SetCompare1(TIM3, RATIO) // PA6
#define RATIO_1(RATIO) TIM_SetCompare2(TIM3, RATIO) // PA7
#define RATIO_2(RATIO) TIM_SetCompare3(TIM3, RATIO) // PB0
#define RATIO_3(RATIO) TIM_SetCompare4(TIM3, RATIO) // PB1
#define RATIO_4(RATIO) TIM_SetCompare1(TIM4, RATIO) // PB6
#define RATIO_5(RATIO) TIM_SetCompare2(TIM4, RATIO) // PB7
#define RATIO_6(RATIO) TIM_SetCompare3(TIM4, RATIO) // PB8
#define RATIO_7(RATIO) TIM_SetCompare4(TIM4, RATIO) // PB9
#define RATIO_8(RATIO) TIM_SetCompare1(TIM5, RATIO) // PA0
#define RATIO_9(RATIO) TIM_SetCompare2(TIM5, RATIO) // PA1
#define RATIO_10(RATIO) TIM_SetCompare3(TIM5, RATIO) // PA2
#define RATIO_11(RATIO) TIM_SetCompare4(TIM5, RATIO) // PA3
void PWM_Init(u16 arr, u16 psc); //頻率為7200000/psc/arr
#endif
// TIM_SetCompare2(TIM3,led0pwmval);調佔空比
c檔案
#include "pwm.h"
//TIM_SetCompare2(TIM3,led0pwmval);調佔空比
//PWM頻率 = 72M / ((arr+1)*(psc+1))(單位:Hz)
//PWM佔空比 = TIM3->CCR1 / arr(單位:%)
//TIM3 TIM4 TIM5 12路 PWM初始化
//PWM輸出初始化
//arr:自動重灌值
//psc:時鐘預分頻數
void PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4|RCC_APB1Periph_TIM5, ENABLE); //使能定時器345時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外設和AFIO複用功能模組時鐘
//設定該引腳為複用輸出功能,輸出TIM3 CH2的PWM脈衝波形 GPIOA.7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7; //A組上管腳
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //複用推輓輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA組
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;//B組上管腳
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB組
//初始化TIM3,4,5
TIM_TimeBaseStructure.TIM_Period = arr; //設定在下一個更新事件裝入活動的自動重灌載暫存器週期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //設定用來作為TIMx時鐘頻率除數的預分頻值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //設定時鐘分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的引數初始化TIMx的時間基數單位
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的引數初始化TIMx的時間基數單位
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根據TIM_TimeBaseInitStruct中指定的引數初始化TIMx的時間基數單位
//初始化TIM3,4,5 Channel,2,3,4 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //選擇定時器模式:TIM脈衝寬度調製模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比較輸出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //輸出極性:TIM輸出比較極性高
//根據T指定的引數初始化外設通道,共12個
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC1Init(TIM5, &TIM_OCInitStructure);
TIM_OC2Init(TIM5, &TIM_OCInitStructure);
TIM_OC3Init(TIM5, &TIM_OCInitStructure);
TIM_OC4Init(TIM5, &TIM_OCInitStructure);
//使能TIM 在CCR1,2,3,4上的預裝載暫存器
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable);
//使能TIM3,4,5
TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM4, ENABLE);
TIM_Cmd(TIM5, ENABLE);
}