You are on page 1of 18

Application Note

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus


AN012703-0608

Abstract
This Application Note provides an overview of Zilogs Z8 Encore! Serial Peripheral Interface (SPI) controller along with a method to interface the Z8 Encore! MCU with an SPI temperature sensor. The SPI-compatible devices such as EEPROMs, Analog-to-Digital Converters (ADC) and Temperature Sensors are widely used in the industry. The Z8 Encore! MCU family features a comprehensive set of peripherals, including an SPI controller that helps reduce the external component requirement and product cost. Note: The source code file AN0127SC01.zip associated with this Application Note is available for download at www.zilog.com.

Discussion
A brief description of the SPI bus, SPI Controller and the SPI temperature sensor is provided below: SPI Bus Overview Z8 Encore! SPI Controller DS1722 Temperature Sensor Overview

SPI Bus Overview


The SPI is a synchronous interface allowing several SPI-type devices to be interconnected. In an SPI environment, there is always at least one device designated as the SPI master while the other SPI-type devices are designated as slave SPI devices. The SPI master device initiates and performs the required data transfers with a slave device. A slave device cannot initiate an SPI transfer (using the SPI control signals) and must be addressed by the master to enable it to communicate. The SPI master generates the SPI clock required for each transfer (1 data bit shifts in/out for each clock period). The master device also selects the slave device for communication. The data transfer mode is a fullduplex mode. SPI Bus Operation SPI is a full duplex, synchronous, characteroriented channel that supports a four-wire interface (serial clock, transmit, receive, and slave select). The SPI block consists of a transmitter and receiver section, a Baud Rate (clock) Generator (BRG) and a control unit. The transmitter and the receiver sections use the same clock.

Z8 Encore! Flash MCU Overview


Z8 Encore! products are based on Zilogs eZ8TM CPU and introduce Flash Memory to Zilogs extensive line of 8-bit microcontrollers. Flash Memory in-circuit programming capability allows for faster development time and program changes in the field. The high-performance register-toregister based architecture of the eZ8 core maintains backward compatibility with Z8 MCU. Featuring eZ8 CPU, the new Z8 Encore! microcontrollers combine a 20 MHz core with Flash Memory, linear-register SRAM, and an extensive array of on-chip peripherals. These peripherals make the Z8 Encore! MCU suitable for various applications including motor control, security systems, home appliances, personal electronic devices, and sensors.

Copyright 2008 by Zilog, Inc. All rights reserved. www.zilog.com

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

During an SPI transfer, both the master and the slave SPI devices send and receive data simultaneously. The data and the serial clock require separate signals. When an SPI transfer occurs, a multi-bit (typically 8-bit) character is shifted out, on one data pin and a multi-bit character is simultaneously shifted in on a second data pin. An 8-bit shift register in the master and another 8-bit shift register in the slave transmit and receive data on the SPI interface. The SPI shift register is a single byte register in both the transmit and receive directions. New data to be transmitted cannot be written into the shift register until the previous transmission is complete, and the received data (if valid) is read. SPI Signals The four basic SPI signals are: MISO (Master-In/Slave-Out) MOSI (Master-Out/Slave-In) SCK (SPI Serial Clock) SS (Slave Select)

DS1722 Temperature Sensor Overview


The DS1722 Digital Thermometer (referred to as Temperature Sensor in this document) with an SPI Interface provides temperature readings that indicate the temperature of the device. No additional components are required. The device is a temperature-to-digital converter. Temperature readings are communicated from the DS1722 Temperature Sensor over an SPI interface. For applications that require greater temperature resolution, you can adjust the readout resolution from 8 to 12 bits by changing bits R2, R1, and R0 in the configuration or status register (see Configuration Register Programming on page 3). The DS1722 Temperature Sensor consists of four major components: Precision thermometer Analog-to-Digital Converter SPI/3-wire interface electronics Data registers

For detailed description on SPI signals, refer to the appropriate document.

Z8 Encore! SPI Controller


Features of the Z8 Encore! SPI Controller include: Full-duplex, synchronous, character-oriented communication Four wire interface Data transfer rates up to a maximum of one-fourth the system clock frequency Error detection Write and mode collision detection Dedicated Baud Rate Generator (BRG)

The DS1722 Temperature Sensor requires no external components. After power-up, you can alter the configuration register to place the device in a continuous temperature conversion mode or in a one-shot conversion mode. In the continuous conversion mode, the DS1722 Temperature Sensor continuously converts the temperature and stores the result in the temperature register. As conversions are performed in the background, reading the temperature register does not affect the conversion that is in progress. In the one-shot temperature conversion mode, the DS1722 Temperature Sensor performs a single temperature conversion, stores the result in the temperature register and returns to the shutdown state. This conversion mode is ideal for powersensitive applications.

