当前位置:网站首页>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);
}
边栏推荐
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- TVS管和ESD管的技术指标和选型指南-嘉立创推荐
- 11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 I
- Objective C message dispatch mechanism
- Design and implementation of secsha system
- Naoqi robot summary 26
- Detailed explanation of pointer and array written test of C language
- Solution to the packaging problem of asyncsocket long connecting rod
- 14种神笔记方法,只需选择1招,让你的学习和工作效率提高100倍!
- Rethinking about MySQL query optimization
猜你喜欢
随机推荐
6-axis and 9-axis IMU attitude estimation
98. Verify the binary search tree ●●
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
Latex multiple linebreaks
orgchart. JS organization chart, presenting structural data in an elegant way
poj 2762 Going from u to v or from v to u? (推断它是否是一个薄弱环节图)
Attacking technology Er - Automation
Development specification: interface unified return value format [resend]
C# Linq Demo
进击的技术er——自动化
成为程序员的你,后悔了吗?
如何提升口才
11gR2 Database Services for &quot;Policy&quot; and &quot;Administrator&quot; Managed Databases (文件 I
regular expression
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
Sum of two numbers, sum of three numbers (sort + double pointer)
UVA – 11637 garbage remembering exam (combination + possibility)
698. 划分为k个相等的子集 ●●
Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes