Microcontrollers and Interfacing week 10 exercises

Similar documents
Microcontrollers and Interfacing

Embedded Systems and Software. Serial Interconnect Buses I 2 C (SMB) and SPI

PIN ASSIGNMENT PIN DESCRIPTION

DS1676 Total Elapsed Time Recorder, Erasable

PIC Serial Peripheral Interface (SPI) to Digital Pot

DS1845 Dual NV Potentiometer and Memory

DS1306. Serial Alarm Real Time Clock (RTC)

User-configurable Resolution. 9 to 12 bits (0.5 C to C)

User-configurable Resolution. 9 to 12 bits (0.5 C to C)

Part 1 Using Serial EEPROMs

DS 1682 Total Elapsed Time Recorder with Alarm

Lecture 25 March 23, 2012 Introduction to Serial Communications

Section 16. Basic Sychronous Serial Port (BSSP)

Parallel Data Transfer. Suppose you need to transfer data from one HCS12 to another. How can you do this?

ADC to I 2 C. Data Sheet. 10 Channel Analog to Digital Converter. with output via I 2 C

2-Wire, 5-Bit DAC with Three Digital Outputs

Temperature Sensor TMP2 PMOD Part 1

Serial Peripheral Interface. What is it? Basic SPI. Capabilities. Protocol. Pros and Cons. Uses

I also provide a purpose-built ADC/DAC board to support the lab experiment. This analogue I/O board in only needed for Part 3 and 4 of VERI.

Evaluation Board for Transducer ADC EVAL-AD7730EB

Growing Together Globally Serial Communication Design In Embedded System

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

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

1.3inch OLED User Manual

Arduino Uno R3 INTRODUCTION

DS Wire Digital Thermometer and Thermostat

SILICON MICROSTRUCTURES

PART OUT7 OUT8 OUT0 OUT31 AVCC REFGND AVDD OUT5 OUT4 OUT3 OUT2 OUT11 OUT1. Maxim Integrated Products 1

DS1305 Serial Alarm Real Time Clock (RTC)

DS1305EN. Serial Alarm Real-Time Clock

Serial Peripheral Interface Bus SPI

Interfacing Z8 Encore! XP MCUs with an I 2 C-Based Character LCD

Functional block diagram AD53x1 (Analog Devices)

Introduction to I2C & SPI. Chapter 22

Twos Complement, Dual 12-Bit DAC with Internal REF and Fast Settling Time AD5399

M68HC08 Microcontroller The MC68HC908GP32. General Description. MCU Block Diagram CPU08 1

Low Voltage, 10-Bit Digital Temperature Sensor in 8-Lead MSOP AD7314

EVALUATION KIT AVAILABLE Multirange, +5V, 12-Bit DAS with 2-Wire Serial Interface ANALOG INPUTS INL (LSB) ±1/2

PART MAX5544CSA MAX5544ESA REF CS DIN SCLK. Maxim Integrated Products 1

Tutorial for I 2 C Serial Protocol

DS WIRE INTERFACE 11 DECOUPLING CAP GND

DS1305EN. Serial Alarm Real-Time Clock

± 2g Tri-axis Accelerometer Specifications

CS Channel Analog Volume Control. Features. Description + _

DS1305. Serial Alarm Real Time Clock (RTC) FEATURES PIN ASSIGNMENT ORDERING INFORMATION

PIC Microcontroller Introduction

LC2 MOS Dual, Complete, 12-Bit/14-Bit Serial DACs AD7244

EE 456 Fall, Table 1 SPI bus signals. Figure 1 SPI Bus exchange of information between a master and a slave.

I2C a learn.sparkfun.com tutorial

COOKING WITH TEAM 279

ORDERING INFORMATION. OPERATION Measuring Temperature A block diagram of the DS1621 is shown in Figure 1. DESCRIPTION ORDERING PACKAGE

Universität Dortmund. IO and Peripheral Interfaces

UART TO SPI SPECIFICATION

11.4 THE SERIAL PERIPHERAL INTERFACE (SPI)

17. I 2 C communication channel

Chapter 1 Introducing the OM-USB Functional block diagram... 5

AMS 5812 OEM pressure sensor with an analog and digital output

