You are on page 1of 6

UNIVERSIDAD POLITECNICA SALESIANA

COMUNICACIÓN VIA USB


INTEGRANTES: Jairo Castillo

Jorge Morales

Jonathan Tipán

En esta práctica podemos comunicarnos via usb del pic2550 al pc a través del HIDTerminal
de Mikrobasic

Circuito para la practica


Código
El siguiente código enciende un led al conectar el puerto usb en el pc es decir se enciende
el led al detectar al dispositivo nuevo.

program TP2550MKB
include "USBdsc" 'descriptor del Programa

dim
k, i as byte 'variables Uso general
userWR_buffer as byte[2] 'Buffer de Transmision USB
userRD_buffer as byte[2] 'Buffer de Recepcion USB

sub procedure interrupt() 'Procedimiento HID para mantener Viva la conexion


asm
call _HID_InterruptProc
end asm
end sub

sub procedure INIT_2550() ' Inicializacion de registros en 18F2550 Limpiar Interrupciones


INTCON2 = 245
INTCON3 = 192
RCON.IPEN = 0
PIE1 = 0
PIE2 = 0
PIR1 = 0
PIR2 = 0
ADCON1 = 15 ' Sin ADC
TRISA = 0 ' Puertos de Salida y a Cero todos
TRISB = 0
TRISC = 0
LATA = 0
LATB = 0
LATC = 0
end sub

main:

INIT_2550() ' Inicializacion de registros

for i=0 to 5
porta = not porta
delay_ms(500)
next i

HID_Enable(@userRD_buffer, @userWR_buffer) 'Inicializacion del USB a HID

Delay_mS(1000)
Delay_mS(1000) 'Pausa para que cargue el PIC
'Loop Infinito
while true

k = HID_read() 'Leemos la longitud del Buffer, No es necesario


i = userRD_buffer[0] 'Asignamos el valor del indice 1 del Buffer de Recepcion a i

select case i 'Evaluamos i


case 1 'Si i=0 entonces todo el PORTA=0
porta=0
case 2 'Si i=1 entonces todo el PORTA=255
porta=255
end select

wend
HID_Disable() 'Deshabilitamos el HID

end.

USANDO HIDTERMINAL PARA VERIFICAR LA CONEXIÓN


Código para encender y apagar un led desde el HIDTerminal

Este código al enviar cualquier número diferente a 0 encenderá el led y


enviando el 0 lo apagará.

program USB_HID_test

include "USBdsc"

dim
k, i, ch as byte
userWR_buffer as byte[64]
userRD_buffer as byte[64]

'******************************************************************************
' Main Interrupt Routine
'******************************************************************************
sub procedure interrupt
'** this is a way to call a procedure from interrupt
HID_InterruptProc
end sub
'******************************************************************************

'******************************************************************************
' Initialization Routine
'******************************************************************************
sub procedure Init_Main
'--------------------------------------
' Disable interrupts
'--------------------------------------
INTCON = 0 ' Disable GIE, PEIE, TMR0IE,INT0IE,RBIE
INTCON2 = 0xF5
INTCON3 = 0xC0
RCON.IPEN = 0 ' Disable Priority Levels on interrupts
PIE1 = 0
PIE2 = 0
PIR1 = 0
PIR2 = 0

ADCON1 = ADCON1 or 0x0F ' Configure all ports with analog function as digital
'--------------------------------------
' Ports Configuration
'--------------------------------------
TRISA = 0
TRISB = 0xFF
TRISC = 0xFF
'TRISD = 0
'TRISE = 0x07

LATA = 0
LATB = 0
LATC = 0
'LATD = 0
'LATE = 0
end sub

'******************************************************************************
' Main Program Routine
'******************************************************************************

main:
Init_Main()

HID_Enable(@userRD_buffer, @userWR_buffer)
Delay_mS(1000)
Delay_mS(1000)

while true
k = HID_Read()
i=0
while i < k
ch = userRD_buffer[0]
userWR_buffer[0] = ch
HID_Write(@userWR_buffer, 1)
inc(i)

if ch>0 then 'PONE EL PUERTO A EN NIVEL ALTO (ENCIENDE EL LED)


porta=255

else 'PONE EL PUERTO A EN NIVEL BAJO (APAGA EL LED)


porta=0
end if

wend
wend
HID_Disable()

end.

You might also like