当前位置:网站首页>STM32 IIC protocol controls pca9685 steering gear drive board
STM32 IIC protocol controls pca9685 steering gear drive board
2022-06-13 01:58:00 【richardgann】
This tutorial is different from most online tutorials , Focus on how to use programming PCA9685, Instead of focusing on the principles , In the end, you are still confused . This is an article to teach you how to output a fixed steering angle , How to migrate programs , from PCA9685 The principle to how to transplant the tutorial , Learn theory in your hands .
Single chip model :stm32f103c8t6
Use stm32 It is not difficult to control a steering gear , But if you control more than one steering gear , Then the hardware resources are stretched .
therefore , Must use artifact ,PCA9685 了 , I believe you can take the initiative to search the Internet PCA9685, I must know PCA9685 It's so awesome , I won't retell it .
Old rules , Submit the source code to , Get the source code first , Listen to me again .
Pay attention to WeChat public number “ Guangyi electronics ”(dlrcclub), reply PCA9685 You can get the network disk link .
We start from the whole keil Take a look at the project :
All we need to pay attention to is two files , One is IIC Driver file , One is PCA9685 The driver file for , For transplantation , Just take these two .C Documents and corresponding .h Put the file in your project directory , You can complete the function call , It's that simple ? Yes , It's that simple . Let's take a closer look at PCA9685 Driving function of , And explain PCA9685 Function functions .
void pca_setfreq(float freq)// Set up PWM frequency
{
u8 prescale,oldmode,newmode;
double prescaleval;
freq *= 0.92;
prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
prescale =floor(prescaleval + 0.5f);
oldmode = pca_read(pca_mode1);
newmode = (oldmode&0x7F) | 0x10; // sleep
pca_write(pca_mode1, newmode); // go to sleep
pca_write(pca_pre, prescale); // set the prescaler
pca_write(pca_mode1, oldmode);
delay_ms(2);
pca_write(pca_mode1, oldmode | 0xa1);
}
We all know that the steering gear is 20ms A periodic pulse produces a 0.5-2.5ms Controlled by a pulse of ,PCA9685 It is necessary to set the steering gear control frequency manually during driving , This function on the graph is used , Please refer to the manual for the specific principle of this function , These principles are not covered here , You just use this function directly , That's all right. .
We set the frequency , Also configure the duty cycle , In this way, the steering gear can be controlled according to different angles ,LED0_ON_L、LED0_ON_H、LED0_OFF_L、LED0_OFF_H+4 These are PCA9685 Of IIC Address , Because we only use this module here , So take the first address , Look down again ,num、on and off Variable ,num Is the channel number of the module , For example, control. 0 Channel number one , control 1 Channel number one ,2 Channel number, etc , It depends on how many steering gears are connected to your module , And which steering gear you want to use ,on and off These two variables are used to configure the duty cycle .
on:pwm Rising count value 0-4096;off:pwm Drop count 0-4096, One pwm The cycle is divided into 4096 Share .
from 0 Start +1 Count , Count on Time hopping goes high , Continue counting to off when , Jump to low level , Until the meter is full 4096 restart . So when on It's not equal to 0 when , Can be used as a delay , When on be equal to 0 when ,0ff/4096 Value , Namely pwm Duty cycle of .
Finally, let's look at the servo channel and the angle control function , And how some parameters in the function are calculated .
void Servo_angle(u8 num,u8 angle)
{
u32 off = 0;
off = (u32)(158+angle*2.2);
pca_setpwm(num,0,off);
}
The main function of this function is to control the channel number and steering gear rotation angle , One of them off = (u32)(158+angle*2.2) Many students can't understand ,158 and 2.2 How did you get it ?
Here we give an example :
To turn to 60 For example :
60 Pulse width corresponding to degrees = 0.5ms+(60/180)*2(2.5ms-0.5ms) = 1.1666ms
Convert to duty cycle = 1.1666ms / 20ms = off /4096,off = 239,50hz The corresponding period is 20ms.
setPWM(num,0,239);
When you see this, you should understand 158 How did you calculate it , Refer to the above 239, You can figure it out 158 Corresponding duty cycle .
PS:0° The corresponding value is about 130,180° The corresponding value is 530, The maximum is 590.
158+angle *2.2 = 360.
In this way, we can define the output channel and angle of the steering gear according to our own ideas .
One of the projects is doc file , Some difficulties in the program are explained and calculated , I hope that helps .
边栏推荐
- uniapp 预览功能
- Application and routine of C language typedef struct
- How to solve practical problems through audience positioning?
- Viewing the ambition of Xiaodu technology from intelligent giant screen TV v86
- Establishment of microservice development environment
- What is Google plus large text ads? How to use it?
- 华为设备配置私网IP路由FRR
- Examples of using the chromium base library
- The new wild prospect of JD instant retailing from the perspective of "hour shopping"
- Detailed explanation of maxpooling corresponding to conv1d, conv2d and conv3d machines of tensorflow2
猜你喜欢
万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
Vscode configuration header file -- Take opencv and its own header file as an example
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
DFS and BFS to solve Treasure Island exploration
Why is "iFLYTEK Super Brain 2030 plan" more worthy of expectation than "pure" virtual human
Implementation of pointer linked list
pringboot之restfull接口规范注解(二)
[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer
What is Google plus large text ads? How to use it?
Use of Arduino series pressure sensors and detected data displayed by OLED (detailed tutorial)
随机推荐
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] (lighting with library function and register respectively)
QT realizes mind mapping function (II)
16 embedded C language interview questions (Classic)
Introduction to ROS runtime
The new wild prospect of JD instant retailing from the perspective of "hour shopping"
一种不带CPU的DPU架构:Hyperion
The first cell of devaxpress CXGRID after inserting a row is in focus editing status
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys
万字讲清 synchronized 和 ReentrantLock 实现并发中的锁
Developer contributions amd Xilinx Chinese Forum sharing - wisdom of questioning
Devaxpress Chinese description --tcximagelist (enhanced image list control)
Magics 23.0如何激活和使用视图工具页的切片预览功能
uniapp 预览功能
5、 Improvement of inventory query function
Delphi Google API text to speech MP3 file
[the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register
指针链表的实现
Vscode configuration header file -- Take opencv and its own header file as an example
三、上传织物图片至SQL Server并提供name进行展示织物照片
LabVIEW大型项目开发提高质量的工具