USB BASED DATA COMMINICATION SYSTEM
Nishith R. Naik
B.E.,Viswesvaraiah Technological University,India, 2007
PROJECT
Submitted in partial satisfaction of the requirements for the degree of
MASTER OF SCIENCE in
ELECTRICAL AND ELECTRONIC ENGINEERING at
CALIFORNIA STATE UNIVERSITY, SACRAMENTO
SUMMER
2010
USB BASED DATA COMMUNICATION SYSTEM
A Project by
Nishith R.Naik
Approved by:
__________________________________________________, Committee Chair
Jing Pang, Ph.D.
___________________________________________________ , Second Reader
John Balachandra, Ph.D.
__________________________
Date ii
Student: Nishith R. Naik
I certify that this student has met the requirements for format contained in the University format manual, and that this project is suitable for shelving in the Library and credit is to be awarded for the project.
_________________________ , Department Chair ________________
Preetham Kumar, Ph.D. Date
Department of Electrical and Electronic Engineering iii
Abstract of
USB BASED DATA COMMUNICATION SYSTEM by
Nishith R. Naik
Implementation of this USB (Universal Serial Bus) based data processing system is done using PIC 18f2550 microcontroller, LCD 16*2 display, USB connector, and
ATMEL AT24C04 Serial EEPROM for storage of data.
The main objective of this project is to establish data communication system between computer and PIC18F2550 microcontroller. In this project Virtual COM port is used through which data is sent and received.
Virtual COM port causes the USB device to appear as an additional COM port available to the PC. Application software can access the USB device in the same way as it would access a standard COM port. Since writing and implementation of USB driver is out of scope of this project, USB driver available to us which is compatible with most of the operating system in the market was used.
In order to send data from computer to microcontroller, Visual Basic script was developed. Visual Basic script has different options such as reading from memory or writing into memory or display stored data on LCD. iv
Since the whole design is too large and complicated for a single student, some simplifications were used in this project.This report will discuss how USB interface between computer and PIC18F2550 is established.
_________________________________________________ , Committee Chair
Jing Pang, Ph.D.
_________________________
Date v
ACKNOWLEDGMENTS
With sincere respect and gratitude, I would like to thank everybody who has helped me in this project .Specially I would like to thank my advisor Dr. Jing pang, without her it was not possible to complete this project. She helped me in technical matter as well as mental encouragement. I reckon her extensive support and guidance throughout the project and even in writing the report. It was a great learning experience to work with her. Also, she has taught me many practical aspects of research.
In addition, I would like to thank Professor Dr. John P. Balachandra for his precious guidance in writing this project report. I would also like to thank Dr. Preetham
B. Kumar, graduate coordinator and Dr.Suresh Vadhva, department chair of the Electrical and Electronic Engineering Department, for his valuable suggestions and support.
At same time I like to thank my project partner Hardik Shroff for making important contribution to this project.
Also, I am thankful for all faculty members of the Electrical and Electronic
Engineering Department for helping me finish my requirements for graduation at
California State University, Sacramento. vi
TABLE OF CONTENTS
Page vi Acknowledgments………………………………………………………………...
List of Tables……………………………………………………………………..
List of Figures……………………………………………………………………. ix x
Chapter
1. INTRODUCTION………………………………………………………………
1
1.1
USB………………………………………………………………………..
1
1.1.1 Functionality of host controller…………………………………………… 2
1.2
Types of Transfer……...…………………………………………………….. 3
1.3 Connector Properties……...……………………..…………………………... 4
1.4 USB Signals…………………………………………………………………... 5
1.5 USB Standard………………………………………………………..……… 5
2. HARDWARE SYSTEM OVERVIEW...……………..……………….………… 7
2.1 Microcontroller PIC18F2550………...…………………………….………… 7
2.1.1 Pin Layout of PIC18F2550…………………………………………………. 8
2.1.2 Pin Function Description…………………………………………………… 9
2.1.3 USB Peripherals for PIC18F2550………………………………………….. 13
2.1.4 Serial Interface Engine…………..…………………………………………. 14
2.2 Atmel EEPROM AT24C04……………………………………………….….. 14 vii
2.2.1 Pin Description for AT24C04………………………………………...…….. 16
2.2.2 Block Diagram for AT24C04……………………………………….……… 17
2.2.3 Transaction…………………………………………………………...…….. 18
2.2.4 I2C Protocol………………………………………………………………… 20
2.3 LCD………………………………………………..…………………………. 22
2.3.1 Parameters…………………………………….……………………………. 23
2.3.2 Pin Configurations……………………………………………………… 23
2.3.3 HD44780 U……………………………………………………………..
2.3.3.1 Features………………………………………………………………..
2.4 USB Cable…………………………………………………………………
3. BLOCK DIAGRAM AND OPERATION…………………………..……….
3.1 Block Diagram…………………………………………………………….
3.2 Power Supply……………………………………………………………...
3.3 PIC18F2550 based USB board ……………………………………………
24
24
27
30
26
27
27
3.4 Software Development ……………………………….…………………… 31
4. RESULTS……………………………………………………………………...
34
34 4.1 Testing of USB LCD program…………………………………………….
5. CONCLUSION………………………………………………………………...
38
38 5.1 Future Improvements……………………………………………………...
Appendix…………………………………………………………………………
References………………………………………………………………………..
40
50 viii
LIST OF TABLES
Page
1. PIC18F2550 Pin out description (MCLR,OSC1,OSC2) ………………..………… 9
2. PIC18F2550 pin out description (Port A)...…………….………………...………….. 10
3. PIC18F2550 Pin out description (Port B)………………………………………...…...11
4. PIC18F2550 pin out description (Port C, VDD, Vusb, Vss)………………….......…..12
5. Pin Description for AT24C04……………………………………….......…....……….16
6. Write Protection for Devices…………………………………………….……...…….17
7. Parameters for JHD162A……………………………………………….…….………23
8. Pin Configuration for JHD162…………………………………………….………….23 ix
LIST OF FIGURES
Page
1. USB Endpoints…………………………………………………...………….. 2
2. USB Connector………………………………………………………………. 4
3. Pin Layout for 18F2550……………………………………………………… 8
4. USB peripherals and options for 18F2550……………………………….….. 13
5. Block diagram for AT24C04………………………………………………… 17
6. Bus Timing……………………………………………………...…………… 18
7. Start and Stop Definition …………………………………………………….. 19
8. Output Acknowledgements …………………….…………………………... 19
9. USB A to B cable used for Connection……………………………………. 26
10. Block Diagram for USB based data processing…………………………… 29
11. PIC18F2550 based USB board …………………………………………….. 30
12. Test case for USB data write ……………………………………………….. 34
13. Display of USB data on LCD ……………………………………………… 35
14. Test case for USB data read………………………………………………… 36
15. Display of USB data read on LCD…………………………………………. 37 x
1
Chapter 1
INTRODUCTION
The focus of this project is to establish USB communication between Computer and Microcontroller. This chapter gives an introduction of USB.
1.1 USB
Universal Serial Bus ( USB ) is a specification to establish communication between devices and a host controller. It was developed and invented by Ajay bhatt while working for Intel. USB is intended for replacing many varieties of serial and parallel ports. USB is used to connect computer peripherals such as mice, keyboards, digital cameras, printers, personal media players, flash drives, and external hard drives. For many of those devices,
USB has become the standard connection method. USB was designed for personal computers, but it has become commonplace on other devices such as smart phone , PDAs and video game consoles, and as a power cord between a device and an AC adapter plugged into a wall plug for charging.
An USB system has an asymmetric design, consisting of a host, a multitude of downstream USB ports, and multiple peripheral devices connected in a tiered-star topology. Additional USB hubs may be included in the tiers, allowing branching into a tree structure with up to five tier levels. A USB host may have multiple host controllers and each host controller may provide one or more USB ports. Up to 127 devices, including hub devices if present may be connected to a single host controller.
2
USB devices are linked in series through hubs. There always exists one hub known as the root hub, which is built into the host controller. So-called sharing hubs, which allow multiple computers to access the same peripheral device(s), also exist and work by switching access between PCs, either automatically or manually. Sharing hubs are popular in small-office environments. In network terms, they converge rather than diverge.
1.1.1
Functionality of host controller
The below figure explain functionality of host controller.
Host Controller
Logical pipes
Device
Figure 1 USB Endpoints [1]
Device endpoints
USB host controller is the hardware inside the computer that provides hi-speed USB functionality to the ports. The main function of host controller is to direct traffic flow to devices, so no USB device can transfer any data on the bus without an explicit request
3 from the host controller. For communication to take place, host controller polls the bus for traffic in a round-robin fashion. The bandwidth of the interface is set by the slowest device connected. All USB compliant devices have endpoints. Endpoints can be described as source or sink of data. USB bus is host centric so endpoints are at the end of communication channel. Endpoints provide interface between hardware of the device and firmware running on device.
1.2 Types of Transfer
Isochronous transfers : In this mode data is transmitted at some guaranteed data rate. It usually transmits data at maximum possible rate but with possible data loss. (e.g. real time audio or video.)
Interrupt transfers : This mode is used when guaranteed response is required. This mode is used in this project microcontroller responds whenever there is data present from computer. In this mode host polls for USB connection and data present. In this project polling is done using USBPoll function.
Bulk transfers : This mode is used when large amount of data is to be transferred at fast rate. It uses all available bandwidth in order to transmit data at fast rate, but with no guarantees on bandwidth or latency (e.g. file transfers).
Control transfers : typically used for short, simple commands to the device, and a status response, used, for example, by the bus control pipe number 0.
4
1.3 Connector Properties
Figure 2 USB Connector [3]
It is difficult to attach a USB connector incorrectly. Most connectors cannot be plugged in upside down, and it is clear from the appearance and kinesthetic sensation of making a connection when the plug and socket are correctly mated. It will take reasonable force to remove or insert USB connector. We have some disadvantage of easily and unintentionally breaking connections that one has intended to be permanent in case of cable accident.
Type-A USB connectors on host devices supply power and type B connectors on target devices receive power. This prevents users from accidentally connecting two USB power supplies to each other, which could lead to dangerously high currents, circuit failures, or even fire. USB does not support cyclical networks and the standard connectors from incompatible USB devices are themselves incompatible. Unlike other communications systems (e.g. RJ-45 cabling) gender changers make little sense with USB and are almost never used.
5
1.4
USB Signals
USB uses 4 shielded wires: It uses two for power VDD & GND, and D+ and D- are used for differential data signals Non Return to Zero Invert(NRZI) encoding scheme used to send data with a sync field to synchronies the host and receiver clocks. In USB data cable D+ and D- signals are transmitted on a twisted pair without any need termination. It combats the effects of electromagnetic noise on longer lines by having half-duplex differential signaling.
1.5
USB Standard
There are total of three USB versions in the market and USB 3.0 is latest and fastest one with data transmission rate of 3.2Gbits/ sec. This topic will discuss all three version and their specifications in brief.
USB 1.0
It was released in January 1996 with specified data rates of 1.5 Mbits/s for lowbandwidth application and 12 Mbits/s for full bandwidth application. There are limitations on timing and power consumption so it does not support extension cables.
USB 2.0
It was released in April 2000. It made a massive improvement in raw bandwidth of
480Mbits/s was achieved. Approximate data transmission rate is 60 Mbytes/s. Its power specification suggests 2.5W of power and 5V voltage supply is provided.USB 2.0 specifications were used in this project.
6
USB 3.0
It added new major feature called “Super Speed”. It provides a fourth transfer mode at
5Gbits/s. Power specifications suggest 4.5W and 5V of power supply is provided. The
USB 3.0 cable consists of two wires for power and ground, two wires for non-
SuperSpeed data, and four wires for SuperSpeed data, and a shield that was not required in previous specifications. For all other detail reader can refer to data sheet.
7
Chapter 2
HARDWARE SYSTEM OVERVIEW
This project used PIC18F2550 microcontroller for data communication.16*2 LCD was used for data display. Atmel AT24C04 serial EEPROM was used to store data externally.
2.1 Microcontroller PIC18F2550
There are several microcontroller available in market. In this project, Microchip’s
PIC18F2550 was used because it has following nice features.
1.
It comes in 28 pin PDIP SOC packaging which makes it easier to use.
2.
It uses nano watt technology.
3.
The device uses In Circuit Serial Programming (ICSP) for programming so it can be programmed in the circuit.
4.
It is having a lot of program memory (16K) and RAM (2K). So it can be used in more demanding application such as USB communication system.
5.
Especially, It supports USB2.0 full speed and low speed operation. This feature was of extreme importance in this project as it uses D+ and D- pins directly to transfer data through USB port of host computer. It is very useful for embedded control and some monitoring applications which require repetitive connection with a Personal Computer via USB for data communication.
8
6.
It can also be programmed using C language. In this project BASIC language is used with Proton compiler. It takes care of low level specifications as it works at abstract level which made implementation of this project in short amount of time.
2.1.1 Pin Layout of PIC18F2550
The figure below shows the pin layout of 28 pin PDIP PIC 18F2550.
Figure 3 Pin Layout of 18F2550 [2]
9
2.1.2 Pin Function Description
The tables in the next few pages will explain each pin and its function. Pins are grouped together in order to make it simple to understand for reader. For further detail refer to datasheet.
Table 1 PIC18F2550 Pin out description (MCLR,OSC1,OSC2) [2]
10
Table 2 PIC18F2550 pin out description (Port A) [2]
11
Table 3 PIC18F2550 Pin out description (Port B) [2]
12
Table 4 PIC18F2550 pin out description(Port C, VDD, Vusb, Vss)[2]
2.1.3 USB Peripherals for PIC18F2550
13
Figure 4 USB peripheral and options for 18F2550 [2]
The above figure shows block diagram of USB peripheral embedded in 18F2550.It is this part which made PIC18F2550 very useful for this project. As shown in diagram it has both internal and external USB transceiver available. In this project internal transceiver is used as it is really simple. It is also having internal pull up resistor that we can make use of. If internal resistor connected with D+ and D- pin is used then it eliminates the need of external pull up resistor thus external hardware is saved. The on-chip USB pull-up resistors are controlled by the UPUEN bit (UCFG<4>). They can only be selected when the on-chip transceiver is enabled. Vusb supplies separate power source for USB
14 operation which is 3.3V. Vusb is connected to internal voltage regulator so it maintains
3.3V of voltage.
2.1.4 Serial Interface Engine
ThePIC18FX455/X550 device family contains a full-speed and low-speed compatible
USB Serial Interface Engine (SIE) that allows fast communication between any USB host and the PIC® microcontroller. The SIE can be interfaced directly to the USB, utilizing the internal transceiver, or it can be connected through an external transceiver. An internal 3.3V regulator is also available to power the internal transceiver in 5V applications. Some special hardware features have been included to improve performance. Dual port memory in the device’s data memory space (USB RAM) has been supplied to share direct memory access between the microcontroller core and the
SIE. Buffer descriptors are also provided, allowing users to freely program endpoint memory usage within the USB RAM space. A Streaming Parallel Port has been provided to support the uninterrupted transfer of large volumes of data, such as isochronous data, to external memory buffers.
2.2 ATMEL EEPROM (AT24C04)
There are many EEPROM available in the market but AT24C04 is chosen because it has following nice features, which makes it very desirable for this project.
15
1.
Low-voltage and Standard-voltage Operation
It works with both low range 1.8V-5.5V and also standard range of 2.7 V- 5.5V.In this project standard voltage range is implemented.
2.
Two-wire Serial Interface
It uses two pins for interface with other devices. This feature is very helpful as
I2C protocol is used to serially transmit data from microcontroller to EEPROM.
By using SDA and SCL pin I2C protocol transfers data from microcontroller to memory.
3.
Schmitt Trigger, Filtered Inputs for Noise Suppression
This features ensures that data written to memory do not get corrupted during read or write operation.
4.
Bidirectional Data Transfer Protocol
5.
100 kHz (1.8V) and 400 kHz (2.7V, 5V) Compatibility
6.
Write Protect Pin for Hardware Data Protection
If WP pin connected to Vcc then it provides hardware data protection.
7.
Partial Page Writes Allowed
8.
High-reliability
This feature is very critical when selecting external memory device.AT24C04 allows 1 Million Write Cycles. Also it retains data for 100 years.
9.
Various packaging available so it can be used in different applications.
16
2.2.1 Pin Description for AT24C04
Table 5 Pin Description for AT24C04 [4]
SERIAL CLOCK (SCL): The SCL input is used to positive edge clock data into each
EEPROM device and negative edge clock data out of each device.
SERIAL DATA (SDA):For serial data transfer, this pin is bidirectional. This pin is opendrain driven and may be wire-ORed with any number of other open-drain or open collector devices.
DEVICE/PAGE ADDRESSES (A2, A1, A0): The A2, A1 and A0 pins are device address inputs that are hard wired for the AT24C01A and the AT24C02. As many as eight 1K/2K devices may be addressed on a single bus system.
The AT24C04 uses the A2 and A1 inputs for hard wire addressing and a total of four 4K devices may be addressed on a single bus system. The A0 pin is a no connect and can be connected to ground.
17
WRITE PROTECT (WP): To provide data protection with hardware, The
AT24C01A/02/04/08A/16A has a Write Protect pin. The Write Protect pin is connected to ground (GND), it allows normal operations. To enable write protection feature, Write protect pin should be connected to VCC. Write protection feature is enabled and operates as shown in Table below.
Table 6 Write Protect for Devices [4]
2.2.2 Block Diagram for AT24C04
Figure 5 Block diagram for AT24C04 [4]
18
2.2.3 Transaction
CLOCK and DATA TRANSITIONS: The SDA pin is normally pulled high with an external device. Data on the SDA pin may change only during SCL low time periods.
Data changes during SCL high periods will indicate a start or stop condition. As shown in diagram whenever SDA goes high during SCL high periods it is a STOP condition. When
SDA goes low during SCL high period it represents START condition.
Figure 6 Bus Timing [4]
START CONDITION: A high-to-low transition of SDA with SCL high is a start condition which must precede any other command.
STOP CONDITION: A low-to-high transition of SDA with SCL high is a stop condition.
After a read sequence, the stop command will place the EEPROM in a standby power mode.
19
Figure 7 Start and Stop definition [4]
ACKNOWLEDGE: All addresses and data words are serially transmitted to and from the
EEPROM in 8-bit words. The EEPROM sends a zero to acknowledge that it has received each word. This happens during the ninth clock cycle.
Figure 8 Output Acknowledgements [4]
20
STANDBY MODE: The AT24C01A/02/04/08A/16A features a low-power standby mode which is activated in two ways:
1. When it is power-up
2. When it receive STOP bit and after any internal operation is done.
MEMORY RESET: It is done in following circumstances.
1.
After an interruption in protocol
2.
Power loss or system reset
In order to reset any 2-wire part these steps should be followed.
1. Clock up to 9 cycles.
2. Look for SDA high in each cycle while SCL is high.
3. Create a start condition.
2.2.4 I2C PROTOCOL: In our design we have used I2C protocol for read/write operation from AT24C04 serial EEPROM. So we will first look in to some basic information about it.
I2C (Inter-Integrated Circuit) is invented by Phillips. It is a multi-master serial singleended computer bus. It is basically used to attach low-speed peripherals to a faster device such as a motherboard or a microcontroller. It uses two bi-directional lines namely SDA and SCL. It has 7 bit address space so that maximum of 112 nodes can communicate on the same bus. The overall communication between master and slave takes place in following manner. First of all master starts communication by sending a start bit and that
21 bit followed by address of the slave it wishes to establish communication with, which is followed by a single bit representing what kind of operation it wants to perform
(read/write). If slave exists on that address then it will send ACK bit so that they can establish communication with each other. The master will then either start sending data or receiving depending upon what kind of transaction is in process.
As explained previously for AT24C04 serial EEPROM, the start bit is indicated by a high-to-low transition of SDA with SCL high and the stop bit is indicated by a low to high transition of SDA with SCL high. If the master wishes to write to the slave then it repeatedly sends a byte with the slave sending an ACK bit. In case of read operation, master repeatedly receives a byte from the slave and it sends back ACK for every byte it receives. To end transmission, master sends a stop bit or it may send another start bit if it wants to retain the control of the bus.
22
2.3 LCD
In this project Liquid crystal display is used because LCD is of much importance when it comes to embedded system. It offers high flexibility to user as he can display the required data on it. But one has to be very careful with his approach when interfacing LCD to microcontroller as it can easily go wrong. If done carefully, LCD interfacing is not complex job but it can go wrong if care is not taken. First thing one should know to begin with is, what LCD driver/controller is used in LCD.LCD driver is a link between the microcontroller and LCD.LCD model JHD162A has driver HD44780U.You have to interface the LCD according to the driver specification.
One of the difficult part in LCD interfacing is the initialization sequence. To initialize
LCD, you have to send command bytes to LCD. For that to happen you have to set the interface mode, direction for address counter, mode of display, set contrast of LCD, horizontal or vertical addressing mode, color format. All of these steps are covered by respective LCD driver datasheet.
2.3.1 Parameters
23
Table 7 Parameters for JHD162A [5]
2.3.2 Pin Configurations
Table 8 Pin Configuration for JHD162A [5]
24
2.3.3 HD44780U
The HD44780U is the chipset controller used for JHD162A. The HD44780U dot-matrix liquid crystal display controller and driver LSI displays alphanumeric, Japanese kana characters, and symbols. By having it controlled by 4 or 8 bit microcontroller we can drive a dot-matrix liquid crystal display. Minimum interface is required as all the functions such as display RAM, character generator, and liquid crystal driver, required for driving a dot-matrix liquid crystal display are internally provided on one chip. A single HD44780U can display up to one 8-character line or two 8-character lines. The
HD44780U character generator ROM is extended to generate 208 5 ´8 dot character fonts and 32 5 ´ 10 dot character fonts for a total of 240 different character fonts. It is suitable for low power application as it requires low power supply (2.7V to 5.5V).
2.3.3.1 Features
5 ´ 8 and 5 ´ 10 dot matrix possible
low power operation support
2.7 to 5.5V
Wide range of liquid crystal display driver power
3.0 to 11V
Liquid crystal drive waveform
A (One line frequency AC waveform)
Correspond to high speed MPU bus interface
2 MHz (when VCC = 5V)
4-bit or 8-bit MPU interface enabled
80 ´ 8-bit display RAM (80 characters max.)
9,920-bit character generator ROM for a total of 240 character fonts
208 character fonts (5 ´ 8 dot)
32 character fonts (5 ´ 10 dot)
64 ´ 8-bit character generator RAM
8 character fonts (5 ´ 8 dot)
4 character fonts (5 ´ 10 dot)
16-common ´ 40-segment liquid crystal display driver
Programmable duty cycles
1/8 for one line of 5 ´ 8 dots with cursor
1/11 for one line of 5 ´ 10 dots with cursor
1/16 for two lines of 5 ´ 8 dots with cursor
Wide range of instruction functions:
Display clear, cursor home, display on/off, cursor on/off, display shift
Pin function compatibility with HD44780S
Automatic reset circuit that initializes the controller/driver after power on
Low power consumption
25
26
2.4 USB Cable:
I have used USB cable readily available in market to connect my personal computer to my microcontroller board. It is a standard USB A to B cable.
Figure 9 USB A to B cable used for Connection. [3]
27
Chapter 3
BLOCK DIAGRAM AND OPERATION
3.1 Block Diagram
The complete block diagram is as shown below. As explained in previous chapter, we are using PIC18F2550 microcontroller for this project and it is at the center of this project.
Pin RC 4 and RC 5 is directly connected to D- and D+ Pin respectively. Pin RB7 and RB
6 is used for connection with EEPROM. Here we are using AT24C04 serial EEPROM and as mentioned earlier whole data transaction is done through SDA and SCL pin. So we have connected Pin RB7 and RB6 with SDA and SCL pin. We also have provision for
LCD display on this kit. Pin RA0- RA3 is used for display. It is connected to LCD. LCD receives data through D4-D7. So, I have established connection between RA0-RA3 of the microcontroller to D4-D7 of the LCD. There also a connection between LCD and microcontroller through pin RA5 as it connected to enable pin of the LCD in this way we are enabling LCD function through microcontroller.
3.2 Power Supply
Connect microcontroller to personal computer through USB cable, the USB cable itself supply enough power to microcontroller and LCD that we do not need external supply from outside. Here we have also made provision for external supply in case USB cable
28 fail to supply so. We can give AC supply and rectify is using bridge rectifier. So we have used 4 diode which forms bridge rectifier. Voltage regulator LM7805 is used to regulate power and ensure that power level is maintained and no damage is caused to circuit. But as per USB specification there is no need for external supply.
29
Figure 10 Block Diagram for USB based data processing
3.3 PIC18F2550 based USB board
30
Figure 11 PIC18F2550 based USB board
31
3.4 Software Development
In order to communicate with personal computer, Visual Basic script was developed. It is having some different options such as read, write, display, clear LCD, and delete from memory so we can decide which operation to perform and give order according to that. As mentioned earlier writing USB driver is out of scope of this project and our microcontroller is capable of performing serial operation, driver readily available in the market was used which helps USB communication through which we can communicate. For that we need to check which port is open and available for communication. When we install driver it configures port for communication.
Whenever device is attached to USB port of the computer, the operating system detects input at USB port. Since the device is human interface device it will not start working like normal USB device. It will only work with the software that was developed for it. In software USBPoll function was used which continuously poll the USB port and check if some device is connected to it or not. It generates interrupt whenever data is present into the buffer. Whenever visual basic software runs it will pop up with a screen that has different options. First of all initialization of microcontroller is done. By configuring device to work full speed mode with USB, enables it to synchronies with
USB operation. Then we are setting our crystal frequency to 48MHZ as PIC18F2550 can run at 48MHZ frequency. Port A is set to digital mode. There is in built comparator in
PIC18F2550. In order to make device work properly we need to disable that feature of microcontroller. This can be achieve by setting CMCON=0. Whenever data is written to
32 microcontroller, data is stored in to buffer and when all the data is received then it starts writing in to EEPROM. So before start of transaction, buffer needs to be clear. In our code there is 3 digit of button code associated with each function. After knowing total length of data by using LEN function, microcontroller doesn’t take in to consideration first three digit of it as these bits are reserved for button code. Whatever data is there after these 3 digits is our actual data that was sent to microcontroller.
When write button on the screen is pressed, button code is associated with that. So we can think this button code as look up table for microcontroller. When we press ‘write’ on screen it generates on button code and it is transmitted from our personal computer to microcontroller and microcontroller sense it and perform task associated with that button code. In case of write operation it performs write operation to EEPROM. For that it put
SCL and SDA pin in a way that it indicates write operation and then it perform write operation.
When read button on screen is pressed, same thing happens and microcontroller performs read operation and displays it on LCD. For read operation to happen microcontroller should know form which address location it should read. Microcontroller remembers start of the address location where it performed last write operation so it goes there and performs read operation.
CMCON: In 18F2550, we have analog comparator module. This particular module contains two comparators that can be configured in a variety of ways. RA0 through RA5 can be selected as inputs to these comparators as well as the on-chip voltage reference
33
Outputs are available at the pin level and can also be read through the control register.
The CMCON register selects the comparator input and output configuration. In this application comparator is not used by any means and we need to disable comparator part of the microcontroller so that RA0 through RA4 can be used for data pins for our LCD application.
ADCON1: When we power on microcontroller, we have by default setting of RA0 through RA3 pins as analog pins. We are using these pins for digital application. We can configure these pins for digital purpose by having access to ADCON1 register. So basically what it does is configuring function of port pins. By setting all bits to ‘1’ we are configuring port for digital I/O operation.
4.1 Testing of USB LCD program
Chapter 4
RESULTS
34
Figure 12 Test case for USB data write
This screenshot from computer screen is for write operation. As soon as I press write button on screen, transaction is initiated. Computer will send data through USB port to microcontroller. This data will contain button code for write operation along with overall string. The microcontroller will recognize from button code and it will start writing our
35 message to EEPROM memory using I2C protocol which quite common with slower peripherals. The physical appearance of LCD will look like below.
Figure 13 Display of USB data on LCD
36
Figure 14 Test case for USB data read
As shown in above figure which is taken from computer screen as I press my read button message from memory will be displayed on LCD. The snapshot of LCD which displays current message is as below.
37
Figure 15 Display of USB data read on LCD
38
Chapter 5
CONCLUSION
The focus of this project was to send data from computer to microcontroller through USB port of computer. Initial plan was to develop USB driver but after some research work it was decided that it is of very large scope for single student. So the alternate way is taken. In this project driver software available in market was used which makes USB appear as virtual COM port in computer. It used PIC18F2550 microcontroller, 16*2 LCD display and AT24C04 external EEPROM. Type A to Type B connector was used for USB interface.
This project was successfully completed with the help of my advisor Dr. Jing
Pang. I have achieved all the requirements to move data from computer to microcontroller. This project gave me opportunity to learn USB communication. Since this project involves both software and hardware part, it made me a better professional as
I improved my skills. This project served as a great learning experience for me because it was by far most challenging project I have ever worked. I became more responsible towards my target achievements.
5.1 Future Improvements
This project is a great stepping stone towards a very good ending. Here I have established
USB based communication. I am moving data from my personal computer to microcontroller for data processing. Right now there is limitation on maximum data that can be sent because external EEPROM is very slow device compare to microcontroller. If
39 some modification is done at software level both in microcontroller and visual basic script then large amount of data can be transferred. Instead moving data it would be great if JPEG image can be moved through USB. And perform image processing and give processed image back to computer.
APPENDIX include PIC18F2550.lib include Variable.INC include My_USB.INC include Stirng.lib include EEPROM.lib
Device = 18F2550
XTAL = 48
' Choose a device with on-board full speed USB
‘ Set the oscillator speed to 48MHz
ADCON1 = 0 '// Set PORTA DIGITAL
CMCON = 0 '// Comparators Off
'--------------------------------------------------------------
' Define EEPROM pins define SDA PORTB.7 ' SDA ON EXTERNAL EEPROM define SCL PORTB.6 ' SCL ON EXTERNAL EEPROM define CON %10100000 ' Target an eeprom
'--------------------------------------------------------------
40
41
LCD_DTPIN = PORTA.0
LCD_RSPIN = PORTA.4
LCD_ENPIN = PORTA.5
LCD_INTERFACE = 4
' LCD data port attached to PORTA
' LCD RS pin attached to PORTA.4
' LCD EN pin attached to PORTA.5
' 4-bit Interface
Cls
Print At 1,1," WEL COME TO "
Print At 2,1,"USB LCD PROJECTS"
PORTC.7 = 0
Repeat
' \
USBPoll ' Wait for the USB interface to become attached
Until PP0 = %00000110 ' /
StrN ARRAY1 = " ARRAY BUFFER\n\r" ' Fill the array with NULL terminated characters
OUT_BUFFER = " STRING BUFFER\n\r" ' Fill the string with NULL terminated characters
While 1 = 1 ' Create an infinite loop
Repeat
USBIn 3, IN_BUFFER, Auto endpoint 3
Until CARRY_FLAG = 0
‘ Wait for USB input
42
' Poll the USB and Receive some data from
' Keep looking until data is able to be received
STR_LEN = Len (IN_BUFFER)
MY_DATA = Left$ (IN_BUFFER, 3)
MY_STR = Mid$ (IN_BUFFER, 4, STR_LEN)
PORTC.7 = 1
Cls
Print At 1,1,"RX MSG FROM USB"
Print At 2,1,MY_STR
DelayMS 1000
GoSub USB_Tx
GoSub CHECK
Clear IN_BUFFER
Clear STR_LEN
Clear MY_DATA
Clear MY_STR
Clear IN_BUFFER
Clear STR_LEN
Return
CHECK:
Select Case MY_DATA
Case "##1"
Clear MY_STR
GoSub READ_MSG
GoSub USB_Tx
Cls
Print At 1,1,"READ MY DATA"
Print At 2,1,MY_STR
DelayMS 1000
Case "##2"
Cls
Print At 1,1,">>STOR MY DATA<<"
43
Print At 2,1,MY_STR
DelayMS 1000
GoSub WRITE_MSG
Case "##3"
GoSub ERAS_MCG
Cls
Print At 1,1,"ERASE ALL DATA"
DelayMS 1000
Case "##4"
Cls
DelayMS 100
Case "##5"
Cls
Print At 1,1,"RX MSG FROM USB"
Print At 2,1,MY_STR
DelayMS 1000
End Select
Clear MY_DATA
44
Clear MY_STR
Clear IN_BUFFER
Clear STR_LEN
PORTC.7 = 0
Return
WRITE_MSG:
I2COUT SDA, SCL, CON , 1, [MY_STR] : DelayMS 100
Return
READ_MSG:
I2Cin SDA, SCL, CON , 1, [MY_STR] : DelayMS 100
Return
ERAS_MCG:
For I = 1 To 20
Next
I2COUT SDA, SCL, CON , I, [0] : DelayMS 50
Return
USB_Tx:
45
__USBOUT_BUFFER = MY_STR
USB TX buffer string
Return
46
‘// Place characters directly into the
VISUAL BASIC SCRIPT
Private Sub Command1_Click()
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
PWR.blink = False
End If
End
End Sub
Private Sub Command2_Click()
MSComm1.Output = "##2" + Text1.Text
End Sub
Private Sub Command3_Click()
MSComm1.Output = "##1"
End Sub
Private Sub Command4_Click()
RichTextBox1 = ""
MSComm1.Output = "##4"
End Sub
47
Private Sub Command5_Click()
MSComm1.Output = "##5" + Text1.Text
End Sub
Private Sub Command6_Click()
MSComm1.Output = "##3"
End Sub
Private Sub Form_Load()
Option1.Value = True
PWR.Status = False
Option1.Value = False
Option2.Value = True
RichTextBox1 = ""
End Sub
Private Sub MSComm1_OnComm()
Dim strData As String * 150
If MSComm1.CommEvent = comEvReceive Then
48
strData = MSComm1.Input
RichTextBox1 = strData
End If
End Sub
Private Sub START_CMD_Click()
If MSComm1.PortOpen = False Then
If Option1.Value = True Then
MSComm1.CommPort = 1
PWR.blink = True
End If
If Option2.Value = True Then
MSComm1.CommPort = 3
MSComm1.PortOpen = True
PWR.blink = True
End If
End If
End Sub
49
REFERENCES
[1] Universal Serial Bus http://en.wikipedia.org/wiki/Universal_Serial_Bus
[2] Microchip PIC18f2550 Data Sheet http://ww1.microchip.com/downloads/en/devicedoc/39632d.pdf
[3] USB 2.0 “A” plug to “B” plug http://www.usbcable.com/
[4] Two channel serial EEPROM http://www.atmel.com/dyn/resources/prod_documents/doc0180.pdf
[5] JHD162A series http://www.electrokit.se/download/JHD162A%20SERIES.pdf
[6] HITACHI HD44780 U driver datasheet http://fab.cba.mit.edu/classes/MIT/863.06/11.13/44780.pdf
[7] USB in a Nutshell http://www.beyondlogic.org/usbnutshell/
[8] How USB port work http://computer.howstuffworks.com/usb.htm
[9] Fairchild LM7805 voltage regulator datasheet http://www.fairchildsemi.com/ds/LM/LM7805.pdf
50