Raspberry Pi - I/O Interfaces

Introduction the Serial Communications Parallel Communications Parallel Communications with Handshaking Serial Communications

Interfacing Techniques in Embedded Systems

Serial Buses in Industrial and Automotive Applications

App Note Application Note: Addressing Multiple FPAAs Using a SPI Interface

Manual iaq-engine Indoor Air Quality sensor

Network Embedded Systems Sensor Networks Fall Hardware. Marcus Chang,

Overview: Functional Description:

IV B.Tech. I Sem (R13) ECE : Embedded Systems : UNIT -4 1 UNIT 4

Digital Thermometer and Thermostat

DS1306 Serial Alarm Real-Time Clock

Application note Differential Pressure Sensor Type D6F-PH (Rev 1.0)

USER MANUAL EXPERIENCE INCREDIBLE PERFORMANCE V2.3

Development and research of different architectures of I 2 C bus controller. E. Vasiliev, MIET

How to Implement I 2 C Serial Communication Using Intel MCS-51 Microcontrollers

Environmental Data Acquisition Using (ENC28J60)

McMaster University Embedded Systems. Computer Engineering 4DS4 Lecture 6 Serial Peripherals Amin Vali Feb. 2016

< W3150A+ / W5100 Application Note for SPI >

PART IN+ IN- TX_DISABLE TX_FAULT BIAS SET BIASMAX 2 APCSET 2 MODSET 2 MOD SET PC_MON BS_MON

PRACTICAL DESIGN TECHNIQUES FOR SENSOR SIGNAL CONDITIONING

DIGITAL HUMIDITY SENSOR HYT-131

DS75 Digital Thermometer and Thermostat

ECE 4510/5530 Microcontroller Applications Week 10

DS1624 Digital Thermometer and Memory

DS1855 Dual Nonvolatile Digital Potentiometer and Secure Memory

ADC128D Bit, 8-Channel, ADC System Monitor with Temperature

FM24CL04 4Kb FRAM Serial Memory

Serial Communication. Simplex Half-Duplex Duplex

Dallas Semiconductor DS1307 Real Time Clock. The DS 1307 is a real-time clock with 56 bytes of NV (nonvolatile)

Altimeter / Barometer Module SMD500 ultra low power, low voltage

AN510 Using SPI protocol with pressure sensor modules

FM24C Kb FRAM Serial Memory Features

DS1625. Digital Thermometer and Thermostat FEATURES PIN ASSIGNMENT

Stereo Audio Volume Control

Display Real Time Clock (RTC) On LCD. Version 1.2. Aug Cytron Technologies Sdn. Bhd.

EPT-200TMP-TS-U2 TMP102 Temperature Sensor Docking Board Data Sheet

+Denotes a lead(pb)-free/rohs-compliant package.

MOS INTEGRATED CIRCUIT

HP07 Digipot Interface Module

DS1870 LDMOS BIAS CONTROLLER EV KIT

Dual Digital Pot (100K) SKU: DFR0520

TS2043 Preliminary CMOS IC

Digital Thermometers and Thermostats with SPI/3-Wire Interface

Transcription:

