• Pic« Basic


  • Ds2438 code

    The DS2438 is a battery monitor IC using the 1-Wire protocol. It measures temperature, voltage and current to name just three of its functions.

    Below is the code I've come up with that reads temperature, voltage and current and displays on LCD.
    Code:
    '****************************************************************
    '*  Name    : DS2438.BAS                                        *
    '*  Author  : Flosi Gu­mundsson                                 *
    '*  Notice  : Copyright (c) 2010 Flosi Gu­mundsson              *
    '*          : All Rights Reserved                               *
    '*  Date    : 13/03/2010                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :  Pogram reads temperature, voltage and current    *
    '*          :  from DS2438 and displays on LCD                  *
    '****************************************************************
    
            Include    "18F452.BAS"
    
            Symbol DQ = PORTA.4                             'DS2438 I/O port bit
            Dim Temp As Word                      'Temperature low/high bytes 
            Dim NEGPOS As Byte                      'Temperature char "-" or "+"
            Dim Result As Byte
            Dim Con As Byte
            Dim Volts As Word
            Dim Amps As Word
            Dim threshold As Byte
            Dim crc As Byte
            Dim mod As Byte
            DelayMS 500
            Cls
                                  
    '        OWrite DQ, 3, [$CC,$4E,$00,$0F]        ' Skip Rom,Write_Scratchpad,Page0, measure Vdd
            OWrite DQ, 3, [$CC,$4E,$00,$07]        ' Skip Rom,Write_Scratchpad,Page0, measure Vad
    loop:    DelayMS 1000
            OWrite DQ,3,[$CC,$44]               ' Skip Rom,Convert_T
            Repeat
            '    DelayMS 10
                ORead DQ,4,[Result]
            Until Result=1        
            OWrite DQ,1,[$CC,$B4]                ' Skip Rom,Convert_V
            OWrite DQ,3,[$CC,$B8,$00]           ' Skip Rom,Recall_Memory
            OWrite DQ,1,[$CC,$BE,$00]           ' Skip Rom,Read_Scratchpad
            ORead DQ,2,[Con,Temp.LowByte,Temp.HighByte,Volts.LowByte,Volts.HighByte,Amps.LowByte,Amps.HighByte,threshold,crc]
            Repeat
            '    DelayMS 10
                ORead DQ,4,[Result]
            Until Result=1
            If Temp.15 = 1 Then
            NEGPOS = "-"
            Temp = ~Temp + 1
            Else
            NEGPOS = "+"
            EndIf
            Print At 0,0, NEGPOS, Dec Temp.HighByte,".",
            Temp.LowByte=Temp.LowByte>>3 Temp.LowByte                   
            Print Dec2 Temp.LowByte *3125
            NEGPOS = "+"
            mod =Volts//100        
            Print At 1,0,Dec Volts/100,".",Dec2 mod,"V"
            Print At 2,0, Dec Amps/(4096*.1)
            GoTo loop
            End
    This article was originally published in forum thread: Ds2438 code started by flosi View original post