• AVR Freaks

Hot!DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER

Page: < 12 Showing page 2 of 2
Gouthami c v
Starting Member
  • Total Posts : 84
  • Reward points : 0
  • Joined: 2019/07/11 01:55:44
  • Location: 0
  • Status: offline
Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/07 03:18:02 (permalink)
Thank you for your reply.
I have setup UART, i need to display cvalue and fvalue on monitor.
here is my code

* File: newmain.c
* Author: gouthami
* Created on August 7, 2019, 12:02 PM
// PIC24F04KA201 Configuration Bit Settings
// 'C' source line config statements
// FBS
// FGS
#pragma config GWRP = OFF // General Segment Code Flash Write Protection bit (General segment may be written)
#pragma config GCP = OFF // General Segment Code Flash Code Protection bit (No protection)
#pragma config FNOSC = FRC // Oscillator Select (Fast RC oscillator (FRC))
#pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled))
#pragma config POSCMOD = NONE // Primary Oscillator Configuration bits (Primary oscillator disabled)
#pragma config OSCIOFNC = OFF // CLKO Enable Configuration bit (CLKO output signal active on the OSCO pin; primary oscillator must be disabled or configured for the External Clock mode (EC) for the CLKO to be active (POSCMD<1:0>))
#pragma config POSCFREQ = HS // Primary Oscillator Frequency Range Configuration bits (Primary oscillator/external clock input frequency greater than 8 MHz)
#pragma config SOSCSEL = SOSCHP // SOSC Power Selection Configuration bits (Secondary oscillator configured for high-power operation)
#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock switching is disabled, Fail-Safe Clock Monitor is disabled)
#pragma config WDTPS = PS32768 // Watchdog Timer Postscale Select bits (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler (WDT prescaler ratio of 1:128)
#pragma config WINDIS = OFF // Windowed Watchdog Timer Disable bit (Standard WDT selected; windowed WDT disabled)
#pragma config FWDTEN = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config BOREN = BOR3 // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware; SBOREN bit disabled)
#pragma config PWRTEN = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BORV = LPBOR // Brown-out Reset Voltage bits (Low-Power Brown-out reset occurs around 2.0V)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RA5 input pin disabled)
#pragma config ICS = PGx2 // Reserved (PGC2/PGD2 are used for programming the device)
// FDS
#pragma config DSWDTPS = DSWDTPSF // Deep Sleep Watchdog Timer Postscale Select bits (1:2,147,483,648 (25.7 Days))
#pragma config DSLPBOR = ON // Deep Sleep Zero-Power BOR Enable bit (Deep Sleep BOR enabled in Deep Sleep)
#pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable bit (DSWDT disabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define FCY 4000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
#include <libpic30.h> //for delay routines
/* Ds18b20 related definition */
#define Skip_Rom 0xCC
#define Convert_T 0x44 
#define write_scratchpad 0x4E
#define Read_scratchpad 0xBE
#define Probe_Data _LATB4 // this is the data line from Maxim probe.
#define Probe_Pin _TRISB4 // Port pin for Maxim probe data
#define PinRB4 PORTBbits.RB4
#define HEATER _LATA6
#define ON 1
#define OFF 0
#define low 0
#define high 1
#define output 0 
#define input 1
void make_low(void) 
Probe_Pin = output; // Makes probe pin an output by putting 0 in TRIS
Probe_Data = low; // Puts a zero on Probe_Pin Lat register
void make_high(void) // I question this. Should let pull up take high.
//Probe_Pin = output; // Makes Probe_Pin an output by putting 0 in TRIS
//Probe_Data = high; // Not sure this is needed as the pull up ought to do it.
Probe_Pin = input; 
void make_input(void)// Makes a floating input. We will let device drive state
// low and pull up resistor drive high
Probe_Pin = input; // Makes Probe_Pin an input by putting a 1 on TRIS
void make_output(void)// Makes a floating input. We will let device drive state
// low and pull up resistor drive high
Probe_Pin = output; // Makes Probe_Pin an output by putting 0 on TRIS
unsigned char read_pin(void) // better to use AND function
{/* This function simply returns what is on Probe_Pin (TRIS)of probe */
unsigned char read_data = 0; // variable for status of pin, 8 bits
//make_input(); // Make Probe_Pin an input by putting 1 on TRIS
Probe_Pin = input;
read_data = PinRB4 & 0x01 ;
return (read_data);
/* During the initialization sequence the bus master transmits
(TX) the reset pulse by pulling the 1-Wire bus low
for a minimum of 480µs. The bus master then releases
the bus and goes into receive mode (RX). When the bus
is released, the 5k? pullup resistor pulls the 1-Wire bus
high. When the DS18B20 detects this rising edge, it waits
15µs to 60µs and then transmits a presence pulse by pulling
the 1-Wire bus low for 60µs to 240µs.
unsigned char reset_detect(void) 
unsigned char presence_detect = 1;
make_low(); // Drive the data pin low (TRIS set to output, lat 0)
__delay_us(480); // delay 480 microseconds 
make_input(); // Make pin an input and wait for presence pulse from slave
__delay_us(70); // delay 70 microsecond (us)
presence_detect = read_pin(); //Sense presence pulse from slave.0 = present
__delay_us(420); // delay 420 microseconds (remainder of 480 read slot) 
make_high(); // Release the bus ?? Should I drive high ??
if (presence_detect == 1)
return 0;
return 1;

return presence_detect;
void write_bit(unsigned char write_bit)// Writes to probe.Maybe I shouldn't use 
//argument name same as function name ?? Th
if (write_bit) 
//writing a bit '1'
make_low(); // Drive the bus low
__delay_us(6); // delay 6 microseconds
make_high(); // Release the bus
__delay_us(64); // delay 64 microseconds

//writing a bit '0'
make_low(); // Make pin low
__delay_us(60); // delay 60 microseconds
make_high(); // Make the pin high
__delay_us(10); // delay 10 microsecond to recover
* Figure 17 illustrates that the sum of TINIT, TRC, and TSAMPLE must be less 
* than 15?s for a read time slot. Figure 18 shows that system timing margin is
* maximized by keeping TINIT and TRC as short as possible and by locating the 
* master sample time during read time slots towards the end of the 15?s period 
unsigned char read_bit(void) {// read time slots must be min of 60 uS
unsigned char read_data;
//reading a bit 
make_low(); // Make the pin low. Only needs 1 uS to initiate read
__delay_us(1); // delay 1 microseconds for read signal
//make_input(); // Makes pin a floating input, starts pulled high
Probe_Pin = input;
__delay_us(12); // causes the read to occur around ideal 13 uS read time
read_data = read_pin(); //Read the pin within 15 uS of read falling edge
// ideally near the end of 15 uS read window per datasheet.
__delay_us(47); // delay 47 uS to finish off the 60 uS read time slot 
return read_data;

void write_byte(unsigned char write_data) 
{// A cool way of reading a byte 1 bit at a time using the AND function to 
// clear all but the bit in focus and transmit each bit as a byte.
unsigned char loop;
for (loop = 0; loop < 8; loop++) {
write_bit(write_data & 0x01); //Sending LS-bit first. Masks LSB with &01
write_data >>= 1; // shift the data byte for the next bit to send

int read_byte(void) // reads 1 byte
unsigned char result=0;// variable result holds byte as a single 8 bit int. Volatile ???
unsigned char loop;
result=result | read_bit()<< loop;
return result;
int main (void)

OSCCON = 0X70; // set internal oscillator to 8MHz 
TRISA= 0; // configure port A as output
TRISB = 1; // configure port B as input
AD1PCFG=0xffff; // configure port B input as digital

U1BRG = 25; //Baud rate set to 9600
IPC2bits.U1RXIP = 4; //interrupt priority level for receive
IPC3bits.U1TXIP = 4; //interrupt ptiority level for transmit
U1STA = 0;
U1MODE = 0; //8-bit data, no parity, 1 stop bit
U1MODEbits.UARTEN = 1; //enable the module

U1STAbits.UTXISEL0 = 0;
U1STAbits.UTXISEL1 = 1; //interrupt generated transmit is complete
IFS0bits.U1RXIF = 0; //clear receive interrupt flag
U1STAbits.UTXEN = 1; //enable transmission
IEC0bits.U1RXIE = 1; //UART receiver interrupt enabled
IEC0bits.U1TXIE = 0; //UART transmitter interrupt disabled

reset_detect();// Checks for reset and if found prints Device Detect to LCD
//if (presence_detect == 0){ // If presence_detect = 0, meaning device present
make_output(); // Set pin to output for next writes
write_byte(0xCC); // Skip ROM, we only have one device and read temper
write_byte(0x44); // Convert T
__delay_ms(750); // For 12 bit res, the max conversion time=750 ms
write_byte(0xCC); // Skip ROM
write_byte(0xBE);// Read Scratchpad Command
unsigned char byte1= read_byte();// Returns 1st byte of Result
unsigned char byte2= read_byte();// Returns 2nd byte of Result 

unsigned int bytes1and2= (byte2<<8 | byte1) ; // combine bytes by shifting byte
double cvalue = bytes1and2 / 16.0;
double fvalue = 1.8*cvalue + 32.0;

if (cvalue>=20)//if temperature is higher than 20 

void uart1putchar(char ch)
while(U1STAbits.UTXBF); //transmit only if TX buffer is empty
U1TXREG = ch;
char uart1getchar()
char temp;
while(IFS0bits.U1RXIF == 0); //wait for the buffer to fill up
temp = U1RXREG; //copy char
IFS0bits.U1RXIF = 0; //reset interrupt
return temp; //return received char

could you please tell me what to do next
Thanking you
post edited by Gouthami c v - 2019/08/12 03:33:47
Gouthami c v
Starting Member
  • Total Posts : 84
  • Reward points : 0
  • Joined: 2019/07/11 01:55:44
  • Location: 0
  • Status: offline
Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/12 03:37:43 (permalink)
Hi everyone,
please help me i have seen so many example of uart code but i couldn't able to do.I need to display my cvalue in above code, please help me for this. 
Thanking you.
post edited by Gouthami c v - 2019/08/12 03:48:26
Page: < 12 Showing page 2 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5