1 SERIAL PERIPHERAL INTERFACE (SPI) HARDWARE Microcontrollers and Interfacing week 10 exercises 1 Serial Peripheral Interface (SPI) hardware Complex devices (persistent memory and flash memory cards, D/A and A/D converters, real-time clocks, etc.) often have serial connections instead of parallel. They behave a little like a shift register, exchanging parallel information using one or two serial data signals and a clock. The two most common protocols are Inter-Integrated Circuit (I 2 C) and Serial Peripheral Interface (SPI). The simpler (and in many ways the more flexible) of the two is SPI. 1.1 External ADC: MCP204 To understand how SPI works, let s increase the resolution of our analogue input by connecting an external analogue-to-digital converter and communicating with it using SPI. A popular device for this is the MCP204 which has four independent 12-bit ADC channels. (pin 14) 5 V power supply CH0 CH1 CH2 CH NC NC DGND 1 2 4 5 6 7 MCP204 14 1 12 11 10 9 8 AGND CLK /SHDN DGND (pin 7) 0 V digital ground AGND (pin 12) 0 V analogue ground (pin 1) reference voltage, sets the upper limit of input voltage (corresponding to the maximum digital A/D output value) CH0 (pins 1 4) the four analogue input channels CLK (pin 11) SPI serial clock input (pin 9) SPI serial data input (equivalent to ) 5V (1) (12) (11) (10) serial clock master in, slave out master out, slave in slave select (active low) CH0 CH1 CH2 CH 10k (pin 10) SPI serial data output (equivalent to ) (pin 8) SPI active-low chip select (equivalent to ) Arduino GND DGND AGND MCP204 Connect a MCP204 ADC to the Arduino. Use the following Arduino pins to carry the SPI signals: SPI / Arduino MCP204 pin 10 pin 8 pin 11 pin 9 pin 12 pin 10 pin 1 pin 11 CLK Pin 1 Be very careful to connect the MCP204 the correct way around. Like the shift register, if the power is connected backwards the device will be damaged. Create a test voltage using a potentiometer (or a sensor of your choice) and connect it to the first input, CH0. 1 Ask the instructor to check your work. 1 of

2 SPI COMMUNICATION SOFTWARE 2 SPI communication software Some microcontrollers (including the Arduino) provide SPI support in hardware. Access to the hardware s SPI signals is made available through a library. Other microcontrollers do not include harware SPI support, and communicating with SPI devices requires manual control of all four SPI signals. We will use both methods to communicate with the MCP204. 2.1 Using the SPI library To use a library, it is sufficient to include the associated header file at the beginning of the program. We will use the SPI library to quickly test our hardware. Create a new sketch. Make the SPI library available by including SPI.h at the beginning. #include <SPI.h> The SPI library manages the CLK, and signals for us. It does not manage the slave select signal, so we will do that ourselves. Create a definition (using #define or const int to associate the symbol N ( slave select, active-low ) with pin number 10. Create a setup() function that performs the following initialisation steps: 1. Start the Serial interface at 9600 baud. 2. Configure the N pin as an OUTPUT, and set its value to inactive.. Start the SPI interface using a clock divider of 4, SPI mode 0,0, and transfers with most significant bit first. const int N = 10; // slave select void setup() Serial.begin(9600); pinmode(n, OUTPUT); digitalwrite(n, HIGH); // device inactive SPI.begin(); SPI.setClockDivider(SPI_CLOCK_DIV4); // 4 MHz SPI.setDataMode(SPI_MODE0); // low, leading SPI.setBitOrder(MSBFIRST); Create a function int readadc(int channel) that reads and returns a value from the given ADC channel. To read from the ADC using SPI, you will have to do the following steps: 1. Turn on the ADC by setting N active. 2. Use SPI.transfer() to send (and receive) three bytes from the ADC. The first byte contains five zeros, a start bit 1, and mode bit 1 (single-ended conversion), and the most significant bit of the three-bit channel number. The second byte contains the least significant two bits of the channel number, followed by 6 zeros. The third byte can contain anything.. Collect the values returned by the second and third calls to SPI.transfer(). The least significant four bits of the first value are the four most significant bits of the ADC result. The second value contains the eight least significant bits of the result. Recombine them into a 12-bit result. 4. Turn off the ADC by setting N inactive. 5. Return the result from the ADC. int readadc(unsigned char channel) digitalwrite(n, LOW); int adval = 0; SPI.transfer(0b00000110 + ((channel >> 2) & 1)); adval = ((unsigned char)spi.transfer(channel << 6) & 0b00001111) << 8; adval = ((unsigned char)spi.transfer(0)); digitalwrite(n, HIGH); return adval; Test your sketch with a loop() that prints the results of reading channel 0, then pauses for a quarter of a second. (Don t forget to open the serial monitor to view the output generated by the sketch.) 2 Ask the instructor to check your work. void loop() Serial.println(readADC(0)); delay(250); 2 of

