1 ECE 480 – Design Team 6 Integrating a Microcontroller with a I2C Peripheral Device Ishaan Sandhu Dr. John R. Deller Application Note Friday April 02, 2010 Executive Summary Any design which requires one or more devise needs a microcontroller. A microcontroller allows for communication between any number of devices and furthermore allows for the implementation of an easy to ready/easy to understand Graphical User Interface (GUI). Through using Inter-Integrated Circuit (I2C) bus specifications, one can communicate with any number of peripheral devices. Through the Universal Synchronous/Asynchronous Receiver/Transmitter (USART) interface, one can connect a microcontroller to a GUI for easy to read output.2 Table of Contents Introduction ................................................................................................................................................................3 Objective .....................................................................................................................................................................3 Resources ...................................................................................................................................................................3 Implementation and Results ......................................................................................................................................3 Parts and Setup ......................................................................................................................................................3 Figure 1: Microchip PIC18F4520 .........................................................................................................................4 Figure 2: Analog Devices AD7745 .......................................................................................................................4 Figure 3: Maxim MAX232 ...................................................................................................................................5 Table 1: Pin Connections ....................................................................................................................................5 Microcontroller Programming ................................................................................................................................5 Example Microcontroller Code ...........................................................................................................................6 User Interface Programming ............................................................................................................................... 13 Figure 4: Sample GUI ....................................................................................................................................... 14 Example Visual Basic Code .............................................................................................................................. 14 Conclusions .............................................................................................................................................................. 18 References ............................................................................................................................................................... 183 Introduction Interfacing peripheral devices with a microcontroller is an important part of any design. Microcontrollers need to exchange data with various devices and allow the user to interpret the output. One such mode of data exchange is Inter-Integrated Circuit (I2C) communication. Using both the Analog Devices AD7745 Capacitance to Digital Converter and the Microchip PIC 18F4520 as examples, this application note will demonstrate the method of communication, via I2C, as well as serial communication as a means to interpret capacitive sensor changes. Objective The objective of this application note is to demonstrate how to communicate between the Analog Devices AD7745 Capacitance to Digital Converter and the user via the Microchip PIC 18F4520 microcontroller. The concepts demonstrated in this application note can be extended to any I2C peripheral device and a compatible I2C microcontroller Resources For this application note, the following resources should be on hand: • Microchip MPLAB IDE v8.42 or later • Microchip ICD2 or equivalent debugger • Microchip PIC 18F4520 Data Sheet • Analog Devices AD7745 Data Sheet • RS232 Wiring Schematics • Microsoft Visual Studio 2008 or later The reader must also have a basic working knowledge of C programming and Visual Basic programming Implementation and Results Parts and Setup The first step in design is to wire all of the components together. The following parts are required: • Microchip PIC 18F4520 • Analog Devices AD7745 + Expansion Board • Breadboard • +5 (V) Power Supply Unit • 10 MHz Crystal Oscillator • MAX232 Chip • RS232 Interface • Microchip ICD2 • 1 Yellow LED4 • 1 Red LED • 1 Green LED • 3 220 Ω Resistors • 4 1 µF Capacitors The pin layout figures below help us see what needs to be connected. The primary connections are listed in Table 1: Pin Connections, and the remainder of the connections can be derived from the datasheets. Figure 1: Microchip PIC18F4520 Figure 2: Analog Devices AD77455 Figure 3: Maxim MAX232 VCC/VDD +5 (V) Source VSS/GND Ground PIC Pin 18 AD Pin 1 PIC Pin 23 AD Pin 16 PIC Pin 13 CLK PIC Pin 29 MAX Pin 10 PIC Pin 30 MAX Pin 9 AD Pin 3 Cap. Sensor (+) AD Pin 8 Cap. Sensor (-) Table 1: Pin Connections Microcontroller Programming The second step is programming the microcontroller. With the PIC18F4520, we must include a few reference files. The first is <p18cxxx.h>, which tells the compiler which family of microcontroller we are using. The second, <usart.h>, tells the compiler that we need to communicate across a serial port. The program must communicate with the User Interface (described below) in order for the user to see meaningful output. The third and most important include file is <i2c.h>. This file allows us to communicate with any number of I2C compatible peripheral devices. The example code below uses the AD7745 as the only peripheral device. The I2C interface has a few key routines:6 • OpenI2C() – Opens the bus between the master (microcontroller) and the
View Full Document