その際のサンプルプログラムについて説明します。
C言語はwiringpiを使います。
#include "common.h"
#include "MCUGear.h"
#include "MCUGearBase.h"
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <wiringPiSPI.h>
#include <stdio.h>
int read12bit2chAD(MCUGear *mcugear, int spichan, char ch);
void IOSimpleSPI(MCUGear *mcugear);
int main(void){
int spiSpeed = 1000000; //1MHz
int spiChan = 0; //CE0
int data0 = 0;
int data1 = 0;
int data[8];
int sw_data = 0;
if(wiringPiSetup() == -1)//wiringpiのセットアップ
return 1;
if(wiringPiSPISetup(spiChan, spiSpeed)== -1) //set CE0 and 1MHz//SPIのセットアップ
return 1;
pinMode(pin, OUTPUT);
pinMode(sw, INPUT);
pinMode(outpin[0], OUTPUT);
pinMode(outpin[1], OUTPUT);
pinMode(outpin[2], OUTPUT);
pinMode(outpin[3], OUTPUT);
pinMode(outpin[4], OUTPUT);
pinMode(outpin[5], OUTPUT);
initBase(); //initialize Baseboard //ベースボード初期化
MCUGear AD2chM = MCUGear(N_SCL_SCL_VSS, 4);
//モジュール基板の裏に設定したアドレスとSPIバス 4ピン
//任意名称「AD2chM」という名前でモジュールを定義しています
//Set IO --------------------------
IOSimpleSPI(&AD2chM);
//GPIOのどの端子をモジュールをどの端子に当てはめるかを定義する
//---------------------------------------------------------------------
while(1){
//2ch AD module
AD2chM.connectModule(); //connect--- //GPIOからモジュールまで配線を再構築
data0 = read12bit2chAD(&AD2chM ,spiChan ,0);//AD0chをAD変換
data1 = read12bit2chAD(&AD2chM ,spiChan ,1);//AD1chをAD変換
AD2chM.disconnectModule(); //disconnect---//GPIOからモジュールまで配線を切断
printf("AD0 = %d, AD1 = %d\n",data0, data1);//ターミナルに表示
}
return 0;
}
void IOSimpleSPI(MCUGear *mcugear){
unsigned char fio[12];
// detect
mcugear->detect_module(fio);//モジュールの取り付け位置を把握
delay(100);
printf("fio[0] = %d",fio[0]);
//1:MISO 13
//2:MOSI 12
//3:SCK 14
//4:CE0 10
mcugear->savePinSetting(0, IO_13, IO_REG_IN_DIR, fio[0]);//input モジュールの0番端子にMISOを割り当てる
mcugear->savePinSetting(1, IO_12, IO_REG_OUT_DIR, fio[1]);//output モジュールの1番にMOSIを割り当てる
mcugear->savePinSetting(2, IO_14, IO_REG_OUT_DIR, fio[2]);//output モジュールの2番にSCKを割り当てる
mcugear->savePinSetting(3, IO_10, IO_REG_OUT_DIR, fio[3]);//output モジュールの3番にCE0を割り当てる
#ifdef BOOST_MODE
mcugear->makeCircuit();//追々説明します
#endif
}
//MCP3202とのSPI通信 通常配線したデジタル回路として扱います
int read12bit2chAD(MCUGear *mcugear, int spichan,char ch){
int value = 0;
unsigned char spiBuff[3];
if(ch == 0)
spiBuff[0] = 0x18;
else if(ch == 1)
spiBuff[0] = 0x1C;
else
return -1;
spiBuff[1] = 0x00;
spiBuff[2] = 0x00;
wiringPiSPIDataRW(spichan,spiBuff,3);
//printf("%x %x %x\n",spiBuff[0],spiBuff[1],spiBuff[2]);
value = (spiBuff[1] <<4) + (spiBuff[2] >>4);
//printf("%d\n", value);
return value;
}
0 件のコメント:
コメントを投稿