Help with DS18B20 program


Closed Thread
Results 1 to 39 of 39

Hybrid View

  1. #1
    Join Date
    Nov 2007
    Posts
    20


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Bruce View Post
    BINGO. You won't need a PIC guru

    You're using the internal oscillator, but you're not writing to OSCCON to configure it for the
    default 4MHz PBP will assume

    Drop OSCCON = %01100000 in there, and I'll bet it starts working just as expected. Check
    out the default value of OSCCON at power-up.

    Its default setting is for 1MHz. Problem solved. Even without a PIC guru..;o}
    Bruce -- It works like a charm now!

    Can't believe .... all I needed to add was that one line for the OSCCON setup. Thanks so much!

    BTW, one line of my program should be corrected. The original line - OWIN Comm_Pin, 2, [RAWTEMP] should be changed to ...
    OWIN Comm_Pin, 2, [RAWTEMP.LowByte, RAWTEMP.HighByte] - since OWIN can read in either a BIT or a BYTE only.
    Last edited by presario1425; - 21st November 2007 at 02:01.

  2. #2


    Did you find this post helpful? Yes | No

    Default

    Just a little contribution to this thread. This code should give you ºC, ºF and Kelvin to 100th of a degree or 100th Kelvin. It also works properly for both positive ºF and negative ºF. This seems to an over looked problem all the time. Especially it can be +ºC/+ºF, -ºC/+ºF or -ºC/-ºF.

    Code:
    ;************************ 16F88 Configuration Fuses ****************************
    
    @ __CONFIG _CONFIG1, _CP_OFF & _CCP1_RB3 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _MCLR_OFF & _PWRTE_ON & _WDT_ON & _XT_OSC
    ; Code Protection - _CP_OFF, CP_ON
    ; CCP1 Pin Selection - _CCP1_RB0, _CCP1_RB3
    ; In-Circuit Debugger Mode - _DEBUG_OFF, _DEBUG_ON
    ; Flash Program Memory Write Enable - _WRT_PROTECT_OFF, _WRT_PROTECT_256, _WRT_PROTECT_2048, _WRT_PROTECT_ALL
    ; Data EE Memory Code Protection - _CPD_OFF, _CPD_ON
    ; Low-Voltage Programming Enable - _LVP_OFF, _LVP_ON
    ; Brown-Out Reset - _BODEN_OFF, _BODEN_ON
    ; RA5/MCLR/Vpp Pin Function - _MCLR_OFF, _MCLR_ON
    ; Power-Up Timer - _PWRTE_OFF, _PWRTE_ON
    ; Watchdog Timer - _WDT_OFF, _WDT_ON
    ; Oscillator Selection - _EXTRC_CLKOUT, _EXTRC_IO, _INTRC_CLKOUT, _INTRC_IO, _EXTCLK, _HS_OSC, _XT_OSC, _LP_OSC, 
    ;
    @ __CONFIG _CONFIG2, _IESO_OFF & _FCMEN_ON
    ; Oscillator Switchover - _IESO_OFF, _IESO_ON
    ; Fail-Safe Clock Monitor - _FCMEN_OFF, _FCMEN_ON
    
    ;*******************************************************************************
    
    ;-------------------------  Port Assignments --------------------------------
    
    ;   PORTB.6   DS18B20 - Data Line
    
    ;--------------------------- Setup DEFINE;s ----------------------------------
    
        DEFINE  OSC 4
        DEFINE  ADC_BITS        10      ; Set resolution of conversion
        DEFINE  ADC_CLOCK       8       ; Set clock source (x/FOSC or FRC)
        DEFINE  ADC_SAMPLEUS    50      ; Set sampling time (in uS)
        
    ;------------------------  Port Initialization -------------------------------
    
        CCP1CON = %00000000         ; Disable CCP Module
        SSPCON.5 = 0                ; Disable SSP Module
        TXSTA.5 = 0                 ; Disable AUSART Tx
        RCSTA.7 = 0                 ; Disable Serial Port
        ANSEL = %01000000           ; Set analog ports to digital mode except RB7/AN6
        CMCON = %00000111           ; Turn off comparator
        OPTION_REG.6 = 0
        ADCON1 = %10000000    
    
        TRISA = %100000
        TRISB = %11110101
    
    ;----------------------------- Debug Settings ----------------------------------
    
        cmd                 con     254         ; Control byte
        clr                 con     1           ; Clear the display
        line1               con     128         ; Move cursor to home position on line 1
        line2               con     192         ; Move cursor to home position on line 2
        line3               con     148         ; Move cursor to home position on line 3
        line4               con     212         ; Move cursor to home position on line 4
    
    ; ----------------------------- Port Aliases ---------------------------------
    
        DSDataPin           var     portb.6
        
    ;---------------------- Miscellaneous Variables ------------------------------
    
        i                   var     byte            ; Loop counter
        x                   var     word            ; Variable for calculations
        DataBit             var     bit             ; Bit shift variable
        dummy               var     word            ; Dummy variable for DIV32 function
        DataByte            var     byte            ; Byte to perform CRC calculation on
        DQ                  var     byte[9]         ; 9 Byte Arrray for DS18B20 data
        CRCCalc             var     byte            ; Calculated CRC value
        NegBit              var     rawtemp.Bit11   ; +/- indicator: 1 = < 0ºC
        RawTemp             var     word            ; Raw Temperature
        SignC               var     byte            ; +/- Sign for celcius temperature
        SignF               var     byte            ; +/- Sign for fahrenheit temperature
        TempC               var     word            ; Temperature in ºC
        TempF               var     word            ; Temperature in ºF
        TempK               var     word            ; Temperature in Kelvin
        CRCError            var     byte            ; Temperature Sensor CRC Error Flag
    
    ;--------------------------- Main Program Loop -------------------------------
    
    main:
        gosub ReadDS18B20
        lcdout cmd,line1,SignC,dec3 TempC/100,".",dec2 TempC//100,223,"C"
        lcdout cmd,line2,SignF,dec3 TempF/100,".",dec2 TempF//100,223,"F"
        lcdout cmd,line3," ",dec3 TempK/100,".",dec2 TempK//100," K"
        goto main
    
    ;---------------------------- Read DS18B20 Sensor ------------------------------
    
    ReadDS18B20:
        owout DSDataPin,1,[$CC,$44]     ; Tell sensor to start a temperature conversion
        owout DSDataPin,1,[$CC,$BE]     ; Tell sensor you want to read the temperature
        owin DSDataPin,0,[STR DQ\9]     ; Receive temperature from the sensor
        RawTemp.Byte0 = DQ[0]           ; Extract the temperature portion and put it in its one word variable
        RawTemp.Byte1 = DQ[1]                      
        gosub GetCRC                    ; Calculate the CRC for comparison
        gosub ConvertCelcius            ; Convert temperature to ºC
        gosub ConvertToFahrenheit       ; Convert temperature to ºF
        gosub ConvertToKelvin           ; Convert temperature to K
        return
    
    ;------------------------ Calculate the CRC from Byte 9 ------------------------
    
    GetCRC:
        for x = 0 to 7                  ; Get CRC for each of the 8 bytes
        DataByte = DQ[x]                ; Assign array pointer using value of x
        gosub CalcCRC                   ; Get CRC value
        next x                          ; Repeat until all bytes are done
        if DQ[8] <> CRCCalc then        ; Do the CRC values match?
            CRCError = 1                ; Set flag indicating a problem with this sensor
            else                        
            CRCError = 0                ; Set flag indicating no problem with this sensor
        endif                           
        CRCCalc = 0                     ; Reset CRC calculation variable
        return
    
    ;--------------------- CRC Bit Calcuation Method -------------------------------
    
    CalcCRC:
        for i = 0 to 7                      ; Do for all 8 bits in DataByte
        DataBit = CRCCalc.0 ^ DataByte.0    ; XOR bit0 of DataByte and CRC
        DataByte = DataByte >> 1            ; Position DataByte for next bit test
        if DataBit = 0 then Shift           ; If test bit not set, just shift CRCCalc
        CRCCalc = CRCCalc ^ $18             ; If set, account for EXOR feedback
    
    shift:
        CRCCalc = CRCCalc >> 1              ; Shift right the CRCCalc byte
        CRCCalc.7 = DataBit                 ; CRC bit 0 to bit bucket
        next i                              ; Data bit rotates into CRC bit 7
        return
    
    ;--------------------------- Convert to Celcius --------------------------------
    
    ConvertToCelcius:
        if negbit = 1 then 
            SignC = "-"
            else
            SignC = "+"
        endif
        if SignC = "-" then
            dummy = 0
            RawTemp.Byte0 = RawTemp.Byte0 ^ 255
            RawTemp.Byte1 = RawTemp.Byte1 ^ 255
            dummy = 625 * RawTemp + 1
           	TempC = DIV32 100
           	else
            dummy = 625 * RawTemp
            TempC = DIV32 100
        endif
        return
        
    ;------------------------ Convert to Fahrenhiet --------------------------------
    
    ConvertToFahrenheit:
        if SignC = "+" then
            SignF = "+"
            dummy = TempC * 18
            TempF = div32 10
            TempF = TempF + 3200
            else
            TempF = (tempc + 5000) * 900
            TempF = DIV32 500
            if TempC => 1778 and SignC = "-" then
                TempF = TempF - 12200
                SignF = "-"
                else				
                TempF = 12200 - TempF
                SignF = "+"
        	endif    
        endif
    	return
    
    ;-------------------------- Convert to Kelvin ----------------------------------
    
    ConvertToKelvin:
    	if SignC = "-" then
        	TempK = 27315 - TempC
            else
        	TempK = tempC + 27315
      	endif
      	return

  3. #3
    Join Date
    Oct 2007
    Location
    Vancouver, BC, Canada
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I just copied the code that Cocacolakid posted, modified a bit, compile the program with version 2.46 of PbP, program run ok. When compile with version 2.50 of PBP, program just stuck/stop at "hello". Both case, I use PM assembler. The chip i am using is pic16f876 @20MHz.

    I don't know if v2.50pbp has problem or with the 1-wire commands (onwin and owout).

    i attached the file below for checking.

    If anyone has time, please confirm/verify/correct me if i did anything wrong.

    Thanks,
    Attached Files Attached Files

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by KVLV View Post
    Hi,
    I just copied the code that Cocacolakid posted, modified a bit, compile the program with version 2.46 of PbP, program run ok. When compile with version 2.50 of PBP, program just stuck/stop at "hello". Both case, I use PM assembler. The chip i am using is pic16f876 @20MHz.

    I don't know if v2.50pbp has problem or with the 1-wire commands (onwin and owout).

    i attached the file below for checking.

    If anyone has time, please confirm/verify/correct me if i did anything wrong.

    Thanks,
    Hi KVLV,
    when you port from one PIC to Another, be aware the configs are likely different, your copied code compiles in MPASM perfectly with the configs commented out as you have them, included they will not compile as the 16f88 chip has different configs I E config1, config2, the 16f876.inc file has the default configs built in which must be commented out with a ; if you wish not to use them, if you do this then you may include your own config like:
    @ __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_ON & _PWRTE_ON & _WDT_ON & _HS_OSC

    and it will compile in VER 2.50
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5
    Join Date
    Oct 2007
    Location
    Vancouver, BC, Canada
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    Joe,
    maybe I rephrase it.
    The program I have attached compile ok with v2.46 and v2.50, no error. I use pic16f876.
    when compile using version 2.46 of pbp, the program is running ok when downloaded to the chip using bootloader. it displays/transmit the temperature reading to the lcd/terminal ok.

    the problem only when compile using v2.50 of pbp, the program (hex file) downloaded/program to the chip using bootloader, the program cannot run/stop/freeze.

    i commented out the configurations because it is not needed when using bootloader (i think it is ok to do so).

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by KVLV View Post
    Joe,

    i commented out the configurations because it is not needed when using bootloader (i think it is ok to do so).
    Without the config statements your program uses the default config statement located in the .inc file of the same name/number as your chip, in this case 16f876 , and the default is for XT_OSC not HS_OSC, and could account for your program's failure to launch as it is set to run at 20 MHZ, the PIC OSC requires more power to operate at 20 than at 4 and the HS_OSC provides that power.
    JS
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7
    Join Date
    Oct 2007
    Location
    Vancouver, BC, Canada
    Posts
    33


    Did you find this post helpful? Yes | No

    Default

    thanks Joe, lets me try again. and post if any result.

  8. #8
    Join Date
    Sep 2008
    Location
    Maine, USA
    Posts
    81


    Did you find this post helpful? Yes | No

    Default Re: Help with DS18B20 program

    I want to thank CocaColaKid for posting his beautiful, awesome, well documented code! I was under the gun to get a temperature sensor working and ported it into my code and it works flawlessly. Thanks for saving me hours of head bashing!

    [QUOTE=CocaColaKid;46483]Just a little contribution to this thread. This code should give you ºC, ºF and Kelvin to 100th of a degree or 100th Kelvin. It also works properly for both positive ºF and negative ºF. This seems to an over looked problem all the time. Especially it can be +ºC/+ºF, -ºC/+ºF or -ºC/-ºF.
    "Do or do not, there is no try" Yoda

Similar Threads

  1. Presetting Configuration Fuses (PIC Defines) into your Program
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 82
    Last Post: - 15th December 2013, 09:54
  2. Replies: 1
    Last Post: - 23rd May 2009, 09:22
  3. Compile and Program help using PICKit2
    By ozarkshermit in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 10th March 2009, 14:51
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. PIC16F684 Program question
    By Nicholas in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 28th December 2006, 14:30

Members who have read this thread : 3

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts