当前位置:网站首页>串口控制舵机转动
串口控制舵机转动
2022-07-02 13:22:00 【让一切都燃烧】
任务要求
- 使用串口通信,电脑串口助手发送给单片机十六进制数据,对应舵机转角0~最大角度
- 发送数据范围0~180
思路
使用查询模式:使用的是disable串口中断
'0’指是是ASCII字符0,不是十六进制
1/9 1/3000
0 1500
45 3000
a度数 x波
a = (x-0.5)90
x=a/90.0+0.5
t = x3000
1/3000
1/9
0 1500 0.5
18 2100 0.7
27 2400 0.8
30.06 2502 0.834 1002
36 2700 0.9
45 3000 1 4.5=(1-0.5)*9
63 3600 1.2 6.3=(1.2-0.5)*9 倒着算
72 3900 1.3
81 4200 1.4
90 4500 1.5 9.0=(1.5-0.5)*9
108 5100 1.7
126 5700 1.9
135 6000 2
162 6900 2.3
180 7500 2.5
利用数据计算出公式,就可以实现无论输入任何角度,舵机都能转出对应的角度
需要usart.h和usart.c串口通信
完成代码:
main.c
int main (void){
//主程序
vu16 t;
delay_ms(500); //上电时等待其他器件就绪
RCC_Configuration(); //系统时钟初始化
RELAY_Init();//继电器初始化
USART1_Init(115200);
I2C_Configuration();//I2C初始化
TIM3_PWM_Init(59999,23); //设置频率为50Hz,公式为:溢出时间Tout(单位秒)=(arr+1)(psc+1)/Tclk 20MS = (59999+1)*(23+1)/72000000
//Tclk为通用定时器的时钟,如果APB1没有分频,则就为系统时钟,72MHZ
//PWM时钟频率=72000000/(59999+1)*(23+1) = 50HZ (20ms),设置自动装载值60000,预分频系数24
while(1){
//查询方式接收
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET){
//查询串口待处理标志位
a =USART_ReceiveData(USART1);//读取接收到的数据
//将角度转换为占空比
t=(a/90.0+0.5)*3000.0;
TIM_SetCompare3(TIM3,t);
printf("%d ",a);
}
}
}
追加任务
while(1)里不能写东西
思路
使用定时器中断








需要tim.c和tim.h采用定时器中断while(1)中就不需要写入程序
完成代码
main.c
int main (void){
//主程序
vu16 a;
vu16 t;
delay_ms(500); //上电时等待其他器件就绪
RCC_Configuration(); //系统时钟初始化
RELAY_Init();//继电器初始化
USART1_Init(115200);
I2C_Configuration();//I2C初始化
OLED0561_Init(); //OLED初始化
OLED_DISPLAY_8x16_BUFFER(0," YoungTalk "); //显示字符串
OLED_DISPLAY_8x16_BUFFER(3," SG90 TEST2 "); //显示字符串
TOUCH_KEY_Init();//按键初始化
TIM3_PWM_Init(59999,23); //设置频率为50Hz,公式为:溢出时间Tout(单位秒)=(arr+1)(psc+1)/Tclk 20MS = (59999+1)*(23+1)/72000000
//Tclk为通用定时器的时钟,如果APB1没有分频,则就为系统时钟,72MHZ
//PWM时钟频率=72000000/(59999+1)*(23+1) = 50HZ (20ms),设置自动装载值60000,预分频系数24
TIM3_Init(59999,23);//定时器初始化,定时1秒(9999,7199)
while(1){
}
}
tim.c
void TIM3_IRQHandler(void)TIM3中断处理函数
在这个函数体内写入串口控制舵机的程序即可
#include "tim.h"
//定时器时间计算公式Tout = ((重装载值+1)*(预分频系数+1))/时钟频率;
//例如:1秒定时,重装载值=9999,预分频系数=7199
void TIM3_Init(u16 arr,u16 psc){
//TIM3 初始化 arr重装载值 psc预分频系数
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能TIM3
TIM3_NVIC_Init (); //开启TIM3中断向量
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_CKD_DIV1
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue); //TIM3初始化设置
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);//使能TIM3中断
TIM_Cmd(TIM3,ENABLE); //使能TIM3
}
void TIM3_NVIC_Init (void){
//开启TIM3中断向量
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x3; //设置抢占和子优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void){
//TIM3中断处理函数
u16 a;
u16 t;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
//判断是否是TIM3中断
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
//此处写入用户自己的处理程序
// GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); //取反LED1
//查询方式接收
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET){
//查询串口待处理标志位
a =USART_ReceiveData(USART1);//读取接收到的数据
//将角度转换为占空比
// i=a/90.0;
// x= a/90.0+0.5;
t=(a/90.0+0.5)*3000.0;
TIM_SetCompare3(TIM3,t);
printf("%d ",a);
}
}
}
边栏推荐
- 云原生的 CICD 框架:Tekton
- LeetCode 3. 无重复字符的最长子串
- The light of ideal never dies
- SQL solves the problem of continuous login deformation holiday filtering
- unity Hub 登錄框變得很窄 無法登錄
- Global and Chinese markets for slotting milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
- 隐私计算技术创新及产业实践研讨会:学习
- How to use stustr function in Oracle view
- Yyds dry goods inventory student attendance system based on QT design
- PCL least median square method fitting plane
猜你喜欢

Where can I open computer administrator permissions

sim2real环境配置教程

The login box of unity hub becomes too narrow to log in

隐私计算技术创新及产业实践研讨会:学习

Mobile web development learning notes - Layout

自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?

通过两级网关设计来路由服务网格流量

The median salary of TSMC's global employees is about 460000, and the CEO is about 8.99 million; Apple raised the price of iPhone in Japan; VIM 9.0 releases | geek headlines

TCP server communication process (important)

Yyds dry goods inventory hands-on teaching you to carry out the packaging and release of mofish Library (fishing Library)
随机推荐
Sim2real environment configuration tutorial
Maui学习之路(三)--Winui3深入探讨
关于mysql安装的一些问题
Global and Chinese markets for carbon dioxide laser cutting heads 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of jacquard looms 2022-2028: Research Report on technology, participants, trends, market size and share
Bib | graph representation based on heterogeneous information network learning to predict drug disease association
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
做机器视觉哪个软件好?
Source code look me
电脑管理员权限在哪里可以打开
PCL 点云镜像变换
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
Yyds dry goods inventory # look up at the sky | talk about the way and principle of capturing packets on the mobile terminal and how to prevent mitm
Global and Chinese market of discharge machines 2022-2028: Research Report on technology, participants, trends, market size and share
AcWing 300. Task arrangement
AWS云主机扩容
潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
dried food! Understand the structural vulnerability of graph convolution networks
Yyds dry goods inventory student attendance system based on QT design
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share