You are on page 1of 13

DIGITAL SYSTEM 2

UNIVERSITY AUTOMOMA OF QUERETARO


ENGINEERING FACULTY

Career:
Automation Engineering
Subject:
Digital System II
Practice 8:
USING USB AS HUMAN INTERFACE DEVICE AND AS A VIRTUAL SERIAL COM

Student:
Dublan Barragn Brenda Santa.
Lab teachers Name:
M. en C. Manuel Toledano Ayala
th

December 14 ,2010

DIGITAL SYSTEM 2

Subject:

DIGITAL SYSTEMS 2

Practice name

USING USB AS HUMAN INTERFACE DEVICE AND AS A VIRTUAL SERIAL COM

Practice number

Length (Hours)

OBJECTIVE (Learning result)


The student will be able to configure a USB interface using the high performance microcontroller PIC18F4550, in
order to exchange data with the PC.

THEORETICAL FRAMEWORK
USB
Alternative to old-fashioned serial and parallel interfaces
Minimises number of PC connectors
Simplifies I/O and offers true plug-n-play
Supports up to 127 devices
Multiple data rates
USB 1.1: full speed [12Mb/s] and low speed [1.5Mb/s]
USB 2.0: high speed [480Mb/s]

Explain the types of transfers used by the USB 2.0


A class is a group usb device characteristics common, ie the same form used to communicate with environment, the
device class provides information about the way the interface communicates with the system, which can locate the
driver that can control the connectivity between the interface and the system.
USB only allows the driver to communicate with the peripheral through plumbing (pipes) established between the
system and USB endpoints delperiferico.Los transfer rates through the pipes depend the endpoint and may be: bulk,
control, interrupt and isochronous.
bulk transfers: large sporadic transfers using all remaining available bandwidth, but with no guarantees on
bandwidth or latency (e.g. file transfers).

DIGITAL SYSTEM 2
Interrupt Transfer:Human interface devices such as mice, keyboards, and joysticks are expected to be
capable of processing input signals fast enough so that the users do not feel a "lag." Traditionally, detected
input signals were handled as interrupt requests; however, with USB, processes cannot be started as interrupt
requests from the input device, as all data transfer requests are initiated by the host. To work around this issue,
the host "polls" the input device periodically, for example every 10 ms, which is quick enough for reflecting
keyboard inputs to the screen without irritating the user. Under the USB specification, this method with the host
periodically transferring data is referred to as an "Interrupt transfer."The 1.5 Mbps low-Speed specification was
designed primarily for low-cost input devices, and is available only for use by devices that use the Interrupt
transfers. Note that mice, keyboards, and other input devices also require the use of Control transfers.
isochronous transfers: at some guaranteed data rate (often, but not necessarily, as fast as possible) but with
possible data loss (e.g. realtime audio or video).
Control Transfer:Unlike Interrupt, Bulk, and Isochronous transfers, Control transfers have rules about the
content of the transferred data.Control transfers are used to exchange device details, allocate USB addresses,
and configure devices, and are hence used by all devices.USB combines these different types of transfers with
a concept of "Interface" to implement various types of devices.

Include the schematic with the minimum circuit necessary to do an USB 2.0 interface with the PIC18F4550

DIGITAL SYSTEM 2

Define the following concepts: HID, Embedded Software, Virtual Serial Com, End Point and Enumeration
process

Human Interface Device (HID) class for use with Universal Serial Bus (USB). Concepts from the USB.
The HID class consists primarily of devices that are used by humans to control the operation of computer systems.
Typical examples of HID class devices include:
Keyboards and pointing devicesfor example, standard mouse devices, trackballs, and joysticks.
Front-panel controlsfor example: knobs, switches, buttons, and sliders.
Controls that might be found on devices such as telephones, VCR remote controls, games or simulation devices for
example: data gloves, throttles, steering wheels, and rudder pedals. 2 Device Class Definition for Human Interface
Devices that may not require human interaction but provide data in a similar format to HID class devices forexample,
bar-code readers, thermometers, or voltmeters.
Many typical HID class devices include indicators, specialized displays, audio feedback, and force or tactile
feedback. Therefore, the HID class definition includes support for various types of output directed to the end user.

Enumeration process is the process of Assigning Addresses and Setting Configurations.

End Point is a connection from the host controller to a logical entity, found on a device, and named an endpoint.
Virtual serial com A virtual serial com is an emulation of the standard serial com. This port is created by software
which enable extra serial ports in an operating system without additional hardware installation .

DIGITAL SYSTEM 2
MATERIALS AND EQUIPEMENT

METHODOLOGY
1.- Type the Source code Enumeration process.c shown in class
2.- Identify and explain each one of the following lines of code:
a) #define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT
b) #include<pic18_usb.h>
c) #include<usb_desc_hid.h>
d) #fuses HSPLL
e) IF (usb_enumerated () )

