当前位置:网站首页>21. PWM application programming
21. PWM application programming
2022-07-05 23:34:00 【anieoo】
One 、PWM Operation introduction
PWM, Programming method and... Of pulse width modulation technology LED Class devices can also pass sysfs Virtual file system control . Go to directory /sys/class/pwm Under the table of contents :
Each of them pwmchipx Corresponds to a pwm controller , Get into pwmchip0 file :
npwm: This is a read-only property , Read the file to know the PWM There are several channels under the controller PWM Output , As follows
in :
It can be seen that I.MX6ULL Every PWM The controller corresponds to only one output .export: And GPIO Control is the same , In the use of PWM Before , You also need to export it , adopt export Attribute to export .unexport: Will export PWM Delete . When used PWM after , We need to export PWM Delete .
enable: Can read but write , write in "0" It is forbidden to PWM; write in "1" To enable PWM. Read the file to get PWM Is the current status prohibited or enabled .
polarity: Used to set polarity , Can read but write , Writable values such as :"normal": Ordinary ;"inversed": reverse ;period: Used for configuration PWM cycle , Can read but write ; Write a string numeric value , With ns( nanosecond ) In units of
duty_cycle: Used for configuration PWM Duty cycle of , Can read but write ; Write a string numeric value , Also with ns In units of .
Two 、 Write application operation
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
static char pwm_path[100];
static int pwm_config(const char *attr, const char *val) // To configure PWM
{
char file_path[100];
int len;
int fd;
sprintf(file_path, "%s/%s", pwm_path, attr);
if (0 > (fd = open(file_path, O_WRONLY))) {
perror("open error");
return fd;
}
len = strlen(val);
if (len != write(fd, val, len)) {
perror("write error");
close(fd);
return -1;
}
close(fd); // Close file
return 0;
}
int main(int argc, char *argv[])
{
/* Check the transmission parameters */
if (4 != argc) {
fprintf(stderr, "usage: %s <id> <period> <duty>\n",
argv[0]);
exit(-1);
}
/* Print configuration information */
printf("PWM config: id<%s>, period<%s>, duty<%s>\n",
argv[1], argv[2],
argv[3]);
/* export pwm */
sprintf(pwm_path, "/sys/class/pwm/pwmchip%s/pwm0", argv[1]);
if (access(pwm_path, F_OK)) {// If pwm0 directory does not exist , Then export
char temp[100];
int fd;
sprintf(temp, "/sys/class/pwm/pwmchip%s/export", argv[1]);
if (0 > (fd = open(temp, O_WRONLY))) {
perror("open error");
exit(-1);
}
if (1 != write(fd, "0", 1)) {// export pwm
perror("write error");
close(fd);
exit(-1);
}
close(fd); // Close file
}
/* To configure PWM cycle */
if (pwm_config("period", argv[2]))
exit(-1);
/* Configure the duty cycle */
if (pwm_config("duty_cycle", argv[3]))
exit(-1);
/* Can make pwm */
pwm_config("enable", "1");
/* Exit procedure */
exit(0);
}
边栏推荐
- 98. Verify the binary search tree ●●
- Creative mode 1 - single case mode
- Debian 10 installation configuration
- ORB_ SLAM2/3
- 3D point cloud slam
- 基于脉冲神经网络的物体检测
- How to insert data into MySQL database- How can I insert data into a MySQL database?
- Fiddler Everywhere 3.2.1 Crack
- Multi camera stereo calibration
- Spire Office 7.5.4 for NET
猜你喜欢
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
698. Divided into k equal subsets ●●
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Sum of two numbers, sum of three numbers (sort + double pointer)
Development specification: interface unified return value format [resend]
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Attacking technology Er - Automation
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Huawei simulator ENSP - hcip - MPLS experiment
随机推荐
(4) UART application design and simulation verification 2 - TX module design (stateless machine)
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
LeetCode——Add Binary
asp. Net pop-up layer instance
Naoqi robot summary 26
CIS benchmark tool Kube bench
Multi view 3D reconstruction
asp.net弹出层实例
How to design API return code (error code)?
判斷二叉樹是否為完全二叉樹
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
Comparison between webgl and webgpu [3] - vertex buffer
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
无刷驱动设计——浅谈MOS驱动电路
MySQL replace primary key delete primary key add primary key
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
Shell: operator
Rethinking about MySQL query optimization