当前位置:网站首页>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);
}边栏推荐
- Xinyuan & Lichuang EDA training camp - brushless motor drive
- MySQL (1) -- related concepts, SQL classification, and simple operations
- 如何提升口才
- Initial experience | purchase and activate typora software
- Opencvsharp (C openCV) shape detection and recognition (with source code)
- LeetCode——Add Binary
- Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
- [Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
- Spire.PDF for NET 8.7.2
- Scala concurrent programming (II) akka
猜你喜欢

Initial experience | purchase and activate typora software

【LeetCode】5. Valid Palindrome·有效回文

Object detection based on impulse neural network

并查集实践

14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!

Dynamic memory management (malloc/calloc/realloc)

Detailed explanation of pointer and array written test of C language

Attacking technology Er - Automation

orgchart. JS organization chart, presenting structural data in an elegant way

YML configuration, binding and injection, verification, unit of bean
随机推荐
保研笔记四 软件工程与计算卷二(8-12章)
开关电源Buck电路CCM及DCM工作模式
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
Cwaitabletimer timer, used to create timer object access
The PNG image is normal when LabVIEW is opened, and the full black image is obtained when Photoshop is opened
February 13, 2022-4-symmetric binary tree
What is the process of building a website
Go语言实现原理——锁实现原理
无刷驱动设计——浅谈MOS驱动电路
Idea connects to MySQL, and it is convenient to paste the URL of the configuration file directly
698. Divided into k equal subsets ●●
Krypton Factor-紫书第七章暴力求解
poj 2762 Going from u to v or from v to u? (infer whether it is a weak link diagram)
11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 I
The maximum happiness of the party
Practice of concurrent search
424. 替换后的最长重复字符 ●●
idea 连接mysql ,直接贴配置文件的url 比较方便
3D point cloud slam
芯源&立创EDA训练营——无刷电机驱动