Analog Devices AN-561 Application Notes

AN-561
AD7416
PIC16F84
SDA
SCL
RA0
RA1
RB2
RB1
RB0
*PC INTERFACE
*OPTIONAL
a
APPLICATION NOTE
One Technology Way • P.O. Box 9106 • Norwood, MA 02062-9106 • 781/329-4700 • World Wide Web Site: http://www.analog.com
AD7416 Connects to the PIC16F84 via Emulated I2C® Interface
by Ken Kavanagh
INTRODUCTION
This application note describes the requirements to con­trol the AD7416 via a PIC microcontroller by emulating
2
an I
C bus interface. It will provide code examples and descriptions of both hardware and software. Although this application uses the PIC16F84, it is possible to modify the code to use other microcontrollers from Microchip and other suppliers. Additional information about the AD7416 is available from Analog Devices (www.analog.com) and information about the PIC micro­controller is available from Microchip (www.microchip.com). Both should be consulted in conjunction with this appli­cation note.
HARDWARE OVERVIEW
Figure 1 shows the basic circuit diagram of the applica­tion. The application consists primarily of the AD7416 10-bit Temperature Sensor and the PIC16F84 micro­controller. An optional PC interface is also shown which allows the temperature reading to be uploaded to a PC for logging or monitoring.
AD7416 General Description
The AD7416 is a 10-bit temperature monitoring device that is supplied in an 8-lead SOIC or µSOIC package. The temperature can be calculated by reading the data regis­ter of the device. Additional registers allow program­ming of high and low temperature limits and an over-temperature indicator (OTI), which becomes active when a programmed limit is exceeded, is provided. The AD7416 uses an I a selectable serial bus address that allows up to eight devices to be connected to a single bus.
PIC16F84 General Description
The PIC16F84 is an 18-lead CMOS EEPROM micro­controller. It contains 1K × 14 on-chip program memory locations, 36 × 8 general-purpose registers and a 64 × 8 EEPROM data memory. The part contains 13 I/O pins, each of which is capable of sinking 25 mA and sourcing 20 mA. The PIC16F84 also contains an 8-bit timer/ counter with an 8-bit programmable prescaler.
I2C is a registered trademark of Philips Corporation.
REV. 0
2
C-compatible serial interface and has
SOFTWARE OVERVIEW
The software routines developed throughout the course of this application can be used to program the on-board registers of the AD7416, as well as read data from the temperature/data register. The communications routines concerned with the AD7416 will emulate an I serial interface on a parallel I/O port such as that of the PIC16F84. These routines could be modified to work with any microcontroller with similar capabilities. The hardware/software application takes the form of a tem­perature monitor that will take a temperature reading at regular intervals and will upload the results to a PC through the PC’s printer port (if connected).
Hardware Pin Associations
Table I shows the pin assignments used in this applica­tion. For simplicity, Port A of the PIC16F84 is reserved for communication with the AD7416. Port B has three lines dedicated to transferring the information from the application board to the PC (if required) or, alter­natively, it could be used to output the data to a latch/ LED display, etc.
PIC16F84 AD7416 PC Interface
PORT A, 0 SDA PORT A, 1 SCL PORT B, 0 SDATA PORT B, 1 SCLK PORT B, 2 SYNC
Figure 1. Basic Circuit Diagram
Table I. Pin Assignments
2
C
AN-561
SOFTWARE DESCRIPTION
This section will deal with the specific routines devel­oped for the PIC16F84 to communicate with the AD7416 by emulating the I
2
C serial bus. The routines discussed here are not intended as a definitive programming struc­ture but, rather, as a guide to users who wish to develop their own routines. This section will discuss the initial­ization of the PIC processor, the configuration of the ports, and the subroutines required to communicate with the AD7416. As with many programming lan­guages, labels will be used to identify particular regis­ters used in the PIC16F84. The use of labels makes programs easier to read and, if necessary, debug. Labels are represented in bold italic format (e.g.,
LABELS
). A list of
all labels used in the program is given in Appendix A.
PIC16F84 Initialization Routine
When power is first applied to the PIC16F84 a number of initialization routines need to be done. These are required to set the configuration (input or output) and state (high or low) of the I/O ports, as well as setting up the timer to generate interrupts at regular intervals, allowing the temperature to be read periodically. The listing for the initialization routine is given below.
Listing 1. Port Initialization
PORTINIT
MOVLW 0xFF MOVWF PORTA ;set port lines high MOVLW 0x4 ;set sclk,sdata low MOVWF PORTB ;& sync high (PC interface) BSF STATUS,RP0 ;point to the OPTION
;and TRIS registers MOVLW 0x00 MOVWF TRISA ;make portA output MOVLW 2 ;setup portB MOVWF TRISB ;sclk=i/p,
;sync & sdata=o/p MOVLW b’10000111' ;set the timer
;prescaler to 256 MOVWF OPTREG ;and copy to OPTREG BCF STATUS,RP0 ;point to port registers RETURN ;exit subroutine
Start and Stop Frame Routines
In the I2C protocol, all transfers of information must begin with a Start Frame and end with a Stop Frame. In the idle state both SDA and SCL lines will be high. A Start Frame is generated by bringing SDA low while SCL is high. This is shown in Figure 2. Similarly a Stop Frame is gen­erated by bringing the SDA line high while SCL is high. This is shown in Figure 3.
SCL
SDA
2
I
C PROTOCOL
PORT = 0x3 PORT = 0x2
PORT = 0x0
SCL
SDA
PIC16F84
EQUIVALENT
Figure 2. Start Frame
Listing 2. Generating Start Frame Signal
STARTFR
;generates a start signal ;assumes both lines are
;outputs and both are high MOVLW 0x2 ;Bring SDA Low MOVWF PORTA MOVLW 0x0 ;SDA and SCL both low MOVWF PORTA ;Start Frame Generated RETURN ;exit subroutine
SCL
SDA
2
C PROTOCOL
I
SCL
SDA
PORT = 0x0 PORT = 0x2 PORT = 0x3
PIC16F84
EQUIVALENT
Figure 3. Stop Frame
Listing 3. Generating a Stop Frame Signal
STOPFR
;generate a stop frame
;assmumes both lines are output
;and both are low
MOVLW 2 ;bring SCL high MOVWF PORTA MOVLW 3 ;SCL and SDA both high MOVWF PORTA ;Stop Frame generated RETURN ;exit subroutine
With routines for generating Start and Stop Frames gen­erated, it is now necessary to develop routines that will allow the user to write to the registers of the AD7416. Figure 7 in the AD7416 data sheet shows a timing dia­gram for such an operation. After a Start Frame signal has been issued the microcontroller must transmit seven bits containing the address of the device it wishes to communicate with. The eighth bit tells the AD7416 that the next operation will be a write or a read opera­tion, and the ninth bit allows the AD7416 to issue an acknowledgment. For simplicity, the seven address bits and the R/W bit will be combined into one byte and the status of the ACK pulse will be read but not acted upon. Figure 4 shows a flowchart of the write operation and Listing 4 shows the code.
–2–
REV. 0
AN-561
BYTE TO BE TRANSMITTED
LOCATED IN TXVAL REGISTER
LOAD COUNT WITH
NUMBER OF BITS
TO SEND (=8)
ROTATE TXVAL SO THAT
MSB MOVES INTO
CARRY BIT
IS THE
CARRY
BIT SET?
NO
BRING SDA AND
SCL LOW
BRING SCL HIGH
BRING SCL LOW
DECREASE COUNT
COUNT
REACHED
ZERO?
CHANGE SDA LINE TO INPUT
BRING SCL HIGH TEST SDA BIT
BRING SCL LOW
CHANGE SDA LINE TO OUTPUT
EXIT SUBROUTINE
HAS
YES
YES
BRING SCL LOW
AND SDA HIGH
BRING BOTH HIGH
BRING SCL LOW
NO
BITHIGH
;send logic 1 MOVLW 0 ;scl, sda low MOVWF PORTA MOVLW 1 ;sda high, scl low MOVWF PORTA MOVLW 3 ;scl, sda high MOVWF PORTA NOP ;let bit settle MOVLW 1 ;scl low, sda high MOVWF PORTA MOVLW 0 MOVWF PORTA ;both low
BITDONE
DECFSZ COUNT,1;decrease count by 1 and
;repeat if not zero GOTO LOOP
CHECKACK
;look for ACK pulse
;Change sda line to input for ACK BSF STATUS,RP0;point to TRIS registers MOVLW 1 MOVWF TRISA ;make sda line an input BCF STATUS,RP0;point to PORT register MOVLW 2 ;scl high MOVWF PORTA NOP ;let bit settle MOVF PORTA,W;read the port BTFSS PORTA,0;check status of ACK bit and
;store in ACKSTATUS register GOTO ACK0 BSF ACKSTATUS,0;positive ACK received GOTO NEXT
Figure 4. SENDBYTE Flowchart
Listing 4. The SENDBYTE Routine
SENDBYTE
;subroutine to send 8 bits to ;AD7416
MOVLW 8 ;holds the number of bits to
;transmit
MOVWF COUNT ;store in memory location
LOOP
RLF TXVAL ;rotate the msb into carry bit BC BITHIGH;branch if bit is high?
BITLOW
;send logic 0 MOVLW 0 ;scl, sda low MOVWF PORTA MOVLW 2 ;scl high, sda low MOVWF PORTA NOP ;let bit settle MOVLW 0 ;scl,sda low MOVWF PORTA GOTO BITDONE;this bit has been
;transmitted
ACK0
BCF ACKSTATUS,0;negative ACK received
NEXT
MOVLW 0 ;scl low MOVWF PORTA ;Change port back to output MOVLW 0 ;preload output register with 0’s MOVWF PORTA BSF STATUS,RP0 MOVLW 0 MOVWF TRISA ;make all port line outputs BCF STATUS,RP0 RETURN ;exit subroutine
The subroutine listed above needs to be called twice in order to set up the AD7416 for reading the temperature. The first instance tells a particular device that it is being addressed and the second instance is used to send the Address Register Pointer Byte. In the case of the AD7416 this means sending 0hex to the device to select the tem­perature. Of course, different registers can be selected by sending different address values. The user should consult the AD7416 data sheet for more information.
REV. 0
–3–
Loading...
+ 5 hidden pages