当前位置:网站首页>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);
}边栏推荐
- Différence entre hors bande et en bande
- Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial
- 2: Chapter 1: understanding JVM specification 1: introduction to JVM;
- Go language implementation principle -- lock implementation principle
- Object detection based on impulse neural network
- [Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
- (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?
- UVA11294-Wedding(2-SAT)
- Neural structured learning - Part 3: training with synthesized graphs
猜你喜欢

CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)

February 13, 2022-4-symmetric binary tree

698. 划分为k个相等的子集 ●●

Rasa 3. X learning series -rasa 3.2.1 new release

Dynamic memory management (malloc/calloc/realloc)

Spire.PDF for NET 8.7.2

CIS基准测试工具kube-bench使用

TVS管 与 稳压二极管参数对比

Fiddler Everywhere 3.2.1 Crack

3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
随机推荐
Scala concurrent programming (II) akka
开关电源Buck电路CCM及DCM工作模式
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
《牛客刷verilog》Part III Verilog企业真题
yate. conf
98. Verify the binary search tree ●●
成为程序员的你,后悔了吗?
二叉树递归套路总结
Initial experience | purchase and activate typora software
orgchart. JS organization chart, presenting structural data in an elegant way
判斷二叉樹是否為完全二叉樹
Objective C message dispatch mechanism
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
6-axis and 9-axis IMU attitude estimation
Summary of binary tree recursive routines
20.移植Freetype字体库
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)
CIS基准测试工具kube-bench使用
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
Common static methods of math class