当前位置:网站首页>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);
}
边栏推荐
- Attacking technology Er - Automation
- 20.移植Freetype字体库
- Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
- CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
- Multi sensor fusion of imu/ optical mouse / wheel encoder (nonlinear Kalman filter)
- Krypton Factor-紫书第七章暴力求解
- 证明 poj 1014 模优化修剪,部分递归 有错误
- asp. Net pop-up layer instance
- Go语言实现原理——锁实现原理
- LeetCode——Add Binary
猜你喜欢
Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
Scala concurrent programming (II) akka
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
《牛客刷verilog》Part III Verilog企业真题
How to design API return code (error code)?
Non rigid / flexible point cloud ICP registration
Huawei simulator ENSP - hcip - MPLS experiment
2:第一章:认识JVM规范1:JVM简介;
Go语言实现原理——Map实现原理
随机推荐
(4)UART应用设计及仿真验证2 —— TX模块设计(无状态机)
Déterminer si un arbre binaire est un arbre binaire complet
AsyncSocket长连接棒包装问题解决
Shell: operator
JVM的简介
【原创】程序员团队管理的核心是什么?
How to insert data into MySQL database- How can I insert data into a MySQL database?
Introduction to JVM
ts类型声明declare
Basic knowledge of database (interview)
Use of grpc interceptor
698. 划分为k个相等的子集 ●●
Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
UART Application Design and Simulation Verification 2 - TX Module Design (Stateless machine)
Detailed explanation of pointer and array written test of C language
Scala concurrent programming (II) akka
无刷驱动设计——浅谈MOS驱动电路
Rasa 3.x 学习系列-Rasa X 社区版(免费版) 更改
Naoqi robot summary 26
(4)UART應用設計及仿真驗證2 —— TX模塊設計(無狀態機)