赞
踩
400孔面包板,stm32F103c8t6系统板,stm32的烧录器DAP或是stlink都可,0.96OLED显示屏,180度舵机*4,杜邦线若干,ASRPRO语音识别模块以及喇叭
Keil5安装教程看江科大,用于烧录程序
天问block:在浏览器搜索直接下载,用于语音识别
取模软件:用于OLED显示表情,可以在江科大的资料里获取
舵机信号线接stm32的 PA0,PA1,PA2,PA3。5V线接stm32的5V(把stm32的5V接出来到面包板上),GND接GND.
oled依次接PB9,PB8,PB7,PB6(注意将底下将PB6连负极,PB7连正极)
ASRPRO与stm32连接:GND---->GND
3V3---->3V3
PA2----->PA10
#include "stm32f10x.h"
void PWM_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//开启GPIOA时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//开启定时器2时钟
//配制GPIO
GPIO_InitTypeDef GPIO_InitStucture;
GPIO_InitStucture.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStucture.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStucture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStucture);
//配制时钟源
TIM_InternalClockConfig(TIM2);
//时基单元的初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=20000-1;
TIM_TimeBaseInitStruct.TIM_Prescaler=72-1;
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
//输出比较初始化
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCStructInit(&TIM_OCInitStructure);
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=0;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC4Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
}
void PWM_SetCompare2(uint16_t Compare)
{
TIM_SetCompare2(TIM2, Compare);
}
void PWM_SetCompare1(uint16_t Compare)
{
TIM_SetCompare1(TIM2, Compare);
}
void PWM_SetCompare3(uint16_t Compare)
{
TIM_SetCompare3(TIM2, Compare);
}
void PWM_SetCompare4(uint16_t Compare)
{
TIM_SetCompare4(TIM2, Compare);
}
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_SetCompare2(uint16_t Compare);
void PWM_SetCompare1(uint16_t Compare);
void PWM_SetCompare3(uint16_t Compare);
void PWM_SetCompare4(uint16_t Compare);
#endif
#include "stm32f10x.h"
#include "PWM.h"
void Servo_Init(void)
{
PWM_Init();
}
void Servo_SetAngle2(float Angle)
{
PWM_SetCompare2(Angle/180*2000+500);
}
void Servo_SetAngle1(float Angle)
{
PWM_SetCompare1(Angle/180*2000+500);
}
void Servo_SetAngle3(float Angle)
{
PWM_SetCompare3(Angle/180*2000+500);
}
void Servo_SetAngle4(float Angle)
{
PWM_SetCompare4(Angle/180*2000+500);
}
#ifndef __SERVO_H
#define __SERVO_H
void Servo_Init(void);
void Servo_SetAngle2(float Angle);//控制PA1 80垂直
void Servo_SetAngle1(float Angle);//控制PA0 155垂直
void Servo_SetAngle3(float Angle);//控制PA2 10垂直 逆时针趴下 顺时针转的不多
void Servo_SetAngle4(float Angle);//控制PA3 155垂直 顺时针趴下
#endif
#include "stm32f10x.h" // Device header
void Serial_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
#ifndef __SERIAL_H
#define __SERIAL_H
void Serial_Init(void);
#endif
#include "stm32f10x.h"
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"
#include "Servo.h"
#include "Key.h"
#include "Serial.h"
uint8_t KeyNum;
float Angle;
uint8_t RxData;
void WoShou(void)
{
OLED_ShowImage(1,1,128,64,WeiXiao);
OLED_Update();
Servo_SetAngle1(100);
Delay_s(1);
Servo_SetAngle1(155);
OLED_ShowImage(1,1,128,64,YanJing);
OLED_Update();
}
void LiZheng(void)
{
Servo_SetAngle2(80);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
}
void PaXia(void)
{
OLED_ShowImage(1,1,128,64,KeAi);
OLED_Update();
Servo_SetAngle2(170);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Servo_SetAngle4(60);
Delay_ms(300);
//OLED_ShowImage(1,1,128,64,YanJing);
//OLED_Update();
}
void FuWOcheng(void)
{
OLED_ShowImage(1,1,128,64,JinBi);
OLED_Update();
Servo_SetAngle2(170);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Servo_SetAngle4(60);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle2(170);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Servo_SetAngle4(60);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle2(170);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Servo_SetAngle4(60);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle2(170);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Servo_SetAngle4(60);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle2(170);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Servo_SetAngle4(60);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
OLED_ShowImage(1,1,128,64,YanJing);
OLED_Update();
}
void TiaoWu(void)
{
OLED_ShowImage(1,1,128,64,AiXin);
OLED_Update();
//1,2
Servo_SetAngle1(65);
Servo_SetAngle2(170);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle2(80);
Delay_ms(500);
Servo_SetAngle1(65);
Servo_SetAngle2(170);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle2(80);
Delay_ms(500);
//1,3
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Delay_ms(500);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Delay_ms(500);
//2,4
Servo_SetAngle2(170);
Servo_SetAngle4(65);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle2(170);
Servo_SetAngle4(65);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle4(155);
Delay_ms(500);
//3,4
Servo_SetAngle3(100);
Servo_SetAngle4(65);
Delay_ms(500);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle3(100);
Servo_SetAngle4(65);
Delay_ms(500);
Servo_SetAngle3(10);
Servo_SetAngle4(155);
Delay_ms(500);
//1,2
Servo_SetAngle1(65);
Servo_SetAngle2(170);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle2(80);
Delay_ms(500);
Servo_SetAngle1(65);
Servo_SetAngle2(170);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle2(80);
Delay_ms(500);
//2,4
Servo_SetAngle2(170);
Servo_SetAngle4(65);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle4(155);
Delay_ms(500);
Servo_SetAngle2(170);
Servo_SetAngle4(65);
Delay_ms(500);
Servo_SetAngle2(80);
Servo_SetAngle4(155);
Delay_ms(500);
//1,3
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
Delay_ms(500);
Servo_SetAngle1(65);
Servo_SetAngle3(100);
Delay_ms(500);
Servo_SetAngle1(155);
Servo_SetAngle3(10);
OLED_ShowImage(1,1,128,64,YanJing);
OLED_Update();
}
void Cheshi(void)
{
}
int main(void)
{
OLED_Init();
PWM_Init();
Servo_Init();
Key_Init();
Serial_Init();
OLED_ShowImage(1,1,128,64,YanJing);
OLED_Update();
while(1)
{
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
{
RxData=USART_ReceiveData(USART1);
if(RxData=='A')
{
WoShou();
}
if(RxData=='B')
{
LiZheng();
}
if(RxData=='C')
{
PaXia();
}
if(RxData=='F')
{
FuWOcheng();
}
if(RxData=='G')
{
TiaoWu();
}
if(RxData=='E')
{
Cheshi();
}
}
//以下注释用于测试
// if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
// {
// RxData=USART_ReceiveData(USART1);
// if(RxData=='A')
// {
// WoShou();
// }
// }
// if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET)
// {
// RxData=USART_ReceiveData(USART1);
// if(RxData=='B')
// {
// Cheshi();
// }
// }
// KeyNum=Key_GetNum();
// if(KeyNum==1)
// {
//
// OLED_ShowNum(1,1,80,3,6);
// OLED_Update();
// Servo_SetAngle2(80);
// }
}
}
建议去学习江科大的视频,学完直接用就好了
视频链接:【[模块教程] 第1期 0.96寸OLED显示屏】https://www.bilibili.com/video/BV1EN41177Pc?vd_source=01ee277e375f34880737712cd6eb00fd
资料下载:https://jiangxiekeji.com/download.html
(侵权必删)
1.先找出舵机的初始角度,令Servo_SetAngle(0);
2.记录它的初始角度,并且调整到舵机的扇叶与舵机的横平面垂直
3.在调整角度时,只需将他垂直的的角度加上他要旋转的角度
https://v.douyin.com/i6NK8dMQ/ 复制此链接,打开Dou音搜索,直接观看视频!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。