AN012703-0608

Page 2 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

The temperature conversion results have a default resolution of 9 bits. In applications where small incremental temperature changes are critical, you can change the conversion resolution from 9 bits to 10 bits, 11 bits, or 12 bits, all of which can be

accomplished by programming the Configuration register. Each additional bit of resolution approximately doubles the conversion time.

Pin Description
1 2 3 4 VDDD CE SCLK GND VDDA SER MODE SDI SDO 5 6 7 8 SERMODE CE SCLK GND VDDA SDO SDI VDDD Serial Interface Mode Chip Enable Serial Clock Ground Analog Supply Voltage Serial Data Out Serial Data In Digital Supply Voltage

Figure 1. Pin Assignment for DS1722 Configuration Register Programming The Configuration register is accessed in the DS1722 at address 00h for a read operation and at address 80h for a write operation. During SPI communication, data is first read from or written to the Configuration register. Table 1 displays the format of the Configuration register. SD is the shutdown bit and 1SHOT is the one shot/continuous temperature conversion bit. The entire register is volatile, and it powers-up in the default state. R0, R1, and R2 (see Table 2) are thermometer resolution bits.

Table 1. Configuration/Status Register


1 MSB 1 1 1SHOT R2 R1 R0 SD LSB

Table 2. Thermometer Resolution Configuration


R2 0 0 0 0 1 R1 0 0 1 1 X R0 0 1 0 1 X Thermometer Resolution 8-bit 9-bit 10-bit 11-bit 12-bit Maximum Conversion Time 0.075s 0.15s 0.3s 0.6s 1.2s

AN012703-0608

Page 3 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Register Address Structure of DS1722 The area of interest in programming the DS1722 is the Configuration register. The programming is performed through the SPI communication interface by selecting the appropriate address of the desired register location. Table 3 lists the addresses for configuration and temperature registers. Table 3. Address Structure
Read Address 00h 01h 02h Write Address 80h No Access No Access Active Register Configuration Temperature LSB Temperature MSB

Z8 Encore! Interface to DS1722 Temperature Sensor


Figure 2 displays the hardware setup of the Z8 Encore! MCU with DS1722 Temperature Sensor. In this setup, the microcontroller [MCUZ8F6403] is the master device and the DS1722 Temperature Sensor is the slave device.

Z8 Encore! SPI Device Driver


The device driver software implementation provided here demonstrates the SPI capability of Z8 Encore! MCU. The main operations performed by the SPI device driver include: Initializing the SPI Port Writing Data to the Port, and Reading Data from the SPI Port

MCU-Z8F6403
PC2/SS PC4 / MOSI PC5 / MISO PC3 / SCK 2 6 5 3

Thermometer DS1722
CE SDI SDO SCLK

Figure 2. Z8 Encore! SPI Bus as Master in a Single Master, Single Slave System

Interfacing SPI Temperature Sensor with Z8 Encore! MCU


The hardware interfacing details and the device driver software implementation are provided in the following sections: Z8 Encore! Interface to DS1722 Temperature Sensor Z8 Encore! SPI Device Driver

The Temperature Sensor specific operations include: Initializing the Temperature Sensor Reading from the Temperature Sensor

The source code allows temperature readings from the sensor in the standalone mode displayed in Figure 3 on page 10. The actual write or read APIs can be easily exported into any user application.

AN012703-0608

Page 4 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Table 4. Register bits used for initialization of SPI Port


Register Name SPICTL Bit Name PHASE; CLKPOL MMEN SPIEN SPIMODE SPIBRH SPIBRL SSIO BRH BRL Explanation PHASE bit is set to 1 and Clock Polarity (CLKPOL) bit is set to 0 to select data transmission on the rising edge and data reception on the falling edge of the clock. MMEN bit is set to enable the SPI controller to Master mode. SPIEN bit is set to enable the SPI bus. SSIO bit is set to 1 to configure the SS pin as an output (for Master mode only) BRH bit is set to 0 and BRL bit is set to 9C to get the baud rate 59076 bps at 18.432 MHz.

