• AVR Freaks

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

Page: 12 > Showing page 1 of 2
Author
Gouthami c v
Starting Member
  • Total Posts : 65
  • Reward points : 0
  • Joined: 2019/07/11 01:55:44
  • Location: 0
  • Status: offline
2019/08/02 00:26:14 (permalink)
0

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

Hi everyone,
I'm new to embedded,i am using DS18B20 (temperature sensor)with PIC240FKA200 to read temperature.But it reads only 2056, i am not getting what is the issue please help me for this.
Here is my code
 

 
/******************************************************************************/
/* Files to Include */
/******************************************************************************/
 
/* Device header file */
#if defined(__XC16__)
#include <xc.h>
#elif defined(__C30__)
#if defined(__PIC24E__)
#include <p24Exxxx.h>
#elif defined (__PIC24F__)||defined (__PIC24FK__)
#include <p24Fxxxx.h>
#elif defined(__PIC24H__)
#include <p24Hxxxx.h>
#endif
#endif
 
#include <stdint.h> /* Includes uint16_t definition */
#include <stdbool.h> /* Includes true/false definition */
#include "system.h" /* System funct/params, like osc/peripheral config */
#include "user.h" /* User funct/params, such as InitApp */
 
 
 
// PIC24F04KA200 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)
 
// FOSCSEL
#pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC oscillator with divide-by-N with PLL module (FRCDIV+PLL))
#pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled))
 
// FOSC
#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)
 
// FWDT
#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 = ON // Windowed Watchdog Timer Disable bit (Windowed WDT enabled)
#pragma config FWDTEN = ON // Watchdog Timer Enable bit (WDT enabled)
 
// FPOR
#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)
 
// FICD
#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 _XTAL_FREQ 8000000
 
/* Ds18b20 related definition */
#define Skip_Rom 0xCC
#define Convert_T 0x44
#define write_scratchpad 0x4E
#define Read_scratchpad 0xBE
#define Resolution_12bit 0x7F
 

#define DS18B20_PIN _LATB14
#define DS18B20_PIN_Dir _TRISB14
 
#define HEATER _LATA6
#define __delay_ms(x)
#define __delay_us(x)
#define ON 1
#define OFF 0
 
/******************************************************************************
*PRIVATE FUNCTION PROTOTYPES *
******************************************************************************/
 
unsigned char reset();
unsigned char read();
void write(char WRT);
 

/******************************************************************************/
/* Main Program */
/******************************************************************************/
int main (void)
{
unsigned short tempL, tempH, Temp;
unsigned int i=0;

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


while(1)
{
if(!reset())
{

write(write_scratchpad);
write(Resolution_12bit);
write(Skip_Rom);
write(Convert_T);
__delay_ms(750);

reset();
write(Skip_Rom);
write(Read_scratchpad);

tempL = read(); //read low temp value
tempH = read(); //read HIGH temp value
i=((unsigned int)tempH<<8) + (unsigned int)tempL;
Temp = i * 0.0625; //temperature resolution of 0.0625 since 12 bits are read
__delay_ms(100);
if(Temp>=20)//if temperature is higher than 20
{
HEATER = ON;
__delay_ms(100);
}
else{
HEATER = OFF;
__delay_ms(100);
}
}
}
while(1) continue;
}
 
/******************************************************************************/
unsigned char reset()
{
DS18B20_PIN_Dir =0;
DS18B20_PIN =0;
__delay_us(480);
DS18B20_PIN_Dir =1;
__delay_us(60);
if(DS18B20_PIN==0)
{
__delay_us(480);
return 0;
}
else
{
__delay_us(480);
return 1;
}
}
unsigned char read()
{
char i, result =0;
DS18B20_PIN_Dir =1;
for(i=0; i<8; i++)
{
DS18B20_PIN_Dir =0;
DS18B20_PIN =0;
__delay_us(2);
DS18B20_PIN_Dir =1;
if(DS18B20_PIN!=0)
result |= 1<<i;
__delay_us(60);
}
}
void write(char WRT)
{
char i, Cmd;
Cmd=WRT;
DS18B20_PIN_Dir =1;
for(i=0; i<8; i++)
{
if((Cmd & (1<<i))!=0)
{
// write 1
DS18B20_PIN_Dir =0;
DS18B20_PIN =0;
__delay_us(480);
DS18B20_PIN_Dir =1;
__delay_us(60);
}
else
{
// write 0
DS18B20_PIN_Dir =0;
DS18B20_PIN =0;
__delay_us(480);
DS18B20_PIN_Dir =1;
__delay_us(60);
}
}
}
 

 Thanking you
