硬件
- 芯片选择
stm32选择了STM32F103 - 电路连接
三个舵机的控制端口分别连PB3(TIM2)、PB0(TIM3)、PB9(TIM4)
学习中遇到的问题
1. 不理解TIM_OC2Init、TIM_OC2PreloadConfig、SetCompare2中2的含义
TIM_OC2Init(TIM2,&TIM_OCInitStrue);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_SetCompare2(TIM2,4500);
OC2Init、OC2PreloadConfig、SetCompare2中的2是频道2的意思,即这里设置了TIM_CH2的功能
例如
TIM_OC3Init(TIM2,&TIM_OCInitStrue);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_SetCompare3(TIM2,4500);
这里就设置了TIM_CH3的功能
2. TIM2的重映像没有屏蔽JTAG功能功能导致无法正常使用
感谢以下两篇文章
stm32 TIM2 重映射
https://blog.csdn.net/qlexcel/article/details/71780452
STM32 TIM2重映射
https://blog.csdn.net/qq_36373500/article/details/78701558
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //重映射必须要开AFIO时钟
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); //TIM2重映像模式1
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //禁止JTAG功能,把PB3作为普通IO口使用
尤其是第三条。思考:一个端口如果不能正常使用,可能是被一些默认功能给占用了,这时候可以选择DISABLE(禁用)掉该端口可能默认使用的功能。
main.c
SetCompare用于修改占空比
#include "stm32f10x.h" //STM32Í·Îļþ
#include "sys.h"
#include "delay.h"
#include "pwm2.h"
#include "pwm3.h"
#include "pwm4.h"
int main(void){
delay_ms(500);
RCC_Configuration();
TIM2_PWM_Init(59999,23);
TIM_SetCompare2(TIM2,4500);
TIM3_PWM_Init(59999,23);
TIM_SetCompare3(TIM3,4500);
TIM4_PWM_Init(59999,23);
TIM_SetCompare4(TIM4,4500);
}
pwm2.h
#ifndef __PWM2_H
#define __PWM2_H
#include "sys.h"
#define RCC_APB1Periph_TIM2_Def RCC_APB1Periph_TIM2
#define RCC_APB2Periph_GPIO_TIM2_Def RCC_APB2Periph_GPIOB
#define GPIO_Pin_TIM2_Def GPIO_Pin_3
#define GPIO_TIM2_Def GPIOB
#define TIM2_Def TIM2
#define TIM2_OC_CH_Init TIM_OC2Init
#define TIM2_OC_CH_PreloadConfig TIM_OC2PreloadConfig
void TIM2_PWM_Init(u16 arr,u16 psc);
#endif
pwm2.c
#include "pwm2.h"
void TIM2_PWM_Init(u16 arr,u16 psc){
GPIO_InitTypeDef GPIO_InitStrue;
TIM_OCInitTypeDef TIM_OCInitStrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2_Def,ENABLE); // TIM
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_TIM2_Def,ENABLE); //GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* reflect */
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE); // reflect PB3 TIM2_CH2
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_TIM2_Def; //Pin
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIO_TIM2_Def,&GPIO_InitStrue); // GPIO
TIM_TimeBaseInitStrue.TIM_Period=arr;
TIM_TimeBaseInitStrue.TIM_Prescaler=psc;
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2_Def,&TIM_TimeBaseInitStrue); //TIM
TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;
TIM2_OC_CH_Init(TIM2_Def,&TIM_OCInitStrue); //TIM_CH
TIM2_OC_CH_PreloadConfig(TIM2_Def,TIM_OCPreload_Enable); //TIM_CH
TIM_Cmd(TIM2_Def,ENABLE); //TIM
}
pwm3.h
#ifndef __PWM3_H
#define __PWM3_H
#include "sys.h"
#define RCC_APB1Periph_TIM3_Def RCC_APB1Periph_TIM3
#define RCC_APB2Periph_GPIO_TIM3_Def RCC_APB2Periph_GPIOB
#define GPIO_Pin_TIM3_Def GPIO_Pin_0
#define GPIO_TIM3_Def GPIOB
#define TIM3_Def TIM3
#define TIM3_OC_CH_Init TIM_OC3Init
#define TIM3_OC_CH_PreloadConfig TIM_OC3PreloadConfig
void TIM3_PWM_Init(u16 arr,u16 psc);
#endif
pwm3.c
#include "pwm3.h"
void TIM3_PWM_Init(u16 arr,u16 psc){
GPIO_InitTypeDef GPIO_InitStrue;
TIM_OCInitTypeDef TIM_OCInitStrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3_Def,ENABLE); // TIM
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_TIM3_Def,ENABLE); //GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_TIM3_Def; //Pin
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIO_TIM3_Def,&GPIO_InitStrue); // GPIO
// GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2,ENABLE); // reflect PB3 TIM2_CH2
TIM_TimeBaseInitStrue.TIM_Period=arr;
TIM_TimeBaseInitStrue.TIM_Prescaler=psc;
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3_Def,&TIM_TimeBaseInitStrue); //TIM
TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;
TIM3_OC_CH_Init(TIM3_Def,&TIM_OCInitStrue); //TIM_CH
TIM3_OC_CH_PreloadConfig(TIM3_Def,TIM_OCPreload_Enable); //TIM_CH
TIM_Cmd(TIM3_Def,ENABLE); //TIM
}
pwm4.h
#ifndef __PWM4_H
#define __PWM4_H
#include "sys.h"
#define RCC_APB1Periph_TIM4_Def RCC_APB1Periph_TIM4
#define RCC_APB2Periph_GPIO_TIM4_Def RCC_APB2Periph_GPIOB
#define GPIO_Pin_TIM4_Def GPIO_Pin_9
#define GPIO_TIM4_Def GPIOB
#define TIM4_Def TIM4
#define TIM4_OC_CH_Init TIM_OC4Init
#define TIM4_OC_CH_PreloadConfig TIM_OC4PreloadConfig
void TIM4_PWM_Init(u16 arr,u16 psc);
#endif
pwm4.c
#include "pwm4.h"
void TIM4_PWM_Init(u16 arr,u16 psc){
GPIO_InitTypeDef GPIO_InitStrue;
TIM_OCInitTypeDef TIM_OCInitStrue;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4_Def,ENABLE); // TIM
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_TIM4_Def,ENABLE); //GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_TIM4_Def; //Pin
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIO_TIM4_Def,&GPIO_InitStrue); // GPIO
// GPIO_PinRemapConfig(GPIO_PartialRemap2_TIM2,ENABLE); // reflect PB3 TIM2_CH2
TIM_TimeBaseInitStrue.TIM_Period=arr;
TIM_TimeBaseInitStrue.TIM_Prescaler=psc;
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM4_Def,&TIM_TimeBaseInitStrue); //TIM
TIM_OCInitStrue.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStrue.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStrue.TIM_OutputState=TIM_OutputState_Enable;
TIM4_OC_CH_Init(TIM4_Def,&TIM_OCInitStrue); //TIM_CH
TIM4_OC_CH_PreloadConfig(TIM4_Def,TIM_OCPreload_Enable); //TIM_CH
TIM_Cmd(TIM4_Def,ENABLE); //TIM
}
网友评论