目录
PWM文件
指令操作PWM
程序操作PWM
程序说明
程序代码
3_PWM_1.c
启动交叉编译工具
编译
拷贝到开发板
测试
在/sys/class/pwm目录下,存放了PWM的文件。
pwmchip0和pwmchip4目录对应了MP157 SoC的2个PWM控制器,pwmchip0对应的是MP157的TIM4,而pwmchip4对应的则是TIM1,并且STM32MP157只提供了一个PWM通道(PA10--TIM1_CH3)。TIM4_CH2用作LCD背光控制。
npwm:只读文件,读取该文件可以得知该PWM控制器下共有几路PWM输出。
cat npwm
export:在使用PWM之前,需要将其导出,通过export属性进行导出。
echo 2 > export
数字 | 对应的通道 |
---|---|
0 | CH1 |
1 | CH2 |
2 | CH3 |
3 | CH4 |
unexport:将导出的PWM通道删除。
echo 2 > unexport
period:用于配置PWM周期,可读可写;写入一个字符串数字值,以ns(纳秒)为单位。最小值为5000。
echo 1000000 > period #设置1ms的周期
duty_cycle:用于配置PWM的占空比,可读可写;写入一个字符串数字值,是以ns为单位。
echo 500000 > duty_cycle #设置0.5ms的占空比
polarity:用于设置极性,可读可写,可写入的值: "normal":普通; "inversed":反转。
echo normal > polarity #设置普通极性
enable:可读可写,写入"0"表示禁止PWM;写入"1"表示使能PWM。读取该文件获取PWM当前是禁止还是使能状态。通常配置好PWM之后,再使能PWM。
echo 1 > enable #使能PWM
./xxx 参数1 参数2
参数1:周期,以us为单位,最小值为5。
参数2:占空比,百分比。
0~100:占空比的百分比。
/*PWM控制
*/
#include
#include
#include
#include
#include
#include //./xxx 周期 占空比(百分比)static char PWM_path[] = "/sys/class/pwm/pwmchip4/pwm2";
static char PWM_export_path[] = "/sys/class/pwm/pwmchip4/export";
static char PWM_period_path[] = "/sys/class/pwm/pwmchip4/pwm2/period";
static char PWM_duty_cycle_path[] = "/sys/class/pwm/pwmchip4/pwm2/duty_cycle";
static char PWM_polarity_path[] = "/sys/class/pwm/pwmchip4/pwm2/polarity";
static char PWM_enable_path[] = "/sys/class/pwm/pwmchip4/pwm2/enable";int main(int argc, char *argv[])
{//检查参数个数if (argc != 3){printf("%s文件的参数个数错误!\n", argv[0]);return -1;}//检查参数char *endptr;//检查参数1是否为纯数字strtol(argv[1], &endptr, 10);if ((endptr == argv[1]) || ((!isspace(*endptr)) && (*endptr != '\0'))){perror("参数错误!\n");return -1;}endptr = NULL;//检查参数2是否为纯数字strtol(argv[2], &endptr, 10);if ((endptr == argv[2]) || ((!isspace(*endptr)) && (*endptr != '\0'))){perror("参数错误!\n");return -1;}//检查参数1的取值范围:>=5int ZhouQi = atol(argv[1]);if (ZhouQi < 5){perror("参数错误!\n");return -1;}//检查参数2的取值范围:0-100int ZhanKongBi = atol(argv[2]);if (ZhanKongBi < 0 || ZhanKongBi > 100){perror("参数错误!\n");return -1;}//检查PWM是否导出int fd;if (access(PWM_path, F_OK)){if (0 > (fd = open(PWM_export_path, O_WRONLY))){perror("文件打开错误!\n");return -1;}if (strlen("2") != (write(fd, "2", strlen("2")))){perror("PWM文件导出错误!\n");return -1;}close(fd);}//配置周期if (0 > (fd = open(PWM_period_path, O_WRONLY))){perror("period文件打开错误!\n");return -1;}char str[100];sprintf(str, "%d", ZhouQi * 1000);if (strlen(str) != write(fd, str, strlen(str))){perror("配置周期错误!\n");return -1;}close(fd);//配置占空比if (0 > (fd = open(PWM_duty_cycle_path, O_WRONLY))){perror("duty_cycle文件打开错误!\n");return -1;}sprintf(str, "%d", ZhouQi * 10 * ZhanKongBi );if (strlen(str) != write(fd, str, strlen(str))){perror("配置占空比错误!\n");return -1;}close(fd);//配置极性if (0 > (fd = open(PWM_polarity_path, O_WRONLY))){perror("polarity文件打开错误!\n");return -1;}if (strlen("normal") != write(fd, "normal", strlen("normal"))){perror("配置极性错误!\n");return -1;}close(fd);//使能if (0 > (fd = open(PWM_enable_path, O_WRONLY))){perror("enable文件打开错误!\n");return -1;}if (strlen("1") != write(fd, "1", strlen("1"))){perror("使能错误!\n");return -1;}close(fd);return 0;
}
source /opt/st/stm32mp1/3.1-snapshot/environment-setup-cortexa7t2hf-neon-vfpv4-ostl-linux-gnueabi
${CC} -o 3_PWM_1 3_PWM_1.c
scp 3_PWM_1 root@10.3.22.219:/home/root/Linux_C_YingYong_BianCheng/JiaoCheng/3_PWM/
输出周期为1ms,占空比为30%的PWM。
./3_PWM_1 1000 30