2023年 beagle bone black教程3-点灯的三种方式

2024-01-07 03:40

本文主要是介绍2023年 beagle bone black教程3-点灯的三种方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

beagle bone black点灯的三种方式

  • 1 基于上下拉的点灯
    • 1.1 终端控制
    • 1.2 代码控制
  • 2 基于电平控制的点灯
    • 2.1 认识引脚
    • 2.2 编程控制
  • 3 基于PWM的控制
    • 3.1 PWM引脚定义
    • 3.2 终端控制
    • 3.3 编程控制

1 基于上下拉的点灯

1.1 终端控制

在linxu中,为了控制引脚输出,我们需要各种初始化,比较麻烦。侯然想了一种方法:引脚是可以配置为上拉或下拉的,这样在默认的时候,它便输出高低电平。这样一句话就搞定。

引脚配置语句:

config-pin <pin> <filename> #配置引脚为对应filename的功能
config-pin -l <pin># 查询引脚支持的模式
config-pin -q <pin> #查看引脚目前配置模式

我们随便取一个引脚为:p9.14,先查询引脚当前模式和可以设置的模式

config-pin -q p9.14
config-pin  -l  p9.14

在这里插入图片描述
当前模式:默认 可以配置的模式:gpio_pu(上拉) / gpio_pu(下拉)
因此,我们将其配置为上拉,它就默认是高电平了。

sudo config-pin p9.14 gpio_pu

同理,我们将其配置为下拉,它就默认是低电平了。

sudo config-pin p9.14 gpio_pd

拿万用变测试一下确实ok。

1.2 代码控制

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>void led_on(void)
{system("config-pin p9.14 gpio_pd");
}void led_off(void)
{system("config-pin p9.14 gpio_pu");
}void main(void)
{while(1){sleep(1);led_on();sleep(1);led_off();} 
}

实际观察确实ok,就是终端一直输出如下数据,比较烦人
在这里插入图片描述

2 基于电平控制的点灯

2.1 认识引脚

老实讲,网上的beaglebone black的引脚图很多,但是!!!你得知道哪个是对的
在这里插入图片描述我们使用P9.16 作为led进行控制。它是GPIO_51。

首先我们先将引脚模式配置为gpio( gpio_pu 或者gpio_pd都行,就是默认上拉还是下拉)

config-pin p9.16 gpio_pu

其次,到gpio类中进行实现。

cd /sys/class/gpio/
 echo 51 > export #变成用户可用的状态
cd gpio51
echo out > direction
echo 1 > value //高电平echo 0 > value //低电平

观察没有问题。

2.2 编程控制

我这里用了三个引脚进行操作,其中两个LED,一个蜂鸣器。

引脚编号用途
P9.14gpio50LED0
P9.16gpio51LED1
P8.19gpio22BEEP

你要问我这LED和蜂鸣器哪里来的? 我在板子上外接的。你们没有就用万用表去测也行,就是不那么直观…