For information on the bit fields of the registers, see Z8 Encore! SPI Register Description on page 6. Initializing the SPI Port This routine initializes a specified port (Port C) for SPI function, sets the baud rate and the mode of operation. Table 4 lists the registers and their bit descriptions. Writing Data to the Port This routine describes the data write functionality of the SPI device driver. The data written to the SPI port are either the device register address or the control word for configuring the device. The main functions in the SPI data write operation include: Setting the SSV bit High, enabling the chip select. Writing the address byte into SPI data register. Waiting for buffer to empty. Writing the data byte into SPI data register. Waiting for buffer to empty. Setting the SSV bit LOW to disable the chip select.

Reading Data from the SPI Port This routine describes the data read functionality of the SPI device driver. The data that is read from the SPI port is the temperature reading of the device that was addressed by the write operation. The main functions in the SPI data read operation include: Setting the SSV bit High, in the SPI mode register, enabling the chip select. Writing the address byte into SPI data register. Waiting for buffer to empty. Shifting data from Temperature Sensor to SPI data register. Waiting for buffer to empty. Reading the data byte from the SPI data register. Setting the SSV bit Low to disable the chip select.

Initializing the Temperature Sensor This routine describes initialization of the Temperature Sensor. Initialization varies from device to device. In this example, the sensor is initialized for 12-Bit ADC resolution and continuous operating mode.

AN012703-0608

Page 5 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Reading from the Temperature Sensor This routine is the main routine for reading the temperature from the Temperature Sensor. The three Temperature Sensor APIs are: Temperature Sensor initialization Read SPI Write SPI

SPI Baud Rate High and Low Byte Registers The SPI Baud Rate High and Low Byte registers combine to form a 16-bit reload value, BRG[15:0], for the SPI Baud Rate Generator. The reload value must be greater than or equal to 0002H for proper SPI operation (maximum baud rate is system clock frequency divided by 4). The SPI baud rate is calculated by the following equation: SPI Baud Rate = System Clock Frequency (bits/s) (Hz) 2 x BRG [15:0]

The Temperature Sensor can convert the data continuously. A timer is set to generate interrupts periodically to read the temperature from the sensor. The temperature reading from the sensor is therefore performed within the timer interrupt routine.

Z8 Encore! SPI Register Description


The Z8 Encore! SPI registers are briefly described in this section. SPI Control Registers The following registers comprise the SPI control registers and can be custom configured (see Table 5 through Table 10): SPI Data register (SPIDATA) SPI Control register (SPICTL) SPI Status register (SPISTAT) SPI Mode register (SPIMODE) SPI Baud Rate High Byte register (SPIBRH) SPI Baud Rate Low Byte register (SPIBRL)

AN012703-0608

Page 6 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Table 5. SPI Data Register (SPIDATA)


BITS FIELD RESET R/W ADDR Bit Position Value (H) [7:0] DATA Description Transmit/receive data X R/W X R/W X R/W X R/W F60H 7 6 5 4 DATA X R/W X R/W X R/W X R/W 3 2 1 0

Table 6. SPI Control Register (SPICTL)


BITS FIELD RESET R/W ADDR Bit Position Value (H) [7] [6] [5] [4] [3] [2] [1] [0] IRQE STR BIRQ PHASE CLKPOL WOR MMEN SPIEN Description Interrupt Request Enable Start an SPI Interrupt Request BRG Timer Interrupt Request Phase Select Clock Polarity Wire-OR(Open-Drain)Mode Enable SPI Master Mode Enable SPI Enable 7 IRQE 0 R/W 6 STR 0 R/W 5 BIRQ 0 R/W 4 PHASE 0 R/W F61H 3 CLKPOL 0 R/W 2 WOR 0 R/W 1 MMEN 0 R/W 0 SPIEN 0 R/W

AN012703-0608

Page 7 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Table 7. SPI Status Register (SPISTAT)


BITS FIELD RESET R/W ADDR
Note: R/W*-Read access. Write 1 to clear the bit to 0.

7 IRQ 0 R/W*

6 OVR 0 R/W*

5 COL 0 R/W*

3 Reserved 0 R F62H

1 TXST 0 R

0 SLAS 0 R

Bit Position Value (H) [7] [6] [5] [4:2] [1] [0] IRQ OVR COL Reserved TXST SLAS

Description Interrupt Request Overrun Collision These bits are reserved and must be 0 Transmit Status Slave Select (If SPI is enabled as a Master, this bit is not applicable)

Table 8. SPI Mode Register (SPIMODE)


