
2000 Microchip Technology Inc.
Preliminary
DS41124C-page 75
PIC16C745/765
10.9.7
EXAMPLE
This example shows how the USB functions are used.
This example first initializes the USB peripheral, which
allows the host to enumerate the device. The enumer-
ation process occurs in the background, via an Inter-
rupt
Service
Routine.
This
function
waits
until
enumeration is complete, and then polls EP1 OUT to
see if there is any data available. When a buffer is
available, it is copied to the IN buffer.
Presumably
your application would do something more interesting
with the data than this example.
; ******************************************************************
; Demo program that initializes the USB peripheral, allows the Host
;
to Enumerate, then copies buffers from EP1OUT to EP1IN.
; ******************************************************************
main
call
InitUSB
; Set up everything so we can enumerate
ConfiguredUSB
; wait here until we have enumerated.
CheckEP1
; Check Endpoint 1 for an OUT transaction
bankisel
buffer
; point to lower banks
movlw
buffer
movwf
FSR
; point FSR to our buffer
movlw
1
; check end point 1
call
GetUSB
; If data is ready, it will be copied.
btfss
STATUS,C
; was there any data for us?
goto
PutBuffer
; Nope, check again.
; Code host to process out buffer from host
PutBuffer
bankisel
buffer
; point to lower banks
; save buffer length
movlw
buffer
movwf
FSR
; point FSR to our buffer
movlw
0x81
; put 8 bytes to Endpoint 1
call
PutUSB
btfss
STATUS,C
; was it successful?
goto
PutBuffer
; No: try again until successful
goto
idleloop
; Yes: restart loop
end
10.9.8
ASSEMBLING THE CODE
The code is designed to be used with the linker. There
is no provision for includable files. The code comes
packaged as several different files:
USB_CH9.ASM - handles all the Chapter 9 com-
mand processing.
USB_DEFS.INC - #Defines used throughout the
code.
USB_MAIN.ASM - Sample interrupt service
routine.
HIDCLASS.ASM - Handles the HID class specific
commands.
10.9.8.1
Assembly Options
There are two #defines at the top of the code that con-
trol assembly options.
10.9.8.2
#define ERRORCOUNTERS
This define includes code to count the number of
errors that occur, by type of error. This requires extra
code and RAM locations to implement the counters.
10.9.8.3
#define FUNCTIONIDS
This is useful for debug. It encodes the upper 6 bits of
USWSTAT (0x197) to indicate which function is exe-
cuting. See the defines in USB_DEFS.INC for the
codes that will be encoded.
745cov.book Page 75 Wednesday, August 2, 2000 8:24 AM