How to Smart Battery Monitor - DS2438

First of all, I DO NOT advertise any product, but I was really amased by capabilities of the IC...

 

Just an idea on what we will be working on... Enjoy the vid!!!

 

http://www.youtube.com/watch?v=lkWBCdWDAr4

 

The DS2438 is a everything about the battery IC... It can measure Voltage of the battery and record it, it can measure current in and out the battery pack... It can also measure the temperature of the pack... And also has some spare EEPROM room... A all in one chip!!! The communication protocol is 1wire so, I was not just getting the readings directly... In 1wire communication many devices can exist on the same bus. So the microcontroller has to address every device seperatelly... How? Every 1wire device has it's special ID number which is laser in the ROM. In order to select a device, you firstly have to reset the device with a pulse. After that the device repplies with a presence pulse. Then the master has to choose a ROM function. There are 4 ROM functions in every 1wire device...

The READ ROM function which makes the device to sent the lasered ID to the master and then the master gains access to the CONTROL functions of the device.

The MATCH ROM function in which the master sents out 64bit. If all these 64bit match the device special ID then the device is accessible to the control function. If not, the device is inaccessible.

The SEARCH ROM function. Actually I have never used it, so I may be mistaken. When selected this function makes the device to sent 128bits. These 128bits are actually the 64bits of the lasered ROM. But how can they be double??? The device firstly sents two bits, the first with the TRUE value of the first bit of the lasered ROM (64bit all together) and the other with the FALSE value. Then the master must sent a bit back to the device. If this bit matches the first one of the lasered ROM, then this continues with the next bit and so on until the last bit matches. If so the device is further accessible. If not the device is inaccessible.

The SKIP ROM function, which just skips the ROM and gives you access in the CONTROL functions of the device.

And here is the flowchart from the .pdf of the device:

Flowchart1

If multiple device exist on the same bus, only the MATCH ROM function is recommented. Otherwise, a data collition may occure resulting to wrong data.

 

Now, that was some general info on accessing a 1wire device. Let's say we have a DS2438Z and we have accessed the CONTROL functions of the device. What next???

Most 1wire devices uses a scratchpad to communicate with the master. Whatever the master wants to say to the device writes it on the scratchpad and reverse. So controlling the scratchpad of the device in co-operation with the EEPROM, It's the most important thing in the DS2438Z (plus every 1wire device).Here are the basic functions with the scratchpad: (when I refer to SP it's scratchpad)

NOTE: In DS2438 the scratchpad consists of 8 pages. The right address for every page is from h00 to h07

 

The WRITE SP function. In this function the master sents the number of the page of the SP
and then the master sents bytes of data. The maximum number of bytes is 8. If the 8th byte is reached the deviced must be reseted by the master.

The READ SP function. In this function the master sents the number of the page it wants to read and then the device starts to sent the 8 bytes each page has. If the 8th page is reached then the deviced must be reseted by the master. The reading can be ended prematurely by having the master to reset the device.

The COPY SP function. In this function the master sents the number of the page of the scratchpad which will be copied to the respective page of the EEPROM of the device. When this action ends the device must be reseted by the master.

The RECALL MEMORY function. In this function the master sents the number of the page of the EEPROM of the device that will be copied to the respective page of the scratchpad. It's the reverse action of what COPY SP function does. When this action ends the device must be reseted by the master. The RECALL MEMORY function is nessessary for reading the data out of the device. When the device measures current, voltage or temperature it stores the data in the first page (h00) of the EEPROM. Cause the master can only read the scratchpad, we have to copy the data from the EEPROM to the scratch pad. And this is what we do here.

Now in order to have new data available (temperature, voltage, current) be must tell the device to convert the respective readings to data. Current readings are automatically converted every 27msec. But we must tell the device to convert the temperature and the voltage readings. This is done with the following functions:

The CONVERT T function. With this function we convert temperature readings to data. When this ends the device must be reseted.

The CONVERT V function. With this function we convert voltage readings to data. When this ends the device must be reseted.

Note that with every function that consist of the CONTROL functions we can reset the device prematurly. This DOESN'T mean that the function will not take place. Every action that must be done will be done no matter if reset occures. Here is the flowchart of the CONTROL functions:

Flowchart2Flowchart3

 

Now let's get a little bit more practical - enough with theory. The DS2438 -yes- it can measure voltage and sent it to us. But, from where does it gets the voltage readings? Mama - Dallas has configured the chip to read the voltage on VDD pin, but we want to use the VAD pin, which is connected straight to the source (the battery pack). So we must reconfigure it. How? Guessed right... back to theory.

The most important page of the scratchpad of the DS2438Z is page h00 or the first page. The page looks like that, it's important to note:

 

Page_h00

 