Function for usb

usb_init()

Initializes the USB hardware. Will then wait in an infinite loop for the USB
peripheral to be connected to bus (but that doesn't mean it has been
enumerated by the PC). Will enable and use the USB interrupt.

usb_init_cs()

The same as usb_init(), but does not wait for the device to be connected to the
bus. This is useful if your device is not bus powered and can operate without a
USB connection.

usb_task()

If you use connection sense, and the usb_init_cs() for initialization, then you
must periodically call this function to keep an eye on the connection sense pin.
When the PIC is connected to the BUS, this function will then perpare the USB
peripheral. When the PIC is disconnected from the BUS, it will reset the USB
stack and peripheral. Will enable and use the USB interrupt.

usb_enumerated()

Returns TRUE if the device has been enumerated by the PC. If the device has
been enumerated by the PC, that means it is in normal operation mode and
you can send/receive packets.

DIGITAL SYSTEM 2

Relevant CDC
Functions:
A CDC USB device will emulate an RS-232 device, and will appear on your PC as a COM port. The
follow functions provide you this virtual RS-232/serial interface
usb_cdc_kbhit()

The same as kbhit(), returns TRUE if there is 1 or more character in the


receive buffer.

usb_cdc_getc()

The same as getc(), reads and returns a character from the receive buffer. If
there is no data in the receive buffer it will wait indefinitely until there a character
has been received.

usb_cdc_putc(c)

The same as putc(), sends a character. It actually puts a character into the
transmit buffer, and if the transmit buffer is full will wait indefinitely until there is
space for the character.

pic_usb.h

Hardware layer driver for the PIC16C765 family PICmicro controllers with an
internal USB peripheral.

pic_18usb.h

Hardware layer driver for the PIC18F4550 family PICmicro controllers with an
internal USB peripheral.

usb.h

Common definitions and prototypes used by the USB driver

usb.c

The USB stack, which handles the USB interrupt and USB Setup Requests on
Endpoint 0.

usb_cdc.h

A driver that takes the previous include files to make a CDC USB device, which
emulates an RS232 legacy device and shows up as a COM port in the MS
Windows device manager.

DIGITAL SYSTEM 2

3.- Compile the code


4.- Program the microcontroller and watch the results (Check point 1)
#include <18f4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay (clock=48000000)
#define LED1 PIN_C0
#define LED2 PIN_C1
#define LED3 PIN_C2
#define LED_ON output_high
#define LED_OFF output_low
#define USB_CON_SENSE_PIN PIN_B2
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_RX_SIZE 8
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_TX_SIZE 8
#include <pic18_usb.h>
#include <usb_desc_hid.h>
#include <usb.c>
void main()
{
LED_ON(LED2);
LED_OFF(LED1);
LED_OFF(LED3);
usb_init();
delay_ms(3000);
while(TRUE)
{
usb_task();
if(usb_enumerated())
LED_ON(LED1);
else
LED_ON(LED3);
}
}

DIGITAL SYSTEM 2

5.- Modify the source code Enumeration process.c in order to show a blinking led while the USB device remains
enumerated.(Check point 2).
#include <18F4550.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#use delay(clock=48000000)
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#include <lcd.c>
#define USB_CON_SENSE_PIN PIN_B2
#include <usb_cdc.h>
void main() {
usb_cdc_init();
usb_init();
lcd_init();
lcd_gotoxy(1,1);
printf(lcd_putc,"Esperando", );
while (TRUE) {
usb_task();
if (usb_enumerated()) {
lcd_putc("\f");
printf(lcd_putc,"enumerado", );
output_low(pin_B0 );
delay_ms(300);
output_high(pin_B1 );
delay_ms(300);
}
}
}

DIGITAL SYSTEM 2

6.- Using as a reference the code virtual com.c write down the code to show in the hyperterminal the voltages on
the analog channels 0 to 4 by writing CH0, CH1, CH2, CH3. Also, when typing PWMXXX on the hyperterminal
(where XXX represent 3 digits) the user will be able to modify the PWM duty cycle. (Check point 3)

