Difference between revisions of "Matrix - Analog to Digital Converter"
(→Download Matrix Source Code) |
(→Download Matrix Source Code) |
||
Line 70: | Line 70: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples. | If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples. | ||
+ | |||
+ | ==与NanoPi 2连接使用== | ||
+ | ===硬件连接=== | ||
+ | 参考下图连接模块Matrix-Analog_to_Digital_Converter和NanoPi 2:<br> | ||
+ | [[File:Matrix-Analog_to_Digital_Converter_nanopi_2.jpg|frameless|600px|Matrix-Analog_to_Digital_Converter_nanopi_2]] | ||
+ | |||
+ | 连接说明: | ||
+ | {| class="wikitable" | ||
+ | |- | ||
+ | |Matrix-Analog_to_Digital_Converter || NanoPi 2 | ||
+ | |- | ||
+ | |SDA || Pin3 | ||
+ | |- | ||
+ | |SCL || Pin5 | ||
+ | |- | ||
+ | |5V || Pin4 | ||
+ | |- | ||
+ | |GND || Pin6 | ||
+ | |} | ||
+ | |||
+ | ===编译测试程序=== | ||
+ | 进入Matrix代码仓库,切换到nanopi2分支 | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ cd matrix | ||
+ | $ git checkout nanopi2 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 编译Matrix配件代码 | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ make CROSS_COMPILE=arm-linux- clean | ||
+ | $ make CROSS_COMPILE=arm-linux- | ||
+ | $ make CROSS_COMPILE=arm-linux- install | ||
+ | </syntaxhighlight> | ||
+ | 注意:请确保你的主机PC当前使用的交叉编译器为NanoPi 2配套的arm-linux-gcc-4.9.3。<br> | ||
+ | 编译成功后库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-Analog_to_Digital_Converter对应的测试程序为matrix-adc。<br> | ||
+ | 硬件驱动模块位于modules目录下,对应的驱动源码都包含在在NanoPi 2的Linux内核仓库里:https://github.com/friendlyarm/linux-3.4.y.git <br> | ||
+ | |||
+ | ===运行测试程序=== | ||
+ | 将带有Debian系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。<br> | ||
+ | 假设rootfs分区的挂载路径为/media/rootfs,执行以下命令将Matrix的硬件驱动、库文件和测试程序拷贝到NanoPi 2的文件系统上。<br> | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ cp modules /media/rootfs/ -r | ||
+ | $ cp install/lib/* /media/rootfs/lib/ -d | ||
+ | $ cp install/usr/bin/* /media/rootfs/usr/bin/ | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 将SD卡重新插入NanoPi 2,上电启动,在Debian的shell终端中执行以下命令加载硬件驱动。<br> | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ cd /modules | ||
+ | $ insmod pcf8591.ko | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | 运行模块Matrix-Analog_to_Digital_Converter的测试程序。<br> | ||
+ | <syntaxhighlight lang="bash"> | ||
+ | $ matrix-adc | ||
+ | </syntaxhighlight> | ||
+ | 注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。<br> | ||
+ | |||
+ | 运行效果如下:<br> | ||
+ | [[File:matrix-adc_result.png|frameless||matrix-adc_result]] <br> | ||
+ | 程序会读取通道0的值。 | ||
+ | |||
+ | ===代码展示=== | ||
+ | <syntaxhighlight lang="c"> | ||
+ | int main(int argc, char ** argv) | ||
+ | { | ||
+ | int i = 0; | ||
+ | int value = 0; | ||
+ | int channel = 0; | ||
+ | |||
+ | if (argc == 2) { | ||
+ | channel = atoi(argv[1]); | ||
+ | } | ||
+ | |||
+ | for (i=0; i<ADC_READ_TIMES; i++) { | ||
+ | if (pcf8591Read(channel, &value) != -1) { | ||
+ | printf("channel%d value=%d\n", channel, value); | ||
+ | } else { | ||
+ | printf("Fail to get channel%d value\n", channel); | ||
+ | } | ||
+ | usleep(10000); | ||
+ | } | ||
+ | return 0; | ||
+ | } | ||
+ | </syntaxhighlight> | ||
==Connect to NanoPi== | ==Connect to NanoPi== |
Revision as of 07:51, 19 February 2016
Contents
1 Introduction
- The Matrix-Analog_to_Digital_Converter is a single-chip, single-supply low-power 8-bit CMOS data acquisition device.
- It utilizes the PCF8591 chip with four analog inputs, one analog output and a serial I2C-bus interface. Three address pins A0, A1 and A2 are used for programming the hardware address, allowing
the use of up to eight devices connected to the I2C-bus without additional hardware. Address, control and data to and from the device are transferred serially via the two-line bidirectional I2C-bus. The maximum conversion rate is given by the maximum speed of the I2C-bus.
- The I2C hardware address is configured to 1001000x
- The operating supply voltage is from 2.5V to 6.0V,Among the 2.54 mm spacing pin header the 5V pin is the supply voltage. If you need 0 - 3.3V analog signals and keep the acquisition resolution you can connect it to a 3.3V supply voltage.
2 Features
- Wide range supply voltage: 2.5V - 6.0V
- I2C interface: 3.3V/5V
- 8-bit A/D x 4
- 8-bit D/A x 1
- Small and easy to be used in various situations
- 2.54 mm spacing pin
- PCB dimension (mm): 16 x 24
- Pin Description:
Pin | Description |
SDA | I2C SDA |
SCL | I2C SCL |
5V | Supply Voltage 5V |
GND | Ground |
AOUT | Analog Output |
A3 | Analog Input3 |
A2 | Analog Input2 |
A1 | Analog Input1 |
A0 | Analog Input0 |
3 Basic Device Operation
The PCF8591's I2C-bus is for bidirectional, two-line communication between different ICs or modules. The two lines are a Serial DAta line (SDA) and a Serial CLock line (SCL). Both lines must
be connected to a positive supply via a pull-up resistor. Data transfer may be initiated only when the bus is not busy. The AINT0 - AINT4 pins are analog inputs which can be configured to single-ended or differential inputs. The supply voltage is 0 - VDD.
1. Each PCF8591 device in an I2C-bus system is activated by sending a valid address to the device. The address consists of a fixed part and a programmable part. The programmable part must be set according to the address pins A0, A1 and A2. The address is always sent as the first byte after the start condition in the I2C-bus protocol. The last bit of the address byte is the read/write-bit which sets the direction of the following data transfer.
2. The second byte sent to a PCF8591 device is stored in its control register and is required to control the device function. The upper nibble of the control register is used for enabling the analog output, and for programming the analog inputs as single-ended or differential inputs. The lower nibble selects one of the analog input channels defined by the upper nibble.
3. The on-chip D/A converter and a high-gain comparator are used temporarily during an A/D conversion cycle. An A/D conversion cycle is always started after sending a valid read mode address to a PCF8591 device. The A/D conversion cycle is triggered at the trailing edge of the acknowledge clock pulse and is executed while transmitting the result of the previous conversion。
4 Download Matrix Source Code
All the matrix modules' code samples are open source. They are maintained on GitHub - https://github.com/friendlyarm/matrix.git
Each branch in this hub contains the matrix modules' code samples for a board that the matrix modules can work with.
- The nanopi branch contains the matrix modules' code samples for the NanoPi
- The nanopi 2 branch contains the matrix modules' code samples for the NanoPi 2
- The tiny4412 branch contains the matrix modules' code samples for the Tiny4412
- The raspberrypi branch contains the matrix modules' code samples for the RaspberryPi
Please follow the steps below to get the source code:
Install the git utility on a PC running Ubuntu14.04
$ sudo apt-get install git
Clone the matrix code from GitHub
$ git clone https://github.com/friendlyarm/matrix.git
If this is successful a "matrix" directory will be generated, which will contain all the matrix modules' code samples.
5 与NanoPi 2连接使用
5.1 硬件连接
参考下图连接模块Matrix-Analog_to_Digital_Converter和NanoPi 2:
连接说明:
Matrix-Analog_to_Digital_Converter | NanoPi 2 |
SDA | Pin3 |
SCL | Pin5 |
5V | Pin4 |
GND | Pin6 |
5.2 编译测试程序
进入Matrix代码仓库,切换到nanopi2分支
$ cd matrix $ git checkout nanopi2
编译Matrix配件代码
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
注意:请确保你的主机PC当前使用的交叉编译器为NanoPi 2配套的arm-linux-gcc-4.9.3。
编译成功后库文件位于install/lib目录下,而测试程序则位于install/usr/bin目录下,模块Matrix-Analog_to_Digital_Converter对应的测试程序为matrix-adc。
硬件驱动模块位于modules目录下,对应的驱动源码都包含在在NanoPi 2的Linux内核仓库里:https://github.com/friendlyarm/linux-3.4.y.git
5.3 运行测试程序
将带有Debian系统的SD卡插入一台运行Linux的电脑,可以挂载SD卡上的boot和rootfs分区。
假设rootfs分区的挂载路径为/media/rootfs,执行以下命令将Matrix的硬件驱动、库文件和测试程序拷贝到NanoPi 2的文件系统上。
$ cp modules /media/rootfs/ -r $ cp install/lib/* /media/rootfs/lib/ -d $ cp install/usr/bin/* /media/rootfs/usr/bin/
将SD卡重新插入NanoPi 2,上电启动,在Debian的shell终端中执行以下命令加载硬件驱动。
$ cd /modules $ insmod pcf8591.ko
运行模块Matrix-Analog_to_Digital_Converter的测试程序。
$ matrix-adc
注意:此模块并不支持热插拔,启动系统前需要确保硬件连接正确。
5.4 代码展示
int main(int argc, char ** argv) { int i = 0; int value = 0; int channel = 0; if (argc == 2) { channel = atoi(argv[1]); } for (i=0; i<ADC_READ_TIMES; i++) { if (pcf8591Read(channel, &value) != -1) { printf("channel%d value=%d\n", channel, value); } else { printf("Fail to get channel%d value\n", channel); } usleep(10000); } return 0; }
6 Connect to NanoPi
6.1 Preparations
Please install a Debian on a NanoPi and an appropriate cross compiler on a PC. Please refer to wiki:NanoPi
Compile a NanoPi kernel. Note: please use the kernel's source code from the nanopi-v4.1.y-matrix branch.
$ git clone https://github.com/friendlyarm/linux-4.x.y.git $ cd linux-4.x.y $ git checkout nanopi-v4.1.y-matrix $ make nanopi_defconfig $ touch .scmversion $ make
6.2 Hardware Connection
Please refer to the following connection diagram to connect the Matrix-Analog_to_Digital_Converter to the NanoPi
Connection Details:
Matrix-Analog_to_Digital_Converter | NanoPi |
SDA | Pin3 |
SCL | Pin5 |
5V | Pin4 |
GND | Pin6 |
6.3 Compile Test Program
Please login the matrix hub and enter the nanopi branch
$ cd matrix $ git checkout nanopi
Compile the matrix code
$ make CROSS_COMPILE=arm-linux- clean $ make CROSS_COMPILE=arm-linux- $ make CROSS_COMPILE=arm-linux- install
Note: please make sure to install the cross compiler "arm-linux-gcc-4.4.3" on your PC, which is used to compile files for the NanoPi-Debian.
Generated library files are under the "install/lib" directory. Applications are under the "install/usr/bin" directory. The test program for the "Matrix-Analog_to_Digital_Converter" module is "matrix-adc".
6.4 Run Test Program
Please copy the library files and test program to the NanoPi
$ cp install/usr/bin/* nanopi_rootfs/usr/bin/ $ cp install/lib/* nanopi_rootfs/lib/ -d
Power on the NanoPi and run the following command in Debian's terminal
Note: this module is not plug and play therefore before running the module please make sure it is connected to a NanoPi.
$ matrix-adc
6.5 Code Sample
int main(int argc, char ** argv) { int devFD; int data, channel, mode; if ((devFD = pcf8591Init()) == -1) { printf("Fail to init pcf8591\n"); return -1; } if (pcf8591SetCtrl(devFD, PCF8591_INIT_AD_CONTROL) == -1) { printf("Fail to Set pcf8591 control AD\n"); pcf8591DeInit(devFD); return -1; } mode = 0; printf("pcf8591 working as AD in mode%d\n",mode); for (channel = PCF8591_AIN_CHANNEL0; channel <= PCF8591_AIN_CHANNEL3; channel++) { data = pcf8591Read(devFD, mode, channel); printf("Channel%d's value: %d\n",channel,data); } pcf8591DeInit(devFD); return 0; }
7 Connect to Tiny4412
7.1 Preparations
Please refer to the Tiny4412's user's manual to install a UbuntuCore on the Tiny4412 and install an appropriate cross compiler on a PC.
Note: only the Tiny4412SDK-1506 carrier board can work with this module.
7.2 Hardware Connection
Please refer to the following diagram to connect the Matrix-Analog_to_Digital_Converter to the Tiny4412
Connection Details:
Matrix-Analog_to_Digital_Converter | NanoPi |
SDA | CON18 SDA |
SCL | CON18 SCL |
5V | CON18 5V |
GND | CON18 GND |
7.3 Compile Test Program
Please login the Matrix hub and enter the matrix-tiny4412 branch
$ cd matrix $ git checkout tiny4412
Compile the matrix code
$ make CROSS_COMPILE=arm-linux-gnueabihf- clean $ make CROSS_COMPILE=arm-linux-gnueabihf- $ make CROSS_COMPILE=arm-linux-gnueabihf- install
Note: please make sure to install the cross compiler "arm-linux-gnueabihf-gcc-4.7.3" on your PC, which is used to compile files for the Tiny4412-UbuntuCore.
Generated library files are under the "install/lib" directory. Applications are under the "install/usr/bin" directory. The test program for the "Matrix-Analog_to_Digital_Converter" module is "matrix-adc".
7.4 Run Test Program
Please copy the library files and test program to the Tiny4412
$ cp install/usr/bin/* tiny4412_rootfs/usr/bin/ $ cp install/lib/* tiny4412_rootfs/lib/ -d
Power on the Tiny4412 and run the following command in UbuntuCore's terminal
$ matrix-adc
7.5 Code Sample
int main(int argc, char ** argv) { int devFD; int data, channel, mode; if ((devFD = pcf8591Init()) == -1) { printf("Fail to init pcf8591\n"); return -1; } if (pcf8591SetCtrl(devFD, PCF8591_INIT_AD_CONTROL) == -1) { printf("Fail to Set pcf8591 control AD\n"); pcf8591DeInit(devFD); return -1; } mode = 0; printf("pcf8591 working as AD in mode%d\n",mode); for (channel = PCF8591_AIN_CHANNEL0; channel <= PCF8591_AIN_CHANNEL3; channel++) { data = pcf8591Read(devFD, mode, channel); printf("Channel%d's value: %d\n",channel,data); } pcf8591DeInit(devFD); return 0; }
8 Connect to RaspberryPi
9 Connect to Arduino
10 Resources