Code:
'***************************************************************************
'* Name : KK 18F4550 C152.pbp *
'* Author : Robert Hedan *
'* Date : Apr 7 2023 *
'* Version : 1.0 *
'* Notes : MICROSOFT FLIGHT SIMULATOR 2023 *
'* Cessna 152 control panel (Asobo) *
'* Hardware : PIC 18F4550, 20mhz crystal *
'* : Lab X1 Experimental Board *
'* Oscillator Jumpers A = 1-2 *
'* B = 1-2 *
'* C = 2-3 *
'* : PICkit4 Programmer v? *
'* Software : PIC Basic Pro v3.0.8.4 *
'* : MicroCode Studio Plus v5.0.0.5 *
'* : MPASM v5.62 *
'* PIC mods : plastic USB connector hot-glued onto top of PIC *
'* : leads soldered directly to top of PIC pins *
'* : two 0.1uF ceramic caps in parallel across VUSB and VSS *
'* : USB VDD is NOT connected *
'* : USB GND is wired to PIC *
'* : D- wired to C4 (pin 23) *
'* : D+ wired to C5 (pin 24) *
'***************************************************************************
#CONFIG
CONFIG PLLDIV=5, CPUDIV=OSC1_PLL2, USBDIV=2, FOSC=HSPLL_HS
CONFIG PWRT=OFF, BOR=ON, BORV=1, VREGEN=ON, WDT=OFF
CONFIG CCP2MX=ON, PBADEN=OFF, LPT1OSC=OFF, MCLRE=ON
CONFIG STVREN=ON, LVP=OFF, ICPRT=OFF, XINST=OFF
#ENDCONFIG
DEFINE OSC 48
;================================== USB NOTES ==================================
; If you get an ERROR saying these registers are undefined?
; Uncomment the corresponding line in DT_HID260:
; UIE VAR BYTE EXT ; for PBP versions prior to 2.50
; UEIE VAR BYTE EXT
; UIR VAR BYTE EXT
; - DT
;-------------------------------------------------------------------------------
; Before you put the PIC to sleep, you'll need to put the USB module in suspend
; mode UCON.1, which can only be done reliably when unplugged.
;
; This disables the USB voltage regulator and stops the clock to the SIE, which
; will give the lowest current consumption.
;
; The USB routines are not setup to handle a resume.
; So your best bet it probably to connect the +5V from the USB cable to one of
; the INT pins with a pull-down resistor.
;
; When the cable gets plugged in, it will generate an interrupt, at which point
; you would execute an @ RESET to reset the processor and initialize the USB again.
; - DT
;-------------------------------------------------------------------------------
; Enable bits (IE), Interrupt Flags (IF) or Priority bits (IP).
; Don't "mess" with them. That includes GIE and PEIE or GIEL.
; - DT
;-------------------------------------------------------------------------------
; Put the Interrupt definitions before the HID stuff.
; - DT
;-------------------------------------------------------------------------------
; Both USB_INSIZE and USB_OUTSIZE can only be 8,16,32 or 64.
; If you want to send 10-bytes per report, it needs to be 16.
; - DT
;-------------------------------------------------------------------------------
; Each USB status LED is optional, comment them if not used.
; They can be assigned to any pin, and no further action is required.
; - DT
;===============================================================================
;--- Setup all digital pins-----------------------------------------------------
INCLUDE "ALLDIGITAL.pbp" ; Set all pins as digital
;--- Initialize ----------------------------------------------------------------
ADCON1 = %00001111 ; A/D Control
' Bit 7, 0 = n/a
' Bit 6, 0 = n/a
' Bit 5, Vref- Source
' 0 = VSS
' 1 = Vref- (AN2)
' Bit 4, Vref+ Source
' 0 = VSS
' 1 = Vref+ (AN3)
' Bit 3-0 1111 = All digital
' 1110 = All digital, AN0 analog
' 1101 = All digital, AN0 & AN1 analog
INTCON2 = %00000000
' Bit 7, 0 = enabled, Pull-up resistors
' Bit 6, 0 = falling edge, B0 interrupt
' Bit 5, 0 = falling edge, B1 interrupt
' Bit 4, 0 = falling edge, B2 interrupt
' Bit 3, 0 = n/a
' Bit 2, 0 = low priority, TMR0 interrupt
' Bit 1, 0 = n/a
' Bit 0, 0 = low priority, Port B on-change
;--- 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 "USBDemon1"
DEFINE USB_PRODUCTNAME "LabX1 Simple"
DEFINE USB_SERIAL "001"
DEFINE USB_INSIZE 16 ; IN report is PIC to PC (8, 16, 32, or 64)
DEFINE USB_OUTSIZE 8 ; OUT report is PC to PIC
DEFINE USB_POLLIN 1 ; Polling times in mS, MIN=1 MAX=10
DEFINE USB_POLLOUT 10
DEFINE USB_LEDPOLARITY 1 ; LED ON State [0 or 1] (default = 1)
DEFINE USB_PLUGGEDLED PORTD,0 ; LED indicates USB is connected
DEFINE USB_TXLED PORTD,1 ; " " data being sent to PC
DEFINE USB_RXLED PORTD,2 ; " " data being received from PC
' Using only 4 bit bus to control LCD
DEFINE LCD_DREG PORTD ' Set LCD data port
DEFINE LCD_DBIT 4 ' Set starting data bit
DEFINE LCD_RSREG PORTE ' Set LCD register select port
DEFINE LCD_RSBIT 0 ' Set LCD register select bit
DEFINE LCD_EREG PORTE ' Set LCD enable port
DEFINE LCD_EBIT 1 ' Set LCD enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 1000 ' Set command delay time in microseconds
DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds
TRISA = %00000000
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE = %00000000
LoopCount var byte
UsbReceivedData var byte
GOTO ProgramStart
HandleRX:
UsbReceivedData = 1 ' Set USB data received flag
RETURN
ProgramStart:
FOR LoopCount = 1 TO 8 ' clear USB receive buffer
USBRXBuffer[LoopCount] = 0
NEXT LoopCount
PAUSE 1500 ' wait for PIC and LCD to stabilize
LCDOUT $FE,1,"LabX1 18F4550 USB"
ProgramMain:
IF NOT Plugged THEN ' check if USB is plugged
LCDOUT $FE,$C0,"USB not plugged... "
WHILE NOT Plugged:WEND ' wait until USB is plugged
LCDOUT $FE,$C0," "
ENDIF
@ ON_USBRX_GOSUB _HandleRX ; check if USB data is received
IF UsbReceivedData = 1 THEN
REM Do want you want with data received from USB in this IF statement
LCDOUT $FE,$C0, _ ' display USB data received
bin8 USBRXBuffer[0], ":", bin8 USBRXBuffer[1]
ARRAYWRITE USBTXBuffer, _ ' echo USB data back to PC
[BIN8 USBRXBuffer[0], BIN8 USBRXBuffer[1]]
IF TX_READY THEN GOSUB DoUSBOut
UsbReceivedData = 0 ' Reset USB data received flag
ENDIF
REM Your main process goes here:
REM - check input buttons.
REM - turn LEDs ON/OFF.
REM - communicate with other PICs.
REM - send USB data to PC.
GOTO ProgramMain ' main loop
END
Bookmarks