【STM32MP157应用编程】3.控制PWM

目录

PWM文件

指令操作PWM

程序操作PWM

程序说明

程序代码

3_PWM_1.c

启动交叉编译工具

编译

拷贝到开发板

测试

PWM文件

在/sys/class/pwm目录下,存放了PWM的文件。

 

         pwmchip0pwmchip4目录对应了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

数字对应的通道
0CH1
1CH2
2CH3
3CH4

 unexport:将导出的PWM通道删除。

echo 2 > unexport

指令操作PWM

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

 

程序操作PWM

程序说明

./xxx 参数1 参数2

参数1:周期,以us为单位,最小值为5。

参数2:占空比,百分比。

        0~100:占空比的百分比。

程序代码

3_PWM_1.c

/*
	PWM控制
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>

//./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的取值范围:>=5
    int ZhouQi = atol(argv[1]);
    if (ZhouQi < 5)
    {
        perror("参数错误!\n");
        return -1;
    }
    //检查参数2的取值范围:0-100
    int 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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因心,三人水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值