DS18b20 with PIC microcontroller

Carl Arellano's picture

Offline

Joined: Nov 20, 2018

Replies: 2
DS18b20 with PIC microcontroller
January 17, 2019 - 10:26am

I am creating a project using PIC16f18426 and Temperature sensor (DS18b20). I followed the tutorial in circuit digest by Mr Sourav Gupta about PIC16f877a with DS18b20. I removed the LCD part and try to debug the program. However, I don't have any readings. I used 32 MHz as the oscillator and here is my code for configuration settings:
I hope you can help me. Thank you very much.
// CONFIG1
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = OFF // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = ON // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTS = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 2.45V)
#pragma config ZCDDIS = OFF // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled, SWDTEN is ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)

// CONFIG4
#pragma config BBSIZE = BB512 // Boot Block Size Selection bits (512 words boot block size)
#pragma config BBEN = OFF // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block Write Protection bit (Application Block not write protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block not write protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration Register not write protected)
#pragma config WRTD = OFF // Data EEPROM write protection bit (Data EEPROM NOT write protected)
#pragma config WRTSAF = OFF // Storage Area Flash Write Protection bit (SAF not write protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF // UserNVM Program memory code protection bit (UserNVM code protection disabled)

Carl Arellano's picture

Offline

Joined: Nov 20, 2018

Replies: 2
In addition,
January 17, 2019 - 10:28am
In addition,
void main(void) {
    TRISC = 0x00;   //setting the Port C to be output
    TRISA = 0xFF;   //setting Port A to be input
    OSCCON1 = 0b01100000;
    OSCFRQ = 0b00000110;
    
    unsigned short TempL, TempH;
unsigned int t, t2;
float difference1=0, difference2=0;        
    lcd_init();    
    while(1){
        float i=0;
/*------------------------------------------------------*/        
ow_reset();
write_byte(write_scratchpad);
    write_byte(0);
write_byte(0);
write_byte(resolution_12bit); // 12bit resolution
ow_reset();
write_byte(skip_rom); 
write_byte(convert_temp); 
 
while (read_byte()==0xff);
__delay_ms(500); 
ow_reset();
write_byte(skip_rom);
write_byte(read_scratchpad);
TempL = read_byte();
TempH = read_byte();
            
 
i=((unsigned int)TempH << 8 ) + (unsigned int)TempL; //put both value in one variable
i = i * 6.25;   //calculations used from the table provided in the data sheet of ds18b20
 
/*This if-else condition done because LCD would not refresh till temperature change -.20 or +.20 degree*/
pre_val=aft_val;  
 
 
difference1 = pre_val - i; 
difference2 = i - pre_val;
 
if(difference1 > temp_gap || difference2 > temp_gap){
aft_val = i;
lcd_com (0x80);
lcd_puts ("Water Analyzer");
lcd_com (0xc0);
lcd_bcd (5, aft_val);
lcd_data(223);
lcd_puts("C    ");
 
}
else{
lcd_com (0x80);
lcd_puts ("Water Analyzer");
lcd_com (0xc0);
lcd_bcd (5, pre_val);
lcd_data(223);
lcd_puts("C    ");
}
        
        }            
             
    return;
}
Sourav Gupta's picture

Offline

Joined: Feb 12, 2018

Replies: 10
Problem with the Oscillator part.
February 14, 2019 - 12:07am

The ds18b20 follow strict time line pattern to communicate. As far as I remember the project was build using 20 Mhz . The time duration changed for the communication function used in the ds18b20 header file.

Rectifying the function will help you to recover the project.