Code:
'***************************************************************************
'* Name : BasicUSB.pbp *
'* Author : Darrel Taylor *
'* Notice : Copyright (c) 2009 *
'* Date : 7/23/2009 *
'* Version : 1.x ' Modified by scalerobotics *
'* Notes : I tried to edit it to work on a PIC18F67J50 and I failed *
'* : Worked great on other chips, but I can't seem to figure out *
'* : the problem for PIC18F67J50. Board is PIC_LCD3310, which does*
'* : work with the USB bootloader,so probably not a hardware issue*
'***************************************************************************
'DEFINE LOADER_USED 1
'DEFINE RESET_ORG 1000h ' For Microchip USB Bootloader
;--- if you un-comment these, you must comment the ones in the .inc file ---
ASM ; 18F2550/4550, 20mhz crystal
; __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
; __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
; __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
; __CONFIG _CONFIG3H, _PBADEN_OFF_3H
; __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
;ASM ; 18F13K50/14K50 Only 12mhz crystal can be used for USB
; CONFIG CPUDIV=NOCLKDIV ; CPU runing at full speed
; CONFIG USBDIV=OFF ; Low speed USB clock diviser disabled, not used here
; CONFIG FOSC=HS ; Use of High Speed crystal oscillator
; CONFIG PLLEN=ON ; Enable PLL multiplier, 12Mhz oscilator x 4
; CONFIG PCLKEN=ON ; Primary clock source is enabled
; CONFIG FCMEN=OFF ; Failsafe clock off
; CONFIG IESO=OFF ; Oscillator switchover mode disabled
; CONFIG WDTEN=ON ; Watchdog timer on
; CONFIG WDTPS=512 ; Watchdog divider is 512
; CONFIG MCLRE=OFF ; Disable MCLR pin, enable RC3
; CONFIG STVREN=ON ; Stack full/underflow will cause reset
; CONFIG LVP=OFF ; Low voltage programming disabled
; CONFIG BBSIZ=OFF ; 512kW block boot size
; CONFIG XINST=OFF ; Extended instruction mode disabled
;ENDASM
#config ;pic18F67J50 on Olimex PIC_LCD3310 usb board
CONFIG PLLDIV = 5 ;for 20 mhz crystal
CONFIG XINST = OFF
CONFIG STVREN = off ;stack overflow reset
CONFIG WDTEN = OFF ;added
CONFIG CP0 = OFF ;added
CONFIG IESO = OFF ;added
CONFIG FCMEN = OFF ;added
CONFIG CCP2MX = DEFAULT ;added
CONFIG WDTPS = 32768
CONFIG CPUDIV = OSC1 ;DIVIDE BY 1 MODE
CONFIG FOSC = HSPLL
CONFIG MSSPMSK = MSK5 ;add
#endconfig
DEFINE OSC 48
clear
OSCTUNE.6 = 1 ' Enable PLL for 18F87J50 family
;--- 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 "Darrel Taylor"
DEFINE USB_PRODUCTNAME "DT_HID"
DEFINE USB_SERIAL "001"
DEFINE USB_INSIZE 32 ; IN report is PIC to PC (8,16,32,64)
DEFINE USB_OUTSIZE 16 ; OUT report is PC to PIC
DEFINE USB_POLLIN 10 ; Polling times in mS, MIN=1 MAX=10
DEFINE USB_POLLOUT 10
; --- Each USB LED is optional, comment them if not used ----
;DEFINE USB_LEDPOLARITY 1 ; LED ON State [0 or 1] (default = 1)
;DEFINE USB_PLUGGEDLED PORTB,0 ; LED indicates if USB is connected
;DEFINE USB_TXLED PORTC,2 ; " " data being sent to PC
;DEFINE USB_RXLED PORTC,1 ; " " data being received from PC
OutCount VAR WORD : OutCount = 0
Value VAR WORD
X VAR WORD
;--- Setup ADC -------------------------------------------------------------
DEFINE ADC_BITS 10 ; Number of bits in ADCIN result
' Configure ADC
' =============
' See thread http://www.picbasic.co.uk/forum/showthread.php?t=11128&highlight=18f67j50
' for more information about setting analog, and the WDTCON.4 on this chip
WDTCON.4 = 1 ' Switch to Alternate SFR
ANCON0_ALT = %10011110
' -xx------ Not Implemented
' 1--1111-- AN7, & AN<4:1> = Digital
' -------0- AN0 = Analog
ANCON1_ALT = %11111111
' 11111111- AN<15:10> = Digital
WDTCON.4 = 0 ' Back to Default SFR
ADCON0 = %00000001
' 0-------- VCFG1, Vref- 0 = AVss (1=AN2)
' -0------- VCFG2, Vref+ 0 = AVdd (1=AN3)
' --0000--- CHS<3:0>, Channel select -- 0
' ------0-- Go/Done, don't care for now
' -------1- ADON, Enable ADC
ADCON1 = %10000111
' 1-------- ADFM, Right justified result
' -0------- ADCAL, Normal A/D converter operation
' --000---- ACQT<2:0>, A/D Aquisition time = 0 Tad
' -----111- ADCS<2:0>, A/D conversion clock = FRC
;--- The Main Loop ---------------------------------------------------------
TRISD = 0
PORTD.0 = 1 'turn on g sensor
PORTD.1 = 0 'select scale for g sensor
PORTD.2 = 0 'select scale for g sensor
TRISA = %00000111 'set porta.0, 1, and 2 as inputs
TRISB = 0
CM1CON1 = 7 'CM1CON in PBP3 as CM1CON1
CM2CON1 = 7 'CM2CON in PBP3 as CM2CON1
TRISC = 0
TRISE = 0
pause 100
;--- The Main Loop ---------------------------------------------------------
Main:
FOR X = 0 to 1000 ; Check for incomming USB data while pausing
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1
NEXT X
ADCIN 10, Value
OutCount = OutCount + 1
ARRAYWRITE USBTXBuffer,["AN0=",DEC Value," -",DEC OutCount," "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected
GOTO Main
;---- This just sends the received packet back to the PC -------------------
HandleRX:
ARRAYWRITE USBTXBuffer,[STR USBRXBuffer\USBBufferSizeRX]
return
Bookmarks