Datasheet
Library Example
This example reads the temperature using DS18x20 connected to pin PORTB.2.
After reset, MCU obtains temperature from the sensor and prints it on the LCD.
Make sure to pull-up PORTB.2 line and to turn off the PORTB leds.
// LCD module connections
sbit LCD_RS at PORTD.B2;
sbit LCD_EN at PORTD.B3;
sbit LCD_D4 at PORTD.B4;
sbit LCD_D5 at PORTD.B5;
sbit LCD_D6 at PORTD.B6;
sbit LCD_D7 at PORTD.B7;
sbit LCD_RS_Direction at DDRD.B2;
sbit LCD_EN_Direction at DDRD.B3;
sbit LCD_D4_Direction at DDRD.B4;
sbit LCD_D5_Direction at DDRD.B5;
sbit LCD_D6_Direction at DDRD.B6;
sbit LCD_D7_Direction at DDRD.B7;
// End LCD module connections
// OneWire pinout
sbit OW_Bit_Write at PORTB.B2;
sbit OW_Bit_Read at PINB.B2;
sbit OW_Bit_Direction at DDRB.B2;
// end OneWire definition
// Set TEMP_RESOLUTION to the corresponding resolution of used
DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
const unsigned short TEMP_RESOLUTION = 9;
char *text = "000.0000";
unsigned temp;
void Display_Temperature(unsigned int temp2write) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;
// check if temperature is negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
}
350
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroC PRO for AVR
CHAPTER 6