2.2 Implementing SPI manually CHALLENGES 2.2 Implementing SPI manually Let s simulate what would be needed on a microcontroller that does not have SPI hardware or a library giving access to it. This involves driving the SPI slave select, clock and data signals directly using digitalwrite(). (This technique is informally known as bit banging the interface.) Save your sketch from the previous section under a new name. We are going to remove all references to the SPI library and generate the necessary SPI signals explicitly. Delete the #include <SPI.h> from the beginning of the sketch. Keep the defintion for N. Add three more definitions for (pin 11), (pin 12), and (pin 1). In setup(), delete the four lines that configure the SPI library. Replace them with code that performs the following actions: 1. Configure as an OUTPUT. 2. Configure as an INPUT.. Configure as an OUPUT and set it to idle (LOW). Ask the instructor to check your work. Implement a function void sendbit(int value) that writes the least significant bits of value to the SPI device. You will have to perform two steps to do this: 1. Write (using digitalwrite()) the least significant bit (only) of value to. 2. Generate a positive pulse on by setting it HIGH then back to LOW. Implement a function int recvbit(void) that reads one bit from the SPI device. You will have to perform four steps to do this: 1. Set active (HIGH) to ensure data is available on. 2. Use digitalread() to obtain the value (0 or 1) of.. Set inactive (LOW) to end the clock cycle. 4. Return the bit that was read from. 4 Ask the instructor to check your work. Delete the three lines in readadc() that refer to SPI.transfer(). Replace them with code that explicitly initiates a conversion. (Refer to the timing diagram in Appendix B.) Your code will perform the following steps: 1. Use sendbit() to send a 1 (start bit) followed by another 1 (single-ended mode) to the ADC. (We do not need to send the initial 5 zeros that were necessary when using the byte-oriented SPI library.) 2. Use sendbit() to write the least significant three bits of channel to the ADC.. Use sendbit() to write two don t care bits to the ADC. 4. Use recvbit() to read 12 bits from the ADC. For each bit read you will have to shift adval one bit left, then add to it the bit read from the ADC. At the end of this you will have reconstructed the 12 bits of ADC result. 5. Return the final value of adval. Test your code. (You can leave the original loop() unmodified.) 5 Ask the instructor to check your work. Challenges Modify loop() (in either of your sketches) to display the values of all four ADC channels. Connect one or more voltage sources to the other ADC input channels and verify that the sketch prints the correct results. 6 Ask the instructor to check your work. Disable (comment out) the Serial.println() and delay() from loop() in your two sketches. Modify loop to perform many conversions (call readadc() many times). Use millis() to record the time before and after performing the conversions. Calculate how many conversions per second are being peformed. SPI library conversions per second: Manual SPI conversions per second: 7 Ask the instructor to check your work. Connect a MCP4288 digital-to-analogue converter and use it to generate a sine wave. (See the appendix for the relevant parts of the data sheet.) of