#1

21 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/02 01:30:54 (permalink)
    5 (2)
    Move the "#pragma config" lines to BEFORE all the #include lines.
    Get rid of all this fluff
    /* Device header file */
    #if defined(__XC16__)
     #include <xc.h>
    #elif defined(__C30__)
     #if defined(__PIC24E__)
     #include <p24Exxxx.h>
     #elif defined (__PIC24F__)||defined (__PIC24FK__)
     #include <p24Fxxxx.h>
     #elif defined(__PIC24H__)
     #include <p24Hxxxx.h>
     #endif
    #endif

    and just put
    #include <xc.h>

     
    Replace
    #define _XTAL_FREQ 8000000

    with
    #define FCY 8000000
    #include <libpic30.h>  //for delay routines

     
    Get rid of these lines
    #define __delay_ms(x)
    #define __delay_us(x)

     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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/02 02:12:27 (permalink)
    0
    Thank you for replying 
    I have changed code as you told

     
     
     
    /******************************************************************************/
    /* Files to Include */
    /******************************************************************************/
    // PIC24F04KA200 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)
     
     
     
    // FOSCSEL
    #pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC oscillator with divide-by-N with PLL module (FRCDIV+PLL))
    #pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled))
     
     
     
    // FOSC
    #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)
     
     
     
    // FWDT
    #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 = ON // Windowed Watchdog Timer Disable bit (Windowed WDT enabled)
    #pragma config FWDTEN = ON // Watchdog Timer Enable bit (WDT enabled)
     
     
     
    // FPOR
    #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)
     
     
     
    // FICD
    #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>
    #include <stdint.h> /* Includes uint16_t definition */
    #include <stdbool.h> /* Includes true/false definition */
    #include "system.h" /* System funct/params, like osc/peripheral config */
    #include "user.h" /* User funct/params, such as InitApp */
    #define FCY 8000000
    #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 Resolution_12bit 0x7F
     
     
     

    #define DS18B20_PIN _LATB14
    #define DS18B20_PIN_Dir _TRISB14
     
     
     
    #define HEATER _LATA6
     
     
     
    #define ON 1
    #define OFF 0
     
     
     
    /******************************************************************************
    *PRIVATE FUNCTION PROTOTYPES *
    ******************************************************************************/
     
     
     
    unsigned char reset();
    unsigned char read();
    void write(char WRT);
     
     
     

    /******************************************************************************/
    /* Main Program */
    /******************************************************************************/
    int main (void)
    {
    unsigned short tempL, tempH, Temp;
    unsigned int i=0;

    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


    while(1)
    {
    if(!reset())
    {

    write(write_scratchpad);
    write(Resolution_12bit);
    write(Skip_Rom);
    write(Convert_T);
    __delay_ms(750);

    reset();
    write(Skip_Rom);
    write(Read_scratchpad);

    tempL = read(); //read low temp value
    tempH = read(); //read HIGH temp value
    i=((unsigned int)tempH<<8) + (unsigned int)tempL;
    Temp = i * 0.0625; //temperature resolution of 0.0625 since 12 bits are read

    if(Temp>=20)//if temperature is higher than 20
    {
    HEATER = ON;
    __delay_ms(100);
    }
    else{
    HEATER = OFF;
    __delay_ms(100);
    }
    }
    }
    while(1) continue;
    }
     
     
     
    /******************************************************************************/
    unsigned char reset()
    {
    DS18B20_PIN_Dir =0;
    DS18B20_PIN =0;
    __delay_us(480);
    DS18B20_PIN_Dir =1;
    __delay_us(60);
    if(DS18B20_PIN==0)
    {
    __delay_us(480);
    return 0;
    }
    else
    {
    __delay_us(480);
    return 1;
    }
    }
    unsigned char read()
    {
    char i, result =0;
    DS18B20_PIN_Dir =1;
    for(i=0; i<8; i++)
    {
    DS18B20_PIN_Dir =0;
    DS18B20_PIN =0;
    __delay_us(2);
    DS18B20_PIN_Dir =1;
    if(DS18B20_PIN!=0)
    result |= 1<<i;
    __delay_us(60);
    }
    }
    void write(char WRT)
    {
    char i, Cmd;
    Cmd=WRT;
    DS18B20_PIN_Dir =1;
    for(i=0; i<8; i++)
    {
    if((Cmd & (1<<i))!=0)
    {
    // write 1
    DS18B20_PIN_Dir =0;
    DS18B20_PIN =0;
    __delay_us(480);
    DS18B20_PIN_Dir =1;
    __delay_us(60);
    }
    else
    {
    // write 0
    DS18B20_PIN_Dir =0;
    DS18B20_PIN =0;
    __delay_us(480);
    DS18B20_PIN_Dir =1;
    __delay_us(60);
    }
    }
    }
     
     
     
     

     
     Program debugger is getting halt at__delay_us(480)
     
    post edited by Gouthami c v - 2019/08/02 02:39:10
    #3
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/02 02:55:26 (permalink)
    5 (2)
    Turn off the WDT.
    Do not turn it on until you understand how it works, and how to stop it resetting your processor.
     
    You seem to have adapted some code from another PIC which doesn't have LAT registers.
    You are using a single definition for DS18B20_PIN to both write and read the pin.
    This is wrong. Writes should be to _LATB14 but reads should be from _PORTB14.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    maxruben
    Super Member
    • Total Posts : 3354
    • Reward points : 0
    • Joined: 2011/02/22 03:35:11
    • Location: Sweden
    • Status: offline
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/02 03:31:31 (permalink)
    5 (1)
    Do you have a pull-up resistor on the DS18B20 data line?
    You are not waiting to let the pull-up resistor pull up the data line at all (in case of a read "1" slot), after you have done the initial 2us of bringing the data line low in the read slot in your read() function. See figure 16 in the datasheet: https://datasheets.maximintegrated.com/en/ds/DS18B20.pdf
    You have to wait until the pull-up resistor would have pulled the data line high in case of a read "1" slot, but no longer than 15us. The master should sample the data line just before the 15 us sample time since the master brought the data line low. Since you are doing a __delay_us(2) just before you turn the PIC pin into an input, you should wait another 12-13 us before sampling the pin.
     
    I am not sure why you get 128 as a result but you are mixing floating point/double constants with short integers when doing Temp=i*0.0625; I am not quite sure how this is promoted and converted by the XC16 compiler.
     
    Just compare i with the 16 bit int that corresponds to your switching temperature.
     
    if (i<(20<<4))
     
    i should not be unsigned since the sensor can read negative temperature and will report this as a 2 complement number. Se figure 4 in the datasheet.
     
    Good luck!
     
    /Ruben
     
     
    #5
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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/04 23:34:14 (permalink)
    0
    Hi,
    I have changed code as per ur guide its worked.Thank you so much for ur support.
     Thank you ric and maxruben
    #6
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/04 23:38:58 (permalink)
    5 (1)
    Post your final code. It could be useful to other people who find this topic with a search.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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/04 23:43:50 (permalink)
    0
    Final code

    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.
    #include <xc.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define FCY 16000000
    #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 _LATB14 // this is the data line from Maxim probe.
    #define Probe_Pin _TRISB14 // Port pin for Maxim probe data
    #define PinRB14 PORTBbits.RB14
    #define HEATER _LATA6
    #define ON 1
    #define OFF 0
    #define low 0
    #define high 1
    #define output 0
    #define input 1
    // ***** Prototypes ******
    void make_low(void);
    void make_high(void);
    void make_input(void);
    void make_output(void);
    unsigned char read_pin(void);
    unsigned char reset_detect(void);
    void write_bit(unsigned char write_bit);
    unsigned char read_bit(void);
    int read_byte(void);
    void write_byte(unsigned char write_data);
    void read_temp(void);
    unsigned char presence_detect;
    int byte1 = 0;
    int byte2 = 0;
    long int bytes1and2 = 0;
    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 = PinRB14 & 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)
    {
    __delay_us(480);
    return 0;
    }
    else
    {
    __delay_us(480);
    return 1;
    }
    __delay_ms(2000);
    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
    }
    else
    {
    //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
    }
    }
    /**********************************************************************
    * SEE FIGURES 16 - 18 OF MAXIM DATASHEET FOR READ TIMING
    * 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
    {
    int result=0;// variable result holds byte as a single 8 bit int. Volatile ???
    int loop;
    for(loop=0;loop<8;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


    while(1)
    {
    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
    reset_detect();
    write_byte(0xCC); // Skip ROM
    write_byte(0xBE);// Read Scratchpad Command
    int byte1= read_byte();// Returns 1st byte of Result
    int byte2= read_byte();// Returns 2nd byte of Result

    // Don't know why you make it long int. Why not just int?
    long int bytes1and2= (byte2<<8 | byte1) ; // combine bytes by shifting byte2

    double cvalue = bytes1and2 / 16.0;
    double fvalue = 1.8*cvalue + 32.0;

    if (cvalue>20)//if temperature is higher than 20
    {
    HEATER = ON;
    __delay_ms(100);
    }
    else{
    HEATER = OFF;
    __delay_ms(100);
    }
    }
    }

    #8
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/05 00:31:07 (permalink)
    4 (1)
    Your config lines have vanished. They should be in there.
     
    You don't appear to need either of these headers:
    #include <stdio.h>
    #include <stdlib.h>

     
    You don't need prototypes for all these functions, as you've defined them in "bottom up" order anyway.
    // ***** Prototypes ******
    void make_low(void);
    void make_high(void);
    void make_input(void);
    void make_output(void);
    unsigned char read_pin(void);
    unsigned char reset_detect(void);
    void write_bit(unsigned char write_bit);
    unsigned char read_bit(void);
    int read_byte(void);
    void write_byte(unsigned char write_data);
    void read_temp(void);

    The return value and scratch variable inside read_byte() should be "unsigned char", not "int"
     
    Once you've done that, you can also make "byte1" and "byte2" unsigned char as well.
     
    Once you've done that, your following comment is quite true
     // Don't know why you make it long int. Why not just int?
     long int bytes1and2= (byte2<<8 | byte1) ; // combine bytes by shifting byte2

    i.e. "bytes1and2" should be an "unsigned int".
    (Or signed if it's a "two's-complement" value you are reading.)
     
     
     
     
    post edited by ric - 2019/08/05 00:34:48

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #9
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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/05 02:14:55 (permalink)
    0
    Hi,
    I have corrected code

    /*
    * File: newmain.c
    * Author: gouthami
    *
    * Created on August 5, 2019, 2:35 PM
    */
    // PIC24F04KA200 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)
    // FOSCSEL
    #pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC oscillator with divide-by-N with PLL module (FRCDIV+PLL))
    #pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled))
    // FOSC
    #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)
    // FWDT
    #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))
    // FPOR
    #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)
    // FICD
    #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 16000000
    #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 _LATB14 // this is the data line from Maxim probe.
    #define Probe_Pin _TRISB14 // Port pin for Maxim probe data
    #define PinRB14 PORTBbits.RB14
    #define HEATER _LATA6
    #define ON 1
    #define OFF 0
    #define low 0
    #define high 1
    #define output 0
    #define input 1
    unsigned char presence_detect;
    int byte1 = 0;
    int byte2 = 0;
    long int bytes1and2 = 0;
    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 = PinRB14 & 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)
    {
    __delay_us(480);
    return 0;
    }
    else
    {
    __delay_us(480);
    return 1;
    }
    __delay_ms(2000);
    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
    }
    else
    {
    //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
    }
    }
    /**********************************************************************
    * SEE FIGURES 16 - 18 OF MAXIM DATASHEET FOR READ TIMING
    * 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;
    for(loop=0;loop<8;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


    while(1)
    {
    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
    reset_detect();
    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 byte2

    double cvalue = bytes1and2 / 16.0;
    double fvalue = 1.8*cvalue + 32.0;

    if (cvalue>=20)//if temperature is higher than 20
    {
    HEATER = ON;
    __delay_ms(100);
    }
    else{
    HEATER = OFF;
    __delay_ms(100);
    }
    }
    }
     
     
     

    Thanking you
    #10
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/05 02:36:28 (permalink)
    4 (1)
    You still have unnecessary global definitions for
    int byte1 = 0;
    int byte2 = 0;
    long int bytes1and2 = 0;

     
    You calculate
    double fvalue = 1.8*cvalue + 32.0;

    but never use that value.
     
    If your only aim is to detect when cvalue is more than 20, there's no point doing the calculation in your code at all.
    Just reverse the calculation on paper. When cvalue = 20 => bytes1and2 = 20 * 16 = 320
    So you could just test for when bytes1and2 > 320.
     
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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/05 02:42:12 (permalink)
    0
    Thank you i'll correct it and check in hardware
    #12
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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/05 21:15:24 (permalink)
    0
    Hi,
    Its worked here is my code
     
    /*
    * File: newmain.c
    * Author: gouthami
    *
    * Created on August 5, 2019, 2:35 PM
    */
    // PIC24F04KA200 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)
    // FOSCSEL
    #pragma config FNOSC = FRCPLL // Oscillator Select (Fast RC oscillator with divide-by-N with PLL module (FRCDIV+PLL))
    #pragma config IESO = OFF // Internal External Switch Over bit (Internal External Switchover mode disabled (Two-Speed Start-up disabled))
    // FOSC
    #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)
    // FWDT
    #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))
    // FPOR
    #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)
    // FICD
    #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 16000000
    #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 _LATB14 // this is the data line from Maxim probe.
    #define Probe_Pin _TRISB14 // Port pin for Maxim probe data
    #define PinRB14 PORTBbits.RB14
    #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 = PinRB14 & 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)
    {
    __delay_us(480);
    return 0;
    }
    else
    {
    __delay_us(480);
    return 1;
    }
    __delay_ms(2000);
    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
    }
    else
    {
    //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
    }
    }
    /**********************************************************************
    * SEE FIGURES 16 - 18 OF MAXIM DATASHEET FOR READ TIMING
    * 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;
    for(loop=0;loop<8;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


    while(1)
    {
    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
    reset_detect();
    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 byte2



    if (bytes1and2>=320)//if temperature is higher than 20
    {
    HEATER = ON;
    __delay_ms(100);
    }
    else{
    HEATER = OFF;
    __delay_ms(100);
    }
    }
    }
     
     
    I have one question is there any way to display data on monitor?could you please let me know.
     
    Thanking you.
     
    #13
    maxruben
    Super Member
    • Total Posts : 3354
    • Reward points : 0
    • Joined: 2011/02/22 03:35:11
    • Location: Sweden
    • Status: offline
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/05 22:56:23 (permalink)
    0
    You can transmit the data over a serial port (UART) and look at it with a terminal software like putty or realterm. If you don't have a serial port on your computer, you can use a USB to RS232 converter.
     
    Just make sure that interrupts (if you use them) will not interfere with the 1-wire timing.
     
    /Ruben
    #14
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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 01:55:41 (permalink)
    0
    Hi,
    I am trying to configure UART to PIC24F04KA201 for temperature sensor(DS18B20) from yesterday,but i couldn't able to do it so i am asking help.Could any one help me. Just i have done uart configuration, after i couldn't able to do.

     
    void uart_setup(void) {

    IFS0bits.U1TXIF = 0; //clear transmit interrupt flag
    IEC0bits.U1TXIE = 1; //UART transmitter interrupt enabled
    IPC2bits.U1RXIP = 4; //interrupt priority level for receive
    IPC2bits.U1TXIP = 4; //interrupt priority level for transmit
     
    U1BRG = 25; //Baud rate set to 9600

    U1MODE = 0; //8-bit data, no parity, 1 stop bit

    U1STA = 0;
    U1STAbits.UTXISEL0 = 0;
    U1STAbits.UTXISEL1 = 0; //interrupt generated transmit is complete

    U1MODEbits.UARTEN = 1; //enable the module
    U1STAbits.UTXEN = 1; //enable transmission
     

     
     Could help me for further step.
    Thanking you.
    post edited by Gouthami c v - 2019/08/07 02:05:21
    #15
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/07 02:17:22 (permalink)
    0
    Do NOT enable transmit interrupts!
    The simplest code just polls the transmitter ready flag.
    Using interrupts requires an interrupt service routine, output FIFO buffer, and a lot of extra code.
     
    You did not show how you tried to send characters, or reveal what happened when you tried to use this code.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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 02:29:39 (permalink)
    0
    I have one more doubt how to consider RB2(pin6) and RB7(pin11) are RxD & TxD respectively, in PIC24F04KA201.
     
     
     
     
     
    Thanking you.
     
    #17
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/07 02:33:06 (permalink)
    0
    I don't understand what you mean by "consider", or what you are asking.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #18
    Gouthami c v
    Starting Member
    • Total Posts : 65
    • 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 02:35:33 (permalink)
    0
    I mean how to select RB2 and RB7 as RXD and TXD.
    #19
    ric
    Super Member
    • Total Posts : 23591
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: DS18B20(Temperature sensor) interface with PIC240FKA200 to turn on HEATER 2019/08/07 02:40:25 (permalink)
    0
    That chip does not have PPS, so you have no choice which pins are used.
    Just enabling the UART makes those pins become RXD and TXD, as explained in the datasheet.
    You just have to make sure the input pin (U1RX) is not in analog mode, but that is not a consideration on your PIC, as that pin is not analog capable.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5