Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4746

18F45K50 usb problem

$
0
0
hello everybody,

I try to use PIC18F45K50 with internal usb module for communicate with my PC.
I don't use a external crystal but only the internal clock (may be my mistake?)

If I use only USBService in my loop program, my HID device works well
but if I include USBOut, my program crash, and the PC lost the HID device

may be somebody can help me to find my mistake

Code:

#CONFIG
    CONFIG PLLSEL = PLL3X            ;3x clock multiplier
    CONFIG CFGPLLEN = ON            ;PLL Enabled
    CONFIG CPUDIV = NOCLKDIV            ;CPU uses system clock (no divide)
    CONFIG LS48MHZ = SYS48X8        ;System clock at 48 MHz, USB clock divider is set to 8
    CONFIG FOSC = INTOSCIO            ;Internal oscillator
    CONFIG PCLKEN = OFF            ;Primary oscillator shutdown firmware controlled
    CONFIG FCMEN = OFF            ;Fail-Safe Clock Monitor disabled
    CONFIG IESO = OFF            ;Oscillator Switchover mode disabled
    CONFIG nPWRTEN = ON            ;Power up timer enabled
    CONFIG BOREN = ON            ;BOR controlled by firmware (SBOREN is enabled)
    CONFIG BORV = 190            ;BOR set to 1.9V nominal
    CONFIG nLPBOR = OFF            ;Low-Power Brown-out Reset disabled
    CONFIG WDTEN = ON            ;WDT enabled in hardware (SWDTEN ignored)
    CONFIG WDTPS = 512            ;1:512
    CONFIG CCP2MX = RC1            ;CCP2 input/output is multiplexed with RC1
    CONFIG PBADEN = OFF            ;PORTB<5:0> pins are configured as digital I/O on Reset
    CONFIG T3CMX = RC0            ;T3CKI function is on RC0
    CONFIG SDOMX = RB3            ;SDO function is on RB3
    CONFIG MCLRE = ON            ;MCLR pin enabled; RE3 input disabled
    CONFIG STVREN = ON            ;Stack full/underflow will cause Reset
    CONFIG LVP = OFF            ;Single-Supply ICSP disabled
    CONFIG XINST = OFF            ;Instruction set extension and Indexed Addressing mode disabled
#ENDCONFIG

DEFINE OSC          48

USBBufferSizeMax            con 16      ' maximum buffer size
USBBufferSizeTX            con 16      ' PIC -> PC
USBBufferSizeRX            con 16      ' PC -> PIC

USBBuffer        Var Byte[USBBufferSizeMax]
USBBufferCount  Var Byte

' include the HID descriptor
include        "DESCUSBProject.bas"

OSCCON  = %01110000    'OSCint = 16Mhz
OSCCON2 = %00010000    'PLL enable

TRISB  = %11100001
ANSELB  = %00000000    ' port B mode Digital

TRISC  = %10111111
ANSELC  = %00000000    ' port C mode digital

TRISD  = %00001111
ANSELD  = %00000000    ' port D mode digital

TRISE  = %11111111
ANSELE  = %00000000    ' port E mode digital

clear

usbinit ' initialise USB...
gosub DoUSBIn      'empty buffer

hserout ["reset",13]

ProgramStart:
    clearwdt
    'gosub DoUSBIn

    USBBuffer[1] = 5
    gosub DoUSBOut
   
    goto ProgramStart 
   
' ************************************************************
' * receive data from the USB bus                            *
' ************************************************************
DoUSBIn:
  USBBufferCount = USBBufferSizeRX              ' RX buffer size
  USBService                                    ' keep connection alive
  USBIn 1, USBBuffer, USBBufferCount, DoUSBIn  ' read data, if available
  return
   
' ************************************************************
' * wait for USB interface to attach                        *
' ************************************************************
DoUSBOut:
  USBBufferCount = USBBufferSizeTX              ' TX buffer size
  USBService                                    ' keep connection alive
  USBOut 1, USBBuffer, USBBufferCount, DoUSBOut ' if bus available, transmit data
  return


Viewing all articles
Browse latest Browse all 4746

Trending Articles