-
-
Save dj1711572002/416c9cf6f21a4b79d137c6bd7d5a8055 to your computer and use it in GitHub Desktop.
NucleoL432KC- ADS122U04 uart1 drive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "HardwareSerial.h" | |
| //For Teensy4.1 | |
| //----------------Texsus ADS122U04-------------------------- | |
| int DRDY = 23; //DataRdy Pin 23 | |
| long ft1, ft2; | |
| int RREGwait; | |
| int flipN; | |
| uint8_t RR0, RR1, RR2, RR3, RR4; | |
| uint8_t d0, d1, d2, d3, d4, d5, d6; | |
| long data1chi, data2chi; | |
| double data1ch, data2ch; | |
| int DRDYflag = 0; | |
| int res; | |
| int cd=200; | |
| HardwareSerial Serial1(D0,D1); | |
| //--------------toint32----------------------------- | |
| int Toint24(byte d0, byte d1, byte d2) | |
| { | |
| long val; | |
| if ((d2 & B10000000) == B10000000) | |
| { //最上位ビットたっていればマイナス | |
| val=(d0+d1*256+d2*256*256)-0xFFFFFF+1; | |
| } | |
| else | |
| { | |
| val = d0+d1*256+d2*256*256; | |
| } | |
| return val; | |
| } | |
| //-------------------------------------------------- | |
| void key() | |
| { | |
| Serial.println("Input 'a' key"); | |
| char c=' '; | |
| while(c!='a'){ | |
| c=Serial.read(); | |
| } | |
| //Serial.clear(); | |
| } | |
| int readReg(int n) | |
| { | |
| //Serial.println("In readReg"); | |
| int RREGwait=1200; | |
| int regN=0; | |
| int RR; | |
| if(n==0){regN=0x20;} | |
| if(n==1){regN=0x22;} | |
| if(n==2){regN=0x24;} | |
| if(n==3){regN=0x26;} | |
| if(n==4){regN=0x28;} | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(regN);//Synchronization word 0x55 Read REGister00=0010 0000=0x20 | |
| delayMicroseconds(RREGwait); | |
| int flag=0; | |
| while (flag==0) | |
| { | |
| if(Serial1.available()>0) | |
| { | |
| RR = Serial1.read(); | |
| delayMicroseconds(RREGwait); | |
| Serial.printf("readRegister%x=>[0x%x]",n,RR); | |
| Serial.println(); | |
| flag=1; | |
| } | |
| } | |
| //Serial5.clear(); | |
| return RR; | |
| } | |
| void setup() { | |
| // シリアル設定 | |
| Serial.begin(115200);//USB | |
| Serial1.begin(115200.); //Teensy4.1 RX5-ADS122 RX5(21)-TX,TX5(20)-RX | |
| delay(5000); | |
| Serial.println("-----------------START SET UP--------------"); | |
| delayMicroseconds(1000); | |
| //key(); | |
| //=============ADS122U04レジスタ初期設定========================================== | |
| //ADS122U04 RESET | |
| Serial1.write(0x55); | |
| Serial.print("write 0x55"); | |
| delayMicroseconds(cd); | |
| Serial1.write(0x06); | |
| Serial.print("write 0x06"); | |
| delayMicroseconds(1000); | |
| res= readReg(0); | |
| //================Register Setting============================================================================= | |
| RREGwait = 1000; //RREG wait time usec 2*Tbaud(10usec) | |
| //-----Register00[7:4(MUX)3:1(GAIN)0(PGA_BYPASS)]-------------------------------------------------------------- | |
| Serial.println("Register0 writing"); | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(0x40);//WriteREGister 0x04 0x00(0000register00 Selected) | |
| delayMicroseconds(cd); | |
| //Serial5.write(0x0E);//7:0(0000 1110=0x0E)[7:4(0000)MUX AINp=AIN0,AINn=AIN1 3:1(111)GAIN128, 0:0(0)PGA enabled] | |
| Serial1.write(0x0C);//7:0(0000 1100=0x0C)[7:4(0000)MUX AINp=AIN0,AINn=AIN1 3:1(110)GAIN64, 0:0(0)PGA enabled] | |
| delayMicroseconds(1000); | |
| res=readReg(0); | |
| //-----Register01[7:5(DataRate)4:(OperationMode)3:(ConversionMode)2:1(VREF)0:(TemperatureSensor mode)]--------- | |
| Serial.println("Register1 writing"); | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(0x42);// WriteREGister 0x04 0x02(0010register01 Selected) | |
| delayMicroseconds(cd); | |
| Serial1.write(0xCE);//7:0(1100 1110=0xCE)[7:5(110)DataRate1000sps,4:(0)Normal Mode, 3:(1)Continuous conversion mode,2:1(11)VDD-VSS VRef,0:(0)Temp disabled] | |
| //Serial5.write(0xAE);//7:0(1010 1110=0xAE)[7:5(101)DataRate600sps,4:(0)Normal Mode, 3:(1)Continuous conversion mode,2:1(11)VDD-VSS VRef,0:(0)Temp disabled] | |
| //Serial5.write(0x8E);//7:0(1000 1110=0x8E)[7:5(100)DataRate330sps,4:(0)Normal Mode, 3:(1)Continuous conversion mode,2:1(11)VDD-VSS VRef,0:(0)Temp disabled] | |
| //Serial5.write(0x4E);//7:0(0100 1110=0x0E)[7:5(010)DataRate90sps,4:(0)Normal Mode, 3:(1)Continuous conversion mode,2:1(11)VDD-VSS VRef,0:(0)Temp disabled] | |
| delayMicroseconds(1000); | |
| res=readReg(1); | |
| // key(); | |
| //-----Register02[7:DRDY)6:(DCNT)5:4(CRC)3:(BCS)2:0(IDAC)]----------------------------------------------------- | |
| Serial.println("Register2 writing"); | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(0x44);// WriteREGister 0x04 0x04(0100register02 Selected) | |
| delayMicroseconds(cd); | |
| Serial1.write(0x00);//7:0(0000 1000=0x08)[7:(0)DRDY,6:(0)DCNT disable, 5:4(00)) inverte,3:(1)BCS Off(0),2:0(0)IDAC off] | |
| delayMicroseconds(1000); | |
| res=readReg(2); | |
| //----Register03[7:5(I1MUX)4:2(I2MUX)1(RSERVED)0(AUTO)]---------------------------------------------------- | |
| Serial.println("Register3 writing"); | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(0x46);// WriteREGister 0x04 0x06(0110register03 Selected) | |
| delayMicroseconds(cd); | |
| Serial1.write(0x00);//7:0(00000000=0x00)[7:5(000)default,4:2(000)default, 1:(0)Reserved,0:(0)Manual Read | |
| delayMicroseconds(1000); | |
| res=readReg(3); | |
| delayMicroseconds(1000); | |
| //----Register04[7:(Reserved)6:(GPIO2DIR)5:(GPIO1DIR)4:(GPIO0DIR)3:(GPIO2SEL)2:(GPIO2DAT)1:(GPIO1DAT)0:(GPIODAT0)]---------------------------------------------------- | |
| Serial.println("Register4 writing"); | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(0x48);// WriteREGister 0x04 0x08(1000register04 Selected) | |
| delayMicroseconds(cd); | |
| Serial1.write(0x48);//7:0(0100 1000=0x08)[6:(1)GPIO2output,5:(0)GPIO1Input, 4:(0)GPIO0Input,3:(1)GPIO2SEL DRDY,2:(0)GPIO2DAT Low,1:(0)GPIO1DAT Low,0:(0)GPIO0DAT Low | |
| delayMicroseconds(1000); | |
| res=readReg(4); | |
| Serial.println("CHECK Written Registers"); | |
| delay(5000); | |
| //key(); | |
| //Serial.println("====Start/Sync Go============="); | |
| //Start/Sync | |
| Serial1.write(0x55); | |
| delayMicroseconds(cd); | |
| Serial1.write(0x08); | |
| delayMicroseconds(100);//************************このWAIT重要************************************ | |
| //ピン割り込み設定 | |
| pinMode(D3, INPUT_PULLUP); | |
| //attachInterrupt(digitalPinToInterrupt(2),呼び出される関数,FALLING); | |
| attachInterrupt(D3, flip,FALLING);//DRDY 下がりエッジで割り込み関数flipへ飛ぶ | |
| } | |
| void loop() { | |
| // put your main code here, to run repeatedly: | |
| delayMicroseconds(50000);//5msec Period | |
| Serial.print("*"); | |
| } | |
| //==================InterruptIn DRDY==================== | |
| void flip() { | |
| flipN++; | |
| //delayMicroseconds(100000); | |
| if (flipN % 2 == 0 )//偶数回は1CH,奇数回は2CH Ch間ずれ1.4msec 周期2.65msec 370Hz | |
| { | |
| //1CH-----Register00[7:4(MUX)3:1(GAIN)0(PGA_BYPASS)]-------------------------------------------------------------- | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| Serial1.write(0x40);//WriteREGister 0x04 0x00(0000register00 Selected) | |
| Serial1.write(0x0E);//7:0(0000 1110=0x0E)[7:4(0000)MUX AINp=AIN0,AINn=AIN1 3:1(111)GAIN128, 0:0(0)PGA enabled] | |
| //Serial5.write(0x0C);//7:0(0000 1100=0x0c)[7:4(0000)MUX AINp=AIN0,AINn=AIN1 3:1(110)GAIN64, 0:0(0)PGA enabled] | |
| delayMicroseconds(50); | |
| Serial1.write(0x55); | |
| delayMicroseconds(cd); | |
| Serial1.write(0x10); | |
| delayMicroseconds(10); | |
| d0 = Serial1.read(); | |
| delayMicroseconds(10); | |
| d1 = Serial1.read(); | |
| delayMicroseconds(10); | |
| d2 = Serial1.read(); | |
| delayMicroseconds(10); | |
| data1chi=Toint24(d0,d1,d2); | |
| //Serial.printf("[CH1]%d:d0=%x,d1=%x,d2=%x,data=%d\n\r",micros(),d0,d1,d2,data1chi); | |
| ft1 = (int)(micros()); | |
| //Serial.printf("%d,%6.2f\n\r",data1chi,ft1); | |
| } | |
| else | |
| { | |
| //2CH-----Register00[7:4(MUX)3:1(GAIN)0(PGA_BYPASS)]-------------------------------------------------------------- | |
| delayMicroseconds(50); | |
| Serial1.write(0x55);//Synchronization word 0x55 | |
| delayMicroseconds(cd); | |
| Serial1.write(0x40);//WriteREGister 0x04 0x00(0000register00 Selected) | |
| delayMicroseconds(cd); | |
| Serial1.write(0x6E);//7:0(0110 1110=0x6E)[7:4(0110)MUX AINp=AIN2,AINn=AIN3 3:1(111)GAIN128, 0:0(0)PGA enabled] | |
| //Serial5.write(0x60);//7:0(0110 0000=0x60)[7:4(0110)MUX AINp=AIN2,AINn=AIN3 3:1(000)GAIN1, 0:0(0)PGA enabled] | |
| delayMicroseconds(100); | |
| Serial1.write(0x55); | |
| delayMicroseconds(cd); | |
| Serial1.write(0x10); | |
| delayMicroseconds(10); | |
| d3 = Serial1.read(); | |
| delayMicroseconds(10); | |
| d4 = Serial1.read(); | |
| delayMicroseconds(10); | |
| d5 = Serial1.read(); | |
| delayMicroseconds(10); | |
| data2chi=Toint24(d3,d4,d5); | |
| //Serial.printf("[CH2]%d:d3=%x,d4=%x,d5=%x,data=%4.4f\n\r",micros(),d3,d4,d5,data2ch); | |
| ft2 = (int)(micros()); | |
| Serial.printf("%d,%d,%d,%d\n\r",data1chi,data2chi,ft1,ft2); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment