There are currently 1 user and 4 guests online.
Laser Tank - Basic tracked robot with Targeting Laser pod!
How to Smart Battery Monitor - DS2438
Submitted by TrickyNekro on March 13, 2008 - 1:01pm.
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!!!
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:
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
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:
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:
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:
The acronames were displayed previously.
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
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:
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]----------------------------------------------------
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]----------------------------------------------------
And the results of the program. A image can be very rewarding:
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:
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!!!