A Serial communication Reference A shift register can convert a single serial signal (containing a fixed-length message sequence of N single bits) into N parallel signals. We can generalise this very simple protocol in several ways: connect the serial signal to more than one external converter device, send information in both directions (from microcontroller to device, or from device back to microcontroller), use variable-length message sequences, etc. Two popular general protocols are the Serial Peripheral Interface (SPI) and Inter-Integrated Circuit (I 2 C). Both are insystem protocols, meant for communication between components within a single piece of equipment. Useful devices that use these protocols include Electrically-Erasable Programmable Read-Only Memory (EEPROM), Analog-to-Digital Converter (ADC), Digital-to-Analog Converter (DAC), real-time clocks (RTC), various sensors, Liquid Crystal Display (LCD) controllers, Secure Digital (SD) memory cards, the SM (system management) bus on Intel motherboards, etc. A.1 Inter-Integrated Circuit (I 2 C) I 2 C was developed in 1982 by Philips Semiconductor to enable communication between digital devices in television sets. I 2 C is a bus-based protocol. Up to 127 devices can be connected to the bus, each having a unique 7-bit address. Devices communicate by exchanging byte-oriented messages with each other. I 2 C therefore creates a mini network, in which senders and receivers are chosen dynamically according to a relatively complex protocol. An I 2 C bus needs just two wires: serial data SDA and serial clock SCL. The bus can be in one of two states, active or idle. When idle, both SDA and SCL are high, and no communication is taking place. When the bus is active, the clock cycles from high to low and back again while serial communication takes place on SDA. During a clock cycle, SDA is permitted to change only when SCL is low and must remain stable (for the receiver to sample) when SCL is high. Any device can initiate a message send by (temporarily) claiming ownership of the I 2 C bus while the bus is idle. Ownership is claimed by generating a falling edge on SDA while SCL is high, a condition that should never occur while the bus is active. serial clock SCL serial data SDA bus idle bus claimed bit 1 bit 2 bit For the duration of the message exchange, the device that initiates the exchange is called the master and the device responding to the message is the slave. The master controls SCL throughout the message exchange. At various times during a message exchange, one of these devices is the transmitter and the other is the receiver. The transmitter controls SDA, regardless of which device is the master. Every byte transmitted is followed by a single-bit acknowledgement from the destination device. Nine bits of data are therefore exchanged for each byte of data transmitted, with the receiver controlling the final acknowledgement bit. serial clock SCL transmitter SDA receiver SDA bit 1 bit 2 bit bit 4 bit 5 bit 6 bit 7 bit 8 ack At the end of data transfer, the master releases the bus by generating a rising edge on SDA while SCL is high, which (again) is a condition that never occurs while the bus is active. serial clock SCL transmitter SDA bit 7 bit 8 (ack) bus release bus idle Each message begins with the master transmitting a byte containing seven address bits and one R/W direction bit. The direction bit indicates if the master is writing to the slave or reading from it. The slave sends back a single bit acknowledging the receipt of the message. Master and slave then exchange an arbitrary number of data bytes, in the direction specified by the direction bit. SDA (bus claim) seven-bit slave address R/W ack 1 slave address and direction byte eight data bits ack N data bytes SDA (bus release) I 2 C permits two devices to communicate with each other in both directions, but only in one direction at a time (for a given message). It is therefore a half-duplex protocol. I 2 C has relatively low performance, with typical maximum clock speeds of 100 khz or 400 khz. It is particularly good for configuring and monitoring devices that have control registers and/or status registers. 1

A.2 Serial Peripheral Interface (SPI) SPI was developed by Motorola in 1985 for communication between a microcontroller (originally the M68HC11) and its external devices. SPI is a point-to-point protocol. It connects a single master device to one or more slave devices. The slave devices have no address, but each one has an active-low slave select () input. When is inactive (high), the slave device ignores its other SPI input signals and disables its SPI output (by making it high-impedance). This allows several slaves to share a single SPI connection, but only one of them can be active at a given time. The lack of addressing makes SPI a relatively simple protocol. master serial clock master out, slave in master in, slave out slave select (active low) SPI needs four wires: an active-low slave select, a serial clock and two uni-directional data signals, masterout/slave-in and master-in/slave-out. Several different conventions are possible for clocking, but the most usual is mode 0,0 in which the clock idles low (polarity 0) and received data signals are sampled on the leading (positive) edge of the clock signal (phase 0). slave phase 0 = leading edge serial clock slave select master out msb......... lsb master in msb......... lsb polarity 0 = idle low SPI clock mode clock active (phase,polarity) idles edge 0,0 low leading (rising) 0,1 low trailing (falling) 1,0 high leading (falling) 1,1 high trailing (rising) When using multiple slaves, each slave requires its own separate signal. SPI therefore requires + N wires to communicate with N slave devices. master serial clock master out, slave in master in, slave out 0 1 slave 0 slave 1 slave 2 slave 2 slave selects If N is large then the number of outputs required to generate the signals can be reduced using a serial to parallel converter (shift register) or a binary decoder. (A binary decoder has N inputs, representing a N-bit binary numeric value, and 2 N outputs of which only one is active at any time, selected according to the numeric value of the input number. It effectively converts a N-bit address into N individual chip select signals.) SPI permits the master and slave to exchange information in both directions at the same time. It is therefore a full-duplex protocol. SPI has relatively high performance, with typical maximum clock frequencies of 20 0 MHz (fast enough to transmit high-definition multi-channel audio, for example). It is particularly good for transferring streams of data, for example between a microcontroller and an external analogue-to-digital or digital-to-analogue converter. D0 D1 serial clock master out, slave in master in, slave out slave number N 0 N 1 N to 2 N binary decoder 0 1 2 slave selects 0 1 2 2

B MCP204: 4-channel 12-bit A/D converter with SPI The 10-bit ADC on the Arduino can be too limited for some applications. Increasing the resolution to 12 bits is easy using an external ADC. (Higher resolution ADCs are available, but beyond 14 bits or so it becomes quite difficult to design and build circuits with low enough noise to make the extra bits useful.) A popular device is the MCP204, containing 4 separate 12-bit A/D converters. The maximum clock frequency is 2 MHz, or a maximum conversion rate of approximately 100,000 samples per second. 5 V power supply DGND 0 V digital ground AGND 0 V analogue ground reference voltage, sets the upper limit of input voltage (corresponding to the maximum digital A/D output value) CH0 CH4 the four analogue input channels CLK SPI serial clock input SPI serial data input (equivalent to ) SPI serial data output (equivalent to ) SPI active-low chip select (equivalent to ) CH0 CH1 CH2 CH NC NC DGND 1 2 4 5 6 7 MCP204 14 1 12 11 10 9 8 AGND CLK /SHDN To perform an A/D conversion, the number of the channel to be read is clocked into the device via as a five-bit value: a single start bit (high), a single bit to select single-ended or differential mode, followed by three bits of binary channel number (for protocol compatibility with the MCP208, which has eight input channels). The device then begins the requested conversion. Two clock cycles later it provides the result of the conversion on as a 1-bit value, as one leading zero bit followed by the 12 result bits. The 12 bits of converted value are therefore sent back to the master device beginning three clock cycles after the address has been supplied, or seven cycles after the start bit was written. MCU latches data from A/D converter on rising edges of SCLK SCLK 1 2 4 5 6 7 8 9 10 11 12 1 14 15 16 Data is clocked out of A/D converter on falling edges SGL/ Start D2 D1 DO DIFF 17 18 19 20 21 22 2 24 Don t Care HI-Z NULL BIT B11 B10 B9 B8 B7 B6 B5 B4 B B2 B1 B0 MCU Transmitted Data (Aligned with falling edge of clock) MCU Received Data (Aligned with rising edge of clock) Start Bit 0 0 0 0 0 1 SGL/ D2 DIFF D1 DO X X X X X X X X X X X X X X??????????? 0 (Null) B11 B10 B9 B8 B7 B6 B5 B4 B B2 B1 B0 X = Don t Care Bits Data stored into MCU receive register after transmission of first 8 bits Data stored into MCU receive register after transmission of second 8 bits Data stored into MCU receive register after transmission of last 8 bits When communicating over SPI one byte at a time (with the Arduino SPI library, for example) three bytes of data must be exchanged to initiate the conversion and then read back the result. From sending the start bit to receiving the last bit of converted result is a total of 19 clock cycles. Sending five zero bits before the start will therefore conveniently right-justify the result in the last 12 bits of the 24 that are read back.

