I'm developing a project with the PIC16F877A, the MLX90614 sensor and the oled display SSD1306, the code compiled normally but in the simulation the display receives a temperature that has nothing to do with the sensor. I believe I'm missing out on setting up something of I2C in the code, but I don't know what it might be. Can you help me?
English only please MOD edit
C:
/********************************************** *******************************************
Used Crystal Oscillator @ 8MHz
This is free software with no warranty.
[URL]http://simple-circuit.com/[/URL]
************************************************** *************************************/
#use FAST_IO(C)
//OLED SSD1306 Reset Pin Setting
#define SSD1306_RST PIN_D4
//MLX90614 I2C Address
#define MLX90614_I2C_ADDRESS 0x5A
#define AMB_TEMP 0X06 #define OBJ_TEMP 0X07
#incluir <16F877A.h>
#fusíveis HS, NOWDT, NOPROTECT, NOLVP #use
delay(clock = 8MHz)
#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // initializes I2C
// includes the source code of the SSD1306 OLED
driver #incluir <SSD1306.c>
int16 temperature;
character degree[] = {0, 7, 5, 7, 0}; custom
degree symbol // Function to read the temperature of the MLX90614
int16 Ler_Sensor(char Temp_Source) {
int16 Var_Temp;
i2c_start();
if(!i2c_write(MLX90614_I2C_ADDRESS << 1)) {
returns false; // If unable to initiate I2C communication, return false
}
i2c_write(Temp_Fonte);
i2c_start();
i2c_write((MLX90614_I2C_ADDRESS << 1) | 1); Least significant bit (LSB) set to read
Var_Temp = i2c_read(0);
Var_Temp = (i2c_read(0) << 8) + Var_Temp;
i2c_stop();
return Temp_var;
}
// Function to clean only the temperature area on the OLED
display void Limpar_Temperatura() {
SSD1306_GotoXY(6, 7); // Position the cursor at the temperature
position SSD1306_PutC(" "); Write blanks to clear the area of the temperature
SSD1306_GotoXY(11, 7); Positions the cursor at the position of the degree
symbol SSD1306_PutC(" "); Clears the degree
symbol }
//Function to show the temperature on the OLED
display void Mostrar_Temperatura(char Temp_Source, int16 temperature) {
if(Temp_Source == AMB_TEMP || Temp_Source == OBJ_TEMP) {
SSD1306_GotoXY(6, 7);
printf(SSD1306_PutC, "%ld.%02ld", temperature / 100, abs(temperature) % 100); // Displays the temperature to two decimal
places SSD1306_GotoXY(11, 7);
SSD1306_PutCustomC(grade); degree symbol (°)
}
other {
SSD1306_GotoXY(6, 7); // Go to column 4, row 2
SSD1306_PutC("Error!");
}
}
//main
function main empty(empty) {
atraso_ms(1000);
// Initializes OLED SSD1306 with an I2C address = 0x7A (default address)
setup_comparador(NC_NC_NC_NC);
setup_adc_ports(NO_ANALOGS);
SSD1306_Init(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);
SSD1306_GotoXY(5, 2);
SSD1306_PutC("SENSOR MLX90614");
SSD1306_GotoXY(6, 5);
SSD1306_PutC("TEMPERATURE:");
i2c_Init(MLX90614_I2C_ADDRESS);
atraso_ms(2000);
while (TRUE) {
// Clean only the temperature area before displaying the new reading
Limpar_Temperatura();
// Read the ambient
temperature Temp = Ler_Sensor(AMB_TEMP);
Mostrar_Temperatura(AMB_TEMP,Temp);
atraso_ms(2000); Wait 2 seconds before clearing the display and taking a new reading
// Clear the screen before displaying the next reading
Limpar_Temperatura();
// Read the object's
temperature Temp = Ler_Sensor(OBJ_TEMP);
Mostrar_Temperatura(OBJ_TEMP,Temp);
atraso_ms(2000); Wait 2 seconds before clearing the display and taking a new scan
}
}
English only please MOD edit
Last edited: