大家好,最近在调试BBB如何读取编码器,出现了点问题,希望高手能帮忙解决下,以下是我测试的步骤。1、测试的编码器为ROTARY增量式编码器,共4根线(vcc、GND、A相、B相);
2、通过图一给的引脚定义,我选择P8_41和P8_42引脚,分别作为A、B相,VCC接P9_08的sys_5v,GND接P9_02的DGND;
3、连接板后加载设备树,如图二,由于不知道对于哪个eqep,所以把三个都加载上,并在程序里也把三个都读取并显示出来;
4、编写的程序如下:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#define ENCODERDATA "/sys/devices/ocp.3/48300000.epwmss/48300180.eqep/"
#define ENCODERDATA1 "/sys/devices/ocp.3/48302000.epwmss/48302180.eqep/"
#define ENCODERDATA2 "/sys/devices/ocp.3/48304000.epwmss/48304180.eqep/"
#define MAX_LINE 100
struct encoderdata{
char encoder1[MAX_LINE];
char encoder2[MAX_LINE];
char encoder3[MAX_LINE];
};
main(int argc, char **argv)
{
struct encoderdata p1;
FILE *stream = NULL;
FILE *stream1 = NULL;
FILE *stream2 = NULL;
while(1)
{
stream = fopen(ENCODERDATA"position","r+");
if(!stream){
printf("file error \n");
return ;}
stream1 = fopen(ENCODERDATA1"position","r+");
if(!stream1){
printf("file error1 \n");
return ;}
stream2 = fopen(ENCODERDATA2"position","r+");
if(!stream2){
printf("file error2 \n");
return ;}
fgets(p1.encoder1, MAX_LINE, stream);
fgets(p1.encoder2, MAX_LINE, stream1);
fgets(p1.encoder3, MAX_LINE, stream2);
fclose(stream);
fclose(stream1);
fclose(stream2);
usleep(100000);
printf("\nThe current encoder data is: %s, %s, %s\n",p1.encoder1,p1.encoder2,p1.encoder3);
}
}
5、但是在执行时,读不到编码器数据,如图三。
求高手指教!
|
图一.png (111.57 KB, 下载次数: 0)
图一、引脚选择
图三.png (35.62 KB, 下载次数: 0)
图三、运行结果
图二.png (46.78 KB, 下载次数: 0)
图二、加载设备树
|