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

张开发
2026/5/12 22:12:13 15 分钟阅读

分享文章

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-pinpinfilename#配置引脚为对应filename的功能config-pin-lpin# 查询引脚支持的模式config-pin-qpin#查看引脚目前配置模式我们随便取一个引脚为p9.14先查询引脚当前模式和可以设置的模式config-pin-qp9.14 config-pin-lp9.14当前模式默认可以配置的模式gpio_pu(上拉) /gpio_pu(下拉)因此我们将其配置为上拉它就默认是高电平了。sudoconfig-pin p9.14 gpio_pu同理我们将其配置为下拉它就默认是低电平了。sudoconfig-pin p9.14 gpio_pd拿万用变测试一下确实ok。1.2 代码控制代码如下#includestdio.h#includestdlib.h#includestdbool.h#includeunistd.hvoidled_on(void){system(config-pin p9.14 gpio_pd);}voidled_off(void){system(config-pin p9.14 gpio_pu);}voidmain(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/echo51export#变成用户可用的状态cdgpio51echooutdirectionecho1value //高电平echo0value //低电平观察没有问题。2.2 编程控制我这里用了三个引脚进行操作其中两个LED一个蜂鸣器。引脚编号用途P9.14gpio50LED0P9.16gpio51LED1P8.19gpio22BEEP你要问我这LED和蜂鸣器哪里来的 我在板子上外接的。你们没有就用万用表去测也行就是不那么直观…控制LED和蜂鸣器每过一段时间亮/响一下代码如下#includestdio.h#includestdlib.h#includesys/types.h#includesys/stat.h#includefcntl.h#includeunistd.h#includestring.hcharexport_path[]/sys/class/gpio;charled0_path[]/sys/class/gpio/gpio50;charled1_path[]/sys/class/gpio/gpio51;charbeep_path[]/sys/class/gpio/gpio22;#defineled0_on()file_config(led0_path,value,0)#defineled0_off()file_config(led0_path,value,1)#defineled1_on()file_config(led1_path,value,0)#defineled1_off()file_config(led1_path,value,1)#definebeep_on()file_config(beep_path,value,0)#definebeep_off()file_config(beep_path,value,1)staticvoidfile_config(char*path,char*file,constchar*val){charfile_path[100];//文件路径 文件夹路径 文件名sprintf(file_path,%s/%s,path,file);intfdopen(file_path,O_WRONLY);intlenstrlen(val);if(len!write(fd,val,len)){perror(write error);printf(%s\r\n,file_path);}close(fd);//关闭文件}intmain(void){printf(hello\r\n);intret;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);}return0;}3 基于PWM的控制3.1 PWM引脚定义上边的控制方式是比较通用的但是偶尔也会出现bug比如没有操作权限。但是使用PWM就不会有该问题因为只需要往里面输入数据就行。本人所使用的镜像为2020年的debian 10.3且为flash版本。引脚定义为9.14pwm4 :09.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-lp9.14 config-pin-lp9.16 config-pin-qp9.14 config-pin-qp9.16可以看到现在模式为gpio_pu(这是是啥都不重要) 以及可以配置的模式包括pwm。将引脚配置为pwm模式config-pin p9.14 pwm config-pin p9.16 pwmPWM接口已经配置好了进入pwm对应的类文件夹中。cd/sys/class/pwm/ls可以看到pwm4:0和pwm4:1 已经被系统导出就不需要我们导出了。可以直接操作对应功能了。cdpwm-4:0ls这里只需要关注几个参数参数说明period一个周期的总时间大小duty_cycle周期里有效输出的时间enable使能polarity输出极性 0有效输出为高电平默认1有效输出为高电平假如说我们要输出50%的占空比。可以将period设置为100duty_cycle设置为50enable1echo100peroidecho50duty_cycleecho1enable然后外部可以使用万用表去测电压将是50%。3.3 编程控制#includestdio.h#includestdlib.h#includesys/types.h#includesys/stat.h#includefcntl.h#includeunistd.h#includestring.hcharexport_path[]/sys/class/pwm/pwmchip4;charled0_path[]/sys/class/pwm/pwm-4:0;charled1_path[]/sys/class/pwm/pwm-4:1;#defineled0_on()file_config(led0_path,duty_cycle,100)#defineled0_off()file_config(led0_path,duty_cycle,0)staticvoidfile_config(char*path,char*file,constchar*val){charfile_path[100];//文件路径 文件夹路径 文件名sprintf(file_path,%s/%s,path,file);intfdopen(file_path,O_WRONLY);intlenstrlen(val);if(len!write(fd,val,len)){perror(write error);printf(%s\r\n,file_path);}close(fd);//关闭文件}voidled0_set(intval){chartemp[10];sprintf(temp,%d,val);file_config(led0_path,duty_cycle,temp);}voidled1_set(intval){chartemp[10];sprintf(temp,%d,val);file_config(led1_path,duty_cycle,temp);}intmain(void){printf(hello\r\n);intret;system(config-pin p9.14 pwm);//led0system(config-pin p9.16 pwm);//led1if(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){staticinta0;aa5;led0_set(a);led1_set(a);sleep(1);if(a50)a0;}return0;}个人在外部连接了两个led这两个引脚的电平从0%增加到50%。不停往复。gcc-opwm pwm.c ./pwm用pwm的好处是执行不需要sudo权限。上边的gpio操作不给容易出问题。另外添加了一个蜂鸣器P8.19pwm7.9#includestdio.h#includestdlib.h#includesys/types.h#includesys/stat.h#includefcntl.h#includeunistd.h#includestring.h#includestdbool.hcharexport_path[]/sys/class/pwm/pwmchip4;charled0_path[]/sys/class/pwm/pwm-4:0;charled1_path[]/sys/class/pwm/pwm-4:1;charbeep_path[]/sys/class/pwm/pwm-7:0;#defineled0_on()file_config(led0_path,duty_cycle,100)#defineled0_off()file_config(led0_path,duty_cycle,0)#definebeep_on()file_config(beep_path,duty_cycle,100)#definebeep_off()file_config(beep_path,duty_cycle,0)staticvoidfile_config(char*path,char*file,constchar*val){charfile_path[100];//文件路径 文件夹路径 文件名sprintf(file_path,%s/%s,path,file);intfdopen(file_path,O_WRONLY);intlenstrlen(val);if(len!write(fd,val,len)){perror(write error);printf(%s\r\n,file_path);}close(fd);//关闭文件}voidled0_set(intval){chartemp[10];sprintf(temp,%d,val);file_config(led0_path,duty_cycle,temp);}voidled1_set(intval){chartemp[10];sprintf(temp,%d,val);file_config(led1_path,duty_cycle,temp);}intmain(void){printf(hello\r\n);intret;system(config-pin p9.14 pwm);//led0system(config-pin p9.16 pwm);//led1system(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){staticinta0;staticbool btrue;aa5;led0_set(a);led1_set(a);if(b)beep_on();elsebeep_off();b!b;sleep(1);if(a50)a0;}return0;}蜂鸣器将隔1s响一次。

更多文章