当前位置:网站首页>Serial port controls steering gear rotation
Serial port controls steering gear rotation
2022-07-02 16:43:00 【Let everything burn】
List of articles
Mission requirements
- Use serial port communication , Computer serial assistant sends hexadecimal data to MCU , Corresponding steering angle 0~ Maximum Angle
- Send data range 0~180
Ideas
Use query mode : It uses disable Serial interrupt
'0’ Refers to ASCII character 0, It's not hexadecimal
1/9 1/3000
0 1500
45 3000
a Degrees x wave
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 Count backwards
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
Use the data to calculate the formula , You can input any angle , The steering gear can turn out the corresponding angle
need usart.h and usart.c Serial port communication
Completion code :
main.c
int main (void){
// The main program
vu16 t;
delay_ms(500); // Wait for other devices to be ready when powered on
RCC_Configuration(); // System clock initialization
RELAY_Init();// Relay initialization
USART1_Init(115200);
I2C_Configuration();//I2C initialization
TIM3_PWM_Init(59999,23); // Set the frequency to 50Hz, Formula for : Overflow time Tout( Unit second )=(arr+1)(psc+1)/Tclk 20MS = (59999+1)*(23+1)/72000000
//Tclk Clock for general timer , If APB1 There is no frequency division , Then it is the system clock ,72MHZ
//PWM clock frequency =72000000/(59999+1)*(23+1) = 50HZ (20ms), Set the auto load value 60000, The prescaled coefficients 24
while(1){
// Receive by query
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET){
// Query the serial port pending flag bit
a =USART_ReceiveData(USART1);// Read received data
// Convert the angle to the duty cycle
t=(a/90.0+0.5)*3000.0;
TIM_SetCompare3(TIM3,t);
printf("%d ",a);
}
}
}
Additional tasks
while(1) You can't write in it
Ideas
Use timer interrupt 








need tim.c and tim.h Use timer interrupt while(1) There is no need to write a program
Completion code
main.c
int main (void){
// The main program
vu16 a;
vu16 t;
delay_ms(500); // Wait for other devices to be ready when powered on
RCC_Configuration(); // System clock initialization
RELAY_Init();// Relay initialization
USART1_Init(115200);
I2C_Configuration();//I2C initialization
OLED0561_Init(); //OLED initialization
OLED_DISPLAY_8x16_BUFFER(0," YoungTalk "); // display string
OLED_DISPLAY_8x16_BUFFER(3," SG90 TEST2 "); // display string
TOUCH_KEY_Init();// Key initialization
TIM3_PWM_Init(59999,23); // Set the frequency to 50Hz, Formula for : Overflow time Tout( Unit second )=(arr+1)(psc+1)/Tclk 20MS = (59999+1)*(23+1)/72000000
//Tclk Clock for general timer , If APB1 There is no frequency division , Then it is the system clock ,72MHZ
//PWM clock frequency =72000000/(59999+1)*(23+1) = 50HZ (20ms), Set the auto load value 60000, The prescaled coefficients 24
TIM3_Init(59999,23);// Timer initialization , timing 1 second (9999,7199)
while(1){
}
}
tim.c
void TIM3_IRQHandler(void)TIM3 Interrupt handling function
Write the program of serial port to control the steering gear in this function body
#include "tim.h"
// Timer time calculation formula Tout = (( Reload value +1)*( The prescaled coefficients +1))/ clock frequency ;
// for example :1 Second timing , Reload value =9999, The prescaled coefficients =7199
void TIM3_Init(u16 arr,u16 psc){
//TIM3 initialization arr Reload value psc The prescaled coefficients
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStrue;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);// Can make TIM3
TIM3_NVIC_Init (); // Turn on TIM3 Interrupt vector
TIM_TimeBaseInitStrue.TIM_Period=arr; // Set auto reload load value
TIM_TimeBaseInitStrue.TIM_Prescaler=psc; // The prescaled coefficients
TIM_TimeBaseInitStrue.TIM_CounterMode=TIM_CounterMode_Up; // The counter overflows up
TIM_TimeBaseInitStrue.TIM_ClockDivision=TIM_CKD_DIV1; // The division factor of the clock , It plays a little delay role , General set to TIM_CKD_DIV1
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStrue); //TIM3 Initialize settings
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);// Can make TIM3 interrupt
TIM_Cmd(TIM3,ENABLE); // Can make TIM3
}
void TIM3_NVIC_Init (void){
// Turn on TIM3 Interrupt vector
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x3; // Set preemption and sub priority
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM3_IRQHandler(void){
//TIM3 Interrupt handling function
u16 a;
u16 t;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET){
// Judge whether it is TIM3 interrupt
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
// Write your own handler here
// GPIO_WriteBit(LEDPORT,LED1,(BitAction)(1-GPIO_ReadOutputDataBit(LEDPORT,LED1))); // Take the opposite LED1
// Receive by query
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) != RESET){
// Query the serial port pending flag bit
a =USART_ReceiveData(USART1);// Read received data
// Convert the angle to the duty cycle
// 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);
}
}
}
边栏推荐
- Multi task prompt learning: how to train a large language model?
- LeetCode 4. 寻找两个正序数组的中位数(hard)
- ROW_ NUMBER()、RANK()、DENSE_ Rank difference
- ROW_NUMBER()、RANK()、DENSE_RANK区别
- Win11应用商店无法加载页面怎么办?Win11商店无法加载页面
- AcWing 300. Task arrangement
- Summary | three coordinate systems in machine vision and their relationships
- Data security industry series Salon (III) | data security industry standard system construction theme Salon
- 大廠面試總結大全
- Seal Library - installation and introduction
猜你喜欢

Ranger (I) preliminary perception

SSM integration exception handler and project exception handling scheme

Seal Library - installation and introduction

大厂面试总结大全

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 2. Add two numbers
![[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology](/img/5c/5f835c286548907f3f09ecb66b2068.jpg)
[Yu Yue education] reference materials of sensing and intelligent control technology of Nanjing University of Technology
![[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)](/img/c7/1949894e106036d2b412bcd6f70245.jpg)
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
![OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation](/img/8b/36be3191a7d71f4a8c8181eaed8417.jpg)
OSPF - route aggregation [(summary) including configuration commands] | address summary calculation method - detailed explanation

La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
随机推荐
Data security industry series Salon (III) | data security industry standard system construction theme Salon
pwm呼吸灯
Aike AI frontier promotion (2.15)
外企高管、连续创业者、瑜伽和滑雪高手,持续迭代重构的程序人生
自注意力机制和全连接的图卷积网络(GCN)有什么区别联系?
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
TCP congestion control details | 2 background
Foreign enterprise executives, continuous entrepreneurs, yoga and skiing masters, and a program life of continuous iteration and reconstruction
Masa framework - DDD design (1)
曆史上的今天:支付寶推出條碼支付;分時系統之父誕生;世界上第一支電視廣告...
Multi task prompt learning: how to train a large language model?
Sim2real environment configuration tutorial
Trigger: MySQL implements adding or deleting a piece of data in one table and adding another table at the same time
[fluent] dart data type number type (DART file creation | num type | int type | double type | num related API)
Penetration tool - intranet permission maintenance -cobalt strike
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
SSM整合-异常处理器及项目异常处理方案
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
unity Hub 登錄框變得很窄 無法登錄
Pandora IOT development board learning (RT thread) - Experiment 2 RGB LED experiment (learning notes)