当前位置:网站首页>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);
}边栏推荐
- How to design API return code (error code)?
- Objective C message dispatch mechanism
- asp. Net pop-up layer instance
- Calculating the number of daffodils in C language
- Shell: operator
- The PostgreSQL column reference 'ID' is ambiguous - PostgreSQL column reference'id'is ambiguous
- LeetCode——Add Binary
- Object detection based on impulse neural network
- MySQL (1) -- related concepts, SQL classification, and simple operations
- Scala concurrent programming (II) akka
猜你喜欢

Go语言实现原理——Map实现原理

Huawei simulator ENSP - hcip - MPLS experiment

【原创】程序员团队管理的核心是什么?

How to design API return code (error code)?

STM32__06—单通道ADC

Non rigid / flexible point cloud ICP registration

Go语言实现原理——锁实现原理

Spire Office 7.5.4 for NET

Use of grpc interceptor

Neural structured learning - Part 2: training with natural graphs
随机推荐
并查集实践
Pyqt control part (I)
Idea connects to MySQL, and it is convenient to paste the URL of the configuration file directly
Qcombox (rewrite) + qcompleter (auto completion, auto loading the drop-down options of qcombox, setting the background color)
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
Brushless drive design -- on MOS drive circuit
Initial experience | purchase and activate typora software
VS2010编写动态链接库DLL和单元测试,转让DLL测试的正确性
Basic knowledge of database (interview)
TVS管和ESD管的技術指標和選型指南-嘉立創推薦
11gR2 Database Services for &quot; Policy&quot; and &quot; Administrator&quot; Managed databases (file I
MySQL (2) -- simple query, conditional query
Huawei simulator ENSP - hcip - MPLS experiment
STM32__06—单通道ADC
2: Chapter 1: understanding JVM specification 1: introduction to JVM;
How to improve eloquence
Leetcode buys and sells stocks
ORB_ SLAM2/3
【原创】程序员团队管理的核心是什么?
Leetcode sword finger offer brush questions - day 21