EM9287是英創(chuàng)公司新近推出的,專門針對網(wǎng)絡(luò)通訊、智能終端等應(yīng)用的工控主板產(chǎn)品。EM9287可選擇預(yù)裝Linux-3.9.7操作系統(tǒng)。在Linux-3.9.7中,AD數(shù)據(jù)采集已采用新的工業(yè)IO(簡稱IIO)驅(qū)動程序框架,本文主要介紹應(yīng)用程序如何讀取相關(guān)的AD數(shù)據(jù)。此外,英創(chuàng)公司的另一款主流工控主板產(chǎn)品EM9280也升級到Linux-3.9.7,具有與EM9287完全相同的AD通道,因此本文所介紹的AD數(shù)據(jù)讀取方法同樣適用于EM9280。
EM9287支持2路單端模擬信號輸入AIN1和AIN2,輸入量程為 0V – 3.6V,信號管腳在CN1的17#和18#管腳。除了支持2路AD外,應(yīng)用程序還可讀取主板的CPU芯片iMX28的芯片當(dāng)前溫度,以了解設(shè)備的運(yùn)行情況。以下是測試程序:
#include
#include
#include
#include
#include
char iiotype[16][32] = {
''in_voltage0_raw'',
'in_voltage1_raw',
'in_voltage2_raw',
'in_voltage3_raw',
'in_voltage4_raw',
'in_voltage5_raw',
'in_voltage6_raw',
'in_voltage7_raw',
'in_temp8_raw',
'in_temp9_raw',
'in_voltage10_raw',
'in_voltage11_raw',
'in_voltage12_raw',
'in_voltage13_raw',
'in_voltage14_raw',
'in_voltage15_raw' };
// return = 0: voltage value from AIN1, unit: mV
// < 0: failed?
int read_AIN1(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[0]);
fp = fopen(filename, 'rt' );
if( fp==NULL )
{
printf('open %s fail!
', filename);
*fvoltage = 0.0;
ret = -1;
return ret;
}
fread( buf, 1, sizeof(buf), fp );
fclose(fp);
// convert to integer
sscanf( buf, '%d', &value );
*fvoltage = 0.9 * value;
return ret;
}
// return = 0: voltage value from AIN2, unit: mV
// < 0: failed?
int read_AIN2(float *fvoltage)
{
int value, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[1]);
fp = fopen(filename, 'rt' );
if( fp==NULL )
{
printf('open %s fail!
', filename);
*fvoltage = 0.0;
ret = -1;
return ret;
}
fread( buf, 1, sizeof(buf), fp );
fclose(fp);
// convert to integer
sscanf( buf, '%d', &value );
*fvoltage = 0.9 * value;
return ret;
}
// return = 0: temperature value from CPU, unit: K (Kalvin)
// < 0: failed?
int read_CPU_temperature(float *ftemp)
{
int v_ch9, v_ch8, ret = 0;
char filename[80];
FILE *fp;
char buf[20];
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[9]);
fp = fopen(filename, 'rt' );
if( fp==NULL )
{
printf('open %s fail!
', filename);
*ftemp = 0.0;
ret = -1;
return ret;
}
fread( buf, 1, sizeof(buf), fp );
fclose(fp);
// convert to integer
sscanf( buf, '%d', &v_ch9 );
sprintf( filename, '/sys/bus/iio/devices/iio:device0/%s', iiotype[8]);
fp = fopen(filename, 'rt' );
if( fp==NULL )
{
printf('open %s fail!
', filename);
*ftemp = 0.0;
ret = -1;
return ret;
}
fread( buf, 1, sizeof(buf), fp );
fclose(fp);
// convert to integer
sscanf( buf, '%d', &v_ch8 );
*ftemp = ((float)(v_ch9 - v_ch8) * 1012) / 4000;
return ret;
}
int main(int argc, char** argv)
{
int ret = 0;
float fvalue;
printf('EM9280/EM9287 IIO AD test v2.0
');
// read AIN1
ret = read_AIN1(&fvalue);
if(ret < 0)?
{
return ret;
}
printf('AIN1 = %.2f mV
', fvalue);
// read AIN2
ret = read_AIN2(&fvalue);
if(ret < 0)?
{
return ret;
}
printf('AIN2 = %.2f mV
', fvalue);
// read CPU temperature
ret = read_CPU_temperature(&fvalue);
if(ret < 0)?
{
return ret;
}
printf('CPU Temperature = %.1f K, (%.1f C degree)
', fvalue, (fvalue - 273));
return ret;
}
在控制臺(console)窗口,也可通過Linux命令查看IIO驅(qū)動的原始通道:
進(jìn)一步,可用cat命令讀取各個通道AD轉(zhuǎn)換的原始數(shù)據(jù),EM9280、EM9287內(nèi)部AD分辨率為12-bit,所以原始數(shù)據(jù)范圍為:0 – 4095。開發(fā)人員可通過以下命令來快速了解設(shè)備的AD數(shù)據(jù)狀況。
#cat in_voltage0_raw // 讀取CH0的原始數(shù)據(jù)
#cat in_voltage1_raw // 讀取CH1的原始數(shù)據(jù)
#cat in_temp8_raw // 讀取CPU溫度的原始數(shù)據(jù)
#cat in_temp9_raw // 讀取CPU溫度的原始數(shù)據(jù)
-
Linux
+關(guān)注
關(guān)注
87文章
11232瀏覽量
208956 -
嵌入式主板
+關(guān)注
關(guān)注
7文章
6085瀏覽量
35225
發(fā)布評論請先 登錄
相關(guān)推薦
評論