新手入门
- 积分
- 8
- 金钱
- 8
- 注册时间
- 2023-4-10
- 在线时间
- 1 小时
|
1金钱
#include "stm32f10x.h"
#include "stm32f10x_tim.h"
#include "delay.h"
#define TIM2_UP_IRQn TIM2_IRQn + 1
#define TIM3_UP_IRQn TIM3_IRQn + 1
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //使能AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能TIM2时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //使能TIM3时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1| GPIO_Pin_8 | GPIO_Pin_15; //配置GPIO引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO速度为50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 |GPIO_Pin_4 |GPIO_Pin_5|GPIO_Pin_15; //配置GPIO引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出模式
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIO
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_BaseInitStructure.TIM_Period = 1000-1; //设置定时器周期为1000-1
TIM_BaseInitStructure.TIM_Prescaler = 72-1; //设置定时器分频系数为72-1
TIM_BaseInitStructure.TIM_ClockDivision = 0; //设置时钟分割
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_BaseInitStructure); //初始化TIM1的基本定时器
TIM_ARRPreloadConfig(TIM2, ENABLE); //ARR寄存器的值可以被更新到影子寄存器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //使能输出
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性为高电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 100; //设置通道1的占空比为10%
TIM_OC1Init(TIM2, &TIM_OCInitStructure); //初始化TIM2的1号通道输出比较模式
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM1的1号通道比较器的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 300; //设置通道2的占空比为30%
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //初始化TIM1的2号通道输出比较模式
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM1的2号通道比较器的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 600; //设置通道3的占空比为60%
TIM_OC3Init(TIM2, &TIM_OCInitStructure); //初始化TIM1的3号通道输出比较模式
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM1的3号通道比较器的预装载寄存器
TIM_OCInitStructure.TIM_Pulse = 900; //设置通道4的占空比为90%
TIM_OC4Init(TIM2, &TIM_OCInitStructure); //初始化TIM1的4号通道输出比较模式
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable); //使能TIM1的4号通道比较器的预装载寄存器
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //
TIM_BDTRInitStructure.TIM_DeadTime = 0x90; //死区时间为144个定时器时钟周期
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure); //初始化TIM1的BDTR寄存器
TIM_BaseInitStructure.TIM_Period = 1000-1;
TIM_BaseInitStructure.TIM_Prescaler = 72-1;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_BaseInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = 100;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 300;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 600;
TIM_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse = 900;
TIM_OC4Init(TIM3, &TIM_OCInitStructure);
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_Cmd(TIM2, ENABLE); //使能TIM1
TIM_Cmd(TIM3, ENABLE); //使能TIM2
TIM_CtrlPWMOutputs(TIM1, ENABLE); //使能TIM1的PWM输出
}
int main()
{
NVIC_InitTypeDef NVIC_InitStruct;
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM1的更新事件中断
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = TIM2_UP_IRQn; //选择TIM1的更新事件
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为0
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; //子优先级为0
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能该通道中断
NVIC_Init(&NVIC_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = TIM3_UP_IRQn; //选择TIM1的更新事件
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为0
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1; //子优先级为1
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能该通道中断
NVIC_Init(&NVIC_InitStruct);
GPIO_Configuration(); //配置GPIO
TIM_Configuration(); //配置TIM
/*uint16_t servo_angle_1_init[4] = {100, 100, 100, 100}; //四个舵机初始角度均为100度
uint16_t servo_angle_2_init[4] = {100, 100, 100, 100}; //四个舵机初始角度均为100度*/
while(1)
{
/*static uint16_t servo_angle_1[4] = {100, 100, 100, 100}; //四个舵机初始角度均为100度
static uint16_t servo_angle_2[4] = {100, 100, 100, 100}; //四个舵机初始角度均为100度*/
TIM_SetCompare1(TIM1,500); //设置舵机1号通道占空比 小舵机1
delay_ms(100);
TIM_SetCompare1(TIM1,500); //设置舵机1号通道占空比 小舵机1
delay_ms(100);
TIM_SetCompare1(TIM2,1000); //设置舵机5号通道占空比 大舵机1
delay_ms(100);
TIM_SetCompare1(TIM2,0); //设置舵机5号通道占空比 大舵机1
delay_ms(100);
TIM_SetCompare2(TIM1,1000); //设置舵机2号通道占空比 小舵机2
delay_ms(100);
TIM_SetCompare2(TIM1,0); //设置舵机2号通道占空比 小舵机2
TIM_SetCompare2(TIM2,1000); //设置舵机6号通道占空比 大舵机2
TIM_SetCompare2(TIM2,0); //设置舵机6号通道占空比 大舵机2
TIM_SetCompare3(TIM1,1000); //设置舵机3号通道占空比 小舵机3
TIM_SetCompare3(TIM1,0); //设置舵机3号通道占空比 小舵机3
TIM_SetCompare3(TIM2,1000); //设置舵机7号通道占空比 大舵机3
TIM_SetCompare3(TIM2,0); //设置舵机7号通道占空比 大舵机3
TIM_SetCompare4(TIM1,1000); //设置舵机4号通道占空比 小舵机4
TIM_SetCompare4(TIM1,0); //设置舵机4号通道占空比 小舵机4
TIM_SetCompare4(TIM2,1000); //设置舵机8号通道占空比 大舵机4
TIM_SetCompare4(TIM2,0); //设置舵机8号通道占空比 大舵机4
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清除TIM1更新事件中断标志位
TIM_ClearITPendingBit(TIM2, TIM_IT_Update); //清除TIM1更新事件中断标志位
}
}
|
|