We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]
posted on 2021/03/16 10:26:54 PM CET
Hi,
I recently bought Easy PIC Fusion 7 Development board and trying to interface SHT31 Sensor with Microcontroller P24EP512GU810 using MikroC. I have been trying from last couple of days but no luck yet. Any kind of help will be much appreciated. I am not using the in built library because I dont have the SHT3x click and, I am using SHT31 probe from DFRobot (https://wiki.dfrobot.com/SHT31_Temperature_Humidity_Sensor_Weatherproof_SKU_SEN0385). Below is my code:
unsigned int t_m=0, t_l=0, t_c=0, h_m=0, h_l=0, h_c=0;
void SHT31_measure() {
I2C2_Start(); // issue I2C start signal
if(I2C2_Write(68)==0){ // send byte via I2C + W (command to SHT31)
UART1_Write_Text("Yes");
}
else UART1_Write_Text("No");
I2C2_Write(0x2c); // Single Measurement Mode
I2C2_Write(0x06); // Clock Streching Enabled / High Rpeatability
I2C2_Stop();
Delay_ms(5);
I2C2_Start();
I2C2_Write(69); // send byte via I2C + R (command to SHT31)
Delay_ms(200); // Wait for measurement to be done
t_m = I2C2_Read(0); // Temperature MSB and Send Acknowledge
t_l = I2C2_Read(0); // Temperature LSB and Send Acknowledge
t_c = I2C2_Read(0); // Temperature CRC and Send Acknowledge
h_m = I2C2_Read(0); // Humidity MSB and Send Acknowledge
h_l = I2C2_Read(0); // Humidity LSB and Send Acknowledge
h_c = I2C2_Read(1); // Humidity CRC and Send Not Acknowledge
I2C2_Stop();
UART1_Write_Text("Data Read Done");
UART1_Write(13);
UART1_Write(10);
}
void main() {
// Setting output frequency to 140MHz
PLLFBD = 70; // PLL multiplier M=70
CLKDIV = 0x0000; // PLL prescaler N1=2, PLL postscaler N2=2
ANSELA = 0x00; // Convert all I/O pins to digital
ANSELB = 0x00;
ANSELC = 0x00;
ANSELD = 0x00;
ANSELE = 0x00;
TRISA = 0x00; // Make port A as Output
PPS_Mapping(100, _INPUT, _U1RX); // Sets pin RP100 to be Input, and maps U1RX to it
PPS_Mapping(101, _OUTPUT, _U1TX); // Sets pin RP101 to be Output, and maps U1TX to it
UART1_Init(56000); // Initialize UART module at 56000 bps
Delay_ms(100); // Wait for UART module to stabilize
I2C2_Init(100000); // Initialize I2C @ 100KHz
Delay_ms(100); // Wait to stabalize
UART1_Write_Text("Start");
UART1_Write(13);
UART1_Write(10);
while (1) { // Endless loop
SHT31_measure(); // Measure Temperature and Humidity
UART1_Write(t_m); // Display all the data received (Raw Data), except 2 CRC Bytes
UART1_Write_Text("|") ;
Delay_ms(10);
UART1_Write(t_l);
UART1_Write_Text("|") ;
delay_ms(10);
UART1_Write(h_m);
UART1_Write_Text("|") ;
delay_ms(10);
UART1_Write(h_l);
UART1_Write(13);
UART1_Write(10);
delay_ms(1000); // Wait here for a Second
}
}