+ All Categories
Transcript

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

MINIMAL NETWORK ELEMENT III

LPC1768 1-Wire Digital Thermometer programing

Version 1.0

Huy Nguyen

Team Members

Bikash Shakya (15 ECTS) CoachesHuy Nguyen (15 ECTS) Bernt Sundström

Markku Antikainen (24 ECTS) Hans Eriksson

Mudassir Asif (24 ECTS) Robert Olsson

Muhammad Ziad (24 ECTS)

Naresh Kumar Khatri (24 ECTS) ChampionSiddharth Sharma (15 ECTS) Björn Pehrson

27th December, 2010

1

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

History

Date Version Modification

27th December, 2010 1 Created

2

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

Table of ContentsI. Maxim DS18B20 thermometer.........................................................................................................4

I.1. Introduction...............................................................................................................................4

I.2. Powering the DS18B20.............................................................................................................4

I.3. ROM and Memory....................................................................................................................6

II. Programing.......................................................................................................................................7

II.1. Initialization.............................................................................................................................7

II.2. ROM commands......................................................................................................................8

II.3. Function commands.................................................................................................................8

II.4. Read and Write signal..............................................................................................................9

II.5. Time estimation......................................................................................................................10

II.6. Store temperature into program variable................................................................................11

III. Program data structure and functions...........................................................................................11

Reference............................................................................................................................................13

3

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

I. Maxim DS18B20 thermometer

I.1. IntroductionThe DS18B20 is a Maxim digital thermometer with the operating temperature from -55ºC to +125ºC and the supply power from 3.0V to 5.0V. It possesses many good characteristics. First, it contains a direct-to-digital temperature sensor with the resolution from 9 to 12 bits allowing us to have an increment of 0.5ºC, 0.25ºC, 0.125ºC or 0.0625ºC. This sensor is quite accurate. In the range of -10ºC to 85ºC, its accuracy is 0.5ºC. Its conversion time is also sufficiently short, 750ms in the worst case. Second, the DS18B20 implements the Maxim's exclusive one-wire bus protocol which requires only one data line to communicate with the micro-controller. The one wire bus protocol also enables us to connect multiple devices to the same bus and controls each of them separately using their addresses. This together with the unique 64 bit serial code that is attached with each DS18B20, allows us to connect with a very large number of DS18B20 thermometers via only one micro controller pin. Last but not least is the ability to power the DS18B20 without any external power supply. In this case, the thermometer will be powered via the data line (i.e the one-wire bus). When the bus is high, the internal capacitor of the DS18B20 will be charged which then will be used to power the thermometer. With this ability, we will need only two lines, the bus and the ground, to operate the thermometer.

I.2. Powering the DS18B20The DS18B20 can be powered by two ways. The first is using an external power supply on the VDD pin. The second is via the 1-Wire bus which is called "parasite power" mode. In the second case, the VDD will be connected to the ground. The two power modes are illustrated in figure 1 and figure 2:

4

Figure 1: Power DS18B20 using external power supply (reproduced from [1])

Figure 2: Power DS18B20 via 1-Wire bus (reproduced from [1])

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

In figure 2, a MOSFET is used to pull up the 1-Wire bus to provide sufficient power that is required by the thermometer when it performs temperature conversion or memory to EEPROM copying. The switching to the MOSFET pullup must be taken place within 10us after the Convert T [44h] or Copy Scratchpad [48h] command and has to be kept during the conversion time or data transfer time. In our project, we have modified the schematic in figure 2 a little bit to try to avoid the use of MOSFET and it works fine in case of one thermometer on the bus with the conversion action. Our schematic is showed in figure 3:

DS18B20 recommends not to use the parasite power mode in case the temperature is higher than +100ºC. The 1-Wire master, the micro-controller, can determine the power mode of the DS18B20 by using a sequence of Skip ROM [CCh], Read Power Supply [B4h] and a "read time slot". During the "read time slot", if the bus is low, the parasite power mode is being used. Otherwise, the power mode is the external power supply mode.

I.3. ROM and MemoryThe ROM contains a 64 bit code that uniquely identifies a DS18B20. The format of the ROM code is showed in figure 4.

The memory map of the thermometer is demonstrated in figure 5. Byte 0 and byte 1 of the Scratchpad memory together make up the temperature register. This register contains the temperature after each temperature conversion. The format of this register is illustrated in figure 6. The number of valid bits in this register are different depending on the configured resolution of the temperature conversion. If the resolution is 12 bit, all the bits are valid. In case the resolution is 11 bit, 10 bit, or 9 bit, the corresponding invalid bits are bit 0, bit 0 and bit 1, or bit 0, bit 1 and bit 2.

