|
BeagleBone Black设计:ADC应用和摇杆调试!
[复制链接]
换上电容屏之后,八路ADC完全可以放开了!
但是,内核里貌似是通过Ti_tscadc.c 初始化ADC的所以添加了函数:
可以注意到,通道选择了8路,但是用于触摸的通道就是0了,没有用于触摸屏的通道了!
使能8个ADC引脚之后!
重新编译内核!
写入内核之后!
可以在目录 sys/bus/iio/devices/iio\:device0 下看到:
8路ADC全部都有了!
接下来就是测试摇杆了!
摇杆用的是ADC4和ADC5,参考wytalfred老大的ADC程序讲解!具体程序如下:
#include
#include
#include
#include
#define X "/sys/bus/iio/devices/iio:device0/in_voltage4_raw"
#define Y "/sys/bus/iio/devices/iio:device0/in_voltage5_raw"
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define MAX_BUF 64
void main()
{
int fd_x,fd_y, len;
char x_buf[MAX_BUF],y_buf[MAX_BUF];
char ch_x[5],ch_y[5];
int i;
int J_x,J_y;
int fd_key, len_key;
char ch_key;
char buf_key[MAX_BUF];
int green;
//export gpio1_17,green
fd_key = open(SYSFS_GPIO_DIR "/export", O_WRONLY);
len_key = snprintf(buf_key,sizeof(buf_key),"49");
write(fd_key,buf_key,len_key);
close(fd_key);
//set direction
snprintf(buf_key,sizeof(buf_key),SYSFS_GPIO_DIR"/gpio49/direction");
fd_key = open(buf_key, O_WRONLY);
write(fd_key, "in", 3);
close(fd_key);
while(1)
{
snprintf(buf_key,sizeof(buf_key),SYSFS_GPIO_DIR"/gpio49/value");
fd_key = open(buf_key, O_RDONLY);
read(fd_key,&ch_key,1);
green=atoi(&ch_key);
snprintf(x_buf,sizeof(x_buf),X);
snprintf(y_buf,sizeof(y_buf),Y);
fd_x = open(x_buf, O_RDONLY);
fd_y = open(y_buf, O_RDONLY);
read(fd_x,ch_x,4);
read(fd_y,ch_y,4);
J_x=atoi(ch_x);
J_y=atoi(ch_y);
if(J_x>3048&&J_x<4096)
{
printf("%s\n","Right");
}
if(J_x<1048)
{
printf("%s\n","Left");
}
if(J_y>3048&&J_x<4096)
{
printf("%s\n","Up");
}
if(J_y<1048)
{
printf("%s\n","Down");
}
if(J_x>3048&&J_y>3048)
{
printf("%s\n","Right-up");
}
if(J_x<1048&&J_y>3048)
{
printf("%s\n","Left-up");
}
if(J_x>3048&&J_y<1048)
{
printf("%s\n","Right-down");
}
if(J_x<1048&&J_y<1048)
{
printf("%s\n","Left-down");
}
if(green==0)
{
printf("%s\n","Red button!test over!");
break;
}
for(i=0;i<50;i++)
{
usleep(1000);//pause for 1 ms
}
}
}
ADC最大电压为1.8V,在中间位置不动时,两路ADC电压都为0.9V左右!所以ADC值为2048左右!
上对应着Y的最大值4000左右;
下对应着Y的最小值100左右;
右对应着X的最大值4000左右;
左对应着X的最小值100左右;
右上、右下、左上、左下方向则是两路结合判断!
上面的程序比较粗糙,只是简单测试,大牛勿喷我!
测试效果:
接下来再逐步完善吧!
|
|