Digole
Digital Solutions
iDigole Buyer ProtectioniDigole Buyer
Protection
My shopping cart
0 Items:
Secure Checkout
Visit Our Live Auctions
Search store:
 

✨✨✨How does Digole display work?Try Display Emulate Tool ✨✨✨
Please select DHL as shipping from China due to most international flights cancels

Home : ~Project~ :

2-18B20 Temperature Monitor


In stock: no
   
Description
Feedback

Need one Digole Smart Display Module(DSDM)? buy them here

 Use any of DSDM to work with a DS18B20 1 wire temperature sensor, to show the temperatur on the screen, you will learn how to deal with a 1 wire sensor.

Source Code: (Download: Source Code.Hex File)

#define    MHZ    *1
#define XTAL_FREQ 16MHZ

#ifndef    XTAL_FREQ
#define    XTAL_FREQ    64MHZ        /* Crystal frequency in MHz */
#endif


#if    XTAL_FREQ == 8MHZ   ///* 4x to make 1 mSec */
#define uS_CNT  246
#endif

#if    XTAL_FREQ > 8MHZ
#define uS_CNT  248
#endif

#define IN 1
#define OUT 0
//change the following 2 lines if you are using different ports and pins, here used portA.1
#define DIR TRISA1
#define DATA PORTAbits.RA1

void DelayMs(unsigned char);
void Delay1Us(void);
void DelayUs(unsigned char x);
unsigned char DS18B20_Reset(void);
void DS18B20_Write_1bit(unsigned char b);
unsigned char DS18B20_Read_1bit(void);
void DS18B20_Write_1byte(unsigned char b);
unsigned char DS18B20_Read_1byte(void);
void DS18B20_Skip(void);
unsigned char DS18B20_crc8(const unsigned char *addr, unsigned char len);
#include <htc.h>
#include <stdio.h>
#include <digole.h>


unsigned char n;
unsigned int temperature;
float tempC;
void main(void) {
    n = 0;
    OSCCON = 0B11110000; //16 MHz, idle mode
    OSCTUNE = 0B00000000; //x4 = 64Mhz
//    initDisplay();
    ANS1=0;
    DIR = OUT;
    if (DS18B20_Reset()) {
        drawStr(digole_screen_info.w/8-11,0,"TEST DS18B20");
        while (1) {
            DS18B20_Reset();
            DelayMs(2);                 //wait enough time for 18B20 to release the wire
            DS18B20_Skip();             //if there are multiple 18B20 on one wire, you need send rom
            DS18B20_Write_1byte(0x44);  //start temperature sample
            DelayMs(250);
            DelayMs(250);
            DelayMs(250);
            DelayMs(250);
            DS18B20_Reset();
            DelayMs(2);
            DS18B20_Skip();
            DS18B20_Write_1byte(0xBE);  //read temperature
            temperature=DS18B20_Read_1byte();
            temperature+=(DS18B20_Read_1byte())*256;
            tempC=temperature/16.0;     //12bit resoution
            setPrintPos(0, 2, _TEXT_);  //set print position at 0 colunm, 2 row
            printFloat(tempC,1);
            printChar('C');
        }
    } else
        printString("No DS18B20 Hook up");
    while(1) SLEEP();
}

void DelayMs(unsigned char cnt) {
    unsigned char i;
    while (cnt--) {
        i = 4;
        while (i--) {
            DelayUs(uS_CNT); /* Adjust for error */
        };
    };
}

void DelayUs(unsigned char x) { //call function need 4 instruction cycles, so, it's not accurate for x<4
    while (--x > 0) {
#if    XTAL_FREQ == 8MHZ
        asm("nop");
#endif
#if    XTAL_FREQ == 16MHZ
        asm("nop");
        asm("nop");
#endif
#if    XTAL_FREQ == 32MHZ
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
#endif
#if    XTAL_FREQ == 64MHZ
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
#endif
    }

}

unsigned char DS18B20_Reset(void) {
    DIR = OUT;
    DATA = 0;
    DelayUs(200);
    DelayUs(200);
    DelayUs(200);
    DIR = IN;
    DelayUs(100);
    if (DATA)
        return 0; //no chip response the reset
    else
        return 1; //good
}

void DS18B20_Write_1bit(unsigned char b) {
    DIR = OUT;
    DATA = 0;
    DelayUs(8);
    DATA = b;
    DelayUs(55);
    DATA = 1;
    DelayUs(5);
    DIR = IN;
}

unsigned char DS18B20_Read_1bit(void) {
    unsigned char d;
    DIR = OUT;
    DATA = 0;
    DelayUs(1);
//    DATA = 1;
    DIR = IN;
    DelayUs(10);
    d = DATA;
    DelayUs(50);
    DIR = OUT;
    DATA = 1;
    return (d);
}

void DS18B20_Write_1byte(unsigned char b) {
    for (unsigned char n = 1; n; n <<= 1)
        DS18B20_Write_1bit((b & n) ? 1 : 0);
}

unsigned char DS18B20_Read_1byte(void) {
    unsigned char data;
    data = 0;
    for (unsigned char n = 1; n; n <<= 1)
        if (DS18B20_Read_1bit())
            data |= n;
    return data;
}
// Do a ROM select

void DS18B20_Select(unsigned char ROM[8]) {
    DS18B20_Write_1byte(0x55); // Choose ROM
    for (unsigned char i = 0; i < 8; i++) DS18B20_Write_1byte(ROM[i]);

}
// Do a ROM skip

void DS18B20_Skip(void) {
    DS18B20_Write_1byte(0xCC);
}

unsigned char DS18B20_crc8(const unsigned char *addr, unsigned char len) {
    unsigned char crc = 0;

    while (len--) {
        unsigned char inbyte = *addr++;
        for (unsigned char i = 8; i; i--) {
            unsigned char mix = (crc ^ inbyte) & 0x01;
            crc >>= 1;
            if (mix) crc ^= 0x8C;
            inbyte >>= 1;
        }
    }
    return crc;
}

More practice: Try to use large font to display the temperature on screen, switch display between Celsius and Fahrenheit

How to install MPLAB IDE and Hi-tech C compiler
DSDM Hardware Specifications
DSDM Programmer Manual
Download "digole.h" file for MPLAB IDE and HI-TECH C

Copyright Digole Digital Solutions, 2008-2021. All rights reserved.
Powered by Digole