Fujitsu Microelectronics Europe Application Note MCU-AN-390105-E-V11 F²MC-8L/16LX FAMILY 8/16-BIT MICROCONTROLLER MB90340 SPI COMMUNICATION TO EXTERNAL ADC (for MAX1286) APPLICATION NOTE SPI COMMUNICATION TO ADC Revision History Revision History Date 2003-05-28 2010-06-11 Issue V1.0; MWi, first version V1.1; MWi, CPHA, CPOL logic corrected This document contains 14 pages. MCU-AN-390105-E-V11 -2- © Fujitsu Microelectronics Europe GmbH SPI COMMUNICATION TO ADC Warranty and Disclaimer Warranty and Disclaimer The use of the deliverables (e.g. software, application examples, target boards, evaluation boards, starter kits, schematics, engineering samples of IC’s etc.) is subject to the conditions of Fujitsu Microelectronics Europe GmbH (“FME”) as set out in (i) the terms of the License Agreement and/or the Sale and Purchase Agreement under which agreements the Product has been delivered, (ii) the technical descriptions and (iii) all accompanying written materials. Please note that the deliverables are intended for and must only be used for reference in an evaluation laboratory environment. The software deliverables are provided on an as-is basis without charge and are subject to alterations. It is the user’s obligation to fully test the software in its environment and to ensure proper functionality, qualification and compliance with component specifications. Regarding hardware deliverables, FME warrants that they will be free from defects in material and workmanship under use and service as specified in the accompanying written materials for a duration of 1 year from the date of receipt by the customer. Should a hardware deliverable turn out to be defect, FME’s entire liability and the customer’s exclusive remedy shall be, at FME´s sole discretion, either return of the purchase price and the license fee, or replacement of the hardware deliverable or parts thereof, if the deliverable is returned to FME in original packing and without further defects resulting from the customer’s use or the transport. However, this warranty is excluded if the defect has resulted from an accident not attributable to FME, or abuse or misapplication attributable to the customer or any other third party not relating to FME or to unauthorised decompiling and/or reverse engineering and/or disassembling. FME does not warrant that the deliverables do not infringe any third party intellectual property right (IPR). In the event that the deliverables infringe a third party IPR it is the sole responsibility of the customer to obtain necessary licenses to continue the usage of the deliverable. In the event the software deliverables include the use of open source components, the provisions of the governing open source license agreement shall apply with respect to such software deliverables. To the maximum extent permitted by applicable law FME disclaims all other warranties, whether express or implied, in particular, but not limited to, warranties of merchantability and fitness for a particular purpose for which the deliverables are not designated. To the maximum extent permitted by applicable law, FME’s liability is restricted to intention and gross negligence. FME is not liable for consequential damages. Should one of the above stipulations be or become invalid and/or unenforceable, the remaining stipulations shall stay in full effect. The contents of this document are subject to change without a prior notice, thus contact FME about the latest one. © Fujitsu Microelectronics Europe GmbH -3- MCU-AN-390105-E-V11 SPI COMMUNICATION TO ADC Contents Contents REVISION HISTORY.............................................................................................................. 2 WARRANTY AND DISCLAIMER............................... ERROR! BOOKMARK NOT DEFINED. CONTENTS ............................................................................................................................ 3 0 INTRODUCTION................................................................................................................ 5 1 MAX1286 ........................................................................................................................... 6 1.1 ADC .......................................................................................................................... 6 1.2 Connection to MB90340............................................................................................ 6 1.3 Communication Timing ............................................................................................. 7 1.4 1.3.1 Read Cycle Timing of MAX1286 ................................................................. 7 1.3.2 “X-Bits” at end of communication ................................................................ 7 Example Code .......................................................................................................... 8 1.4.1 1.4.2 1.4.3 Initial Functions and Declarations ............................................................... 8 1.4.1.1 Flowchart .................................................................................... 8 1.4.1.2 C Code........................................................................................ 9 Read Data from ADC ................................................................................ 10 1.4.2.1 Flowchart .................................................................................. 10 1.4.2.2 C Code...................................................................................... 11 Example of usage of Get_ADC function in Main Function ........................ 12 1.4.3.1 Flowchart .................................................................................. 12 1.4.3.2 C Code...................................................................................... 13 2 BIBLIOGRAPHY.............................................................................................................. 14 2.1 Related Documents................................................................................................. 14 MCU-AN-390105-E-V11 -4- © Fujitsu Microelectronics Europe GmbH SPI COMMUNICATION TO ADC Introduction 0 Introduction This application note describes how to communicate via SPI using the MB90340-UART with an external ADC. In this note a MAX1286 from Maxime is used. Please note, that this document only gives a rough overview about the communication. The described source codes were written for understanding not for code size or speed. Neither interrupts nor timer were used. Time critical program code is always performed by simple flag polling or wait loops. Please also refer to the Application Note MCU-AN-300002-E-SPI for further SPI specification and nomenclature. © Fujitsu Microelectronics Europe GmbH -5- MCU-AN-390105-E-V11 SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1 MAX1286 THIS CHAPTER DESCRIBES HOW TO COMMUNICATE WITH THE MAX1286 ADC 1.1 ADC The MAX1286 is a 150ksps, 12-Bit, 2-Channel Single-ended ADC. The MAX1286 has the following pin-out: • Vcc 1 8 SCLK AIN1 2 7 DOUT AIN2 3 6 CNVST GND 4 5 REF Pin names: Vcc AIN1 AIN2 GND SCLK DOUT CNVST REF 1.2 Power Supply (+ 5 volts) Analog Input Channel 1 Analog Input Channel 2 Ground Serial Clock Input Serial Data Output Conversion Start Input External Reference Voltage Input Connection to MB90340 The ADC can be connected as in the following schematic. Please note, that no power supply pins and other MCU-Pins are drawn than those for the connection to the ADC. MCU-AN-390105-E-V11 -6- © Fujitsu Microelectronics Europe GmbH SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.3 Communication Timing Detailed description of the timing and timing parameters can be found in the corresponding datasheet of the MAX1286 ADC. 1.3.1 Read Cycle Timing of MAX1286 A read cycle (read Channel 1 from ADC) has the following bit timing (SPI-CPOL = 0, SPICPHA = 0): ~3Ms CNVST SCK DOUT D7 D6 D5 D4 D3 D2 D1 D0 The CNVST signal is performed by Port-Pin. The ADC conversion time is about 3 Ms. After this time reading from the ADC is allowed. Data sampling of the UART is performed at the rising edge of the SCK signal. 1.3.2 “X-Bits” at end of communication Because the ADC has a resolution of 12 bits and the UART can only read 8 bit frames synchronously, the read-out frame is 16 bit. The ADC sets the MSB of the 12 bit conversion data to the first bit. Because of this the last read 4 bits are not valid. SCK from UART 1st 8 Bits DOUT (SIN0) D11 D10 D9 D8 D7 2nd 8 Bits D6 D5 D4 D3 D2 D1 D0 X X X X Therefore the read-out data has to be shifted 4 times right. The upper 4 bits have to be filled with “0” then. © Fujitsu Microelectronics Europe GmbH -7- MCU-AN-390105-E-V11 SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.4 Example Code The following code shows how to establish a SPI communication to the ADC. 1.4.1 Initial Functions and Declarations 1.4.1.1 Flowchart InitUART InitPorts wait Set Baud rate Set Port-Register 9 to “0x00” Argument: j Set clock inversion (ESCR-SCES = 1) Set clock delay (ECCR-SCDE = 1) Reception and Transmission enable Set Port 90 to Output for CNVST-Pin Do loop from “0” to “j” Init Port 0 for LED bargraph Set Mode 2, enable SCLK, enable SOT Set MSB first MCU-AN-390105-E-V11 -8- © Fujitsu Microelectronics Europe GmbH SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.4.1.2 C Code void InitUART(void) { BGR0 = 15; ESCR0 = 0x01; ECCR0 = 0x10; SCR0 = 0x03; SMR0 = 0x83; SSR0 = 0x04; } void InitPorts(void) { // Bit#0: CNVST PDR9 = 0x00; DDR9 = 0x01; PDR0 = 0x00; DDR0 = 0xFF; // // // // // // 1M Bit/s @ 16 MHz SCES = 1 => CPOL = 0 SCDE = 1 => CPHA = 0 reception and transmission enable Mode 2, SCLK enable, SOT enable MSB first, no interrupts // All Low // P90 = CNVST (Start convert) // LED-Port on Flash-CAN-100P-340 } void wait(unsigned char a) { unsigned char i; for(i = 0; i < a; i++); } © Fujitsu Microelectronics Europe GmbH -9- MCU-AN-390105-E-V11 SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.4.2 Read Data from ADC 1.4.2.1 Flowchart Get_ADC Set CNVST=1 (PDR90 =1) Wait for 4 NOPs Set CNVST=0 (PDR90 =0) Wait for 5 Ms Write dummy data to produce SCK Y RDRF == 0 ? N Read reception data and shift left 4 times Write dummy data to produce SCK Y RDRF == 0 ? N Read reception data and shift right 4 times and “OR” it to the last data Return data to caller MCU-AN-390105-E-V11 - 10 - © Fujitsu Microelectronics Europe GmbH SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.4.2.2 C Code unsigned int Get_ADC(void) { unsigned int data; PDR9 = 0x01; #pragma asm NOP NOP NOP NOP #pragma endasm // Start convert PDR9 = 0x00; wait(1); // Conversion Time (5 Fs approx.) TDR0 = 0x00; // Set dummy data to produce SCK while (SSR0_RDRF == 0); data = (RDR0 << 4); // Read bits 11-4 TDR0 = 0x00; // Set dummy data to produce SCK while (SSR0_RDRF == 0); data |= (RDR0 >> 4); // Read bits 3-0 return data; } © Fujitsu Microelectronics Europe GmbH - 11 - MCU-AN-390105-E-V11 SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.4.3 Example of usage of Get_ADC function in Main Function 1.4.3.1 Flowchart main Call InitPorts Call InitUART Call Get_ADC Use upper 8 bits of ADC data and put result to the LEDs of the Starterkit Board (as bar graph) MCU-AN-390105-E-V11 - 12 - © Fujitsu Microelectronics Europe GmbH SPI COMMUNICATION TO ADC Chapter 1 MAX1286 1.4.3.2 C Code void main(void) { unsigned int adc; unsigned char data; InitPorts(); InitUART(); // Initialize CNVST, LEDs // Init UART for SPI communication while(1) { adc = Get_ADC(); data = ((adc >> 4) & 0xFF); // bits 11-4 if (data > 227) PDR0 = 0xFF; else if (data > 198) PDR0 = 0x7F; else if (data > 170) PDR0 = 0x3F; else if (data > 142) PDR0 = 0x1F; else if (data > 113) PDR0 = 0x0F; else if (data > 85) PDR0 = 0x07; else if (data > 57) PDR0 = 0x03; else if (data > 28) PDR0 = 0x01; else PDR0 = 0x00; } } © Fujitsu Microelectronics Europe GmbH - 13 - MCU-AN-390105-E-V11 SPI COMMUNICATION TO ADC Chapter 2 Bibliography 2 Bibliography THIS CHAPTER NAMES THE RELATED DOCUMENTS 2.1 Related Documents • MCU-AN-300002-E-SPI Fujitsu Application Note about SPI Modes • DS MAX1286-MAX1289 Maxime data sheet of MAX1286 MCU-AN-390105-E-V11 - 14 - © Fujitsu Microelectronics Europe GmbH