Code:
'***************************************************************************
'* Name : USBdemo_DTHID.pbp *
'* Author : 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 *
'***************************************************************************
; 18F47J53, no crystal - 8 mhz internal
#CONFIG
config WDTEN = OFF ;//WDT disabled (enabled by SWDTEN bit)
CONFIG PLLDIV = 2 ;//Divide by 2 (8 MHz internal oscillator input)
config STVREN = ON ; //stack overflow/underflow reset enabled
config XINST = OFF ;//Extended instruction set disabled
config CPUDIV = OSC1 ;//No CPU system clock divide
config CFGPLLEN = ON ; //turn on PLL config
config CP0 = OFF ; //Program memory is not code-protected
config OSC = INTOSCPLL;HSPLL ; //HS oscillator, PLL enabled, HSPLL used by USB
config FCMEN = OFF ; //Fail-Safe Clock Monitor disabled
config IESO = OFF ; //Two-Speed Start-up disabled
config WDTPS = 32768 ; //1:32768
config DSWDTOSC = INTOSCREF ;//DSWDT uses INTOSC/INTRC as clock
config RTCOSC = T1OSCREF ;//RTCC uses T1OSC/T1CKI as clock
config DSBOREN = OFF ;//Zero-Power BOR disabled in Deep Sleep
config DSWDTEN = OFF ;//Disabled
config DSWDTPS = 8192 ;//1:8,192 (8.5 seconds)
config IOL1WAY = OFF ;//IOLOCK bit can be set and cleared
config MSSP7B_EN = MSK7 ;//7 Bit address masking
config WPFP = PAGE_1 ;//Write Protect Program Flash Page 0
config WPEND = PAGE_0 ;//Start protection at page 0
config WPCFG = OFF ;//Write/Erase last page protect Disabled
config WPDIS = OFF ;//WPFP[5:0], WPEND, and WPCFG bits ignored
#ENDCONFIG
DEFINE OSC 48
CLEAR
;--- Setup Interrupts ------------------------------------------------------
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USB_Handler
endm
INT_CREATE ; Creates the interrupt processor
endasm
;--- 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
TRISA = %00000011
TRISB = 0
OUTPUT PORTC.1
OUTPUT PORTC.2
ADCON1.7 = 0 ; left justify (Change this if ADFM in diff register)
ADCON0 = %0001 ; AN0/AN1 Analog
;--- The Main Loop ---------------------------------------------------------
Main:
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
GOTO Main
;--- 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