Difference between revisions of "Matrix - Rotary Encoder/zh"
From FriendlyELEC WiKi
(→连接NanoPC-T2) |
|||
(16 intermediate revisions by 2 users not shown) | |||
Line 17: | Line 17: | ||
==工作原理== | ==工作原理== | ||
− | == | + | ==硬件连接== |
− | + | ===连接NanoPi NEO/NanoPi NEO Air=== | |
− | + | NanoPi M1和NanoPi NEO以及NanoPi NEO Air的前24Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,并且使用同一份代码。<br> | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | 参考下图连接模块:<br> | |
− | + | [[File:Matrix-Rotary_Encoder_nanopi_NEO.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_NEO]] | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | [[File:Matrix- | + | |
连接说明: | 连接说明: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | |Matrix-Rotary_Encoder || NanoPi | + | |Matrix-Rotary_Encoder || NanoPi NEO |
|- | |- | ||
− | | | + | |R || Pin7 |
+ | |- | ||
+ | |G || Pin8 | ||
+ | |- | ||
+ | |B || Pin10 | ||
|- | |- | ||
− | | | + | |V || Pin4 |
|- | |- | ||
− | | | + | |G || Pin6 |
+ | |} | ||
+ | |||
+ | ===连接NanoPi M1=== | ||
+ | 参考下图连接模块:<br> | ||
+ | [[File:Matrix-Rotary_Encoder_nanopi_m1.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_m1]] | ||
+ | |||
+ | 连接说明: | ||
+ | {| class="wikitable" | ||
|- | |- | ||
− | | | + | |Matrix-Rotary_Encoder || NanoPi M1 |
|- | |- | ||
− | | | + | |R || Pin7 |
+ | |- | ||
+ | |G || Pin8 | ||
+ | |- | ||
+ | |B || Pin10 | ||
|- | |- | ||
+ | |V || Pin4 | ||
+ | |- | ||
+ | |G || Pin6 | ||
|} | |} | ||
− | === | + | ===连接NanoPi 2=== |
− | + | 参考下图连接模块:<br> | |
− | + | [[File:Matrix-Rotary_Encoder_nanopi_2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_2]] | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | [[File:Matrix- | + | |
连接说明: | 连接说明: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | |Matrix-Rotary_Encoder || NanoPi | + | |Matrix-Rotary_Encoder || NanoPi 2 |
|- | |- | ||
− | | | + | |R || Pin7 |
+ | |- | ||
+ | |G || Pin8 | ||
+ | |- | ||
+ | |B || Pin10 | ||
|- | |- | ||
− | | | + | |V || Pin4 |
|- | |- | ||
− | | | + | |G || Pin6 |
+ | |} | ||
+ | |||
+ | ===连接NanoPi M2 / NanoPi 2 Fire=== | ||
+ | NanoPi M2和NanoPi 2 Fire的40 Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,这里仅以NanoPi M2为例。<br> | ||
+ | 参考下图连接模块:<br> | ||
+ | [[File:Matrix-Rotary_Encoder_nanopi_m2.jpg|frameless|600px|Matrix-Rotary_Encoder_nanopi_m2]] | ||
+ | |||
+ | 连接说明: | ||
+ | {| class="wikitable" | ||
|- | |- | ||
− | | | + | |Matrix-Rotary_Encoder || NanoPi M2 |
|- | |- | ||
− | | | + | |R || Pin7 |
+ | |- | ||
+ | |G || Pin8 | ||
+ | |- | ||
+ | |B || Pin10 | ||
|- | |- | ||
+ | |V || Pin4 | ||
+ | |- | ||
+ | |G || Pin6 | ||
|} | |} | ||
− | === | + | ===连接NanoPC-T2/NanoPC-T3=== |
− | + | 由于NanoPC-T2跟NanoPC-T3的引脚是一样的,所以连接方式是一样的,这里仅以T2为例,参考下图连接模块:<br> | |
− | + | [[File:Matrix-Rotary_Encoder_NanoPC-T2.jpg|frameless|600px|Matrix-Rotary_Encoder_NanoPC-T2]] | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | [[File:Matrix- | + | |
连接说明: | 连接说明: | ||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
− | |Matrix-Rotary_Encoder || | + | |Matrix-Rotary_Encoder || NanoPC-T2 |
|- | |- | ||
+ | |R || Pin15 | ||
+ | |- | ||
+ | |G || Pin16 | ||
+ | |- | ||
+ | |B || Pin17 | ||
+ | |- | ||
+ | |V || Pin29 | ||
+ | |- | ||
+ | |G || Pin30 | ||
|} | |} | ||
− | == | + | ==编译运行测试程序== |
− | + | 启动开发板并运行Debian系统,进入系统后克隆Matrix代码仓库: | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | $ | + | $ apt-get update && apt-get install git |
− | $ git | + | $ git clone https://github.com/friendlyarm/matrix.git |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | 克隆完成后会得到一个名为matrix的目录。 | ||
− | + | 编译并安装Matrix: | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | $ | + | $ cd matrix |
− | $ make | + | $ make && make install |
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
− | |||
− | + | 运行测试程序: | |
− | + | ||
− | + | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | $ | + | $ matrix-rotary_encoder |
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。<br> | |
− | + | 运行效果如下:<br> | |
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
− | + | Get sw=0 value=1 | |
+ | Get sw=0 value=2 | ||
+ | Get sw=0 value=3 | ||
+ | Get sw=0 value=4 | ||
+ | Get sw=0 value=5 | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | 旋转模块时,能读到对应的值。 | |
− | == | + | ==代码说明== |
+ | 所有的开发板都共用一套Matrix代码,本模块的测试示例代码为matrix-rotary_encoder,内容如下: | ||
<syntaxhighlight lang="c"> | <syntaxhighlight lang="c"> | ||
int main(int argc, char ** argv) | int main(int argc, char ** argv) | ||
Line 272: | Line 158: | ||
int encoderSw = 0; | int encoderSw = 0; | ||
int encoderValue = 0; | int encoderValue = 0; | ||
− | int swPin = | + | int board; |
− | int siaPin = | + | int swPin = GPIO_PIN(7); |
− | int sibPin = | + | int siaPin = GPIO_PIN(8); |
+ | int sibPin = GPIO_PIN(10); | ||
+ | if ((board = boardInit()) < 0) { | ||
+ | printf("Fail to init board\n"); | ||
+ | return -1; | ||
+ | } | ||
+ | if (board == BOARD_NANOPI_T2) { | ||
+ | swPin = GPIO_PIN(15); | ||
+ | siaPin = GPIO_PIN(16); | ||
+ | sibPin = GPIO_PIN(17); | ||
+ | } | ||
+ | system("modprobe "DRIVER_MODULE); | ||
if (rotaryEncoderInit(swPin, siaPin, sibPin)) { | if (rotaryEncoderInit(swPin, siaPin, sibPin)) { | ||
printf("Fail to init rotary encoder\n"); | printf("Fail to init rotary encoder\n"); | ||
− | + | goto err; | |
} | } | ||
− | |||
signal(SIGINT, encoderHandler); | signal(SIGINT, encoderHandler); | ||
for (i=0; i<ENCODER_READ_TIMES; i++) { | for (i=0; i<ENCODER_READ_TIMES; i++) { | ||
Line 286: | Line 182: | ||
rotaryEncoderRead(ENCODER_VALUE, &encoderValue); | rotaryEncoderRead(ENCODER_VALUE, &encoderValue); | ||
printf("Get sw=%d value=%d\n", encoderSw, encoderValue); | printf("Get sw=%d value=%d\n", encoderSw, encoderValue); | ||
− | + | sleep(1); | |
} | } | ||
rotaryEncoderDeInit(); | rotaryEncoderDeInit(); | ||
+ | err: | ||
+ | system("rmmod "DRIVER_MODULE); | ||
return 0; | return 0; | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | API说明参考维基:[[Matrix API reference manual/zh|Matrix API reference manual]] <br> | |
− | + | ||
− | + | ||
− | + | ||
==相关资料== | ==相关资料== |
Latest revision as of 08:56, 10 November 2016
Contents
1 介绍
2 特性
File:Matrix-Rotary Encoder PCB.png
- 引脚说明:
名称 | 描述 |
3 工作原理
4 硬件连接
4.1 连接NanoPi NEO/NanoPi NEO Air
NanoPi M1和NanoPi NEO以及NanoPi NEO Air的前24Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,并且使用同一份代码。
参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_NEO
连接说明:
Matrix-Rotary_Encoder | NanoPi NEO |
R | Pin7 |
G | Pin8 |
B | Pin10 |
V | Pin4 |
G | Pin6 |
4.2 连接NanoPi M1
参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_m1
连接说明:
Matrix-Rotary_Encoder | NanoPi M1 |
R | Pin7 |
G | Pin8 |
B | Pin10 |
V | Pin4 |
G | Pin6 |
4.3 连接NanoPi 2
参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_2
连接说明:
Matrix-Rotary_Encoder | NanoPi 2 |
R | Pin7 |
G | Pin8 |
B | Pin10 |
V | Pin4 |
G | Pin6 |
4.4 连接NanoPi M2 / NanoPi 2 Fire
NanoPi M2和NanoPi 2 Fire的40 Pin引脚定义是一模一样的,所以它们操作Matrix配件的步骤是一样的,这里仅以NanoPi M2为例。
参考下图连接模块:
Matrix-Rotary_Encoder_nanopi_m2
连接说明:
Matrix-Rotary_Encoder | NanoPi M2 |
R | Pin7 |
G | Pin8 |
B | Pin10 |
V | Pin4 |
G | Pin6 |
4.5 连接NanoPC-T2/NanoPC-T3
由于NanoPC-T2跟NanoPC-T3的引脚是一样的,所以连接方式是一样的,这里仅以T2为例,参考下图连接模块:
Matrix-Rotary_Encoder_NanoPC-T2
连接说明:
Matrix-Rotary_Encoder | NanoPC-T2 |
R | Pin15 |
G | Pin16 |
B | Pin17 |
V | Pin29 |
G | Pin30 |
5 编译运行测试程序
启动开发板并运行Debian系统,进入系统后克隆Matrix代码仓库:
$ apt-get update && apt-get install git $ git clone https://github.com/friendlyarm/matrix.git
克隆完成后会得到一个名为matrix的目录。
编译并安装Matrix:
$ cd matrix $ make && make install
运行测试程序:
$ matrix-rotary_encoder
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
运行效果如下:
Get sw=0 value=1 Get sw=0 value=2 Get sw=0 value=3 Get sw=0 value=4 Get sw=0 value=5
旋转模块时,能读到对应的值。
6 代码说明
所有的开发板都共用一套Matrix代码,本模块的测试示例代码为matrix-rotary_encoder,内容如下:
int main(int argc, char ** argv) { int i = 0; int encoderSw = 0; int encoderValue = 0; int board; int swPin = GPIO_PIN(7); int siaPin = GPIO_PIN(8); int sibPin = GPIO_PIN(10); if ((board = boardInit()) < 0) { printf("Fail to init board\n"); return -1; } if (board == BOARD_NANOPI_T2) { swPin = GPIO_PIN(15); siaPin = GPIO_PIN(16); sibPin = GPIO_PIN(17); } system("modprobe "DRIVER_MODULE); if (rotaryEncoderInit(swPin, siaPin, sibPin)) { printf("Fail to init rotary encoder\n"); goto err; } signal(SIGINT, encoderHandler); for (i=0; i<ENCODER_READ_TIMES; i++) { rotaryEncoderRead(ENCODER_SW, &encoderSw); rotaryEncoderRead(ENCODER_VALUE, &encoderValue); printf("Get sw=%d value=%d\n", encoderSw, encoderValue); sleep(1); } rotaryEncoderDeInit(); err: system("rmmod "DRIVER_MODULE); return 0; }
API说明参考维基:Matrix API reference manual