前几天在B站看到一个无聊的盒子,觉得还挺好的, 想自己做一个 结果舵机力气太小 拉不动,项目就烂尾了 (家境贫寒,买不起996r舵机和无刷电机) 把代码和元器件分享出来,也算做个保存 用到的东西主要有stm32 舵机 还有一个开关
有条件的,舵机一定要买大力气一点的T.T 或者买大力气的步进电机
代码: STM32F103ZET6工程文件(基于正点原子,参考野火的视频,用到usamrt调试):
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "gpio_in.h"
#include "usmart.h"
#include "timer.h"
#include "dj.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init();
uart_init(115200);
TIM3_PWM_Init(20000 - 1, 72 - 1);
usmart_dev.init(SystemCoreClock / 1000000);
while (1)
{
dongzuo_fuwei();
dongzuo_guanbi();
Key_Scan(GPIOA, GPIO_Pin_0);
delay_ms(500);
}
}
dj.c:
#include "dj.h"
void dj1(u16 dushu)
{
TIM_SetCompare1(TIM3, dushu);
}
void dj2(u16 dushu)
{
TIM_SetCompare2(TIM3, dushu);
}
void dongzuo_fuwei(void)
{
dj2(500);
delay_ms(200);
dj1(2200);
}
void dongzuo_guanbi(void)
{
uint16_t i;
dj2(500);
for (i = 2200; i > 800; i -= 10)
{
dj1(i);
delay_ms(5);
}
delay_ms(200);
dj2(1500);
delay_ms(500);
dj2(500);
delay_ms(500);
for (i = 800; i < 2200; i += 10)
{
dj1(i);
delay_ms(5);
}
}
dj.h:
#ifndef __DJ_H
#define __DJ_H
#include "sys.h"
#include "timer.h"
#include "delay.h"
void dj1(u16 dushu);
void dj2(u16 dushu);
void dongzuo_fuwei(void);
void dongzuo_guanbi(void);
#endif
主要是定时器和外部输入的运用,输出硬件PWM(不同于15的软件PWM,这个pwm稳如狗)
用的是正点原子的基础源码,time3,输出不映射的PWM,PA6和PA7,具体看官方中文手册的119页
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //是否启用重映射 使能这个就是部分重映射了,还有全部重映射就是调用一个重映射函数就行了,个人理解全部重映射:定时器的全部通道都映射过去了。 启用重映射还要记得设置新的引脚哦,以及是复用功能!!!
#include "timer.h"
#include "gpio_out.h"
void TIM3_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitstruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitstruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitstruct.TIM_Period = arr;
TIM_TimeBaseInitstruct.TIM_Prescaler = psc;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitstruct);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
}
void TIM2_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitstruct;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitstruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitstruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitstruct.TIM_Period = arr;
TIM_TimeBaseInitstruct.TIM_Prescaler = psc;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitstruct);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
}
usmart调试(调用正点原子的串口调试代码修改而来,方便控制角度和调试,可以直接用串口去控制角度,具体可以参考正点原子的视频和工程文件,比用keilMDK的调试方便多了):
一定要点发送心寒!!!
效果视频:sg90舵机太烂了,有钱一定要换996R舵机,本来还有一个oled屏幕要弄个表情的。。。。以后有机会再搞,或者有想送谁礼物可以再做一个了
|