5

Figure 3: Parasite power mode using in the project (adapted from [1])

Figure 4: DS18B20 ROM code (reproduced from [1])

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

TH and TL can be used to either store user bytes or correspondingly store the upper bound and lower bound alarm value. When the perceived temperature is higher than the value in the TH or lower than the value in the TL, the alarm flag of the DS18B20 will be set.

The configuration Register allows us to configure the resolution of the temperature conversion. Only two bits, bit 5 and bit 6, are used to select the resolution mode. The pairs of value and mode are demonstrated in figure 7.

The TH, TL and configuration register can be copied from the Scratchpad Memory to EEPROM to avoid the lost of them when the power is off. They also can be loaded from EEPROM to Scratchpad Memory.

6

Figure 5: DS18B20 memory map (reproduced from [1])

Figure 6: DS18B20 temperature register (reproduced from [1])

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

II. ProgramingTo get the thermometer perform any action, we need to follow three steps:

• Initialization

• ROM command

• Function command

One example of command sequence on detecting the current temperature is illustrated in figure 9.

II.1. InitializationIn the initialization phase, the master (micro-controller) will send a reset pulse and the thermometer, DS18B20, upon receiving the signal will send a presence pulse to inform its presence to the master. The timing of the initialization is showed in figure 8.

7

Figure 7: Configuration Register and Resolution table (reproduced from [1])

Figure 8: Initialization timing (reproduced from [1])

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

II.2. ROM commandsThey are issued only after a presence pulse in the previous step was detected. It consists the following commands:

• Search ROM [F0h]: is used to identify all the slaves on the bus. If there is only one slave, the Read ROM [33h] command is recommended.

• Read ROM [33h]: is used to read the 64 bit ROM code when there is only one slave on the bus.

• Match ROM [55h]: is used to specify a particular device. This command is followed by a 64 bit ROM code

• Skip ROM [CCh]: is used to address all devices on the bus. No ROM code is sent with this command.

• Alarm Search [ECh]: is used by the master to determine if there is any slave that has alarm flag set.

II.3. Function commandsThese commands are issued after the ROM command. They are listed below:

• Convert T [44h]: commands the thermometer to do the temperature conversion and store the result in the scratchpad memory

• Write Scratchpad [4Eh]: writes 3 bytes continuously to the scratchpad, TH, TL and configuration register.

• Read Scratchpad [BEh]: reads all the contents of the scratchpad, from byte 0 to byte 8.

• Copy Scratchpad [48h]: copies TH, TL and configuration register to EEPROM.

• Recall E2 [B8h]: copies TH, TL and configuration register from EEPROM to scratchpad memory.

• Read power supply [B4h]: to determine if any device on the bus uses the parasite power mode.

8

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

II.4. Read and Write signalThe timing for read and write time slot is illustrated in figure 10. The write time slot is used by the master (the micro-controller) to send one data bit to the slave. The data to be written to the bus is usually commands and their corresponding parameters. The length of each write slot should be more than 60us with at least 1us between them. In order to write 0, the bus should be pulled down all the write slot. For 1, the bus has to be pull low at least 1us but smaller than 15us and then pulled up the remaining time. Detail of how to manage the bus is showed in figure 10.

The bus master issues read time slots for the slave to send slave's data to the master. Each data bit requires one read time slot. The read slot is initiated when the master pulls the bus low for at least 1us. The master will then release the bus for the DS18B20 to transmit the bit value. Thus, the master after initializing the read slot should change to the receiving mode to sample the bit value. The sampling should be happened within 15us from the beginning of the read slot. For more detail on timing, see figure 10.

9

Figure 9: Example of command sequence (reproduced from [1])

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

II.5. Time estimationThe most important thing when communicates with the DS18B20 is timing. We need to estimate the time carefully, or the communication will be failed. In our project, the timing is achieve by using the for loop. Thus, the time estimation is turned to the loop estimation. This estimation will be much easier if we have tools for debugging such as oscilloscope. In case we do not have it (such as in our project) we can do it by examining the program assembly code, figuring out the part for timing and estimating the time according to the assembly instructions and the CPU timing. For example, we have the following C code:

int loop;

for (loop = 0; loop < 160; loop++)

