Hi,
I try to read pressure value from a sensor, but it always return same value until I disconnect the power and reconnect, reset still give same value. Please help.
#define CAL_L -200000 //0-1.6MPA
#define CAL_H 1800000 //0-1.6MPA
#define DEVICE_ADDRESS (0x78)
void DelayMs(int ms) //8MHz/1MS—› æ≥ÖÚ
{
delay(ms*1);
}
void write8(byte value) {
Wire.beginTransmission((uint8_t)DEVICE_ADDRESS);
Wire.write((uint8_t)value);
Wire.endTransmission();
}
bool JHM1200_IsBusy(){
char status;
status=read8(0xAC);
Serial.print("Status:"+String((int)status));
status = (status >> 5) & 0x01;
//Serial.println("Statusss:"+String(status));
return status;
}
void JHM1200_get_cal()
{
char buffer[6] = {0};
ulong press_raw = 0;
uint temp_raw = 0;
double press = 0.0, temp = 0.0;
buffer[0] = 0xAC;
write8(0xAC);
while (1)
{
if (JHM1200_IsBusy())
{
DelayMs(1);
Serial.println("is busy");
}
else
break;
}
Wire.requestFrom((uint8_t)DEVICE_ADDRESS, 6); // request 6 bytes from slave device #8
int i=0;
while (Wire.available()) { // slave may send less than requested
char c = Wire.read(); // receive a byte as character
buffer[i]=c;
i++;
}
//根据校准范围将返回的压力和温度值转换为实际值
press_raw = ((ulong)buffer[1] << 16) | ((ulong)buffer[2] << 8) | buffer[3];
temp_raw = ((ulong)buffer[4] << 8) | (buffer[5] << 0);
press = (double)press_raw / 16777216;
press = press * (CAL_H - CAL_L) + CAL_L;
temp = (double)temp_raw / 65536;
temp = temp * 19000 - 4000;
// my_temp = press;// 压力数值单位为PA
Serial.println("Press_raw"+String(press_raw)+"/press="+String(press)+"/temp="+String(temp));
}
void setup() {
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
}
void loop() {
delay(100);
JHM1200_get_cal();
delay(300);
}
Is the name of the JHM1200_get_cal() function significant ?
To me it would seem to indicate that it might be getting calibration data. Did you write the function ?
No, it's not a calibrating function. I convert this code from another code for a difference controller.
What's strange to me is that JHM1200_get_cal() keeps responding the same output until I reconnect the power
Pleas provide a link to the datasheet of the sensor that you are using
Sorry, it is a Chinese product and its vendor just give me their code which only works with their controller(not arduino).
What I am thinking of is a way to reset I2C or reset the power to I2C.
Here's their code:
Koepel
June 26, 2021, 11:25am
6
You made a good effort translating that code, but you should start with the most simple sketch that you can write.
Let's do this step by step.
This seems to be the manufacturer's page of the MHM1200: http://www.jiuhaomicro.com/product/showproduct.php?id=54&lang=en
It is a 24-bit ADC with gain, running at 1.8 to 3.6V. That means its I2C bus is not compatible with a 5V Arduino board.
Is there a datasheet ? Without datasheet we would be guessing what to do.
Which Arduino board do you use ?
Run a I2C Scanner to find the address.
Does it have a special "identifier" or "who am I" register that can be read ?
Ignore that it can be busy for now, just put a delay in your sketch.
Write 0xAC and read 6 bytes is indeed what should be done.
This is what I mean with a simple sketch:
void loop()
{
Wire.beginTransmission( 0x78);
Wire.write( 0xAC);
Wire.endTransmission();
delay( 1000);
int n = Wire.requestFrom( 0x78, 6);
Serial.print( "Received ");
Serial.print( n);
Serial.print( " bytes: ");
for( int i=0; i<n; i++)
{
Serial.print( Wire.read(), HEX);
Serial.print( ", ");
}
Serial.println();
delay( 1000);
}
@Koepel , thanks for your code.
Skipping the busy check(JHM1200_IsBusy()) solve the problem. I try your code it works as expected.
Koepel
June 27, 2021, 10:15am
8
You didn't show the read8() function that you use in JHM1200_IsBusy() ?
uint8_t read8(byte reg) {
uint8_t value;
Wire.beginTransmission((uint8_t)WP120_ADDRESS);
Wire.write((uint8_t)reg);
if (0 == Wire.endTransmission(false)) {
Wire.requestFrom((uint8_t)WP120_ADDRESS, (byte)1);
value = Wire.read();
}else
return 0;
return value;
}
Koepel
June 28, 2021, 5:12pm
10
That is okay, if the JHM1200 supports a repeated start.