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 control 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 microcontroller is available from Microchip (www.microchip.com).
Both should be consulted in conjunction with this application note.
HARDWARE OVERVIEW
Figure 1 shows the basic circuit diagram of the application. The application consists primarily of the AD7416
10-bit Temperature Sensor and the PIC16F84 microcontroller. 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 register of the device. Additional registers allow programming 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 microcontroller. 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 temperature 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 application. 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, alternatively, it could be used to output the data to a latch/
LED display, etc.
PIC16F84AD7416PC Interface
PORT A, 0SDA
PORT A, 1SCL
PORT B, 0SDATA
PORT B, 1SCLK
PORT B, 2SYNC
Figure 1. Basic Circuit Diagram
Table I. Pin Assignments
2
C
AN-561
SOFTWARE DESCRIPTION
This section will deal with the specific routines developed 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 structure but, rather, as a guide to users who wish to develop
their own routines. This section will discuss the initialization of the PIC processor, the configuration of the
ports, and the subroutines required to communicate
with the AD7416. As with many programming languages, labels will be used to identify particular registers 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
MOVLW0xFF
MOVWFPORTA;set port lines high
MOVLW0x4;set sclk,sdata low
MOVWFPORTB;& sync high (PC interface)
BSF STATUS,RP0 ;point to the OPTION
;and TRIS registers
MOVLW0x00
MOVWFTRISA;make portA output
MOVLW2;setup portB
MOVWFTRISB;sclk=i/p,
;sync & sdata=o/p
MOVLWb’10000111';set the timer
;prescaler to 256
MOVWFOPTREG;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 generated 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
MOVLW0x2;Bring SDA Low
MOVWFPORTA
MOVLW0x0;SDA and SCL both low
MOVWFPORTA;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
MOVLW2;bring SCL high
MOVWFPORTA
MOVLW3;SCL and SDA both high
MOVWFPORTA;Stop Frame generated
RETURN;exit subroutine
With routines for generating Start and Stop Frames generated, 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 diagram 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 operation, 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
MOVLW0;scl, sda low
MOVWFPORTA
MOVLW1;sda high, scl low
MOVWFPORTA
MOVLW3;scl, sda high
MOVWFPORTA
NOP;let bit settle
MOVLW1;scl low, sda high
MOVWFPORTA
MOVLW0
MOVWFPORTA;both low
BITDONE
DECFSZ COUNT,1;decrease count by 1 and
;repeat if not zero
GOTOLOOP
CHECKACK
;look for ACK pulse
;Change sda line to input for ACK
BSF STATUS,RP0;point to TRIS registers
MOVLW1
MOVWFTRISA;make sda line an input
BCFSTATUS,RP0;point to PORT register
MOVLW2;scl high
MOVWFPORTA
NOP;let bit settle
MOVFPORTA,W;read the port
BTFSSPORTA,0;check status of ACK bit and
;store in ACKSTATUS register
GOTOACK0
BSFACKSTATUS,0;positive ACK received
GOTONEXT
Figure 4. SENDBYTE Flowchart
Listing 4. The SENDBYTE Routine
SENDBYTE
;subroutine to send 8 bits to
;AD7416
MOVLW8;holds the number of bits to
;transmit
MOVWFCOUNT;store in memory location
LOOP
RLFTXVAL;rotate the msb into carry bit
BCBITHIGH;branch if bit is high?
BITLOW
;send logic 0
MOVLW0;scl, sda low
MOVWFPORTA
MOVLW2;scl high, sda low
MOVWFPORTA
NOP;let bit settle
MOVLW0;scl,sda low
MOVWFPORTA
GOTOBITDONE;this bit has been
;transmitted
ACK0
BCFACKSTATUS,0;negative ACK received
NEXT
MOVLW0;scl low
MOVWFPORTA
;Change port back to output
MOVLW0;preload output register with 0’s
MOVWFPORTA
BSF STATUS,RP0
MOVLW0
MOVWFTRISA;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 temperature. 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
You need points to download manuals.
1 point = 1 manual.
You can buy points or you can get point for every manual you upload.