-
-
Save stickbreaker/60a7305b9eb8afb6e9748dc1b6d5e566 to your computer and use it in GitHub Desktop.
#include <Wire.h> | |
#define DEVID 0x6D | |
int scan(bool display,bool search){ | |
if(search){ | |
uint8_t pinlist[]={0,2,4,5,12,13,14,15,16,17,18,19,21,22,23,25,26,27,32,33}; | |
for(uint8_t _sda =0; _sda<sizeof(pinlist); _sda++){ | |
Serial.printf("\n"); | |
for(uint8_t _scl=0; _scl<sizeof(pinlist); _scl++){ | |
if(_scl==_sda) continue; | |
Serial.printf(" (%02d,%02d)",pinlist[_sda],pinlist[_scl]); | |
if(Wire.begin(pinlist[_sda],pinlist[_scl],100000)){ | |
if(scan(false,false)>0){ | |
Serial.printf("\n Found devices at sda=%d scl=%d",pinlist[_sda],pinlist[_scl]); | |
scan(true,false); | |
} | |
} | |
} | |
} | |
Wire.begin(SDA,SCL,100000); | |
} | |
uint8_t cnt=0; | |
if(display) Serial.printf("\n Scanning I2C Addresses\n"); | |
for(uint8_t i=0;i<128;i++){ | |
Wire.beginTransmission(i); | |
uint8_t ec=Wire.endTransmission(true); | |
if(ec==0){ | |
if(display){ | |
if(i<16)Serial.print('0'); | |
Serial.print(i,HEX); | |
} | |
cnt++; | |
} | |
else if(display) Serial.print(".."); | |
if(display){ | |
Serial.print(' '); | |
if ((i&0x0f)==0x0f)Serial.println(); | |
} | |
} | |
if(display){ | |
Serial.print("Scan Completed, "); | |
Serial.print(cnt); | |
Serial.println(" I2C Devices found."); | |
} | |
return cnt; | |
} | |
void dispBuff(uint8_t *buf, uint32_t len,uint16_t offset,uint16_t width=32){ | |
char *asciibuf = (char*)malloc(width+1); | |
uint32_t bufPos=0; | |
uint32_t adr=0; | |
bool first = true; | |
asciibuf[0] ='\0'; | |
while(adr<len){ | |
if((((offset+adr)&(width-1))==0)||first){ | |
if(asciibuf[0]) Serial.printf(" %s\n",asciibuf); | |
Serial.printf("0x%04x:",(uint16_t)(offset+adr)); | |
bufPos = 0; | |
if(first){ //space over to align text | |
while(bufPos<((offset+adr)&(width-1))){ | |
Serial.printf(" "); | |
asciibuf[bufPos++] = ' '; | |
} | |
} | |
asciibuf[bufPos] = '\0'; | |
first = false; | |
} | |
Serial.printf(" %02x",buf[adr]); | |
char ch=buf[adr]; | |
if((ch<32)||(ch>127)) ch ='.'; | |
bufPos+=sprintf(&asciibuf[bufPos],"%c",ch); | |
adr++; | |
} | |
while(bufPos<width){ | |
Serial.print(" "); | |
bufPos++; | |
} | |
Serial.printf(" %s\n",asciibuf); | |
free(asciibuf); | |
} | |
void dispBuff(char *buf, uint16_t len,uint16_t offset,uint16_t width=32){ | |
dispBuff((uint8_t*)buf,len,offset,width); | |
} | |
void dumpNormalXGZP(uint8_t devID){ | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0); | |
Wire.endTransmission(false); | |
uint8_t count =Wire.requestFrom(devID,(uint8_t)11); //Read first eleven byte of ADC | |
if(count == 11){// successful read | |
uint8_t reg = Wire.read(); | |
Serial.printf("SPI_CTRL(0x00)=0x%02X, %s %s %s\n",reg,(reg&0x80)?"4-Wire SPI":"3-Wire SPI", | |
(reg&0x40)?"LSB":"MSB", (reg&0x20)?"SoftReseting":""); | |
reg = Wire.read(); | |
Serial.printf("PartID(0x01)=0x%02X\n",reg); | |
reg = Wire.read(); | |
Serial.printf("Status(0x02)=0x%02X, %s%s%s%s\n",reg,(reg&0x80)?"VinP Short to VDD":"", | |
(reg&0x40)?" VinP Short to GND":"", | |
(reg&0x20)?" VinN Short to VDD":"", | |
(reg&0x10)?" VinN Short to GND":"", | |
(reg&0x01)?" Sample Ready":""); | |
for(auto a = 3 ; a< 6; a++){ | |
reg=Wire.read(); // unknown bytes, ignore | |
Serial.printf("Unk(0x%02X)=0x%02X\n",a,reg); | |
} | |
uint32_t data; | |
data = Wire.read(); | |
data *= 256; | |
data += Wire.read(); | |
data *= 256; | |
data += Wire.read(); | |
Serial.printf("DATA(0x06..0x08)= %d\n",data); | |
data = Wire.read(); | |
data *= 256; | |
data += Wire.read(); | |
Serial.printf("Temp(0x09..0x0A)= %d\n",data); | |
} | |
else { // comm failure | |
Serial.printf(" Read failed =%d(%s)\n",Wire.lastError(), Wire.getErrorText(Wire.lastError())); | |
} | |
} | |
uint8_t initXGZP(uint8_t devID){ | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing against 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0xA5); // Sys Config Register | |
Wire.endTransmission(false); | |
uint8_t count = Wire.requestFrom(devID,(uint8_t)1); | |
if(count ==1){ | |
uint8_t reg = Wire.read(); | |
Serial.printf(" Read System Configuration register =0x%02X\n"); | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0xA5); // sys configuration register | |
// DAC ON, Single Conversion, Vout=Fixed by Vext*1.5, Vext=3.6V, Calibrated Output, Diag=on | |
Wire.write(0xFD); // | |
uint8_t err =Wire.endTransmission(); | |
if(err != 0){ | |
Serial.printf("Writing to register 0xA5 value 0xFD failed=%d(%s)\n",err,Wire.getErrorText(err)); | |
return 0; // fail | |
} | |
bool ready=false; | |
uint32_t tick=millis(); // timout | |
while(!ready && (millis()-tick<1000)){ | |
Wire.beginTransmission(devID); | |
ready = I2C_ERROR_OK==Wire.endTransmission(); | |
} | |
if(ready){ | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0xA5); | |
Wire.write(0x0A); | |
err = Wire.endTransmission(); | |
if(err != 0){ | |
Serial.printf("Writing to register 0xA5 value 0x0A failed=%d(%s)\n",err,Wire.getErrorText(err)); | |
return 0; // fail | |
} | |
} | |
else { | |
Serial.printf("Sensor did not respond after first config set\n"); | |
return 0; | |
} | |
} | |
else { | |
Serial.printf("Reading from 0xA5 failed=%d(%s)\n",Wire.lastError(),Wire.getErrorText(Wire.lastError())); | |
return 0; | |
} | |
return 1; // worked? | |
} | |
void dumpOTPXGZP(uint8_t devID){ | |
uint8_t buf[50]; | |
uint16_t blockLen =25; // OTP memory length | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0xA4); // start of otp memory registers | |
Wire.endTransmission(false); | |
i2c_err_t err =Wire.readTransmission(devID, buf, blockLen); | |
if(err == I2C_ERROR_OK){ | |
dispBuff(buf, blockLen, 0xA4); | |
} | |
else { | |
Serial.printf("Error reading OTP registers =%d(%s)\n",Wire.lastError(),Wire.getErrorText(Wire.lastError())); | |
} | |
} | |
void setup(){ | |
Serial.begin(115200); | |
if(!Wire.begin(21,22,100000)){ //use default pins and 100khz | |
Serial.printf("Wire Failed to initialize.\n Locking up"); | |
while(1); | |
} | |
Serial.printf(" delaying 1/2sec to allow sensor to wakeup\n"); | |
delay(500); | |
Wire.beginTransmission(DEVID); | |
uint8_t err = Wire.endTransmission(); | |
if(err == I2C_ERROR_OK){ | |
Serial.printf(" Sensor Answered ID=0x%02X. ",DEVID); | |
dumpNormalXGZP(DEVID); | |
dumpOTPXGZP(DEVID); | |
} | |
else { // no sensor | |
Serial.printf(" No Sensor Found, scanning i2c bus for sensors \n"); | |
scan(true,false); | |
Serial.printf(" locking up"); | |
while(1); | |
} | |
if(!initXGZP(DEVID)){ | |
Serial.printf("Init of Sensor at 0x%02X Failed.\n",DEVID); | |
scan(false,true); // init failed so try to find sensor with all possible pin configurations | |
Serial.printf(" Adjust SDA, SCL to match pins, and recompile/download\n Locking up\n"); | |
while(1); | |
} | |
} | |
unsigned char Get_XGZPC_Value(uint8_t devID, float *Value) | |
{ | |
float XGZPC_Value = 0; | |
bool ready=false; | |
uint32_t tick=millis(); | |
while(!ready && (millis()-tick<1000)){ // Wait upto 1sec for conversion to complete | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0x02); // status register | |
uint8_t err =Wire.endTransmission(); // might need to use ReSTART(Wire.endTransmission(false)) if this doesn't work. | |
if(err == I2C_ERROR_OK) { | |
uint8_t count = Wire.requestFrom(devID,(uint8_t)1); // get status byte | |
if (count==1){ // got data | |
uint8_t status = Wire.read(); | |
ready = (status & 0x01)==0x01; // data ready! | |
} | |
} | |
else { // sensor did not respond? | |
Serial.printf("Trying to check Status, Sensor did not respond =%d(%s)\n",Wire.lastError(),Wire.getErrorText(Wire.lastError())); | |
} | |
} | |
if(ready){ | |
Wire.beginTransmission(devID); | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0x06); // data register | |
uint8_t err =Wire.endTransmission(); // might need to use ReSTART(Wire.endTransmission(false)) if this doesn't work. | |
if(err == I2C_ERROR_OK) { | |
uint8_t count = Wire.requestFrom(devID,(uint8_t)5); // get data and temp | |
if (count==5){ // got data | |
XGZPC_Value = Wire.read() * 65536.0 + Wire.read() * 256.0 + Wire.read(); | |
XGZPC_Value = (XGZPC_Value*100.0) / 8388608.0; | |
*Value = XGZPC_Value; | |
Serial.printf("temp = %d\n",(Wire.read()*256)+Wire.read()); | |
Wire.beginTransmission(devID); // start next sample? | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0x30); | |
Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate | |
uint8_t err = Wire.endTransmission(); | |
if(err != 0){ | |
Serial.printf(" next Sample start failed? i2cError = %d(%s)\n",err, Wire.getErrorText(err)); | |
return 0; | |
} | |
else return 1; | |
} | |
else{ | |
Serial.printf("Read data failed =%d(%s)\n",Wire.lastError(), Wire.getErrorText(Wire.lastError())); | |
return 1; | |
} | |
} | |
else { | |
Serial.printf("setData address failed =%d(%s)\n",Wire.lastError(), Wire.getErrorText(Wire.lastError())); | |
return 1; | |
} | |
} | |
else{ | |
Serial.printf(" Timeout Data not available, Re Sampling."); | |
Wire.beginTransmission(devID); // start next sample? | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing agains 24LCxx eeprom (two byte reg address) | |
Wire.write(0); | |
} | |
Wire.write(0x30); | |
Wire.write(0x0a); // Data and Temp conversion, Single shot, immediate | |
uint8_t err = Wire.endTransmission(); | |
if(err != 0){ | |
Serial.printf(" next Sample start failed? i2cError = %d(%s)\n",err, Wire.getErrorText(err)); | |
} | |
if((devID>=0x50)&&(devID<=0x57)){ // for testing against 24LCxx eeprom | |
bool ready = false; | |
uint32_t tick=millis(); | |
while(!ready && (millis()-tick<1000)){ // wait for prior write to complete | |
Wire.beginTransmission(devID); | |
ready = I2C_ERROR_OK == Wire.endTransmission(); | |
} | |
if(ready){ | |
Wire.beginTransmission(devID); | |
Wire.write(0); | |
Wire.write(2); | |
Wire.write(1); // set sample ready bit | |
Wire.endTransmission(); | |
} | |
} | |
return 1; | |
} | |
} | |
void loop(){ | |
float XGZPC_Data; | |
if(Get_XGZPC_Value(DEVID,&XGZPC_Data)){ | |
Serial.printf(" Value =%6.2f\n",XGZPC_Data); | |
} | |
else { | |
Serial.printf("error\n"); | |
} | |
} | |
/* Example out on ESP32 that does not have this sensor present. | |
Reading from 0xA5 failed=2(ACK) | |
Init of Sensor at 0x6D Failed. | |
(00,02) (00,04) (00,05) (00,12) (00,13) (00,14) (00,15) (00,16) (00,17) (00,18) (00,19) (00,21) (00,22) (00,23) (00,25) (00,26) (00,27) (00,32) (00,33) | |
(02,00) (02,04) (02,05) (02,12) (02,13) (02,14) (02,15) (02,16) (02,17) (02,18) (02,19) (02,21) (02,22) (02,23) (02,25) (02,26) (02,27) (02,32) (02,33) | |
(04,00) (04,02) (04,05) (04,12) (04,13) (04,14) (04,15) (04,16) (04,17) (04,18) (04,19) (04,21) (04,22) (04,23) (04,25) (04,26) (04,27) (04,32) (04,33) | |
(05,00) (05,02) (05,04) (05,12) (05,13) (05,14) (05,15) (05,16) (05,17) (05,18) (05,19) (05,21) (05,22) (05,23) (05,25) (05,26) (05,27) (05,32) (05,33) | |
(12,00) (12,02) (12,04) (12,05) (12,13) (12,14) (12,15) (12,16) (12,17) (12,18) (12,19) (12,21) (12,22) (12,23) (12,25) (12,26) (12,27) (12,32) (12,33) | |
(13,00) (13,02) (13,04) (13,05) (13,12) (13,14) (13,15) (13,16) (13,17) (13,18) (13,19) (13,21) (13,22) (13,23) (13,25) (13,26) (13,27) (13,32) (13,33) | |
(14,00) (14,02) (14,04) (14,05) (14,12) (14,13) (14,15) (14,16) (14,17) (14,18) (14,19) (14,21) (14,22) (14,23) (14,25) (14,26) (14,27) (14,32) (14,33) | |
(15,00) (15,02) (15,04) (15,05) (15,12) (15,13) (15,14) (15,16) (15,17) (15,18) (15,19) (15,21) (15,22) (15,23) (15,25) (15,26) (15,27) (15,32) (15,33) | |
(16,00) (16,02) (16,04) (16,05) (16,12) (16,13) (16,14) (16,15) (16,17) (16,18) (16,19) (16,21) (16,22) (16,23) (16,25) (16,26) (16,27) (16,32) (16,33) | |
(17,00) (17,02) (17,04) (17,05) (17,12) (17,13) (17,14) (17,15) (17,16) (17,18) (17,19) (17,21) (17,22) (17,23) (17,25) (17,26) (17,27) (17,32) (17,33) | |
(18,00) (18,02) (18,04) (18,05) (18,12) (18,13) (18,14) (18,15) (18,16) (18,17) (18,19) (18,21) (18,22) (18,23) (18,25) (18,26) (18,27) (18,32) (18,33) | |
(19,00) (19,02) (19,04) (19,05) (19,12) (19,13) (19,14) (19,15) (19,16) (19,17) (19,18) (19,21) (19,22) (19,23) (19,25) (19,26) (19,27) (19,32) (19,33) | |
(21,00) (21,02) (21,04) (21,05) (21,12) (21,13) (21,14) (21,15) (21,16) (21,17) (21,18) (21,19) (21,22) | |
Found devices at sda=21 scl=22 | |
Scanning I2C Addresses | |
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. | |
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. | |
20 21 .. .. .. .. .. 27 .. .. .. .. .. .. .. .. | |
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. | |
40 .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. | |
50 51 52 53 54 55 56 .. .. .. .. .. .. .. .. .. | |
.. .. .. .. .. .. .. .. 68 .. .. .. .. .. .. .. | |
.. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. | |
Scan Completed, 12 I2C Devices found. | |
(21,23) (21,25) (21,26) (21,27) (21,32) (21,33) | |
(22,00) (22,02) (22,04) (22,05) (22,12) (22,13) (22,14) (22,15) (22,16) (22,17) (22,18) (22,19) (22,21) (22,23) (22,25) (22,26) (22,27) (22,32) (22,33) | |
(23,00) (23,02) (23,04) (23,05) (23,12) (23,13) (23,14) (23,15) (23,16) (23,17) (23,18) (23,19) (23,21) (23,22) (23,25) (23,26) (23,27) (23,32) (23,33) | |
(25,00) (25,02) (25,04) (25,05) (25,12) (25,13) (25,14) (25,15) (25,16) (25,17) (25,18) (25,19) (25,21) (25,22) (25,23) (25,26) (25,27) (25,32) (25,33) | |
(26,00) (26,02) (26,04) (26,05) (26,12) (26,13) (26,14) (26,15) (26,16) (26,17) (26,18) (26,19) (26,21) (26,22) (26,23) (26,25) (26,27) (26,32) (26,33) | |
(27,00) (27,02) (27,04) (27,05) (27,12) (27,13) (27,14) (27,15) (27,16) (27,17) (27,18) (27,19) (27,21) (27,22) (27,23) (27,25) (27,26) (27,32) (27,33) | |
(32,00) (32,02) (32,04) (32,05) (32,12) (32,13) (32,14) (32,15) (32,16) (32,17) (32,18) (32,19) (32,21) (32,22) (32,23) (32,25) (32,26) (32,27) (32,33) | |
(33,00) (33,02) (33,04) (33,05) (33,12) (33,13) (33,14) (33,15) (33,16) (33,17) (33,18) (33,19) (33,21) (33,22) (33,23) (33,25) (33,26) (33,27) (33,32) Adjust SDA, SCL to match pins, and recompile/download | |
Locking up | |
this output show that there are 12, I2C devices connected to pins: sda=21 scl=22 | |
*/ |
@stickbreaker
this my new sensor value output without putting any pressure.
seems its working .. from the code sir i think its kpa unit.
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
pressure Value = 13.00 kpa
temp = 0
pressure Value = 13.01 kpa
temp = 0
Hi, I'm new writing on the GitHub forum and pretty new programming micros as well.
I'm doing a project with STM32f103c8t6 (Blue Pill) and an XGZP6847D pressure (and vacuum) sensor, but I can't get good readings, the values oscillate and I think I'm not reading correctly.
I do the reading with a program written on the own sensor datasheet. I changed the value of the variable "K" but it doesn't work properly.
I have also tried it with an Arduino uno board, but I don't read correctly either.
Searching in Google I have come to your GitHub and I thought I found the solution.
I wanted to test your program to try to read the pressure correctly, but I have found the following errors compiling it.
I apologize if these are very basic mistakes.
On the Arduino board I get the following error: 'class HardwareSerial' has no member named 'printf'
On the STM32f103c8t6 (Blue Pill) I get the next one: 'class USBSerial' has no member named 'printf' (the same problem).
First of all I thought that it miss a library, but I think this isn't the problem.
I use the Arduino IDE.
I wanted to use Platformio, but I had some problems to connect the STM32 micro and I went back to the Arduino IDE.
I connect the STM32 directly to USB (this was difficult but I got it).
Can you give me some light with this problem to try run your program and then, try to fix my sensor reading problem?
Thank you very much and sorry if this is a very basic problem.
@stickbreaker
the esp 32 recognze the sensor sir. but value 187.5 kpa sir? . idont blow any pressure on the sensor sir
i will convert it to psi and maybe i try to put some pull up resistor the 4.7k
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:9280
load:0x40080400,len:5848
entry 0x40080698
delaying 1/2sec to allow sensor to wakeup
Sensor Answered ID=0x6D. SPI_CTRL(0x00)=0x00, 3-Wire SPI MSB
PartID(0x01)=0xFF
Status(0x02)=0x01,
Unk(0x03)=0x00
Unk(0x04)=0x00
Unk(0x05)=0x00
DATA(0x06..0x08)= 15711543
Temp(0x09..0x0A)= 0
0x00a4: ff 0a fb ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff 00 .........................
Read System Configuration register =0x01
temp = 0
Value =187.30
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.11
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0
Value =187.12
temp = 0