Code:
'***************************************************************************
'* Name : USBRX_INT.PBP *
'* Author : Darrel Taylor *
'* Date : 8/17/2011 *
'* Version : 1.0 *
'* Target : 18F4550 *
'* Compiler : PBP3 *
'***************************************************************************
CLEAR
DEFINE OSC 48 ; Internal processor frequency
#CONFIG
CONFIG FOSC = HSPLL_HS
CONFIG PLLDIV = 5
CONFIG USBDIV = 2
CONFIG CPUDIV = OSC1_PLL2
CONFIG FCMEN = OFF, IESO = OFF, PWRT = ON, BOR = OFF
CONFIG VREGEN = ON, WDT = OFF, CCP2MX = OFF, PBADEN = OFF
CONFIG MCLRE = ON, STVREN = ON, LVP = OFF, XINST = OFF
#ENDCONFIG
DEFINE ADC_BITS 10 'Number of bits in ADCIN result
;---[Setup Interrupts]------------------------------------------------------
DEFINE USE_LOWPRIORITY 1 ; Use Low Priority Interrupts
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; Allows re-entry to PBP from ASM interrupts
INCLUDE "ReEnterPBP-18LP.bas" ; Include if using Low Pr. PBP INTS
ASM
#define USBRX_INT HLVD_INT ; use HLVD as false Low Priority INT
;----[High Priority Interrupts]---------------------------------------------
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USB_INT, USBRX_CHECK, ASM, no
INT_Handler USB_Handler
INT_Handler TMR1_INT, _TMR1_ISR, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
;----[Low Priority Interrupts]----------------------------------------------
INT_LIST_L macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USBRX_INT, USB_Receive, PBP, yes
endm
INT_CREATE_L ; Creates the Low Priority interrupt processor
INT_ENABLE USBRX_INT
ENDASM
;---[Setup USB]-------------------------------------------------------------
INCLUDE "DT_HID260.pbp"
DEFINE USB_VENDORID 6017
DEFINE USB_PRODUCTID 2000
DEFINE USB_VERSION 2
DEFINE USB_VENDORNAME "Darrel Taylor"
DEFINE USB_PRODUCTNAME "USBRX_INT"
DEFINE USB_SERIAL "001"
DEFINE USB_INSIZE 64 ; 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 PORTD,0 ; LED indicates if USB is connected
DEFINE USB_TXLED PORTD,1 ; " " data being sent to PC
DEFINE USB_RXLED PORTD,2 ; " " data being received from PC
ADval VAR WORD
;----[Initialize]-----------------------------------------------------------
ADCON1 = %1101 ; AN0 and AN1 Analog
ADCON2.7 = 1 ; Right justify A/D results
T1CON = $31 ; Prescaler = 8, TMR1ON
;----[Main Program Loop]----------------------------------------------------
Main:
PAUSE 200
;--Read AN0--
ADCIN 0, ADval
ARRAYWRITE USBTXBuffer,[6,0,"0 ",DEC ADval,0]
GOSUB DoUSBOut
;--Read AN1--
ADCIN 1, ADval
ARRAYWRITE USBTXBuffer,[6,0,"1 ",DEC ADval,0]
GOSUB DoUSBOut
GOTO Main
;----[USB - Handle received reports]----------------------------------------
; This is called from a low priority interrupt whenever a report is rcvd
HandleRX:
SELECT CASE USBRXBuffer(0)
CASE "1" : SOUND PORTC.2, [100, 50]
CASE "2" : SOUND PORTC.2, [120, 50]
CASE "3" : TOGGLE PORTD.3
CASE "4" : ; send string back to PC
ARRAYWRITE USBTXBuffer,[5," ",STR USBRXBuffer(1)\USBBufferSizeRX-1]
GOSUB DoUSBOut
END SELECT
PAUSE 100
RETURN
;----[Timer1 interrupt blinks an LED]--------------------------------------
TMR_DIV VAR BYTE
TMR1_ISR:
TMR_DIV = TMR_DIV + 1
if TMR_DIV = 12 THEN
TOGGLE PORTD.3
TMR_DIV = 0
ENDIF
@ INT_RETURN
;===[Generates false L.P. interrupt on USB received reports]=*===*===*===*=
ASM
USBRX_CHECK
btfss INTCON, GIEL ; if low priority ints are available
goto Check_Done
btfss _Plugged ; and USB is connected
goto Check_Done
btfss _RX_READY ; and report has been received
goto Check_Done
btfsc _RX_Rcvd ; and the RX buffer is released
goto Check_Done
bsf USBRX_INT ; set the false interrupt flag
Check_Done
INT_RETURN
;
USB_Receive
ON_USBRX_GOSUB _HandleRX
INT_RETURN
ENDASM
Bookmarks