' ' Implement the functionality of a Jan Axelson demo. ' Accept two numbers from the host, increment each then send them back. ' ' The application to run on the PC is named USBHIDIO.exe and can be found in the Includes\USB_18 folder ' Device = 18F14K50 Declare Xtal = 48 ' Set the oscillator speed to 48MHz (using a 20MHz crystal) Declare USB_Descriptor = "Jadesc.Inc" ' Point to the Demo Descriptor file (located in the Includes\USB_18 folder) ' Declare USB_Descriptor = "MZDESC.Inc" Dim PP0 As Byte System ' UsbPoll status return Dim Buffer[6] As Byte ' USB IO buffer Symbol LED = PORTC.7 Symbol Carry_Flag = STATUS.0 ' High if microcontroller does not have control over the buffer Symbol TRNIF = UIR.3 ' Low if USB Busy Config_Start CPUDIV = NOCLKDIV ' No CPU System Clock divide USBDIV = On ' USB Clock comes directly from the OSC1/OSC2 oscillator i deli se sa 2 FOSC = HS ' HS oscillator PLLEN = On ' Oscillator multiplied by 4 PCLKEN = On ' Primary clock enabled FCMEN = Off ' Fail-Safe Clock Monitor disabled IESO = Off ' Oscillator Switchover mode disabled PWRTEN = On ' PWRT enabled BOREN = On ' Brown-out Reset enabled and controlled by software (SBOREN is enabled) BORV = 19 ' VBOR set to 1.9 V nominal WDTEN = Off ' WDT is controlled by SWDTEN bit of the WDTCON register WDTPS = 128 ' Watchdog Timer Postscale 1:128 MCLRE = On ' MCLR pin enabled, RE3 input pin disabled HFOFST = Off ' The system clock is held off until the HFINTOSC is stable. STVREN = Off ' Stack full/underflow will not cause Reset LVP = Off ' Single-Supply ICSP disabled BBSIZ = Off ' 512W boot block size XINST = Off ' Instruction set extension and Indexed Addressing mode disabled Debug = Off ' Background debugger disabled and RA0 and RA1 configured as general purpose I/O pins CP0 = Off ' Block 0 not code-protected CP1 = Off ' Block 1 not code-protected CPB = Off ' Boot block not code-protected CPD = Off ' Data Eeprom not code-protected WRT0 = Off ' Block 0 not write-protected WRT1 = Off ' Block 1 not write-protected WRTB = Off ' Boot block not write-protected WRTC = Off ' Configuration registers not write-protected WRTD = Off ' Data Eeprom not write-protected EBTR0 = Off ' Block 0 not protected from table reads executed in other blocks EBTR1 = Off ' Block 1 not protected from table reads executed in other blocks EBTRB = Off ' Boot block not protected from table reads executed in other blocks Config_End '----------------------------------------------------------------------- ' The main program loop starts here DelayMS 100 ' Wait for things to stabilise Declare All_Digital = True ' Set PORTA and PORTD to digital mode Clear ' Clear all RAM before we start Repeat ' \ USBPoll ' Wait for the USB interface to become attached Until PP0 = %00000110 ' / While 1 = 1 ' Create an infinite loop Low LED ' Extinguish the LED while waiting for USB input Repeat in: ' Wait for USB input of 2 numbers. USBIn 1, Buffer,Auto ' Poll the USB and Receive some data from endpoint 1 Until Carry_Flag = 0 ' Keep looking until data is able to be received ' ' Message received. So Illuminate the LED, Increment the bytes and transmit them back. ' High LED ' Illuminate the LED when bytes received Inc Buffer#0 ' Increment the first byte received Inc Buffer#1 ' Increment the second byte received Inc Buffer#2 Inc Buffer#3 Inc Buffer#4 Inc Buffer#5 Repeat USBOut 1, Buffer , Auto ' Send the 2 bytes back from endpoint 1 Until Carry_Flag = 0 ' Keep trying if the microcontroller does not have control over the buffer Repeat : Until TRNIF = 1 ' Wait for completion before continuing Wend ' Wait for next buffer input