Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
SCP System_AD7194_SPI_NucleoL432KC_6CHdata_SerialOutPut
// AD7194 EVAL Board mbed NUCLEO 432KC SPIIF Program
// Differential 1Ch AIN1(+) AIN2(-)
//2019/03/23 Shinshu-Makers
//For SCP system version UP 2020/9/18
//0000AD7194_SPI_6CH_VB_L432KC_rev12.cc
#include "mbed.h"
//---------------------------------------------------
//AD7194 definition
SPI ad7194 (PA_7,PA_6,PA_5); //NUCLEO L432KC MOSI, MISO, SCLK
DigitalOut CS(PA_4); // Chip select
//ESP-NOW timing definition
Serial pc(USBTX, USBRX); // tx, rx for debug terminal
Serial atom(PA_9,PA_10);//TX,RX
InterruptIn pinint(PB_5);
DigitalOut pinout(PA_8);
//------------------------------------
DigitalOut myled(LED1);
//-----Timing In -----------------------
//DigitalOut timing(PA_8);//Timing signal
//DigitalOut rst(PB_5); //Reset signal
int m=0;//total data Counter
int n=0;//1sec data counter
int period;//microsec
int ti,ti_1,tmi,tmi_1; //period
int inter,inter_1;
int tstart=0;
int tnow=0;
volatile int flag;//edge
//DigitalOut ADReset(p15); // Pin 15 is reset line for AD9850
Timer t;
Timer tmicro;
int i,j,k;
int dnum=0;
int MA=1;//Moving Average
int data,data0,data1,data2 ;
int sdata,sdata0,sdata1,mdata0,mdata1,mdata2,sdata_1;
int condata0,condata1,condata2;
int cReg0,cReg1;
float mV[6][1000];
float mV0,mV1,mV2,mV3,mV4,mV5;
float mV0sum,mV1sum,mV2sum,mV3sum,mV4sum,mV5sum;
void ch_Change(int ch){
char chN;
switch (ch){
case 0:
chN=0x01;
break;
case 1:
chN=0x23;
break;
case 2:
chN=0x45;
break;
case 3:
chN=0x67;
break;
case 4:
chN=0x89;
break;
case 5:
chN=0xAB;
break;
}
//設定レジスタへ設定データ書き込み
ad7194.write(0x10);//設定レジスタ書き込み指定
ad7194.write(0x00) ;//設定レジスタ23-16bit書き込み=デフォルトのまま
ad7194.write(chN) ;//設定レジスタ15-8bit書き込み=1CH設定 AIN1+ AIN1-(=AIN2)
ad7194.write(0x17) ;//設定レジスタ7-0bit書き込み=BUF Gain128
//Config書き込み確認
ad7194.write(0x50);//Configレジスタ書き込み指定
condata0=ad7194.write(0x00) ;//MODEレジスタ23-16bit書き込み=連続変換モード+InternalClock4.8MHz
condata1=ad7194.write(0x00) ;//MODEレジスタ15-8bit書き込み=デフォルトのまま
condata2=ad7194.write(0x00) ;//MODEレジスタ7-0bit書き込み=4800Hz設定
ad7194.write(0x40);//status request
sdata=ad7194.write(0x0);//status read
//pc.printf("ch=%d,sdata=%x:condata:%x,%x,%x\n\r",ch,sdata,condata0,condata1,condata2);
}
//======================Interrupt==============================
//=============================================================
void flip()
{
tstart=t.read_us();
//atom.printf("%d\n\r",t.read_ms());
pinout= !pinout;
myled=1;
n=0;
}
//=============================================================
//=============================================================
int main()
{
atom.baud(115200);
pinint.rise(&flip);// Pin Interrupt flip
pc.baud(115200);
CS=0;//Chip Selct Low設定
//set_AD7194();
//MODEレジスタへモード設定データ書き込み
ad7194.write(0x08);//MODEレジスタ書き込み指定
ad7194.write(0x08) ;//MODEレジスタ23-16bit書き込み=連続変換モード+InternalClock4.8MHz
ad7194.write(0x00) ;//MODEレジスタ15-8bit書き込み=デフォルトのまま
ad7194.write(0x01) ;//MODEレジスタ7-0bit書き込み=4800Hz設定
//mode書き込み確認
ad7194.write(0x48);//MODEレジスタ書き込み指定
mdata0=ad7194.write(0x00) ;//MODEレジスタ23-16bit書き込み=連続変換モード+InternalClock4.8MHz
mdata1=ad7194.write(0x00) ;//MODEレジスタ15-8bit書き込み=デフォルトのまま
mdata2=ad7194.write(0x00) ;//MODEレジスタ7-0bit書き込み=4800Hz設定
//pc.printf("mdata:%x,%x,%x|n\r",mdata0,mdata1,mdata2);
//設定レジスタへ設定データ書き込み
ad7194.write(0x10);//設定レジスタ書き込み指定
ad7194.write(0x00) ;//設定レジスタ23-16bit書き込み=デフォルトのまま pseudo=0
ad7194.write(0x01) ;//設定レジスタ15-8bit書き込み=1CH設定 AIN1+ AIN1-(AIN2)
ad7194.write(0x17) ;//設定レジスタ7-0bit書き込み=BUF Gain128
//Config書き込み確認
ad7194.write(0x50);//Configレジスタ書き込み指定
condata0=ad7194.write(0x00) ;//MODEレジスタ23-16bit書き込み=連続変換モード+InternalClock4.8MHz
condata1=ad7194.write(0x00) ;//MODEレジスタ15-8bit書き込み=デフォルトのまま
condata2=ad7194.write(0x00) ;//MODEレジスタ7-0bit書き込み=4800Hz設定
//pc.printf("mdata:%x,%x,%x,condata:%x,%x,%x|n\r",mdata0,mdata1,mdata2,condata0,condata1,condata2);
t.start();
tmicro.start();
i=0;
m=0;
flag=0;//edge 0
while(1)
{
ti=t.read_us();
period=ti-ti_1;
if (period>12500)
{
ti_1=ti;
for (i=0;i<6;i++)
{
dnum++;//data set counter
ch_Change(i%6);//ch change configuration
ad7194.write(0x40);//status request
sdata_1=sdata0;
sdata0=ad7194.write(0x0);//status read
ad7194.write(0x58);
data0=ad7194.write(0x0);
data1=ad7194.write(0x0);
data2=ad7194.write(0x0);
mV[i%6][dnum%MA]=((float)data0*65536+(float)data1*256+(float)data2)/5084;
mV[i%6][dnum%MA]=((int)data0*65536+(int)data1*256+(int)data2)/5084;
//mV[i%6][dnum%MA]= (float)(((int)data0 << 16) + ((int)data1 << 8) +(int)data2) ;
wait_us(1000);//WaitいれないとCH切り替えできない
//pc.printf("==============%4.1f,%x,%x,%x,%4.4f\n\r",mV,data0,data1,data2,t.read());
//pc.printf("mV[%d][%d]=%5.2f\n\r",i%6,dnum%MA,mV[i%6][dnum%MA]);
}
//==========MOving Average========================
mV0sum=0;
mV1sum=0;
mV2sum=0;
mV3sum=0;
mV4sum=0;
mV5sum=0;
for(j=0;j<MA;j++)
{
mV0sum=mV0sum+mV[0][j];
mV1sum=mV1sum+mV[1][j];
mV2sum=mV2sum+mV[2][j];
mV3sum=mV3sum+mV[3][j];
mV4sum=mV4sum+mV[4][j];
mV5sum=mV5sum+mV[5][j];
}
mV0=(mV0sum/MA);
mV1=(mV1sum/MA);
mV2=(mV2sum/MA);
mV3=(mV3sum/MA);
mV4=(mV4sum/MA);
mV5=(mV5sum/MA);
m++;//Total data Counter
n++;//1sec data counter
tmi=tmicro.read_us()-tstart;
myled=0;
//pc.printf("A:%d,%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%d\n\r",n,mV1,mV2,mV3,mV4,mV5,mV0,tmi);
atom.printf("A,%d,%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%d\n\r",n,mV1,mV2,mV3,mV4,mV5,mV0,tmi);
// For CPLT Check
//atom.printf("%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%d\n\r",mV1,mV2,mV3,mV4,mV5,mV0,tmi);
//pc.printf("%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%4.0f,%d\n\r",mV1,mV2,mV3,mV4,mV5,mV0,tmi);
}//if end
} //while end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.