Hello
if anyone can help me with a SI1145 sensor.
I' trying usee this sensor with pic24fj, but no good result from the registers.
I'm just try read the PART_ID from the sensor, but I can't read the register at first time. i.e I call a function SI1145_Read_byte(uint8_t regaddress), some thing like this (I will post the code)
but for read the correct value I need call this function two time. at the second call return me a correct value from registe.
example:
If read the register 0x02 (SEQ_ID) I expect read 8dec.
but at first read a NULL value.
maybe with some picture I can explain beter.
I2C.c
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <stdbool.h>
#include "I2C1.h"
#include "TIMERS.h"
void I2C1_ACK_SEND(void)
{
I2C1CONLbits.ACKDT=0;//config to send ACK
while(I2C1CONLbits.ACKEN==1){};
}
uint8_t I2C1_ReadData(void)
{
I2C1CONLbits.RCEN = 1;
uint8_t data=I2C1RCV;
while(I2C1STATbits.RBF==0){};
I2C1_IDLE();//while(I2C2STATbits.TRSTAT==1){};
return data;
}
void I2C1_ReStartCondition()
{
I2C1CONLbits.RSEN=1;
I2C1_IDLE();//while(I2C2CONLbits.RSEN==1){};
}
void I2C1_Send_ACK(void)
{
I2C1CONLbits.ACKDT=0;//config to send ACK
while(I2C1CONLbits.ACKEN==1){};
}
void I2C1_ACK_READ(void)
{
while(I2C1STATbits.ACKSTAT==1){};
}
void I2C1_Send_NACK(void)
{
I2C1CONLbits.ACKDT=1;//config to send NACK
while(I2C1CONLbits.ACKEN==1){};
}
void I2C1_Sendaddress(uint8_t dataaddress)
{
I2C1TRN = dataaddress<<1;
I2C1_IDLE();//while(I2C2STATbits.TRSTAT==1){};
}
void I2C1_SendByte(uint8_t data)
{
I2C1TRN = data;
I2C1_IDLE();//while(I2C2STATbits.TRSTAT==1){};
}
void I2C1_StopCondition(void)
{
I2C1CONLbits.PEN=1;
I2C1_IDLE();//while(I2C2CONLbits.PEN==1){};
}
void I2C1_StartCondition(void)
{
I2C1CON1bits.SEN=1;
I2C1_IDLE();//while(I2C2CONLbits.SEN==1){};
}
void I2C1_IDLE(void)
{
while(I2C1CONLbits.RCEN==1 || I2C1CONLbits.PEN==1 || I2C1CONLbits.RSEN==1 ||
I2C1CON1bits.SEN==1 || I2C1STATbits.TRSTAT==1){};//IDLE
}
void Init_I2C1(void)
{
I2C1BRG = 9;
I2C1CONLbits.I2CSIDL=0;//Continues module operation in Idle mode
I2C1CONLbits.A10M=0;//I2CADD is a 7-bit slave address
I2C1CONLbits.DISSLW=1;//1=Slew rate control is disabled for Standard Speed mode (100 kHz, also disabled for 1 MHz mode) || 0=Slew rate control is enabled for High-Speed mode (400 kHz)
I2C1CONLbits.I2CEN=1;//Enables the I2C2 module, and configures the SDA2 and SCL2 pins as serial port pins
}
SI1145.c
uint8_t SI1145_Burst_Read_Data_UI8(uint8_t add_register_UI8)//uint8_t add_register_UI8, uint8_t dim)//return 8bits data from I2C Register
{
//Host Interface Burst Read sequence
I2C1_StartCondition();
I2C1_SendByte(0xC0);
I2C1_ACK_READ();
I2C1_SendByte(add_register_UI8);
I2C1_ACK_READ();
I2C1_ReStartCondition();
I2C1_SendByte(0xC1);
res = I2C1_ReadData();
I2C1_Send_NACK();
I2C1_StopCondition();
return res;
}
more prints with only one call
register address
register value