BITS FIELD RESET R/W ADDR Bit Position Value (H) [7:5] [4:2] [1] [0] Reserved NUMBITS[2:0] SSIO SSV Description These bits are reserved and must be 0 Number of Date Bits to Transfer Slave Select I/O Slave Select Value (The SSV bit has no effect if SSIO=0 or SPI configured as a Slave) 7 6 RESERVED 0 R 0 R/W F63H 5 4 3 NUMBITS [2:0] 0 R/W 0 R/W 2 1 SSIO 0 R/W 0 SSV 0 R/W

AN012703-0608

Page 8 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Table 9. SPI Baud Rate High Byte Register (SPIBRH)


BITS FIELD RESET R/W ADDR Bit Position Value (H) [7:0] BRH Description SPI Baud Rate High Byte 1 R/W 1 R/W 1 R/W 1 R/W F66H 7 6 5 4 BRH 1 R/W 1 R/W 1 R/W 1 R/W 3 2 1 0

Table 10. SPI Baud Rate Low Byte Register (SPIBRL)


BITS FIELD RESET R/W ADDR 1 R/W 1 R/W 1 R/W 1 R/W F67H 7 6 5 4 BRL 1 R/W 1 R/W 1 R/W 1 R/W 3 2 1 0

Bit Position Value (H) [7:0] BRL

Description SPI Baud Rate Low Byte

For more details on registers, refer to eZ80 CPU User Manual (UM0077).

Testing
Figure 3 on page 10 displays the setup used to test the SPI Temperature Sensors device driver functionality.

AN012703-0608

Page 9 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Z8F6403 MCU
8-Bit

PC2 - SS PC4 - MOSI PC5 - MISO PC3 - SCK

Logic State Analyzer

Port G Port E

8-Bit CLK

SPI Temperature Sensor DS 1722

Figure 3. Test Setup for Z8 Encore! SPI Device Driver The equipment used for testing are listed below: Z8 Encore! Flash Microcontroller Development Kit (Z8ENCORE000ZCO), that contains the ZDS II- Z8 Encore! v4.1.0 Logic state analyzer- HP1661A

Summary
This Application Note provides an overview of the SPI bus and the APIs to interface Z8 Encore! SPI (in master mode) with a DS1722 Temperature Sensor in a slave mode. These SPI device driver APIs can also be used with other devices like ADC and EEPROM, with minimum modification.

The Logic State Analyser is used to capture the binary data from the SPI device driver through Ports G and E of the Z8 Encore! MCU. A test program is written to send the data from the device driver to Port G and Port E. To view this program, refer to the software project files. Alternatively, a PC with the HyperTerminal application, connected through the UART port can also be used to capture the test results, with a test program written specifically for the same. Using the SPI device driver software, the temperature readings (binary data) were successfully taken from the DS1722 Temperature Sensor present on the Z8 Encore! Development Board.

References
The documents associated with Z8 Encore!, eZ8 CPU, and DS1722 are provided below: eZ8TM CPU User Manual (UM0128) DS1722 Digital Thermometer with SPI/3-Wire Interface (DS1722)

AN012703-0608

Page 10 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Appendix AGlossary
Table 11 lists the definitions for terms and abbreviations used in this Application Note.
Table 11. Glossary Terms/Abbreviations API CPU GPIO HEX MCU MISO MOSI SCK SS SPI ZDS Definitions Application Programming Interface Central Processing Unit General-Purpose Input/Output Hexadecimal Microcontroller Unit Master-In/Slave-Out Master-Out/Slave-In SPI Serial Clock Slave Select Serial Peripheral Interface Zilog Developer Studio

AN012703-0608

Page 11 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Appendix BFlowcharts
This appendix contains the flowcharts for the SPI temperature sensor application described in this Application Note. Figure 4 displays the initialization of SPI Device Driver in which the baud rate 59076 bps is calculated for 18.432 MHz clock speed.

START

Configure Port C for alternate function to enable the SPI

Initialize SPI control register PHASE = 1[rising edge] MODE = MASTER MODE SPIEN = ENABLE SPI

Configure SPI mode register bit SSIO as an output

Configure the baud rate generator Set BRG = 0x009C [59076 bits/sec]

END

Figure 4. Z8 Encore! SPI Initialization Routine

AN012703-0608

Page 12 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Figure 5 displays the data write routine of the SPI device driver.

START

Set SSV bit = 1 in SPI MODE Register

Write to SPI Data Register (SPI Data=address)

Tx buffer empty? YES Write to SPI Data Register (SPI Data=data)

NO

Tx buffer empty?

NO

YES
Set SSV bit = 0 in SPI MODE Register

END

Figure 5. SPI Device Driver Write Routine

AN012703-0608

