Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ABSTRACT “The Design and Implementation of the Universal Interface Controller” by Ren-Jie Huang The purpose of “The Design and Implementation of the Universal Interface Controller” is to investigate the design and implementation of the Universal Interface Controller (UIC). The UIC is a flexible interface to communicate sensor data from an I/O device to a host computer at a moderate price. Sensor interfacing devices on the commercial market are expensive and extremely application specific. Such devices as the iCube Digitizer can only convert analog sensor data into a MIDI data stream and cost more than the general hobbyist or student will pay for its limited potential. The flexibility of the UIC allows various applications, including education, art installations, and interactive computer applications, to be feasible using a single device. At the core of the UIC lies the Atmel AT89S8252 micro-controller. This microcontroller has a large amount of memory, 32 versatile I/O pins, a Universal Asynchronous Receiver/Transmitter (UART) and multiple timers/counters, which makes it a perfect candidate for implementing the UIC. Still in the earlier stages of design, the UIC only interfaces to an A/D converter. The low cost of the UIC is contributed to the development tools used in the design of the product. The SP89 programmer used to program the micro-controller is freely distributed and the Simple Device C Compiler (SDCC) is entirely open 1 source. Developing the UIC using free tools helps keep costs to a minimum. The UIC design is comprised of three main components: the micro-controller interface to the A/D converter, the serial interface, and the Application Programmer Interface (API). The micro-controller is interfaced to the I/O device via 5 data lines: I/O Clock, data in, data out, EOC, and nCS. When the A/D converter’s chip select (nCS) is held low, data in clocks 8 bits of data into the A/D converter and data out clocks in the conversion result from the A/D converter to the micro-controller. The serial interface utilizes the on-chip UART that consists of two data communication lines: transmit (TX) and receive (RX). Data is transferred between the host computer and the microcontroller via these lines. However, the two devices cannot be directly connected, as there is a voltage difference between them. A simple line driver/receiver is used to overcome this issue. Finally, the API allows the application programmer to communicate with the UIC through a simple set of interfaces. These functions are used to initialize the UIC, select the I/O device, request for data from the I/O device and read sensor data from the UIC. 2 TABLE OF CONTENT ABSTRACT...........................................................................................................1 LIST OF ILLUSTRATIONS ...................................................................................4 LIST OF ABBREVIATIONS ..................................................................................5 1.0 INTRODUCTION ............................................................................................6 2.0 MICRO-CONTROLLER ..................................................................................7 3.0 INPUT/OUTPUT DEVICES...........................................................................15 4.0 UIC DEVELOPMENT TOOLS ......................................................................20 4.1 C Compiler ................................................................................................20 4.2 Programmer ..............................................................................................22 5.0 UIC DESIGN.................................................................................................27 3.1 Micro-controller and A/D Converter Interface ............................................28 3.2 Serial Interfacing .......................................................................................32 3.3 Application Programmer Interface (API)....................................................34 6.0 CONCLUSION..............................................................................................37 LIST OF CITATIONS ..........................................................................................39 3 LIST OF ILLUSTRATIONS Tables Page Table 1. Port 1 Alternate Functions 11 Table 2. Port 3 Alternate Functions 12 Table 3. Input Register Format 18 Table 4. Programming Instruction Set 24 Figures Figure 1. Block Diagram of the 8051 Core 8 Figure 2. AT89S8252 PDIP Pin Configuration 9 Figure 3. Special Function Register Memory Map 13 Figure 4. TLC2543C Pin Configuration 16 Figure 5. Timing for 8 Clock Data Transfer Using nCS with MSB First 19 Figure 6. Programmer Interface to Micro-Controller 24 Figure 7. Simplified UIC Block Interface Diagram 27 Figure 8. UIC Interface to A/D Converter 28 Figure 9. UIC Interface with A/D Converter Code 30 Figure 10. Serial Interface 33 Figure 11. Serial Interface Code 33 Figure 12. Complete UIC Interface 36 4 LIST OF ABBREVIATIONS A/D .....................................................................................Analog to Digital API ......................................................... Application Programmer Interface EOC ............................................................................... End-of-Conversion I/O ............................................................................................ Input/Output LSB .............................................................................. Least Significant Bit MSB .............................................................................. Most Significant Bit RAM ..................................................................... Random Access Memory RX ................................................................................................... Receive SDCC................................................................. Simple Device C Compiler SFR..................................................................... Special Function Register SPI ..................................................................... Serial Peripheral Interface TX .................................................................................................. Transmit UART .................................... Universal Asyncronous Receiver/Transmitter UIC................................................................ Universal Interface Controller 5 1.0 INTRODUCTION The purpose of this report is to investigate the design and implementation of the Universal Interface Controller (UIC) for the EECE 496 project. My objectives during this project are to design and implement a flexible interface to communicate sensor data between an interface controller and a computer. The interface is controlled by an eight-bit micro-controller, the ATMEL AT89S8252. The application programmer utilizes a set of Application Programmer Interfaces (APIs) to easily setup devices and perform functionality common to all them. There are other such sensor interfacing devices in the commercial market such as the iCube Digitizer; however, they are generally expensive and application specific. On the other hand, the UIC is intended for low-cost use in multiple applications including education, art installations, interactive computer applications, computer interface and instrumentation research and interactive computer-music performance. In addition, a free open source C compiler has been selected to implement the firmware to make the device flexible, portable and less expensive. As discussed above, the focus of this report will be on design and implementation issues and will not cover installation of the development tools, RS232 standards, or basic programming techniques for interfacing with the device or similar devices on the market. This report divides into the following sections: micro-controller, input/output devices, development tools, and design. 6 2.0 MICRO-CONTROLLER The Universal Interface Controller (UIC) is designed using a MCS-51 compatible micro-controller, specifically the Atmel AT89S8252. The core of the MCS-51 family of micro-controllers is the original 8051. The key features of the core include: • 8-bit CPU optimized for control applications • Extensive Boolean processing (single bit logic) capabilities • 64K Program Memory address space • 64K Data Memory address space • 4K bytes of on-chip Program Memory • 128 bytes of on-chip data RAM • 32 bi-directional and individually addressable I/O lines • Two 16-bit timers/counters • Full duplex UART • 6 source/5 vector interrupt structure • On-chip clock oscillator [1] The architectural block diagram for the 8051 core is shown in Figure 1. 7 Figure 1. Block Diagram of the 8051 Core The selected PDIP configuration of the Atmel AT89S8252 device (Figure 2) is an improved micro-controller from the MCS-51 family. Like most devices, it can be found in various pin configurations; however, for the purposes of the initial design of the UIC the PDIP configuration is best suited as it enabled easy maneuverability between test boards. This device is a high performance CMOS 8-bit micro-controller with 10K bytes of in-system reprogrammable memory. The device contains 8K bytes of Flash memory with a life expectancy of 1,000 write/erase cycles, and can be easily programmed and reprogrammed through a Serial Peripheral Interface (SPI). The device also includes 2K bytes of EEPROM with a life expectancy of 100,000 write/erase cycles. In addition to the 10K bytes of non-volatile memory, the AT89S8252 houses 256 bytes of internal Random Access Memory (RAM). [2] This device 8 contains twice the memory of the original 8051 core, which is more than suitable for the UIC. The AT89S8252 provides all the standard features of the original 8051 but includes an additional timer with an advanced feature set, three more interrupt sources, power down and low power idle modes, interrupt recovery from power down and a programmable watchdog counter. The two power saving modes allow for better power management in certain applications. The power down mode maintains the RAM contents of the program while disabling all other chip functionality by stopping the oscillator until the next interrupt or hardware reset. On the other hand, the low power idle mode allows the RAM, interrupts, timers/counters, and serial port to continue running while the CPU is stopped. [2] Figure 2. AT89S8252 PDIP Pin Configuration 9 The 32 programmable input/output (I/O) pins of the AT89S8252 are extremely versatile since they can be reconfigured to perform various tasks. The 32 pins are divided into 4 ports each consisting of 8 pins: port 0 (P0), port1 (P1), port 2 (P2) and port3 (P3). Port 0 is an open drain bi-directional 8-bit I/O port. When 1s are written to the port 0 pins and held at that logic level by software, the port act as high impedance inputs. Otherwise the port acts as an output which can sink up to eight TTL inputs. This port can also be used as the low order address/data bus to access external program and data memory. Only in this mode does this port have internal pull-ups. In addition, during Flash programming and code verification, P0 can be used to receive and output code bytes respectively. Ports 1 to 3 are bi-directional 8-bit I/O ports with pull-ups. Similar to Port 0, when 1s are written to these ports pins and held at that logic level by software, internal pull-ups pull the pins high so they act as inputs. However, these ports have output buffers that can sink/source only four TTL inputs. As general input and output pins, ports 1 through 3 are the same. The difference and versatility lies in the alternate functions these pins may serve. The port 1 pins can be configured for use as the SPI (Table 1). The SPI allows synchronous data transfer between it and peripheral devices or between other AT89S8252 chips. The SPI permits full-duplex, 3-wire synchronous data transfer at a 1.5 MHz maximum bit frequency. It also allows least significant bit (LSB) or most significant bit (MSB) first data transfer. When dealing with the SPI, it is only used for programming the UIC 10 micro-controller. During Flash programming and verification Port 1 receives the low-order address bytes. This port can also be used as timer/counter 2. [2] Port Pin Alternate Functions P1.0 T2 (external count input to Timer/Counter 2), clock-out P1.1 T2EX (Timer/Counter 2 capture/reload trigger and direction control) P1.4 nSS (Slave port select input) P1.5 MOSI (Master data output, slave data input pin for SPI channel) P1.6 MISO (Master data input, slave data output pin for SPI channel) P1.7 SCK (Master clock output, slave clock input pin for SPI) TABLE 1. Port 1 Alternate Functions Port 2 can be used to transmit the high-order address byte to the external data memory when 16-bit addresses are required or during fetches from the external program memory. Port 3 can also be configured to provide numerous special functions such as Universal Asynchronous Receiver/Transmitter (UART) in and out, external interrupts and external data memory strobes (Table 2). [2] 11 Port Pin Alternate Functions P3.0 RXD (serial input port) P3.1 TXD (serial output port) P3.2 nINT0 (external interrupt 0) P3.3 nINT1 (external interrupt 1) P3.4 T0 (timer 0 external input) P3.5 T1 (timer 1 external input) P3.6 nWR (external data memory write strobe) P3.7 nRD (external data memory read strobe) TABLE 2. Port 3 Alternate Functions To customize the chip to achieve the desired configuration, an understanding of the on-chip memory area is required. The map of this memory area is referred to as the Special Function Register (SFR) space (Figure 3). Most of the SFR functions will not be discussed in this report, but detailed descriptions can be found in the AT89S8252 data sheet on the Atmel website. By writing 1s into the desired SFR in software, the device can be modified to enable the alternate functions discussed above. All of the registers are byte addressable; however, few are bit addressable to allow quicker modification of the registers. The registers located in the first column of Table 3 are all bit addressable. As it is clearly shown, most of the addresses are unoccupied. 12 These unoccupied addresses may not be implemented, and reading from these address will result in randomly generated data. The software should never write 1s to these locations, as doing so will result in indeterminate effects. [2] Figure 3. Special Function Register Memory Map The AT89S8252 operates in a voltage range of 4V to 6V and requires a frequency of 0Hz to 24Mhz. An inverting oscillator amplifier or an external clock is required to provide the internal clocking for this device.[2] There is no duty cycle requirement on the external clock; however, the minimum and 13 maximum voltage high and low time specifications must be met. For the UIC, the preferred operating condition is 5V with 11.059MHz. Running the microcontroller at 5V allows this device to be compatible with the optimal operating voltages of other devices connected to the UIC; this dismisses the need for additional power circuitry. The selected frequency of 11.059Mhz allows the device to communicate with the serial port of the host computer through the UART in standard baud rates such as 9600 Kbps and 19200 Kbps. 14 3.0 INPUT/OUTPUT DEVICES The power of the UIC lies in its support for various input and output devices. The UIC is used to communicate sensor data from a wide assortment of devices to the computer. In addition, these devices can be either analog or digital. Due to time constraints of the project, only one device was interfaced to the UIC during the design process. The most interesting of simple devices is used, the analog to digital (A/D) converter. Since the A/D converter can act as both an input and output device, it is the prime candidate for use in the design phase. The Texas Instrument TLC2543C A/D converter is used as the input/output device (Figure 4). The TLC2543C is a high-speed 12-bit, switched capacitor, successive approximation, A/D converter. This device has 11 analog input channels, on-chip system clock, 10-microsecond conversion time, linearity error of plus or minus 1 LSB, unipolar or bipolar output operation and an end-of-conversion (EOC) output. There are three control inputs to this device: chip select (nCS), input-output (I/O) clock, and the address input (DATA INPUT). [3] These inputs are used for communication with the serial port of a host processor or peripheral using a serial three state output. Data input is an 8-bit serial data input. The first four bits are used to select the desired analog input or self-test voltage to be converted. This serial data must be presented with the MSB first in the first four rising clock cycles. The last four bits are then clocked to configure the output data stream. Data out is a three state serial output for the A/D 15 conversion result. To select one of the 11 input channels, an on-chip 14 channel multiplexer is used. This multiplexer is also used for selecting one of three self-test voltages for testing the integrity of the device. Sampling and holding of the analog values are automatic and are performed by the sampleand-hold function. In addition, this device can be supplied a positive and negative reference voltage which together determines the maximum input voltage range. Normally the power supply voltage and ground are fed into the positive and negative reference voltages respectively. [3] Figure 4. TLC2543C Pin Configuration The chip is selected when a low is present at the nCS pin. Initially, the chip select should be high. This disables the I/O clock and the data input and leaves data out in a high impedance state. When nCS goes low, the chip is selected and the data out moves out of the high impedance state. The input data is an 8-bit data stream with a 4-bit channel address (D7-D4), a 2-bit data 16 length select (D3-D2), and output MSB or LSB first bit (D1) and a unipolar or bipolar output select bit (D0). Table 3 shows the input register format. During the transferring of the current input data in this I/O clock sequence, the previous conversion result is also being shifted from the output data register to the data out. The I/O clock receives the input sequence of 8, 12, or 16 clock cycles depending on the data length (D3-D2) selected by the input data register. Also, the I/O clock can be no higher than 4.1 MHz. Sampling occurs after the fourth falling edge of the I/O clock of the input sequence and is held till the last falling edge of the I/O clock sequence. After this point, the EOC is taken low and the conversion begins. When the conversion is finished, the EOC is taken high indicating that the output data is ready for transfer. If the chip selected is switched to high between conversions, the first output data bit occurs on the falling edge of nCS. After outputting the first data bit, the remaining bits are clocked out on the falling edge of each successive I/O clock. The timing diagram for an eight-clock cycle data transfer using the chip select between conversions with the MSB first is shown in Figure 5. [3] 17 Table 3. Input Register Format 18 Figure 5. Timing for 8 Clock Data Transfer Using nCS with MSB First 19 4.0 UIC DEVELOPMENT TOOLS The development of the UIC required tools for programming the microcontroller and compiling C code to run using the MCS-51 family instruction set. In order to keep the cost of the UIC to a minimum, an open source C compiler and a freely distributed programmer are used. These development tools are the Simple Device C Compiler (SDCC) and the SP89 Atmel AT89 micro-controller programmer. 4.1 C Compiler The first step to implementing the UIC is to have the right tools to design the device with as little turn around time as possible. In order to write the maintainable code for the UIC, a C compiler was chosen as the main development tool. The UIC could have been written entirely in assembly; however, learning the MCS-51 instruction set would have been no easy task and maintaining and modifying the code would also have been difficult especially when the project is passed on to the next developer. By developing the UIC in a C environment, the initial learning curve for programming the UIC is short and it can be guaranteed that the next developer can understand the code and make modifications in a short period of time. To keep the cost of this project low so that it is economically viable for the general hobbyist or student, the C compiler must meet the requirement 20 that the program is open source, thus free to the general public. The C compiler selected for the UIC project is the Small Device C Compiler version 2.3.1. The entire source code for the compiler is distributed under the General Public License, which guarantees that the SDCC is free for all its users. The SDCC is a retargettable, optimizing ANSI – C compiler that runs on both Windows and Linux based machines. By selecting a compiler that runs on both platforms, developers will not be restricted to a single platform. The SDCC targets both the Intel 8051 and Zilog Z80 based micro-controllers. SDCC uses ASXXXX, the assembler, and ASLINK, the linker. Also, SDCC has extensive micro-controller specific language extensions that allow for efficient use of the hardware. In addition, micro-controller specific optimizations like global sub expression elimination, loop optimizations, constant folding and propagation, dead code elimination and jump tables for switch statements are performed. The developer has control over which optimizations are performed; however, if it is not specified, then all optimizations will attempt to execute during compile time. The supported data types for use with the SDCC are short (16 bits, 2 byte), char (8 bits, 1 byte), int (16 bits, 2 bytes), long (32 bit, 4 bytes) and float (4 byte IEEE). Furthermore, to facilitate in the task of development with the SDCC, a source level debugger, SDCDB, is provided. Lastly, the compiler allows inline assembler code to be embedded anywhere in the program. This also allows routines developed in assembly to be called by the C code. [4] 21 Using the SDCC to develop programs for use with the micro-controller is extremely simple. The program is written like any other C program using a simple text editor. The only thing required is to include a header file of the micro-controller that is to be programmed. The header file contains a list of all the SFR and its proper memory locations for the device. To control the micro-controller, simply write 1s and 0s to the pins. An understanding of the specifics on how the micro-controller behaves is highly recommended as a large portion of the UIC code is used to setup the micro-controller’s functionality. A simple command line such as “sdcc myProgram.c” will compile the program myProgram and generate the necessary files. After a program has been written and the code has been compiled with no errors, the SDCC will create a number of files. The files of most interest will be the assembly file and Intel hex file. The assembly file with the extension “.asm” can be inspected to ensure the code is working in the proper manner. The Intel hex file with the extension “.ihx” will be the most important file as it is what will be copied into the program space of the micro-controller. Consult the SDCC manual for an in-depth discussion of the compiler and its usage. 4.2 Programmer In order to make use of the micro-controller, firmware must be loaded into the unit. To program the AT89S8252 micro-controller, a programmer is required. Instead of designing a programmer from scratch, a freely distributed 22 commercial programmer for the AT89 series of devices is used. This saves design time since building software that is already widely available in the field is no longer required. The selected programmer for the AT89S8252 is the SP89 programmer version 0.7. When choosing the programmer, the only criteria was to have it platform independent, which would ensure that anyone running a Windows or Linux based machine would have no problems programming the device. The SP89 is suitable for the AT89S8252, AT89LS8252, AT89S53 and AT89LS53. Since this programmer supports a wide variety of Atmel AT89 micro-controllers, the current programmer would still be suitable if a new AT89 micro-controller is desired for use with the UIC. The SP89 programmer also supports chip erasing, flash reading/writing, eeprom reading/writing and write locking.[5] The SP89 is a serial programmer which uses the parallel port so access to the parallel port is required if the program is being run on a Linux machine. The programmer uses the SPI of the micro-controller while reset (RST) is pulled to Vcc to write data to the memory space or to read data from the memory space. The serial programming interface utilizes the SCK, MOSI (input), and MISO (output) pins (Figure 6). When RST is high, the programming enable instruction must be executed before program or erase operations can execute. The serial programming algorithm provided in the AT89S8252 data sheet is then followed to program the device. An oscillator must be placed between XTAL1 and XTAL2 of the AT89S8252 for programming to work. The oscillator used 23 for the programmer has a frequency of 20Mhz. The programming instruction set follows a 3-byte protocol as shown in Table 4. Figure 6. Programmer Interface to Micro-Controller Table 4. Programming Instruction Set 24 The SP89 programs both the program memory and the eeprom memory. Programs can be written to the micro-controller from a file or by a single address. When programming the micro-controller from data in a file, the data must be in hexidecimal format with the extension “.hex”. The compiler used in this project generates the hex file; however, it is in the Intel hexidecimal format with the extension “.ihx”. A simple renaming of the file to contain the proper extension will rectify the problem. In addition, since the compiler only generates a single file for use in the program memory, writing to the eeprom is irrelevant. The following is a description of the command used for programming the UIC. For a complete description of this and all other commands, consult the instruction manual for the SP89 programmer. The command “sp89 –i52 –Cwpf myProgram.hex” will use the SP89 programmer to write the program myProgram into the program area of the micro-controller. The options used for this command are iNN, C, w, p and f. The first option, iNN, is always required for everything SP89 command and is used to initialize the programmer to interface with the correct device. Here the –i52 option informs the programmer that it is connected to an AT89S8252 device. Next, the C option tells the programmer to perform a checksum after writing to the device. The w option tells the programmer to perform a write operation on the device. The p option tells the programmer to access the program area of the device when performing a read or write operation. Finally, the f option tells the programmer that a file is to be read to obtain the data for writing to the 25 micro-controller. The f option is always followed by a filename ending in either the “.hex” or “.eep” extension. 26 5.0 UNIVERSAL INTERFACE CONTROLLER DESIGN The Universal Interface Controller is a flexible interface to communicate sensor data between an interface controller and a computer. This interface is controlled by a micro-controller, specifically the Atmel AT89S8252, which communicates sensor data from an A/D converter to a computer through a serial protocol or other such protocols. The firmware is programmed with the free open source C compiler, SDCC, for flexibility and portability. The firmware communicates with the I/O devices and the computer directly. The micro-controller works as an intermediate stage between the sensor data and the computer to handle all the interfacing functions required. An overly simplified block interface diagram of the UIC is shown in figure 7. The application programmer uses a set of simple Application Programming Interfaces (APIs) to communicate with the UIC. The set of APIs include functions to initialize the UIC, and read and request data from an I/O device. Figure 7. Simplified UIC Block Interface Diagram 27 5.1 Micro-controller and A/D Converter Interface The AT89S8252 micro-controller communicates with the I/O device through a set of pins that have been predetermined by the developer. In the initial design of the UIC, the micro-controller can interface with only an A/D converter, specifically the TLC2543C. The interface between the microcontroller and the A/D converter is shown in figure 8. The pins that interface to the A/D converter are merely general-purpose inputs and outputs. By keeping the interface pins general, the code can be easily ported to another micro-controller. For instance, if the I/O Clock pin of the A/D was controlled by the AT89S8252 specific programmable clock out, compatibility with porting the program over to another micro-controller would be limited by the existence of a similar programmable clock out pin on the new microcontroller. In addition, by using general-purpose inputs and outputs, the application programmer can fully customize the pin configuration of the UIC. Figure 8. UIC Interface to A/D Converter 28 The P2.0 output pin generates the I/O Clock required by the A/D converter. The P2.1 output pin produces an 8-bit stream of data with the MSB first that is clocked into the A/D converter on each rising edge of the I/O Clock. The clocking of this 8-bit stream of data is considered one I/O clock sequence. This pin provides the A/D converter with the data in information that selects the analog input, and describes the data out structure the micro-controller expects to receive on the next I/O clock sequence. The P2.2 input pin receives the 8-bit stream of converted data from the data out pin of the A/D converter. The P2.1 pin sets the format of this 8-bit stream from the previous I/O clock sequence. Even though the A/D converter is a 12-bit A/D converter, it is run in 8-bit mode to facilitate fast transfer of the data since the microcontroller is only an 8-bit device. Here the 4 LSBs are not discarded and not sent to the micro-controller. The P2.3 input pin receives the EOC signal from the A/D converter. When the EOC is low, the micro-controller knows to set the nCS (P2.4) high so that the A/D can complete the conversion of the analog data before the next I/O clock sequence begins. However, the EOC does not serve much of a purpose as the conversion period of this A/D converter is much less than the period of the I/O clock. Therefore, conversion is always completed before the next I/O clock so this input pin should be omitted when interfacing to the TLC2543C to save an extra pin for use with another I/O device. It is left in during implementation because other A/D converters may require a longer conversion period. The P2.4 output pin 29 provides the A/D converter with the chip select signal. The chip is selected when it receives a low. The snippet of code presented in Figure 9 controls the flow of data between the A/D converter and the UIC as discussed above. Figure 9. UIC Interface with A/D Converter Code It can be seen that this piece of code is an interrupt service routine and that the UIC is an interrupt driven device. It services the timer0 interrupt that is 30 hard coded to occur every 256-clock cycles. The purpose of this interrupt service routine is to generate the I/O Clock for the A/D converter, write data to it, and read data from it. The first “if” statement is used to see if the EOC is high. When EOC is high, nCS is low otherwise nCS is high. The additional code within the “if” statement is to ensure that when the chip select makes a transition from high to low, the I/O Clock is low. This is to ensure that no data is clocked into the A/D converter before the debounce time is met. The count variable is to keep track of the current clock cycle in the current I/O clock sequence. After eight clock cycles, the data to be transmitted (myData) to the data in pin of the A/D converter is cleared and set to the new 8-bit value and the data received (newData) from the data out pin of the A/D converter is stored into “adcData” which can now be sent to the computer if requested. The second “if” statement writes to myData to the data in pin of the A/D converter just before the rising edge of the I/O Clock to ensure that the data is valid. The data is shifted out with the MSB first. After the second “if” statement the I/O Clock is inverted. The final “if” statement reads data from the data out pin to the newData variable on the rising edge of the I/O Clock. The format of the output data has been coded to be unipolar and MSB first by the myData. 31 5.2 Serial Interfacing The AT89S8252 micro-controller communicates with the computer through a serial interface. The micro-controller uses the built in UART functionality to send data to and receive data from the computer. The built in UART makes it extremely simple set up a serial interface. Only two lines are required for serial communication: TX and RX. The pins on the serial port should be set up in a Null modem configuration. The only special requirement for enabling the UART is to have an oscillator with a frequency that can generate the specific baud rates required for transmission and retrieval of data. The bestsuited oscillator frequency for this purpose is 11.059 MHz as it allows for the most baud rates. The simplest way to generate the baud rate is to use timer1 in the 8-bit auto reload mode. This will ensure that the timing is correct for data manipulation. The UART should also be set to accept and transmit data using the 8-bit UART protocol (To set up the timer and UART, refer to the AT89S8252 data sheet). When using the 8-bit UART protocol, 10 bits are actually sent for every 8 bits of data. The first bit sent is always the start bit, which is a transition from high to low. The last bit sent is always the stop bit, which is a transition from low to high. This implies that when the line is idle the voltage is held high. This conforms to the RS232 standard for serial data transmission. The only issue with this design is that serial port uses higher voltage levels compared to the voltage levels of the micro-controller so the two cannot be directly connected together. This problem is easy fixed by the 32 use of a line driver/receiver to convert between the voltages for serial communication. A Maxim 232 chip is used for this task. The serial interface between the micro-controller and the computer is shown in figure 10. Figure 10. Serial Interface Figure 11. Serial Interface Code The serial interface of the micro-controller runs in an interrupt driven manner. When the micro-controller finishes receiving eight bits of data from the serial port, the serial receive interrupt (RI) is triggered and the interrupt service routine is called. The received byte is stored in a serial buffer, SBUF. When the serial buffer, SBUF, is written to by software, the micro-controller sends 33 eight bits of data to the computer. After sending the last bit of data, the serial transmit interrupt (TI) is triggered and the interrupt service routine is also called. Because the same interrupt service routine is triggered by two interrupts, the firmware must check to see which interrupt, either TI or RI, triggered the routine to know what to do next. This is why the serial interrupts must be cleared by software and not by hardware as it is for the case of all other interrupts in the AT89S8252. To clarify the use of SBUF, writing to SBUF accesses a transmit register and reading from SBUF accesses a separate receive register. The snippet of code in figure 11 shows the interrupt service routine for the serial interface. When TI triggers the routine, the global flag “sending” is set to false to indicate that it is safe to send the next byte of data. When RI triggers the routine, the received contents are stored into a “serialData” variable and the global flag “dataRX” is set to true to indicate that new data has been received from the computer. The received data can then be used in the main program. 5.3 Application Programmer Interface (API) The application programmer can make use of the UIC through a simple API designed to communicate with the UIC. The simple API only allows the application programmer to interface with the UIC through the use of a serial port. The functions provided by the API include the following: uicInitialize, uicClose, uicADCOn, uicADCOff, uicRequestData and uicRead. The 34 uicInitialize initializes the UIC by setting up a connection to the UIC through the selected serial port and with the selected baud rate. The uicClose releases all of the memory used by the UIC object. The uicADCOn starts the A/D converter data acquisition process for the specified analog input. The uicADCOff stops the A/D converter data acquisition process so another process can be started. The uicRequestData sends a request to the UIC to obtain data from the I/O device. This is always followed by a uicRead to read the data from the serial port. Upon start up of the UIC, it waits to receive data from the computer for configuration, such as which analog input to obtain data from. When the host application uses the uicADCOn function, the UIC configures the 8-bit data in stream to obtain data from the specified analog input. The acquisition process begins and the UIC starts to acquire data from the A/D converter for the specified analog input. When the UIC receives a request for data from the computer, the data from the A/D converter is sent to the computer through the serial port. Figure 12 shows the complete interface between the I/O device, UIC and computer. 35 Figure 12. Complete UIC Interface 36 6.0 CONCLUSION This report investigated the design and implementation of the Universal Interface Controller. The Universal Interface Controller is a flexible sensorinterfacing device for use in multiple applications such as education, interactive computer applications, and interactive computer-music performance at a low cost. Most commercial products are application specific and expensive to purchase. The low cost of the UIC is contributed to the development tools used in the design of the product. Both the SP89 programmer and the SDCC are freely distributed under the General Public License. The Atmel AT89S8252 micro-controller is the core of the UIC and serves as an intermediate stage between the I/O devices and the host computer. This micro-controller has a large amount of memory, 32 versatile I/O pins, a Universal Asynchronous Receiver/Transmitter and multiple timers/counters, make it a perfect candidate for realizing the UIC. Since the UIC is still in the early stages of design, it only interfaces to an A/D converter. The design of the UIC consists of three main components: the microcontroller and A/D converter interface, the serial interface, and the API interface. The micro-controller is interfaced to the A/D converter using 5 data lines: I/O Clock, data in, data out, EOC, and nCS. When the chip select is low, the data in is clocked into the A/D converter and the conversion result from the previous I/O clock sequence is clocked into the micro-controller. The 37 serial interface is comprised of only two communication data lines: TX and RX. The transmit (TX) and receive (RX) lines are used to transfer data between the UIC and the host computer. The hardware in the microcontroller facilitated in the simple design of the serial interface, specifically the presence of an on-chip UART controller. But, due to the difference in voltage levels, a line driver/receiver was required to interface the micro-controller to the host computer. Finally, the API interface allows the application programmer to control the UIC through a simple set of functions. The functions are used for initializing of the UIC, selecting the I/O device, requesting data from the I/O device and reading sensor data from the UIC. 38 LIST OF CITATIONS [1] “MCS-51 Microcontroller Family User’s Manual,” <http://developer.intel.com/design/mcs51/manuals/272383.htm> (1 Mar. 2002) [2] “Atmel 8-bit Microcontroller with 8K Bytes Flash At89S8252,” <http://www.atmel.com/atmel/acrobat/doc0401.pdf> (12 Feb. 2002) [3] “TLC2543C, TLC2543I, TLC2543M 12-bit Analog-to-Digital Converters with Serial Control and 11 Analog Inputs,” <http://www-s.ti.com/sc/ds/ tlc2543.pdf> (1 Mar. 2002) [4] “Small Device C Compiler,” <http://sdcc.sourceforge.net/> (12 Feb. 2002) [5] “SP89 – a programmer for AT89 microcontrollers,” <http://www.xs4all.nl/ ~sbolt/e-ser89.html> (12 Feb. 2002) 39