if(raw_temp & 0x8000) { // If the temperature is negative temp[0] = '-'; // Put minus sign (-) raw_temp = ~raw_temp + 1; // Change temperature value to positive form } else { if((raw_temp >> 4) >= 100) // If the temperatue >= 100 °C temp[0] = '1'; // Put 1 of hundreds else // otherwise temp[0] = ' '; // put space ' ' }
// Put the first two digits ( for tens and ones) temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + '0'; // Put tens digit temp[2] = (raw_temp >> 4) % 10 + '0'; // Put ones digit
// Put the 4 fraction digits (digits after the point) // Why 625: because we're working with 12-bit resolution (default resolution) temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + '0'; // Put thousands digit temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + '0'; // Put hundreds digit temp[6] = (((raw_temp & 0x0F) * 625) / 10 ) % 10 + '0'; // Put tens digit temp[7] = ( (raw_temp & 0x0F) * 625) % 10 + '0'; // Put ones digit
Source Code :
// SSD1306 OLED reset pin definition
#define SSD1306_RST PIN_D4
// DS18B20 Data pin is connected to pin RD5
#define DS18B20_PIN PIN_D5
#include
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay(clock = 8MHz)
#use fast_io(B)
#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM) // initialize I2C
// include SSD1306 OLED driver source code
#include
// Variables declaration
int16 raw_temp;
char *temp = "000.0000 C";
char degree[] = {0, 7, 5, 7, 0}; // degree symbol custom char
int1 ds18b20_start(){
output_low(DS18B20_PIN); // Send reset pulse to the DS18B20 sensor
output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as output
delay_us(500); // Wait 500 us
output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as input
delay_us(100); //wait to read the DS18B20 sensor response
if (!input(DS18B20_PIN)) {
delay_us(400); // Wait 400 us
return TRUE; // DS18B20 sensor is present
}
return FALSE;
}
void ds18b20_write_bit(int1 value){
output_low(DS18B20_PIN);
output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as output
delay_us(2); // Wait 2 us
output_bit(DS18B20_PIN, value);
delay_us(80); // Wait 80 us
output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as input
delay_us(2); // Wait 2 us
}
void ds18b20_write_byte(int8 value){
int8 i;
for(i = 0; i < 8; i++)
ds18b20_write_bit(bit_test(value, i));
}
int1 ds18b20_read_bit(void) {
int1 value;
output_low(DS18B20_PIN);
output_drive(DS18B20_PIN); // Configure DS18B20_PIN pin as output
delay_us(2);
output_float(DS18B20_PIN); // Configure DS18B20_PIN pin as input
delay_us(5); // Wait 5 us
value = input(DS18B20_PIN);
delay_us(100); // Wait 100 us
return value;
}
int8 ds18b20_read_byte(void) {
int8 i, value = 0;
for(i = 0; i < 8; i++)
shift_right(&value, 1, ds18b20_read_bit());
return value;
}
int1 ds18b20_read(int16 *raw_temp_value) {
if (!ds18b20_start()) // Send start pulse
return FALSE;
ds18b20_write_byte(0xCC); // Send skip ROM command
ds18b20_write_byte(0x44); // Send start conversion command
while(ds18b20_read_byte() == 0); // Wait for conversion complete
if (!ds18b20_start()) // Send start pulse
return FALSE; // Return 0 if error
ds18b20_write_byte(0xCC); // Send skip ROM command
ds18b20_write_byte(0xBE); // Send read command
*raw_temp_value = ds18b20_read_byte(); // Read temperature LSB byte and store it on raw_temp_value LSB byte
*raw_temp_value |= (int16)(ds18b20_read_byte()) return 1
}
// main function
void main(void) {
delay_ms(1000);
// Initialize the SSD1306 OLED with an I2C addr = 0x7A (default address)
SSD1306_Init(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS);
// clear the whole display
SSD1306_ClearDisplay();
SSD1306_GotoXY(5, 2);
SSD1306_PutC("DS18B20 SENSOR");
SSD1306_GotoXY(6, 5);
SSD1306_PutC("TEMPERATURE:");
while (TRUE) {
if(ds18b20_read(&raw_temp)) {
if(raw_temp & 0x8000) { // If the temperature is negative
temp[0] = '-'; // Put minus sign (-)
raw_temp = ~raw_temp + 1; // Change temperature value to positive form
}
else {
if((raw_temp >> 4) >= 100) // If the temperatue >= 100 °C
temp[0] = '1'; // Put 1 of hundreds
else // otherwise
temp[0] = ' '; // put space ' '
}
// Put the first two digits ( for tens and ones)
temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + '0'; // Put tens digit
temp[2] = (raw_temp >> 4) % 10 + '0'; // Put ones digit
// Put the 4 fraction digits (digits after the point)
// Why 625: because we're working with 12-bit resolution (default resolution)
temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + '0'; // Put thousands digit
temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + '0'; // Put hundreds digit
temp[6] = (((raw_temp & 0x0F) * 625) / 10 ) % 10 + '0'; // Put tens digit
temp[7] = ( (raw_temp & 0x0F) * 625) % 10 + '0'; // Put ones digit
SSD1306_GotoXY(6, 7);
printf(SSD1306_PutC, temp);
SSD1306_GotoXY(14, 7);
SSD1306_PutCustomC(degree); // degree symbol ( ° )
}
else {
SSD1306_GotoXY(6, 7); // Go to column 4 row 2
SSD1306_PutC(" Error! ");
}
delay_ms(1000); // Wait 1 second
}
}
Sir, please made a simulation project on PH sensor
Ok, we are receiving lot of comments on similar project so we can publish it soon
is there a github link of all the files
How can I make the text display larger?
Use customised text or design library
How to solve the error for pin does not exist in child mode
Make sure your circuit connection as per wiring diagram as shown