{}

When we compile it to the assembly we can get something like the the following assembly code:

.L51:

10

Figure 10: Read and Write slot timing (reproduced from [1])

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

ldr r3, [r7, #4]

add r3, r3, #1

str r3, [r7, #4]

.L50:

ldr r3, [r7, #4]

cmp r3, #159

ble .L51

From the Cortext-M3 instruction set summary, we need about two CPU cycles for the ldr, one cycle for the add, two cycles for the str, 1 cycle for the cmp and from 2 to 4 cycles for the ble. Thus, each for loop will consume from 10 to 12 cycles. With the CPU clock of 4Mhz, each for loop will take from 2.5us to 3us. We can first do our estimation with the worst case, 3us, then check if it works and modify the number of loop if it is necessary.

In case the clock of the CPU is low, with some required timing, we can use the nop commands. We add it to the C program by [3]:

asm volatile("mov r0, r0");

II.6. Store temperature into program variableAs being described in the previous section, the temperature after being measured is stored as a 16-bit sign-extended two's complement number. The sign bits are 0 if the number is positive and 1 otherwise. The procedure of storing the value in the temperature register to a variable is a little bit complexity because of the difference of the number format. The following discussion is only for 12 bit resolution case, the case that all bits of the temperature register are valid. We also concentrate on the case of storing the integer part and the real part of the temperature to two separated integer variables since it is easier for us to print it out. However, it can be easily extended to the case of using only one float variable to store the temperature value. The procedure is:

• First, we get the absolute value of the temperature by adding it with 1 if it is negative and keeping it if it is positive.

• Next, we get the integer part value by using the bit shifting operation.

• Last, we calculate the real part value by using the last four bits.

For implementation detail, please see the onewire_temp2int function of the temp_mon.c.

III. Program data structure and functionsIn program, a struct is used to store the DS18B20 ROM code and its scratchpad memory. The pin that is used to communicate with the DS18B20 is P0.21.

/* data structure for the temparature sensor */

typedef struct

{

11

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

uint8_t id[8]; // device id

uint8_t scratchpad[9]; // the device memory

} temp_sensor_t;

Functions for initialization, for reading ROM code and for reading temperature are also provided in project program. Beside that, facility functions for read one byte from the thermometer and write one byte to the thermometer are also included. Function's definitions together with their descriptions are listed below:

/* * * Init the pin which is used for onewire bus * check if the device is presented or not * Use before any operation * @return 0 if sucess * 1 if fail */uint8_t onewire_init();

/** * The read rom command. Use to read the ID of the temperature sensor. * Use in case there is only one sensor. If more, use search rom instead. * @Param *id an array of 8 bytes which contain the result * @return 0 if success * 1 if fail - cannot find device */uint8_t onewire_read_rom(uint8_t *id);

/** * Read one byte from the one wire bus. * @return the byte read */uint8_t onewire_read_byte();

/** * Write one byte to the one wire bus. * @param byte: the byte to be written */void onewire_write_byte(uint8_t byte);

/** * issue the temperature convert command, wait for the process to finish and read the result * the device memory will be store to the scratchpad * @param *scratchpad the pointer to the scratchpad array * @param *temp the temperature * @return 0 if sucess; * 1 if error - because cannot detect device */uint8_t read_temp(uint8_t *scratchpad);

12

LPC1768 1-Wire Digital Thermometer programming v.1 27th December, 2010

/** * Convert the temperature to a two integer. The first store the integer part of the temperature including sign. * The second integer store the real part. Assume that 12 bit converter is used. Write with the purpose of using with rprintf * @param *int_part the integer that will hold the return integer part * @param *real_part the integer that will hold the return real part * @param *temp the pointer to the temperature to be converted */void onewire_temp2int(int8_t *int_part, uint16_t *real_part, uint8_t *temp);

/** * init pin p0.21 for onewire temp sensor */void temp_mon_init();

Reference[1] Maxim. DS18B20 Programmable Resolution 1-Wire Digital Thermometer. Available at: http://pdfserv.maxim-ic.com/en/ds/DS18B20.pdf. Last visited: December, 2010.

[2] ARM. Cortex-M3 processors. Available at: http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ddi0403c/index.html. Last visited: December, 2010.

[3] ARM GCC Inline Assembler Cookbook. Available at: http://www.ethernut.de/en/documents/arm-inline-asm.html. Last visited: December, 2010.

13


Top Related