Introduction to Microprocessors Feisal Mohammed 12th January 2001 Mini-project This project is to implement a temperature monitor using the PicStic4, the DS1821 temperature sensor and a LCD display. The monitor is to take readings of the ambient temperature every 5 seconds and display these readings on the LCD. The sections that follow explain how the DS1821 communicates and shows a partial implementation of the software to communicate with it. 1 DS1821 Registers The DS1821 temperature sensor 1 communicates via a 1-wire protocol. The DS1821 can operate as a thermostat or a thermometer, the factory settings default to thermometer using the 1-wire protocol to control the device. The data sheet details the specifics of the timing to initialize the device, write or read bits and from these basic functions, the functions for taking a temperature measurement can be developed. The DS1821 has a status register, shown in Figure 1, that controls its modes of operation. There is also a register that keeps the last temperature measured. Finally, there are two other registers which are used only in thermostat mode; the TH and TL registers. The TH register contains the highest temperature above which the output becomes high, and the output will stay active until the temperature falls below the temperature in the TL register. Done 1 NVB THF TLF T/R POL 1SHOT Figure 1: Status register The bits in the STATUS register have the following meanings: DONE Conversion Done bit. 1 = Conversion complete, 0 = conversion in progress. NVB Nonvolatile memory busy flag. 1 = Write to an EE memory cell in progress, 0 = nonvolatile memory is not busy. A write to EE may take up to 10 ms. THF Temperature High Flag. This bit will normally be 0, but will be set to 1 when the temperature exceeds the value of TH. It will remain 1 until reset by writing 0 into this location. This feature provides a method of determining if the DS1821 has ever been subjected to temperatures above TH. This bit is nonvolatile, and is stored in EE memory. TLF Temperature Low Flag. This bit will normally be 0, but will be set to 1 when the temperature is lower than the value of TL. It will remain 1 until reset by writing 0 into this location. This feature provides 1 The DS1821 data sheet is available from the EE25M resource site.
a method of determining if the DS1821 has ever been subjected to temperatures below TL. This bit is nonvolatile, and is stored in EE memory. T/R Power-up mode bit. If set to a 1, the DS1821 will power up in a thermostat mode. If set to a 0, the device will power up in 1-Wire read mode. This bit is nonvolatile. POL Output Polarity Bit. 1 = active high, 0 = active low. This bit is nonvolatile. 1SHOT One-Shot Mode. If 1SHOT is 1, the DS1821 will perform one temperature conversion upon reception of the Start Convert command. If 1SHOT is 0, the DS1821 will continuously perform temperature conversions. Note that the One-Shot mode is available only when the device is in 1-Wire mode. In thermostat mode, the device continuously performs temperature conversions. This bit is nonvolatile. Since all the bits except the DONE bit are non-volatile, the DS1821 needs only to be configured once, before logging starts. 2 Sample Software development In the following sections, the software for reading of the temperature is designed and implemented. The 1-wire protocol can be implemented in a layered fashion, shown in Figure 2 where the innermost layers operate on the bit level. This is an effective way of simplifying the development of the implementation of the protocol. At each level, we will define the modules/functions and how they will interact with the other Byte level operations Bit level operations Read Temperature Figure 2: Layering layers. Now the DS1821 requires that a reset/presence signal be sent before any writing of commands or reading of data is done and the sequence looks like the following: Reset/presence Command function Transaction/Data This reset/presence can be implemented in the innermost layer. The function structure now looks like Figure 3. There may be other functions that will be needed but these can be added as we proceed. It is assumed that all the low-level functions always leave the 1-wire bus in the floating state. Also high-level functions will be 2
readtemperature inittemperature readtempbyte writetempbyte writetempbit readtempbit resetpresence Figure 3: Initial structure written such that the value will be the status of the operation i.e. whether the function was successful or not. We will also need some variables for storing the temperature and for temporary storage. These are given as follows DQ equ 0 ; DQ is RB0 base equ 0x0C temperature equ base+0 ; temperature storage ds_count equ base+1 ; counter ds_ equ base+3 ; for values ds_bit equ base+4 ; to hold a bit read It is assumes that any parameter needed to be passed to a subroutine will be placed in the W register before the subroutine is ed. 2.1 inittemperature This function initializes the DS1821 and is done only once. The pseudocode is as follows. if cannot send reset/presence then error end if Send command byte to enable writing to STATUS register Send configuration byte We can now implement the code. In al the routines, any parameters that are to be passed are placed in the W register before ing the routine and any values are in the ds_ register. ; This function s 1 on success, zero otherwise. ; It initializes the DS1821 in preparation for sampling ; temperatures. 3
; resetpresence also s 1 on success, zero otherwise inittemp resetpresence ; resetpresence takes no movf ds_,f ;... parameters btfsc STATUS,Z ; resetpresence failed 2.2 readtemperature movlw 0x0C ; enable write to STATUS writetempbyte movlw 0x43 ; 1shot conversion, active high, 1 wire mode writetempbyte movlw.1 movwf ds_ Reading of the temperature is just slightly more complicated, we must start the temperature conversion on the device, wait until it has finished conversion and finally read the sampled temperature. The temperature conversion typiy takes 0.4 s but can take as much as 1 s and when it is finished the DONE bit in the STATUS register is set to 1. start temperature conversion repeat delay for 200 ms read the STATUS register until DONE bit of the STATUS is set read converted temperature The code is then ; assume that result from reading will be ;... stored in variable ed temperature readtemp resetpresence btfsc STATUS,Z movlw 0xEE ; start conversion writetempbyte DS_loop_read movlw.200 ; every 200 ms Delay_ms btfsc resetpresence STATUS,Z 4
movlw btfss goto btfsc 0xAC writetempbyte readtempbyte ds_,7 ;... check the DONE bit DS_loop_read resetpresence STATUS,Z 2.3 readtempbyte movlw 0xAA ; read temperature writetempbyte readtempbyte movf ds_,w movwf temperature movlw 1 ; 1 movwf ds_ This function reads a byte and is based on the readtempbit function. Essentially, it s readtempbit eight times, and shifts the bits read into a word. ; readtempbyte movlw.8 ; set loop counter movwf ds_count DS_loop_rbyte readtempbit ; s bit in W rrf ds_bit ; shift bit into Carry rrf ds_,f ; Carry -> register decfsz ds_count goto DS_loop_rbyte 2.4 readtempbit The low-level read function, adjusts the delays such that the master will sample within 15 µs after the DQ line is pulled low. Afterwards the line is left floating to finish the 60 µs time slot plus recovery. 5
readtempbit bsf STATUS,RP0 ; take DQ low bcf TRISB,DQ bcf STATUS,RP0 bcf PORTB,DQ ; wait one cycle to settle bsf STATUS,RP0 ; float DQ bsf TRISB,DQ bcf STATUS,RP0 ; wait for 5 us movf PORTB,W movwf ds_bit ; store bit read ; assume that DQ is connected to RB0, if not ; insert appropriate number of rrf instructions here movlw.15 usec4 ; delay for 15 * 4 = 60 us 3 Presentation of the project The presentation of your final report is very important if you are to obtain full marks. Several points to note are: Refrain from including any extraneous matter, such as specification sheets for the PIC, LCD. etc. I have seen these sheets already and printing them again serves no purpose. Concentrate on what has been done. The idea of this project is for students to design and build a temperature monitor. Therefore I need to know what it is exactly that you have done, not what others have done. Avoid gratuitous comments in your code do not explain the op-code but rather explain what is intented by the code. 3.1 Content The report for the project should contain Overview There must be an overview of the system. This includes the functionality and a physical view of the system. The overview serves to summarize the behavior and set the goals of the system. 6
Circuit diagram The physical layout of the circuit is presented. Any calculations, choices of resistor and/or capacitor values are also shown here along with explanations. Software structure It is essential to show the structure of the software, as an overall picture and in increasing detail. It is much better to have several diagrams that show increasing level of detail than to have everything compressed into one complicated diagram. Testing Testing determines if the completed system meets its requirements. It is not sufficient to say it works. Saying that means that you have not done sufficient tests to validate your work. The result of testing is an assurance that a system will perform as specified, therefore simplistic testing where only a small subset of all inputs or environments are applied will not give that assurance. Discussion A discussion is not a list of problems encountered, it is an analysis of how well the objectives were met, any special problems encountered, possible improvements and further work. You should also mention any trade-offs that were made during development. 3.2 Marking Scheme The marking scheme is given in the following table: Area Marks allocated Presentation 4 Design 5 Implementation 6 Discussion 5 7