当前位置:网站首页>串口控制舵机转动
串口控制舵机转动
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);
}
}
}
边栏推荐
- TCP拥塞控制详解 | 2. 背景
- SQLServer查询哪些索引利用率低
- AWS virtual machine expansion
- Remove the underline in router link
- 学生选课系统(山东农业大学课程设计)
- Summary of monthly report | list of major events of moonbeam in June
- Multi task prompt learning: how to train a large language model?
- Route service grid traffic through two-level gateway design
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验2 RGB LED 实验(学习笔记)
- What is Amazon keyword index? The consequences of not indexing are serious
猜你喜欢
Vscade set multi line display of tab
关于mysql安装的一些问题
原神2.6服务端下载以及搭建安装教程
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
JS learning notes - first acquaintance
⌈ 2022 ⌋ how to use webp gracefully in projects
PCL point cloud image transformation
SSM整合-异常处理器及项目异常处理方案
分析超700万个研发需求发现,这8门编程语言才是行业最需要的!
渗透工具-内网权限维持-Cobalt strike
随机推荐
Analyzing more than 7million R & D needs, it is found that these eight programming languages are the most needed in the industry!
Multi task prompt learning: how to train a large language model?
Library management system (Shandong Agricultural University Curriculum Design)
请问怎么在oracle视图中使用stustr函数
Global and Chinese market of desktop hot melt equipment 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 2. 两数相加
Yyds dry inventory KVM new inventory to expand space for home
Yyds dry inventory method of deleting expired documents in batch
LeetCode 3. 无重复字符的最长子串
通过两级网关设计来路由服务网格流量
隐私计算技术创新及产业实践研讨会:学习
Summary | three coordinate systems in machine vision and their relationships
What if the win11 app store cannot load the page? Win11 store cannot load page
TypeScript数组乱序输出
Yyds dry inventory company stipulates that all interfaces use post requests. Why?
618 reprise en profondeur: la méthode gagnante de la famille Haier Zhi
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
LeetCode 4. 寻找两个正序数组的中位数(hard)
HMS core machine learning service helps zaful users to shop conveniently
Unity Json 编写