This page like the others consists of 8 Bytes of data. The first Byte which is the most important to us is the Status byte. It tell us whether a convertion is over ( voltage(ADB) - temperature(TB) ), if the copy from scratchpad to EEPROM is done(NVB) ( COPY SP function) and the on board configuration.

If the IAD is setted to 1 then current measurements will be taken at the rate of 36.41Hz.

If the CA is setted to 1 then the current accumulator is working. The current accumulator shows as how much current is going in and out the battery pack, but I won't be using it probably in this tutorial so I won't get any further.

If the EE is setted to 1 then the current accumulator data is saved on the EEPROM of the chip, we are not intrested in here neither.

And finally what we wanted from the begining. The AD status bit. This bit if set to 1 the voltage measurement are taken on the Vdd pin. If setted to 0 then the current measurements are taken on the Vad pin.

So we must set this bit to 0. How? Well the first byte of the first page of the DS2438Z looks like this:

 

 

Status_byte

The acronames were displayed previously.

 


Now, X, ADB, NVB, TB can NOT be written, although if mistake occurs that doesn't mean you lose the device. We can only write bits AD,EE,CA,IAD.

In it's binary form this byte comes from mama - Dallas like that:

00001111 or in HEX h0F

 

We want to convert this to:

00000111 or in HEX h07

 

So here is what we'll do in a simples flowchart - table (everything which has a h infront of it is hex code)

 

1. We reset the device.

2. The device repplies with a presence pulse.

3. We write to the device hCC which is the SKIP ROM function in order to access the IC immediately.

4. We write to the device h4E to issue a write to scratchpad function.

5. We write to the device h00 to select the page of the scratch pad.

6. We write to the device h07 which is the first byte we wanted to write to the device as shown.

7. We reset the device to end the write sequence prematurly.

8. The device repplies with a presence pulse.

9. We write to the device hCC to SKIP ROM.

10. We write to the device h48 to issue a Copy SP to EEPROM command. This way we store the changed

byte we previously sent.

11. We write to the device h00. This determines the page of the SP copied to EEPROM, we changed page h00

or the first page of the device so this is the one to copy to the EEPROM.

12. We wait 20msec. The Copy SP to EEPROM usually takes 10msec, but just to be secure we wait a little bit

more.

13. We reset the device.

14. The device sends back a presence pulse.

15. We write to the device hCC to SKIP ROM.

16. We write to the device hBE to issue READ SP command.

17. We write to the device h00 to determine the page of the scratchpad we want to read. (h00 - first page)