Page 13 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Figure 6 displays the data read routine of the SPI device driver.

START

Set SSV bit = 1 in SPI MODE Register

Write to SPI Data Register (SPI Data=address)

Tx buffer empty?

NO

YES Write to SPI Data Register (SPI Data=0x00)

Tx buffer empty?

NO

YES Read from SPI Data Register (Data=SPI Data)

Set SSV bit=0 in SPI MODE Register

END

Figure 6. SPI Device Driver Read Routine

AN012703-0608

Page 14 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Figure 7 displays the routine for Temperature Sensor initialization. This routine varies from device to device. Here, the sensor is initialized for 12-bit ADC resolution and continuous operating mode.

START

Call function to write the control word for 12-bit resolution and continuous ADC operation to SPI slave device

END

Figure 7. Temperature Sensor Initialization

AN012703-0608

Page 15 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Figure 8 displays the flowchart for the routine to read from the temperature sensor. Temperature reading is controlled by the interrupt.

START

Initialize the SPI controller

Initialize the SPI temperature sensor by calling the initialization routine

Initialize timer to generate interrupt for reading the temperature

NO

Interrupt generated? YES Read HIGH data byte from Temperature sensor

Read LOW data byte from Temperature sensor

Figure 8. Reading from the Temperature Sensor

AN012703-0608

Page 16 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Appendix CExamples of Z8 Encore! SPI Bus Operating Modes


This appendix displays example for the operating modes which suggest ways to connect the master or slave device with Z8 Encore! MCU. Z8 Encore! can be used in other modes as well. Figure 8 displays SPI of Z8 Encore! configured as master in a Single Master Multiple Slave system. In this mode, the SS pin of SPI interface can be tied to the VCC, and the GPIO pins can be used as a chip select. Multiple sensors can be connected in parallel and the data can be read one at a time by selecting the sensor chip, using the CE signal.

VCC

SPI Temp. Sensor DS1722 (Sensor -1)

PC2 - SS

GPIO - SS1 GPIO - SS2

MCU-Z8F603

PC4 - MOSI PC5 - MISO PC3 - SCK

SPI Temp. Sensor DS1722 (Sensor -2)

Figure 8. SPI configured as a Master in a Single Master, Multiple Slave System Figure 9 displays SPI of Z8 Encore! configured as a slave. The SS pin and CLK pin are used as input pins.

PC2 - SS PC4 - MOSI MCU-Z8F6403 PC5 - MISO PC3 - SCK SPI Master Device

Figure 9. SPI Configured as a Slave in a Single Master Single Slave System


AN012703-0608 Page 17 of 18

An SPI Temperature Sensor Interface with the Z8 Encore! SPI Bus

Warning: DO NOT USE IN LIFE SUPPORT LIFE SUPPORT POLICY ZILOG'S PRODUCTS ARE NOT AUTHORIZED FOR USE AS CRITICAL COMPONENTS IN LIFE SUPPORT DEVICES OR SYSTEMS WITHOUT THE EXPRESS PRIOR WRITTEN APPROVAL OF THE PRESIDENT AND GENERAL COUNSEL OF ZILOG CORPORATION. As used herein Life support devices or systems are devices which (a) are intended for surgical implant into the body, or (b) support or sustain life and whose failure to perform when properly used in accordance with instructions for use provided in the labeling can be reasonably expected to result in a significant injury to the user. A critical component is any component in a life support device or system whose failure to perform can be reasonably expected to cause the failure of the life support device or system or to affect its safety or effectiveness.

Document Disclaimer 2008 by Zilog, Inc. All rights reserved. Information in this publication concerning the devices, applications, or technology described is intended to suggest possible uses and may be superseded. ZILOG, INC. DOES NOT ASSUME LIABILITY FOR OR PROVIDE A REPRESENTATION OF ACCURACY OF THE INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED IN THIS DOCUMENT. Z I L O G A L S O D O E S N O T A S S U M E L I A B I L I T Y F O R I N T E L L E C T U A L P R O P E RT Y INFRINGEMENT RELATED IN ANY MANNER TO USE OF INFORMATION, DEVICES, OR TECHNOLOGY DESCRIBED HEREIN OR OTHERWISE. The information contained within this document has been verified according to the general principles of electrical and mechanical engineering. Z8, Z8 Encore!, and Z8 Encore! XP are registered trademarks of Zilog, Inc. eZ8 is a trademark of Zilog, Inc. All other product or service names are the property of their respective owners.

AN012703-0608

Page 18 of 18

You might also like