Professional Documents
Culture Documents
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
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)
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
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.
AN012703-0608
Page 3 of 18
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
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
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
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
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.
AN012703-0608
Page 6 of 18
AN012703-0608
Page 7 of 18
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)
AN012703-0608
Page 8 of 18
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
Z8F6403 MCU
8-Bit
Port G Port E
8-Bit CLK
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
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
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
Initialize SPI control register PHASE = 1[rising edge] MODE = MASTER MODE SPIEN = ENABLE SPI
Configure the baud rate generator Set BRG = 0x009C [59076 bits/sec]
END
AN012703-0608
Page 12 of 18
Figure 5 displays the data write routine of the SPI device driver.
START
NO
Tx buffer empty?
NO
YES
Set SSV bit = 0 in SPI MODE Register
END
AN012703-0608
Page 13 of 18
Figure 6 displays the data read routine of the SPI device driver.
START
Tx buffer empty?
NO
Tx buffer empty?
NO
END
AN012703-0608
Page 14 of 18
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
AN012703-0608
Page 15 of 18
Figure 8 displays the flowchart for the routine to read from the temperature sensor. Temperature reading is controlled by the interrupt.
START
NO
Interrupt generated? YES Read HIGH data byte from Temperature sensor
AN012703-0608
Page 16 of 18
VCC
PC2 - SS
MCU-Z8F603
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
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