18. The device sents as 9 bytes. The 8 Bytes of page h00 and a CRC (don't care).

19. We see if Byte 0 matches our pattern (00000111). If yes we are ok!!! If not, you did something wrong.

Of course as soon as you read the first byte do don't have to read the others. So you can issue the reset

earlier if you like.

20. The device after the reset sends a presence pulse. We are done!!!

 

Now we have the device ready and set!!! Nice!!! But since reality is not that theoritical let's see how the code is like. I program in BASIC. It's a easy to learn language which is based on structured programming, so by learning basic which is a enter to Matrix language, you can easier go to C, C++ and other languages that are based on structured programming. Since I use the ATMEL family microcontrollers, I use the BASCOM compiler to write my programs. The free version supports 4 kB of code, which is enough for us now. You can find it at:

http://www.mcselec.com/index.php?option=com_content&task=view&id=14&Itemid=41

 

I assume you 've already have installed and opened the program. Here is the part of code we need to write to reconfigure the device as shown above.

 

' --------[ Device Config]----------------------------------------------------

$regfile = "m16def.dat"
$crystal = 16000000
$baud = 9600


' --------[ LCD Config ]------------------------------------------------------

Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.5
Config Lcdpin = Pin , Db7 = Portc.4 , E = Portc.1 , Rs = Portc.0
Config Lcd = 20 * 4


' --------[ Port Config ]-----------------------------------------------------

Config 1wire = Portb.2


' --------[ Variable Config ]-------------------------------------------------

Dim I As Byte , W As Byte
Dim Crc As Byte
Dim Table(3) As Word
Dim Status As Byte
Dim Thresh As Byte
Dim Reg_no(8) As Byte

' --------[ Characters ]------------------------------------------------------


' --------[ Initialize ]------------------------------------------------------

' Start

' At first we reset the device.
1wreset
' The ATMEGA automatically receives the presence pulse and ignore it.
' Then we skip the ROM.
1wwrite &HCC
' Then we issue the write to SP command.
1wwrite &H4E
' And now we select the page of the scratch pad we want to write in.
1wwrite &H00
' Since we want to write on one byte we sent only one.
1wwrite &H07 ' 07 = 0000 0111
' And we issue reset to stop the write sequence.
1wreset
' And the device gives as back a repply, we ignore it.
' And we skip ROM again
1wwrite &HCC
' Now we copy what we writed on the scratchpad in the EEPROM to secure the data.
1wwrite &H48
' And we select the page to be copied.
1wwrite &H00
' We wait some time so the whole thing is over.
Waitms 50
' And we reset the device.
1wreset
' Again we ignore the presence pulse.
' And now we read what we wrote... if correct we are ok!!!
' First skip the ROM
1wwrite &HCC
' Then we issue the read scratchpad function
1wwrite &HBE
' And we select the page we want to view
1wwrite &H00
' And now... start sucking data baby!!!
' Note: Status is a variable.
Status = 1wread()
' And we see what we have done in binary form!!!
Lcd Bin(status);
' If it's 00000111 we are free to go!!! Nice doing!!!
End 'end program
' Don't forget the End command and leave the processor hanging....

 

Now let's get a program to enable us to read the temperatute, the voltage and the current reading. DS2438 doesn't write all these data on the scratchpad, but on the EEPROM. That's why we have to recall the memory, to have the data written on the scratchpad so the master can read 'em. REMEMBER a 1wire device and a microcontroller (master in general) can only communicate through the scratchpad. So we first issue a RECALL MEMORY function and then we read the scratchpad... So here is the program:

' --------[ Device Config]----------------------------------------------------

$regfile = "m16def.dat"
$crystal = 16000000
$baud = 9600


' --------[ LCD Config ]------------------------------------------------------

Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.5
Config Lcdpin = Pin , Db7 = Portc.4 , E = Portc.1 , Rs = Portc.0
Config Lcd = 20 * 4


' --------[ Port Config ]-----------------------------------------------------

Config 1wire = Portb.2


' --------[ Variable Config ]-------------------------------------------------

Dim I As Byte , W As Byte
Dim Crc As Byte
Dim Temp1 As Byte , Temp2 As Byte
Dim Tempfix As Word
Dim Table(2) As Word
Dim Status As Byte , Thresh As Byte
Dim Reg_no(8) As Byte

' --------[ Initialize ]------------------------------------------------------

' Main routine - The program will be executed continiously
Cls
Cursor Off
Lcd " DS2438Z Report "
Lowerline
Do
' Reset the device.
1wreset
' SKIP ROM.
1wwrite &HCC
' Issue temperature convertion.
1wwrite &H44
' Each temperature convertion takes 10msec, better wait.
Waitms 10
' Reset the device.
1wreset
' SKIP ROM.
1wwrite &HCC
' Issue voltage convertion.
1wwrite &HB4
' Each voltage convertion takes 4msec, better wait.
Waitms 5
' Reset the device.
1wreset
' SKIP ROM.
1wwrite &HCC
' Issue a RECALL MEMORY function on page h00.
1wwrite &HB8
1wwrite &H00
' Reset the device.
1wreset
' SKIP ROM.
1wwrite &HCC
' Issue a READ SP function on page h00.
1wwrite &HBE
1wwrite &H00
' Start sucking data
Status = 1wread()
Temp2 = 1wread()
Temp1 = 1wread()
For I = 1 To 2
Table(i) = 1wread(2)
Next
1wreset
' Display temperature
Lcd " Temp: "
Shift Temp2 , Right , 3
Lcd Temp1
Lcd "."
Tempfix = Temp2 * 3125
If Temp2 > 32 Then
Lcd "0"
Lcd Tempfix
Else
Lcd Tempfix
End If
' Change line
Thirdline
' Display current measurements
Lcd " Drain: "
' Here I multiply current readings with 66.7
' This number represent an equalation based on the
' sense resistor. Please refer to the datasheet for more info.
Table(2) = Table(2) * 66.7
Lcd Table(2)
Lcd " mA "
' Change line
Fourthline
' Display voltage measurements
Lcd " Voltage: "
Lcd Table(1)
Lcd " mV "
Home
Lowerline

Loop ' Loop

End 'end program

 

And the results of the program. A image can be very rewarding:

DS2438_LCD

Note that in the picture voltage is at 4.35V. I was just too lasy to fix this..... sorry...

 

Ok, by now you should know how to operate the DS2438. Here is the schematic suggested by me for the device.For Rsense refer to the datasheet. The bigger the value, the less current can be measured, but the higher the sensitivity is. Enjoy:

Schematics

 

Hope, you have understand the basics of the DS2438Z IC for more informations and functions refer to the datasheet. It would also be very nice to hear from you. IMPORTANT: I'm NOT an engineer, just a high school student, so my help is strictly limited. E-mail at: [email protected] but please don't expect I'll build or test anything for you. I can only answear to some basics!!!