Last active
September 7, 2021 09:50
-
-
Save dj1711572002/47d0090e92f010c5655bddcf245106cc to your computer and use it in GitHub Desktop.
Texas ADC ADS122U04 2CH Sampling on mbed LPC1768
This file contains 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
//----------------Texsus ADS122U04-------------------------- | |
#include "mbed.h" | |
Serial pc(USBTX,USBRX); | |
Serial device(p9, p10);//p9TX p10RX | |
DigitalOut myled(LED1); | |
InterruptIn DRDY(p8);//DataRdy Pin | |
//DigitalIn DRDY(p8);//DataRdy Pin | |
Timer t; | |
double ft1,ft2; | |
int RREGwait; | |
int flipN; | |
uint8_t RR0,RR1,RR2,RR3,RR4; | |
uint8_t d0,d1,d2,d3,d4,d5,d6; | |
double data1ch,data2ch; | |
int DRDYflag=0; | |
//==================InterruptIn DRDY==================== | |
void flip() { | |
flipN++; | |
if(flipN%2==0) | |
{ | |
//1CH-----Register00[7:4(MUX)3:1(GAIN)0(PGA_BYPASS)]-------------------------------------------------------------- | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x40);//WriteREGister 0x04 0x00(0000register00 Selected) | |
device.putc(0x0E);//7:0(0000 1110=0x0E)[7:4(0000)MUX AINp=AIN0,AINn=AIN1 3:1(111)GAIN128, 0:0(0)PGA enabled] | |
wait_us(50); | |
device.putc(0x55); | |
device.putc(0x10); | |
wait_us(10); | |
d0=device.getc(); | |
wait_us(10); | |
d1=device.getc(); | |
wait_us(10); | |
d2=device.getc(); | |
wait_us(10); | |
data1ch=(d0+d1*256+d2*65536)*0.000393391; | |
ft1=(double)t.read_us()/1000; | |
// pc.printf("%d,%8.3f\n\r",data1ch,ft); | |
} | |
else | |
{ | |
//2CH-----Register00[7:4(MUX)3:1(GAIN)0(PGA_BYPASS)]-------------------------------------------------------------- | |
wait_us(50); | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x40);//WriteREGister 0x04 0x00(0000register00 Selected) | |
device.putc(0x6E);//7:0(0110 1110=0x6E)[7:4(0110)MUX AINp=AIN2,AINn=AIN3 3:1(111)GAIN128, 0:0(0)PGA enabled] | |
wait_us(50); | |
device.putc(0x55); | |
device.putc(0x10); | |
wait_us(10); | |
d3=device.getc(); | |
wait_us(10); | |
d4=device.getc(); | |
wait_us(10); | |
d5=device.getc(); | |
wait_us(10); | |
data2ch=(d3+d4*256+d5*65536)*0.000393391; | |
//pc.printf("d0=%x,d1=%x,d2=%x,data=%d\n\r",d0,d1,d2,data); | |
ft2=(double)t.read_us()/1000; | |
//pc.printf("%d:CH2=%d,%8.3f\n\r",flipN,data2ch,ft); | |
pc.printf("%8.3f,%8.3f,%8.3f,%8.3f\n\r",data1ch,data2ch,ft1,ft2); | |
//myled = !myled; | |
//pc.printf("-------------DRDY Falled----------------\n\r"); | |
} | |
} | |
//=========================================================== | |
int main() { | |
pc.baud(460800); | |
device.baud(115200); | |
DRDY.fall(&flip); // attach the address of the flip function to the rising edge | |
//ADS122U04 RESET | |
device.putc(0x55); | |
device.putc(0x06); | |
wait_us(1000); | |
// | |
//================Register Setting============================================================================= | |
RREGwait=500;//RREG wait time usec 2*Tbaud(10usec) | |
//-----Register00[7:4(MUX)3:1(GAIN)0(PGA_BYPASS)]-------------------------------------------------------------- | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x40);//WriteREGister 0x04 0x00(0000register00 Selected) | |
device.putc(0x0E);//7:0(0000 1110=0x0E)[7:4(0000)MUX AINp=AIN0,AINn=AIN1 3:1(111)GAIN128, 0:0(0)PGA enabled] | |
wait_us(1000); | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x20);//Synchronization word 0x55 | |
wait_us(RREGwait); | |
if (device.readable()) | |
{ | |
RR0=device.getc(); | |
} | |
wait_us(1000); | |
pc.printf("\n\r"); | |
pc.printf("********0x0E:"); | |
pc.printf("RR0=%x\n\r",RR0); | |
//-----Register01[7:5(DataRate)4:(OperationMode)3:(ConversionMode)2:1(VREF)0:(TemperatureSensor mode)]--------- | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x42);// WriteREGister 0x04 0x02(0010register01 Selected) | |
device.putc(0xDE);//7:0(1101 1110=0xDE)[7:5(110)DataRate2000sps,4:(1)TURBO Mode, 3:(1)Continuous conversion mode,2:1(11)VDD-VSS VRef,0:(0)Temp disabled] | |
wait_us(1000); | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x22);//Synchronization word 0x55 訂正0x21=>0x22 2021/9/7 | |
wait_us(RREGwait); | |
if (device.readable()) | |
{ | |
RR1=device.getc(); | |
} | |
wait_us(1000); | |
pc.printf("*********0xA8:"); | |
pc.printf("RR1=%x\n\r",RR1); | |
//-----Register02[7:DRDY)6:(DCNT)5:4(CRC)3:(BCS)2:0(IDAC)]----------------------------------------------------- | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x44);// WriteREGister 0x04 0x04(0100register02 Selected) | |
device.putc(0x08);//7:0(0000 1000=0x08)[7:(0)DRDY,6:(0)DCNT disable, 5:4(00)) inverte,3:(1)BCS On,2:0(0)IDAC off] | |
wait_us(1000); | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x24);//Synchronization word 0x55 訂正0x22>0x24 2021/9/7 | |
wait_us(RREGwait); | |
if (device.readable()) | |
{ | |
RR2=device.getc(); | |
} | |
wait_us(1000); | |
pc.printf("********0x08:"); | |
pc.printf("RR2=%x\n\r",RR2); | |
//----Register03[7:5(I1MUX)4:2(I2MUX)1(RSERVED)0(AUTO)]---------------------------------------------------- | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x46);// WriteREGister 0x04 0x06(0110register03 Selected) | |
device.putc(0x00);//7:0(00000000=0x00)[7:5(000)default,4:2(000)default, 1:(0)Reserved,0:(0)Manual Read | |
wait_us(1000); | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x26);//Synchronization word 0x55 訂正0x23>0x26 2021/9/7 | |
wait_us(RREGwait); | |
if (device.readable()) | |
{ | |
RR3=device.getc(); | |
} | |
wait_us(1000); | |
pc.printf("*********0x00:"); | |
pc.printf("RR3=%x\n\r",RR3); | |
wait_us(1000); | |
//----Register04[7:(Reserved)6:(GPIO2DIR)5:(GPIO1DIR)4:(GPIO0DIR)3:(GPIO2SEL)2:(GPIO2DAT)1:(GPIO1DAT)0:(GPIODAT0)]---------------------------------------------------- | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x48);// WriteREGister 0x04 0x08(1000register04 Selected) | |
device.putc(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 | |
wait_us(1000); | |
device.putc(0x55);//Synchronization word 0x55 | |
device.putc(0x28);//Synchronization word 0x55 訂正0x24>0x28 2021/9/7 | |
wait_us(RREGwait); | |
if (device.readable()) | |
{ | |
RR4=device.getc(); | |
} | |
wait_us(1000); | |
pc.printf("*********0x48:"); | |
pc.printf("RR4=%x\n\r",RR4); | |
//Start/Sync | |
device.putc(0x55); | |
device.putc(0x08); | |
// wait_us(100);//************************このWAIT重要************************************ | |
t.start(); | |
//====================LOOP====================================== | |
while(1) | |
{ | |
wait_us(500);//5msec Period | |
//Start/Sync | |
// device.putc(0x55); | |
//device.putc(0x08); | |
//wait_us(100);//************************このWAIT重要************************************ | |
// wait_us(50); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment