Cirrus Logic AN88 User Manual

Application Note
Interfacing the CS5525/6/9 to the PIC16F84
By Keith Coffey
AN88
INTRODUCTION
This applica tion note details the interfac e of Crys-
tal Semiconductor’s CS5525/6/9 Analog-to-Digi­tal Converter (ADC) to the Microchip PIC16 microcontroller series. This note takes the reader through a sim ple e xa mpl e desc ribin g h ow to co m­municate w ith the ADC. All algo rithms discussed are included in the Appendix at the end of this note.
ADC DIGITAL INTERFACE
The CS5525/6/9 interfaces to the PIC16F84 through either a three-wire or a four-wire interface. Figure 1 depict s the inte rface be tween the two de­vices. Though this software was written to inter­face to Port A (RA) on th e PIC16F 84 with a four ­wire interfa ce, the algorit hms can be easily modi­fied to work with the three-wire format.
The ADC’s serial port consists of four control lines: CS
CS
, SCLK, SDI, and SDO.
, Chip S elect, is the contro l line w hich e nables
access to the serial port. SCLK, Serial Clock, is the bit-clock which controls
the shifting of data to or from the ADC’s serial port.
SDI, Serial Data In, is the data signal used to trans­fer data from the PIC16F84 to the ADC.
SDO, Serial Data Out, is the data signal used to transfer output data from the ADC to the PIC16F84.
SOFTWARE DESCRIPTION
This note presents algorithms to initialize the PIC16F84 and the CS5525/6/9, perform a se l f-off­set calibratio n, modi fy the C S5525/ 6/9 gai n regis­ter, and then acquire a conversion. Figure 2 depicts
CS5525/6/9 PIC16F84
CS
SDI
SDO
SCLK
Cirrus Logic, Inc. Crystal Semiconductor Products Division
P.O. Box 17847, Austin, Texas 78760 (512) 445 7222 FAX: (512) 445 7581 http://www.crystal.com
NC (RA0) RA1 RA2
RA3
Figure 1. 3-Wire and 4-Wire Interfaces
CS5525/6/9 PIC16F84
CS
SDI
SDO
SCLK
Copyright  Cirrus Logic, I nc. 1997
(All Rights Reserv ed)
RA0 RA1 RA2
RA3
NOV ‘97
AN88Rev2
1
AN88
a block diagra m overview. While readi ng this ap­plication note, please refer to the Appendix for the code listing.
Initialize
Initialize is a subroutine that configures Port A (RA) on the PIC1 6F84 a nd place s the C S5525/ 6/9
in the command-state. First, RA’s data direction is configured as dep ic te d i n Figure 1 (for more infor ­mation on configuring ports refer to Microchip’s PIC16F8X Data Sheet). After configuring the port, the control ler en te rs a de lay st ate t o al low tim e fo r the CS5525/6 /9’s power-on-re set and oscillato r to start-up (oscillator start-up time is typically 500 ms). The last step is to reinitialize the serial port on the ADC (r einitializing t he serial port is unneces­sary here, it was added for demonstration purposes only). This is implemented by sending the convert­er sixteen byte s of logic 1’s foll owed by one fin al byte, with its LSB logic 0. Once sent, the sequence places the serial port of the ADC into the com­mand-state, where it aw aits a valid command.
After returning to main, the software demo nstrates how to calibra te the convert er’s offset.
START
INITIALIZE
MICROCONTROLLER/CS5525/6/9
SELF-OFFSET CAL.
MODIFY GAIN
ACQUIRE CONVERSION
Figure 2. CS5525/6/9 Software Flowchart
Self-Offset Calib r ation
Calibrate is a subroutine that calibrates the con­verter’s offset. Calibrate first sends 0x000001 (Hex) to the conf iguration register. This i nstructs the converter to perform a self-offset calibration. Then the Done Flag (D F) bit in th e configuration register i s polled until set. Onc e DF is set, it indi­cates that a valid calibration was performed. To minimize digital noise (while performing a calibra­tion or a c onver sion ), ma ny sy ste m d esigners may find it advantageous to add a software delay equiv­alent to a conversion or calibration cycle before polling the DF bit.
Read/Write Gain Register
To modify the gain register the command-byte and data-byte variables are first initialized. This is ac­complished by the MOVLW and MOVWF op­codes. The subroutine write_register uses these variables to set th e conten ts of the gain regi ster in the CS5525/6/9 to 0x800000 (HEX). To do this, write_register first asserts CS
and then it calls send_spi four times (once for the command-byte and three a dditional times for t he 24 bit s of data). Send_spi is a subroutine used to ‘bit-bang’ a byte of information from the PIC16F84 to the CS5525/6/9. A byte is tra nsferre d one bit at a time , MSB (most significan t bit) first, by placing an info rmation b it on RA1 (SDI) and then pulsing RA3 (SCLK). This process is repeated eight times. Figure 3 depicts the timing diag ram for t he write-c ycle in the CS5525/ 6/9’s serial port. Th is al gorit hm de mo nstrat es how to write to the g ain register . It doe s not pe rform a gain calibration. To perform a gain calibration, fol­low the procedu r es ou tlined in the data shee t.
To verify if 0x800000 (HEX) was written to the gain register, read_register is call ed. It dup licates the read-cycle timing diagram depicted in Figure 4. Read_register first asserts CS
and then calls send_spi once to transfe r the com ma nd- byte to the CS5525/6/9. This places the converter into the
2 AN88Rev2
AN88
data-state where it wa its unt il data is rea d from its serial port. To re ceiv e th e da ta, read_register ca ll s receive_spi th ree times. Receive_sp i is a subroutine
used to ‘bit-ba ng’ a byte of informa tion from the ADC to the PIC16F84. Similar to send_spi, receive_spi acquires this information one bit at a time MSB first. When the transfer is complete, the variables high byte, midbyte, and lowbyte contain the CS5525/6/ 9’s 24-bit gain registe r.
Acquire Conversion
To acquire a c onversion the subroutine convert is called. Convert se nds the command-byte 0x0 C to the convert er. This instructs the converter to per­form a single conversion. Then the Done Flag (DF)
bit in the configuration register is polled. When set, DF indicates that a conversion was performed. Once DF is s et, the cont roller reads the conve rs ion data register to acquire the conversion.Figure 6 de­picts how 16-bit and 2 0-bit conversion words are stored in th e microcontroller.
An alternate m ethod ca n be used to acquire a con­version. By se tting the Port Flag bit (PF, the fifth bit in the configuration register), SDO’s function is modified to fall to logic 0 when a conversion is complete (refer to Fi gure 5). By tying SDO t o the controller’s interrupt pin, conversions can be ac­quired via an interrupt service routine.
Figure 3. Write-Cycle Timing
Figure 4. Read-Cycle Timing
AN88Rev2 3
SCLK
SDI
*
t
ommand Time
C
8SCLKs
DO
S
td = XIN/OWR clock cycles for each conversion except the
*
first conversion whichwilltakeXIN/OWR+ 7 clockcycles
Figure 5. Conversion/Acquisition Cycle with the PF Bit Asserted
d
Data SDO Continuous Conversion Read (PF bit = 1)
SCLKs Clear SDO Flag
8
AN88
IN/OWR
X
Clock Cycles
SB
M
ataTime
D 24 SCLKs
SB
L
MSB High-Byte
D19 D18 D17 D16 D15 D14 D13 D12
Mid-Byte
D11 D10 D9 D8 D7 D6 D5 D4
Low-Byte
D3 D2 D1 D0 0 0 OD OF
A) 20-Bit Conversion Data Word
MSB High-Byte
D15 D14 D13 D12 D11 D10 D9 D8
Mid-Byte
D7 D6 D5 D4 D3 D2 D1 D0
Low-Byte
111100ODOF
B) 16-Bit Conversion Data Word
0- always zero, 1
OD - Oscillation Detect, OF - Overflow
Figure 6. Bit Representation/Storage in PIC16F84
- always one,
MAXIMUM SCLK RATE
A machine cyc le in the PIC16F84 c onsists 4 oscil-
lator periods o r 400 ns i f the mic rocontrol ler’s os ­cillator frequency is 10 MHz. Sinc e the CS5525/6/ 9’s maximum SCLK ra te is 2MHz, additiona l no operation (NOP) delays may be necessary to re­duce the tr ansfer rate if the micro controller s ys tem requires higher ra te oscillators.
SERIAL PERIPHERAL INTERFACE
The Serial Peripheral Interface (SPI) developed for Microchip’s controllers wasn’t designed to be as flexible as the SPI port on Motorola’s 68HC05. To get the Micr ochip’s SPI port to functi on with the CS5525/6/9, th e port n ee ds to be ini tia lized t o id le high, and th e CS55 25/6/9’s se rial po rt needs to be reset anytime information is transmitted between the microcontroller and th e c onverter.
DEVELOPMENT TOOL DESCRIPTION
The code in this a ppl ication note was developed
TM
using MPLAB development pa cka ge from Microchip, Inc .
4 AN88Rev2
, an integrated software
AN88
CONCLUSION
This applicat ion note prese nts an example of how to interface t he CS5525/6/9 to the PIC16 F84. It is divided into two main sec ti ons: har dware and soft­ware. The hardware section illustrates both a three­wire and a four-wire interface. The three-wire is
TM
SPI
and MICROWIRE ware, develope d w ith dev elopme nt to ols fro m Mi­crochip, Inc., illustrates how to initialize the converter and microcontroller, calibrate the con-
TM
compat ible. The soft-
verters offset, write to and read from the ADC’s in­ternal register, and acquire a conversion. The software is modularized and illustrates important subroutines, e .g. wr ite_ regi ste r and read _register. The softwa re described in the no te is included in the Appendix at the end of this do cument.
SPITM is a trademark of Motorola. MICROWIRE MPLAB
TM
is a trademark of National Semiconductor.
TM
is a trademark of Microchip.
AN88Rev2 5
Loading...
+ 9 hidden pages