I am a beginner at using I2C communication to read data from sensors. I highly appreciate it if you could help me with this question. I have the sample which can work in STM32F103.
First, write a value of 0X00 to register 0X30, then write a value of 0X08 to register 0X30, and turn on the state machine, so that data conversion will begin inside the chip.
The temperature output register is three 8-bit registers, with a total of 24 data bits. The first ten digits are the integer part, the WD variable is the integer result, and the WENDU variable is the decimal result.
They told me the device address is 0X7f, but I can not find it in the sample code as below:
[Sample code]
#include "stm32f10x_gpio.h" // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f10x_rcc.h" // Keil::Device:StdPeriph Drivers:RCC
#include "systick.h"
#include "iic.h"
signed char adc,adc1,adc2,adc3;
signed long wd,wd1;
u8 yd1=3,yd2=3,yd3=3,yd4=3;
float wendu;
int main(void)
{
SYSTICK_CONFIG();
delay_init(72);
IIC_Init();
delay_ms(3);
IIC_Start(); //initialize cmd
IIC_Send_Byte(0Xfe);
IIC_Wait_Ack();
IIC_Send_Byte(0x30);
IIC_Wait_Ack();
IIC_Send_Byte(0x00);
IIC_Wait_Ack();
IIC_Stop();
IIC_Start(); //turn on the state machine
IIC_Send_Byte(0Xfe);
IIC_Wait_Ack();
IIC_Send_Byte(0x30);
IIC_Wait_Ack();
IIC_Send_Byte(0x08);
IIC_Wait_Ack();
IIC_Stop();
while(1)
{
delay_ms(200);
IIC_Start(); //read register
IIC_Send_Byte(0Xfe);
IIC_Wait_Ack();
IIC_Send_Byte(0x10);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0Xff);
IIC_Wait_Ack();
adc=IIC_Read_Byte(0);
IIC_Stop();
IIC_Start(); //read register
IIC_Send_Byte(0Xfe);
IIC_Wait_Ack();
IIC_Send_Byte(0x11);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0Xff);
IIC_Wait_Ack();
adc1=IIC_Read_Byte(0);
IIC_Stop();
IIC_Start(); //read register
IIC_Send_Byte(0Xfe);
IIC_Wait_Ack();
IIC_Send_Byte(0x12);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(0Xff);
IIC_Wait_Ack();
adc2=IIC_Read_Byte(0);
IIC_Stop();
WD=(adc<<2)+(adc1>>6);
wd1=(adc<<24)+(adc1<<16)+(adc2<<8);
WENDU=(float)wd1/64/65536;
delay_ms(100);
I connect the sense with the Nano. But I can not read the correct value , I do not know why. And this is my code
const int rs = 13, en = 12, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
float num;
byte temp, temp1;
void setup() {
Wire.begin();
Wire.beginTransmission(0X7f);
Wire.write(0x30);
Wire.write(0x00);
Wire.endTransmission();
Wire.beginTransmission(0X7f);
Wire.write(0x30);
Wire.write(0x08);
Wire.endTransmission();
Serial.begin(9600);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
// lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
//lcd.print(millis() / 1000);
Wire.beginTransmission(0X7f);
Wire.write(0x10);
Wire.requestFrom(0X7f,1);
byte x0 = Wire.read();
Wire.endTransmission();
Wire.beginTransmission(0X7f);
Wire.write(0x11);
Wire.requestFrom(0X7f,1);
byte x1 = Wire.read();
// byte x2 = Wire.read();
unsigned int temp = ((unsigned int) x1 >> 6) | ((unsigned int) x0 << 2);
Wire.endTransmission();
byte wd=(temp<<2)+(temp1>>6);
lcd.print(wd);
}