Difference between revisions of "Matrix - Joystick"

From FriendlyARM WiKi
Jump to: navigation, search
Line 9: Line 9:
 
===连接===
 
===连接===
 
*连接到Tiny4412 SDK (1506)
 
*连接到Tiny4412 SDK (1506)
::首先,你需要去掉Tiny4412 SDK 1506底板上面的BP1_EN的跳线帽
+
::需要先连接[[Matrix - Analog to Digital Converter]]配件
::然后,将配件S针脚连接到GPIO PIN1, V接5V,G接地
+
::然后,Joystick与[[Matrix - Analog to Digital Converter]]配件相连接:
 +
::GND针脚接地
 +
::V针脚接5V
 +
::VRX针脚连接[[Matrix - Analog to Digital Converter]]的AIN0针脚
 +
::VRY针脚连接[[Matrix - Analog to Digital Converter]]的AIN1针脚
 +
::SW针脚连接[[Matrix - Analog to Digital Converter]]的AIN2针脚
 +
 
  
 
===Linux下的C示例===
 
===Linux下的C示例===

Revision as of 06:31, 27 July 2015


1 介绍

我们把游戏手柄上用的摇杆拿出来,做成单独的配件。摇杆是由两个滑动变阻器和一个按键组成。当拨动摇杆时,滑动变阻器的阻值就发生变化,对应的X/Y电压值也随之变化,而用力按下摇杆就会触发按键按下,对应的SW信号变为低电平。

2 特性

  • X和Y轴,一个按键
  • 2.54mm排针接口,接线方便,通用性强

3 使用方法

3.1 连接

  • 连接到Tiny4412 SDK (1506)
需要先连接Matrix - Analog to Digital Converter配件
然后,Joystick与Matrix - Analog to Digital Converter配件相连接:
GND针脚接地
V针脚接5V
VRX针脚连接Matrix - Analog to Digital Converter的AIN0针脚
VRY针脚连接Matrix - Analog to Digital Converter的AIN1针脚
SW针脚连接Matrix - Analog to Digital Converter的AIN2针脚


3.2 Linux下的C示例

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include "libfahw.h"
 
#define SW_TRIGGER          (5)
#define PS2_READ_TIMES      (10)
 
static int devFD;
void PS2Handler(int signNum)
{
    if (signNum == SIGINT) {
        printf("Quit reading PS2 rocker\n");
        pcf8591DeInit(devFD);
    }
    exit(0);
}
 
int main(int argc, char ** argv)
{
        int mode = 0x0;
 
        if ((devFD = pcf8591Init()) == -1) {
        printf("Fail to init pcf8591 AD\n");
        return -1;
    }
    if (pcf8591SetCtrl(devFD, PCF8591_INIT_AD_CONTROL) == -1) {
        printf("Fail to Set pcf8591 control AD\n");
        pcf8591DeInit(devFD);
        return -1;
    }
 
    int i = 0;
        int x, y, z;
        signal(SIGINT, PS2Handler);
        for (i=0; i<PS2_READ_TIMES; i++) {
                x = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL0);
                y = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL1);
                z = pcf8591Read(devFD, mode, PCF8591_AIN_CHANNEL2);
                if (z > SW_TRIGGER) {
                        z = 0;
                } else {
                        z = 0;
                } else {
                        z = 1;
                }
                printf("X=%3d Y=%3d Z=%d\n", x, y, z);
                sleep(1);
        }
        pcf8591DeInit(devFD);
        return 0;
}

3.3 编译并运行示例

git clone http://github.com/friendlyarm/fa-hardware.git
cd fa-hardware
cd demo
cd matrix-joystick
make

将编译生成的PS2rocker通过ftp上传到开发板上运行即可测试。

4 相关资料