当前位置:网站首页>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 .
边栏推荐
- Machine learning basic SVM (support vector machine)
- C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。
- [the second day of actual combat of smart lock project based on stm32f401ret6 in 10 days] GPIO and register
- Leetcode question 20
- Use mediapipe+opencv to make a simple virtual keyboard
- swiper 横向轮播 grid
- Delphi 10.4.2 release instructions and installation methods of three patches
- Top level configuration + cooling black technology + cool appearance, the Red Devils 6S Pro is worthy of the flagship game of the year
- Implementation and design of JMeter interface test database assertion for CSDN salary increase technology
- Detailed understanding of white noise
猜你喜欢

Decoding iFLYTEK open platform 2.0 is a fertile land for developers and a source of industrial innovation

C language conditional compilation routine

Matplotlib drawing Chinese garbled code

C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。

What did Hello travel do right for 500million users in five years?

Magics 23.0 how to activate and use the slice preview function of the view tool page
![[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys](/img/fc/f03c7dc4d5ee12aaa301f54e4cd3f4.jpg)
[the fourth day of actual combat of stm32f401ret6 smart lock project in 10 days] voice control is realized by externally interrupted keys

numpy多维数组转置transpose
![[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer](/img/c4/0d97def5fb587b8301bcb907fc6fcf.jpg)
[the 4th day of the 10 day smart lock project based on stm32f401ret6] what is interrupt, interrupt service function, system tick timer

Gome's ambition of "folding up" app
随机推荐
leetcode743. Network latency (medium, Dijkstra)
Application and routine of C language typedef struct
[printf function and scanf function] (learning note 5 -- standard i/o function)
[the third day of actual combat of smart lock project based on stm32f401ret6 in 10 days] communication foundation and understanding serial port
Cmake has no obvious error after compilation, but prompts that pthread cannot be found
Use mediapipe+opencv to make a simple virtual keyboard
Workspace for ROS
Plumber game
路径字段是什么? ——竞价广告
[pytorch FAQ] numpy:dll load failed while importing_ multiarray_ Umath: the specified module could not be found.
华为设备配置CE双归属
Restrict cell input type and display format in CXGRID control
Combining strings and numbers using ssstream
How many smart bids does Google have?
C语言压缩字符串保存到二进制文件,从二进制文件读取压缩字符串后解压。
Looking at Qianxin's "wild prospect" of network security from the 2021 annual performance report
蓝牙模块:使用问题集锦
Interruption of 51 single chip microcomputer learning notes (external interruption, timer interruption, interrupt nesting)
五、库存查询功能的完善
移动IPv6光猫登录的一般ip地址账号与密码,移动光猫变桥接模式