C MCP4288: 2-channel 12-bit D/A converter with SPI MCP4802/4812/4822 8/10/12-Bit Dual Voltage Output Digital-to-Analog Converter with Internal and SPI Interface Features Description MCP4802: Dual 8-Bit Voltage Output DAC MCP4812: Dual 10-Bit Voltage Output DAC MCP4822: Dual 12-Bit Voltage Output DAC Rail-to-Rail Output SPI Interface with 20 MHz Clock Support Simultaneous Latching of the Dual DACs with LDAC pin Fast Settling Time of 4.5 µs Selectable Unity or 2x Gain Output 2.048V Internal Voltage Reference 50 ppm/ C Temperature Coefficient 2.7V to 5.5V Single-Supply Operation Extended Temperature Range: -40 C to +125 C Applications Set Point or Offset Trimming Sensor Calibration Precision Selectable Voltage Reference Portable Instrumentation (Battery-Powered) Calibration of Optical Communication Devices Related Products (1) P/N DAC Resolution No. of Channels MCP4801 8 1 MCP4811 10 1 MCP4821 12 1 MCP4802 8 2 MCP4812 10 2 MCP4822 12 2 MCP4901 8 1 MCP4911 10 1 MCP4921 12 1 MCP4902 8 2 MCP4912 10 2 MCP4922 12 2 Voltage Reference ( ) Internal (2.048V) External Note 1: The products listed here have similar AC/DC performances. The MCP4802/4812/4822 devices are dual 8-bit, 10-bit and 12-bit buffered voltage output Digital-to-Analog Converters (DACs), respectively. The devices operate from a single 2.7V to 5.5V supply with SPI compatible Serial Peripheral Interface. The devices have a high precision internal voltage reference ( = 2.048V). The user can configure the full-scale range of the device to be 2.048V or 4.096V by setting the Gain Selection Option bit (gain of 1 of 2). Each DAC channel can be operated in Active or Shutdown mode individually by setting the Configuration register bits. In Shutdown mode, most of the internal circuits in the shutdown channel are turned off for power savings and the output amplifier is configured to present a known high resistance output load (500 k typical. The devices include double-buffered registers, allowing synchronous updates of two DAC outputs using the LDAC pin. These devices also incorporate a Power-on Reset (POR) circuit to ensure reliable powerup. The devices utilize a resistive string architecture, with its inherent advantages of low DNL error, low ratio metric temperature coefficient and fast settling time. These devices are specified over the extended temperature range (+125 C). The devices provide high accuracy and low noise performance for consumer and industrial applications where calibration or compensation of signals (such as temperature, pressure and humidity) are required. The MCP4802/4812/4822 devices are available in the PDIP, SOIC and MSOP packages. Package Types 8-Pin PDIP, SOIC, MSOP 1 8 V OUTA 2 7 V MCP48X2 SDI 4 6 5 V OUTB LDAC MCP4802: 8-bit dual DAC MCP4812: 10-bit dual DAC MCP4822: 12-bit dual DAC 2010 Microchip Technology Inc. DS22249A-page 1 MCP4802/4812/4822.0 PIN DESCRIPTIONS The descriptions of the pins are listed in Table -1. TABLE -1: PIN FUNCTION TABLE FOR MCP4802/4812/4822 MCP4802/4812/4822 MSOP, PDIP, SOIC Symbol Description 1 Supply Voltage Input (2.7V to 5.5V) 2 Chip Select Input Serial Clock Input 4 SDI Serial Data Input 5 LDAC Synchronization Input. This pin is used to transfer DAC settings (Input Registers) to the output registers (V OUT ) 6 V OUTB DAC B Output 7 V Ground reference point for all circuitry on the device 8 V OUTA DAC A Output.1 Supply Voltage Pins (, V ).4 Serial Data Input (SDI) is the positive supply voltage input pin. The input supply voltage is relative to V and can range from 2.7V to 5.5V. The power supply at the pin should be as clean as possible for a good DAC performance. It is recommended to use an appropriate bypass capacitor of about 0.1 µf (ceramic) to ground. An additional 10 µf capacitor (tantalum) in parallel is also recommended to further attenuate high-frequency noise present in application boards. V is the analog ground pin and the current return path of the device. The user must connect the V pin to a ground plane through a low-impedance connection. If an analog ground path is available in the application Printed Circuit Board (PCB), it is highly recommended that the V pin be tied to the analog ground path or isolated within an analog ground plane of the circuit board..2 Chip Select () is the Chip Select input pin, which requires an active-low to enable serial clock and data functions. SDI is the SPI compatible serial data input pin..5 Latch DAC Input (LDAC) LDAC (latch DAC synchronization input) pin is used to transfer the input latch registers to their corresponding DAC registers (output latches, V OUT ). When this pin is low, both V OUTA and V OUTB are updated at the same time with their input register contents. This pin can be tied to low (V ) if the V OUT update is desired at the rising edge of the pin. This pin can be driven by an external control device such as an MCU I/O pin..6 Analog Outputs (V OUTA, V OUTB ) V OUTA is the DAC A output pin, and V OUTB is the DAC B output pin. Each output has its own output amplifier. The full-scale range of the DAC output is from V to G*, where G is the gain selection option (1x or 2x). The DAC analog output cannot go higher than the supply voltage ( ).. Serial Clock Input () is the SPI compatible serial clock input pin. 2010 Microchip Technology Inc. DS22249A-page 15 4

MCP4802/4812/4822 REGISTER 5-1: WRITE COMMAND REGISTER FOR MCP4822 (12-BIT DAC) W-x W-x W-x W-0 W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x A/B GA SHDN D11 D10 D9 D8 D7 D6 D5 D4 D D2 D1 D0 bit 15 bit 0 REGISTER 5-2: WRITE COMMAND REGISTER FOR MCP4812 (10-BIT DAC) W-x W-x W-x W-0 W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x A/B GA SHDN D9 D8 D7 D6 D5 D4 D D2 D1 D0 x x bit 15 bit 0 REGISTER 5-: WRITE COMMAND REGISTER FOR MCP4802 (8-BIT DAC) W-x W-x W-x W-0 W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x W-x A/B GA SHDN D7 D6 D5 D4 D D2 D1 D0 x x x x bit 15 bit 0 Where: bit 15 A/B: DAC A or DAC B Selection bit 1 = Write to DAC B 0 = Write to DAC A bit 14 Don t Care bit 1 GA: Output Gain Selection bit 1 = 1x (V OUT = * D/4096) 0 = 2x (V OUT = 2 * * D/4096), where internal VREF = 2.048V. bit 12 SHDN: Output Shutdown Control bit 1 = Active mode operation. VOUT is available. 0 = Shutdown the selected DAC channel. Analog output is not available at the channel that was shut down. V OUT pin is connected to 500 k typical) bit 11-0 D11:D0: DAC Input Data bits. Bit x is ignored. Legend R = Readable bit W = Writable bit U = Unimplemented bit, read as 0 -n = Value at POR 1 = bit is set 0 = bit is cleared x = bit is unknown DS22249A-page 22 2010 Microchip Technology Inc. MCP4802/4812/4822 0 1 2 4 5 6 7 8 9 10 11 12 1 14 15 (Mode 1,1) (Mode 0,0) config bits 12 data bits SDI A/B GA SHDN D11 D10 D9 D8 D7 D6 D5 D4 D D2 D1 D0 LDAC V OUT FIGURE 5-1: Write Command for MCP4822 (12-bit DAC). 0 1 2 4 5 6 7 8 9 10 11 12 1 14 15 (Mode 1,1) (Mode 0,0) config bits 12 data bits SDI A/B GA SHDN D9 D8 D7 D6 D5 D4 D D2 D1 D0 X X LDAC V OUT Note: X = don t care bits. FIGURE 5-2: Write Command for MCP4812 (10-bit DAC). 0 1 2 4 5 6 7 8 9 10 11 12 1 14 15 (Mode 1,1) (Mode 0,0) config bits 12 data bits SDI A/B GA SHDN D7 D6 D5 D4 D D2 D1 D0 X X X X LDAC V OUT Note: X = don t care bits. FIGURE 5-: Write Command for MCP4802 (8-bit DAC). 2010 Microchip Technology Inc. DS22249A-page 2 5

D Week 10 example solution to bit banging exercise #define N 10 // slave select pin #define 11 // master out pin #define 12 // master in pin #define 1 // serial clock pin void setup() Serial.begin(9600); pinmode(n, OUTPUT); digitalwrite(n, HIGH); pinmode(, OUTPUT); pinmode(, INPUT); pinmode(, OUTPUT); digitalwrite(, LOW); void sendbit(unsigned char bit) digitalwrite(, bit & 1); digitalwrite(, HIGH); digitalwrite(, LOW); int recvbit(void) digitalwrite(, HIGH); int bit = digitalread(); digitalwrite(, LOW); return bit; int readadc(int channel) digitalwrite(n, LOW); sendbit(1); sendbit(1); sendbit(channel >> 2); sendbit(channel >> 1); sendbit(channel); sendbit(0); sendbit(0); // slave select inactive // clock idle // value to write // clock data into device // clock idle // clock don t care bit into device // result bit from device // clock idle // slave select active // start bit // single-ended mode // discard empty result bit // discard null result bit int advalue = 0; for (int i= 0; i < 12; ++i) advalue = (advalue << 1) + recvbit(); digitalwrite(n, HIGH); // slave select inactive return advalue; void loop() Serial.println(readADC(0)); delay(250); 6