Code:
	'**************************************************************************
'*  Name    : USBdemo_DTHID2.pbp                                          *
'*  Author  : Modification of Steve Monfette/Darrel Taylor                *
'*  Date    : 11/9/2009                                                   *
'*  Version : 2.0                                                         *
'*  Notes   : This is a re-creation of mister-e's USBdemo for DT_HID      *
'*          : Meant to work with mister-e's GUI but with a USB interrupt  *
'*          : service routine where some specific code will be executed   *
'*          : only during USB connection.                                 *
'**************************************************************************
;-- if you un-comment these, you must comment the ones in the .inc file --
ASM  ; 18F2550/4550, 8mhz crystal
   __CONFIG    _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
   __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
   __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
   __CONFIG    _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
   __CONFIG    _CONFIG3H, _PBADEN_OFF_3H
   __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
DEFINE OSC 48
CLEAR
;--- Setup Interrupts ------------------------------------------------------
INCLUDE "DT_INTS-18.bas"     ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP high priority interrupts
ASM
INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
    INT_Handler        USB_INT,    _USBhandler,   PBP,  yes
    endm
    INT_CREATE               ; Creates the interrupt processor
endasm
@    INT_ENABLE   USB_INT    ; enable external (INT) interrupts
;--- Setup USB -------------------------------------------------------------
INCLUDE "DT_HID260.pbp"
DEFINE USB_VENDORID    6017
DEFINE USB_PRODUCTID   2000
DEFINE USB_VERSION     1
DEFINE USB_VENDORNAME  "Mister E/DT"
DEFINE USB_PRODUCTNAME "USBDemo"
DEFINE USB_SERIAL      "001"
DEFINE USB_INSIZE      8    ;  IN report is PIC to PC (8,16,32,64)
DEFINE USB_OUTSIZE     8    ; OUT report is PC to PIC
DEFINE USB_POLLIN      10   ; Polling times in mS, MIN=1 MAX=10
DEFINE USB_POLLOUT     10
; --- Each USB status LED is optional, comment them if not used ------------
; --- They can be assigned to any pin, and no further action is required --- 
DEFINE USB_LEDPOLARITY 1       ; LED ON State [0 or 1]  (default = 1)
;DEFINE USB_PLUGGEDLED  PORTB,7 ; LED indicates if USB is connected
;DEFINE USB_TXLED       PORTB,6 ;  "      "     data being sent to PC
;DEFINE USB_RXLED       PORTB,5 ;  "      "     data being received from PC
;--- Variables -------------------------------------------------------------
Value0      VAR  WORD
Value1      VAR  WORD
X           VAR  WORD
DUTY1       VAR  WORD
DUTY2       VAR  WORD
Old_PORTA   VAR  BYTE
New_PORTA   VAR  BYTE
;--- Setup ADC -------------------------------------------------------------
DEFINE ADC_BITS 8  ; Number of bits in ADCIN result
;--- Initialize ------------------------------------------------------------
CCPR1L = 0
CCPR2L = 0
CCP1CON =   %00001100       ' CCP1, PWM mode
CCP2CON =   %00001100       ' CCP2, PWM mode
PR2     =   249             ' 0-1000 duty range
T2CON   =   %00000101       ' TMR2 on, prescaler 1:4
TRISB = 0
OUTPUT PORTC.1
OUTPUT PORTC.2
    
ADCON2.7 = 0       ; left justify    (Change this if ADFM in diff register)
ADCON1 = %1101     ; AN0/AN1 Analog
;--- The Main Loop ---------------------------------------------------------
Main:   
    'Normally in sleep mode waiting for interrupts
    Toggle PORTE.2      ' Test LED flashing during Main loop
GOTO Main
'----------------Interrupt Service Routines Start Here--------------------
USBhandler:
    FOR X = 0 to 1000         ; Check for incomming USB data while waiting
    @       ON_USBRX_GOSUB  _HandleRX
            PAUSE 1
        
            New_PORTA = PORTA                ; Check PORTA pins
            IF New_PORTA != Old_PORTA THEN
                Old_PORTA = New_PORTA
                ARRAYWRITE USBTXBuffer, ["USB Demo"]
                GOSUB WaitToSend
                ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2, _
                                         PORTA.3, PORTA.4, PORTA.5,0,0]
                GOSUB WaitToSend
            ENDIF
        NEXT X
        
        ADCIN 0, Value0            ; Send A/D about once/sec
        ADCIN 1, Value1
        ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2, _
                                 PORTA.3, PORTA.4, PORTA.5,0,0]
        GOSUB SendIfReady
    'Code to be processed only during USB connection.
        TOGGLE PortE.3      ' Test LED that USB interrupt occured
@ INT_RETURN
;--- Send Data if Plugged and ready, otherwise discard --------------------
SendIfReady:
    IF Plugged AND TX_READY THEN DoUSBOut
RETURN
;--- Wait till Ready to send of until unplugged ---------------------------
WaitToSend:
    WHILE Plugged and !TX_READY : WEND
    IF TX_READY THEN GOSUB DoUSBOut
RETURN
;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
HandleRX:
    ARRAYREAD  USBRXBuffer,[PORTB, DUTY1.LowByte, DUTY1.HighByte, _
                                    DUTY2.LowByte, DUTY2.HighByte]
    CCP1CON.5=DUTY1.1       ' load CCP1 duty value            
    CCP1CON.4=DUTY1.0       '      with Duty1 
    CCPR1L=DUTY1>>2         '
    
    CCP2CON.5=DUTY2.1       ' load CCP2 duty value            
    CCP2CON.4=DUTY2.0       '      with Duty2
    CCPR2L=DUTY2>>2         '
return
 
				
Bookmarks