PRACTICE EVALUATION
QUESTIONARY
1. Add your code with comments to this report
#include <18F4550.h>
#include <string.h>
#include <lcd.c>
#include <stdlib.h>
#include <stdio.h>
#device adc=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#define USB_CON_SENSE_PIN PIN_B2
#include <usb_cdc.h>
int16 r;
float d;
int voltaje,a,buf[6],i=0;
int s1[5]={'c','h','o',0x0d,0};//for ch0
int s2[5]={'c','h','1',0x0d,0};//for ch1
int s3[5]={'c','h','2',0x0d,0};// for ch2
int s4[5]={'c','h','3',0x0d,0};//for ch3
int dac[5]={'d','a','c',0x0d,0};//for pwm
void select(){
//this function is for known what case was select
if((strcmp(buf,s1))==0){// for ch0
a=1;
set_adc_channel(0);
delay_us(10);
r = read_adc(); //read adc
d = 5.0 * r / 1024.0;//converter to volts
}
if(strcmp(buf,s2)==0){//for ch1

DIGITAL SYSTEM 2
a=2;
set_adc_channel(1);
delay_us(10);
r= read_adc();
d = 5.0 * r / 1024.0;
}
if(strcmp(buf,s3)==0){// for ch2
a=3;
set_adc_channel(2);
delay_us(10);
r = read_adc();
d = 5.0 * r / 1024.0;
}
if(strcmp(buf,s4)==0){//for ch3
a=4;
set_adc_channel(3);
delay_us(10);
r= read_adc();
d = 5.0 * r / 1024.0;
}
if(strcmp(buf,dac)==0){// for pwm
a=5;
set_adc_channel(4);
delay_us(10);
r = read_adc();
r = 5.0 * r / 1024.0;
r = r *20;
lcd_putc("\f");
voltaje=(int) r ;
set_pwm1_duty(voltaje);
}
}

void main() {

//main principal

setup_adc_ports(AN0|VSS_VDD);// volt of reference


setup_ccp1(CCP_PWM); //configure for pwm
setup_timer_2(T2_DIV_BY_1,100,1);
usb_cdc_init();
usb_init();
lcd_init();
lcdgotoxy(1,1);
printf(lcd_putc,"esperando", );
while (TRUE) {
usb_task(); //detect usb
10

DIGITAL SYSTEM 2
if (usb_enumerated()) { //enumerated
output_high(PIN_c0); // high if was detect
if(usb_cdc_kbhit())// if is push any keyboard
{
output_toggle(PIN_B0) ; //change 0 to 1
buf[i]=usb_cdc_getc(); //save in buf that was reader
lcd_putc("\f");
printf(lcd_putc,"%s",buf );
if((buf[i])==0x0d){ //if keyboard is enter
buf[i+1]=0;// the next element point a cero
i=0;
//reset i for the other select
}
else // only record the i
i++;
}
select();//the function is call
switch (a){
case (1):
printf(usb_cdc_putc,"\r Voltage=%01.2fV", p);
printf(usb_cdc_putc,"\r ");
lcd_putc("\f");
printf(lcd_putc," Voltage=%.2fV", p);
//this is for reset de select when in again
a=0;
i=0;
//and the same past whit the buffer
//the same for all the case
for(i=0; i<=5; i++)
buf[i]=0;
i=0;
break;
case (2):
printf(usb_cdc_putc,"\r Voltage=%01.2fV", p);
printf(usb_cdc_putc,"\r\n ");
lcd_putc("\f");
printf(lcd_putc," Voltage=%.2fV", p);
a=0;
i=0;
for(i=0; i<=5; i++)
buf[i]=0;
i=0;
break;
case (3):
printf(usb_cdc_putc,"\r Voltage=%0.2fV", p);
printf(usb_cdc_putc,"\r\n ");
lcd_putc("\f");
printf(lcd_putc," Voltage=%.2fV", p);
a=0;
11

DIGITAL SYSTEM 2
i=0;
for(i=0; i<=5; i++)
buf[i]=0;
i=0;
break;
case (4):
printf(usb_cdc_putc,"\r Voltage=%.2fV", p);
printf(usb_cdc_putc,"\r\n ");
lcd_putc("\f");
printf(lcd_putc," Voltage=%.2fV", p);
a=0;
i=0;
for(i=0; i<=5; i++)
buf[i]=0;
i=0;
break;
case (5):
printf(usb_cdc_putc,"\r Duty=%d",voltaje);
printf(usb_cdc_putc,"\r\n ");
lcd_putc("\f");
printf(lcd_putc," Duty=%dV",voltaje);
a=0;
i=0;
for(i=0; i<=5; i++)
buf[i]=0;
i=0;
break;

default:
break;
}
}
}
}

2. Is it necessary to use a driver when setting the microcontroller to behave as an HID device?
Yes
3. Which is the maximum speed when working as a USB 2.0 device?
480 mb/seg.
4. Add the pin diagram for the USB type B connector that you used

12

DIGITAL SYSTEM 2

5. Which problems did you find while realizing this practice?


was very complicated in my machine I could not install the driver, it generates a lot ofme to work conflicts
and problems in practice.

Conclusions
This practice was undoubtedly the most difficult of the semester but I like see how theUSB because my laptop would
not let me install the driver, this practice is difficult butvery interesting.

Bibliography
http://en.wikipedia.org/wiki/Universal_Serial_Bus
http://www.jungo.com/st/usbware_embedded_usb_solution.html

13

You might also like