控制LED和蜂鸣器每过一段时间亮/响一下,代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>char export_path[]="/sys/class/gpio";
char led0_path[]="/sys/class/gpio/gpio50";
char led1_path[]="/sys/class/gpio/gpio51";
char beep_path[]="/sys/class/gpio/gpio22";#define led0_on()  file_config(led0_path,"value","0")
#define led0_off() file_config(led0_path,"value","1")
#define led1_on()  file_config(led1_path,"value","0")
#define led1_off() file_config(led1_path,"value","1")
#define beep_on()  file_config(beep_path,"value","0")
#define beep_off() file_config(beep_path,"value","1")static void  file_config(char* path,char* file,const char* val)
{char file_path[100];//文件路径 = 文件夹路径 + 文件名sprintf(file_path,"%s/%s",path,file);int fd = open(file_path, O_WRONLY);int len=strlen(val);if (len != write(fd, val,len)) {perror("write error");printf("%s\r\n",file_path);}close(fd); //关闭文件}int main(void)
{printf("hello\r\n");int ret;system("config-pin p9.14 gpio_pu");//led0 默认不亮system("config-pin p9.16 gpio_pu");//led1 默认不亮system("config-pin p8.19 gpio_pd");//beep 默认不响if(access(led0_path,F_OK)==-1)判断不存在file_config(export_path,"export","50");if(access(led1_path,F_OK)==-1)file_config(export_path,"export","51");if(access(beep_path,F_OK)==-1)file_config(beep_path,"export","22");file_config(led0_path,"direction","out");file_config(led1_path,"direction","out");file_config(beep_path,"direction","out");led1_off();while(1){led0_off();sleep(1); led0_on();sleep(1);     }return 0;}

3 基于PWM的控制

3.1 PWM引脚定义

上边的控制方式是比较通用的,但是偶尔也会出现bug,比如没有操作权限。但是使用PWM就不会有该问题,因为只需要往里面输入数据就行。

本人所使用的镜像为2020年的debian 10.3,且为flash版本。

引脚定义为:

9.14pwm4 :0
9.16pwm4:1

查看pwm引脚定义:bb.org-overlays/cape-unversal-pwm.txt at master · beagleboard/bb.org-overlays (github.com)

不管版本如何,我们都可以使用以下指令查看引脚属于哪个pwm
使用以下指令查看引脚属于哪个pwm

#P9.21/P9.22
#ls -lh /sys/devices/platform/ocp/48300000.epwmss/48300200.pwm/pwm/#P9.14/P9.16
#ls -lh /sys/devices/platform/ocp/48302000.epwmss/48302200.pwm/pwm/

我们查看p9.14
在这里插入图片描述
如图,说明p9.14 和p9.16是属于pwmchip4 。至于具体哪个是4.0 和4.1 需要测试一下。

3.2 终端控制

首先,查询引脚现在模式以及可以配置的模式

 config-pin -l  p9.14config-pin -l  p9.16config-pin -q  p9.14config-pin -q  p9.16

在这里插入图片描述
可以看到,现在模式为gpio_pu(这是是啥都不重要) 以及可以配置的模式(包括pwm)。

将引脚配置为pwm模式

config-pin   p9.14 pwm
config-pin   p9.16 pwm

在这里插入图片描述
PWM接口已经配置好了!

进入pwm对应的类文件夹中。

cd /sys/class/pwm/
ls

在这里插入图片描述

可以看到pwm4:0和pwm4:1 已经被系统导出,就不需要我们导出了。可以直接操作对应功能了。

cd pwm-4:0
ls

在这里插入图片描述

这里只需要关注几个参数

参数说明
period一个周期的总时间大小
duty_cycle周期里有效输出的时间
enable使能
polarity输出极性 0:有效输出为高电平(默认)
1:有效输出为高电平

假如说,我们要输出50%的占空比。可以将period设置为100,duty_cycle设置为50,enable=1

echo 100 > peroid
echo 50 > duty_cycle
echo 1 > enable

然后外部可以使用万用表去测电压(将是50%)。

3.3 编程控制

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>char export_path[]="/sys/class/pwm/pwmchip4";
char led0_path[]="/sys/class/pwm/pwm-4:0";
char led1_path[]="/sys/class/pwm/pwm-4:1";#define led0_on()  file_config(led0_path,"duty_cycle","100")
#define led0_off() file_config(led0_path,"duty_cycle","0")static void  file_config(char* path,char* file,const char* val)
{char file_path[100];//文件路径 = 文件夹路径 + 文件名sprintf(file_path,"%s/%s",path,file);int fd = open(file_path, O_WRONLY);int len=strlen(val);if (len != write(fd, val,len)) {perror("write error");printf("%s\r\n",file_path);}close(fd); //关闭文件
}void led0_set(int val)
{char temp[10];sprintf(temp,"%d",val);file_config(led0_path,"duty_cycle",temp);
}
void led1_set(int val)
{char temp[10];sprintf(temp,"%d",val);file_config(led1_path,"duty_cycle",temp);
}int main(void)
{printf("hello\r\n");int ret;system("config-pin p9.14 pwm");//led0 system("config-pin p9.16 pwm");//led1 if(access(led0_path,F_OK)==-1)判断不存在file_config(export_path,"export","0");if(access(led1_path,F_OK)==-1)file_config(export_path,"export","1");file_config(led0_path,"period","100");file_config(led1_path,"period","100");file_config(led0_path,"duty_cycle","100");//默认100%高电平file_config(led1_path,"duty_cycle","100");file_config(led0_path,"enable","1");//使能file_config(led1_path,"enable","1");while(1){static int a=0;a=a+5;led0_set(a);led1_set(a);sleep(1); if(a==50) a=0;}return 0;    
}

个人在外部连接了两个led,这两个引脚的电平,从0%增加到50%。不停往复。

gcc -o pwm pwm.c
./pwm

用pwm的好处是,执行不需要sudo权限。上边的gpio操作不给容易出问题。

另外,添加了一个蜂鸣器P8.19:pwm7.9

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
char export_path[]="/sys/class/pwm/pwmchip4";
char led0_path[]="/sys/class/pwm/pwm-4:0";
char led1_path[]="/sys/class/pwm/pwm-4:1";
char beep_path[]="/sys/class/pwm/pwm-7:0";#define led0_on()  file_config(led0_path,"duty_cycle","100")
#define led0_off() file_config(led0_path,"duty_cycle","0")#define beep_on()  file_config(beep_path,"duty_cycle","100")
#define beep_off() file_config(beep_path,"duty_cycle","0")static void  file_config(char* path,char* file,const char* val)
{char file_path[100];//文件路径 = 文件夹路径 + 文件名sprintf(file_path,"%s/%s",path,file);int fd = open(file_path, O_WRONLY);int len=strlen(val);if (len != write(fd, val,len)) {perror("write error");printf("%s\r\n",file_path);}close(fd); //关闭文件
}void led0_set(int val)
{char temp[10];sprintf(temp,"%d",val);file_config(led0_path,"duty_cycle",temp);
}
void led1_set(int val)
{char temp[10];sprintf(temp,"%d",val);file_config(led1_path,"duty_cycle",temp);
}int main(void)
{printf("hello\r\n");int ret;system("config-pin p9.14 pwm");//led0 system("config-pin p9.16 pwm");//led1 system("config-pin p8.19 pwm");//beepif(access(led0_path,F_OK)==-1)判断不存在file_config(export_path,"export","0");if(access(led1_path,F_OK)==-1)file_config(export_path,"export","1");file_config(led0_path,"period","100");file_config(led1_path,"period","100");file_config(beep_path,"period","100");file_config(led0_path,"duty_cycle","100");//默认100%高电平file_config(led1_path,"duty_cycle","100");file_config(beep_path,"duty_cycle","0");  file_config(led0_path,"enable","1");//使能file_config(led1_path,"enable","1");file_config(beep_path,"enable","1");while(1){static int a=0;static bool b=true;a=a+5;led0_set(a);led1_set(a);if(b)beep_on();else beep_off(); b=!b;sleep(1); if(a==50) a=0;}return 0;
}

蜂鸣器将隔1s响一次。

这篇关于2023年 beagle bone black教程3-点灯的三种方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/578608

相关文章

JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法

《JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法》:本文主要介绍JavaScript中比较两个数组是否有相同元素(交集)的三种常用方法,每种方法结合实例代码给大家介绍的非常... 目录引言:为什么"相等"判断如此重要?方法1:使用some()+includes()(适合小数组)方法2

HTTP 与 SpringBoot 参数提交与接收协议方式

《HTTP与SpringBoot参数提交与接收协议方式》HTTP参数提交方式包括URL查询、表单、JSON/XML、路径变量、头部、Cookie、GraphQL、WebSocket和SSE,依据... 目录HTTP 协议支持多种参数提交方式,主要取决于请求方法(Method)和内容类型(Content-Ty

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

使用shardingsphere实现mysql数据库分片方式

《使用shardingsphere实现mysql数据库分片方式》本文介绍如何使用ShardingSphere-JDBC在SpringBoot中实现MySQL水平分库,涵盖分片策略、路由算法及零侵入配置... 目录一、ShardingSphere 简介1.1 对比1.2 核心概念1.3 Sharding-Sp

Java Scanner类解析与实战教程

《JavaScanner类解析与实战教程》JavaScanner类(java.util包)是文本输入解析工具,支持基本类型和字符串读取,基于Readable接口与正则分隔符实现,适用于控制台、文件输... 目录一、核心设计与工作原理1.底层依赖2.解析机制A.核心逻辑基于分隔符(delimiter)和模式匹

Spring创建Bean的八种主要方式详解

《Spring创建Bean的八种主要方式详解》Spring(尤其是SpringBoot)提供了多种方式来让容器创建和管理Bean,@Component、@Configuration+@Bean、@En... 目录引言一、Spring 创建 Bean 的 8 种主要方式1. @Component 及其衍生注解

python中的显式声明类型参数使用方式

《python中的显式声明类型参数使用方式》文章探讨了Python3.10+版本中类型注解的使用,指出FastAPI官方示例强调显式声明参数类型,通过|操作符替代Union/Optional,可提升代... 目录背景python函数显式声明的类型汇总基本类型集合类型Optional and Union(py

Linux系统管理与进程任务管理方式

《Linux系统管理与进程任务管理方式》本文系统讲解Linux管理核心技能,涵盖引导流程、服务控制(Systemd与GRUB2)、进程管理(前台/后台运行、工具使用)、计划任务(at/cron)及常用... 目录引言一、linux系统引导过程与服务控制1.1 系统引导的五个关键阶段1.2 GRUB2的进化优

spring AMQP代码生成rabbitmq的exchange and queue教程

《springAMQP代码生成rabbitmq的exchangeandqueue教程》使用SpringAMQP代码直接创建RabbitMQexchange和queue,并确保绑定关系自动成立,简... 目录spring AMQP代码生成rabbitmq的exchange and 编程queue执行结果总结s

IDEA与MyEclipse代码量统计方式

《IDEA与MyEclipse代码量统计方式》文章介绍在项目中不安装第三方工具统计代码行数的方法,分别说明MyEclipse通过正则搜索(排除空行和注释)及IDEA使用Statistic插件或调整搜索... 目录项目场景MyEclipse代码量统计IDEA代码量统计总结项目场景在项目中,有时候我们需要统计