PDA

View Full Version : USB on 18F27J53. Closer but still struggling with this.



kenif
- 6th August 2011, 20:19
I have an 18F27J53 with 48MHz crystal, HSPLL Div by 12 PLL. The PIC runs other functions as normal. The D+ and D- lines both have short pulses on so the hardware looks good. Tried pulling up D+ and D- (for low and high speed).

Tried many other clock/divider combinations, this approach at least gets on the System Devices list.

18F27J53 is not in the EasyHID Wizard, so I ran it for an 18F4550 and copied the USB line from the .BAS. None of the examples I can find use the newer J devices.

Windows finds the client, but it doesn't like it and lists it as 'Unknown Device'.

Any help or examples appreciated.

mackrackit
- 6th August 2011, 21:09
Give this a try, no guarantees.


CONFIG XINST = OFF
CONFIG WDTPS = 512
CONFIG OSC = HS
CPUDIV = OSC1
CONFIG ADCSEL = BIT12

Being you are using a 48MHz crystal and USB requires 48MHz...
CONFIG OSC = HS Will disable the PLL.
CPUDIV = OSC1 No DIV.

kenif
- 8th August 2011, 04:46
Thanks for coming back.
Tried this with same result. (Previously had HSPLL)
Any more ideas?

LIST
LIST p = 18F27J53, r = dec, w = -311, f = inhx32
INCLUDE "P18F27J53.INC" ; MPASM Header
CONFIG XINST = OFF
CONFIG WDTPS = 512
CONFIG OSC = HS
CPUDIV = OSC1
CONFIG ADCSEL = BIT12
NOLIST

mackrackit
- 8th August 2011, 07:58
Post the whole code and schematic. Maybe then...

ScaleRobotics
- 8th August 2011, 16:00
According to table 3-5 (page 41) of the data sheet, with a 48 mhz crystal, only EC or ECPLL can be used.

mackrackit
- 9th August 2011, 06:47
I saw that, but then I looked at table 3-1 and figured HS would be the way to go...
Maybe it will not work with a 48 MHz crystal?
I am confused again :)

mister_e
- 9th August 2011, 09:47
Microchip do not specify a range for HS clock BUT one thing i'm sure... EC/ECPLL will not work, those mode are when you're using external clock module, not crystal

3.2.3 EXTERNAL CLOCK INPUT
The EC and ECPLL Oscillator modes require an
external clock source to be connected to the OSC1 pin.
There is no oscillator start-up time required after a
Power-on Reset (POR) or after an exit from Sleep
mode.
In the EC Oscillator mode, the oscillator frequency
divided by 4, is available on the OSC2 pin. In the
ECPLL Oscillator mode, the PLL output, divided by 4,
is available on the OSC2 pin. This signal may be used
for test purposes or to synchronize other logic.
Figure 3-3 displays the pin connections for the EC
Oscillator mode.HS mode, CPUDIV set to 1:1, no PLL thing...and away you go.

If this is mounted on a solderless board... I would get rid of the ceramic capacitor around the crystal for testing purpose.

However, there's no advantage to use a 48MHz clock.

ScaleRobotics
- 9th August 2011, 16:18
Oops, thanks Steve and Dave.

It looks to me that 4, 8, 12, and 16 mhz crystals can be used, but they show no examples of higher crystal operation. Unless I am wrong again, it looks to me like 16 mhz is max for this. I have a 47j53 running on a 12 mhz crystal, and I have never seen an example of them running higher than 16.

mister_e
- 9th August 2011, 17:47
aaaaaaaaaaaaaaaaahhhhhhhhhhhhhhhhhhhhhhhhaaaaaaaaa a (refer to this video for the real voice http://www.youtube.com/watch?v=d1ABQuctDOQ ) seems you're right! Datasheet pdf page 535, table 31-10, parameter 1A

So 48MHz crystal will not work. End of the story ;)

kenif
- 10th August 2011, 00:39
It currently has a 48M crystal (as HS and HSPLL) and it's working great - driving LCD.
Tried 4M and 20M.
Don't have 8, 12, 16 ~ time to expand my clock selection.
Schematic and code when I get a break.

kenif
- 10th August 2011, 06:43
Tried 4MHz again with same results: Everything else OK but no USB.

Schematic ~ PIC is wired: 4M on OSC; VUSB cap to gnd; USB on D+, D-; R/C on MCLR.
Plus LCD (forget which pins) which works great.
Oh, plus debug on pin13, works too.

Tried with and without VB GUI running.
Code is untouched from EasyHID Wizard (because there is sign of a helpful 'Put something here'), although I have changed OSC speed:

' ************************************************** **********
' * Auto generated EasyHID file. PBP 2.60 and above *
' ************************************************** **********

' include the HID descriptor
include "DESCUSBProject.bas"

DEFINE OSC 4
DEFINE LOADER_USED 1

USBBufferSizeMax con 8 ' maximum buffer size
USBBufferSizeTX con 8 ' input
USBBufferSizeRX con 8 ' output

' the USB buffer...
USBBuffer Var Byte[USBBufferSizeMax]
USBBufferCount Var Byte

' ************************************************** **********
' * main program loop - remember, you must keep the USB *
' * connection alive with a call to USBService every couple *
' * of milliseconds or so... *
' ************************************************** **********
usbinit ' initialise USB...
ProgramStart:
gosub DoUSBIn
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

Windows responds: 'No driver files required or have been loaded for this device'
http://thundernerd.com/pics/Unknown_USB.jpg

ScaleRobotics
- 10th August 2011, 08:19
A couple of problems here. USB can't run at 4 mhz. You'll need the PLL to pull it up to 48 mhz. If you are, then you define your OSC as 4 mhz, well, that's a problem too.

The HID include file was made for the 4550 and such. I suspect that it can't be used for the 47J53. Better off with dt_hid260 I think, or CDC.


If you are using PBP 2.60, what are your configs in the .inc files?

Also, do you have the capacitors required in page 31 of the data sheet?

ScaleRobotics
- 10th August 2011, 10:46
Here's one that enumerates, and doesn't need a crystal. I can't get it to work with 2.60 and I don't know why. But it does work on PBP 3.0. This is for a 18F47J53, and I only quickly changed the registers to match, so it may need some work for your 27J53, but hopefully it will get you started. You will need Darrel's DT_HID260 from here: http://www.picbasic.co.uk/forum/showthread.php?t=5418&p=80434#post80434

And you will need DT_INTS-18.bas which you can get from here: http://darreltaylor.com/DT_INTS-18/home.html

And you will need the executable or the Visual Basic interface: http://www.picbasic.co.uk/forum/showthread.php?t=5418&p=30334#post30334



'************************************************* **************************
'* 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

kenif
- 25th August 2011, 23:25
Magic, thank you. Upgraded to PBP3 and it all works on 18F27J53.
CCP1/2 had to go to CCP4/5/6, and now we have three PWMs over USB (which was the object of using this device).

As getting USB working on 18F27J53 involves collecting various files and then some tweaking I'll convert the USB demo for this PIC and put them in one place.
(One day soon... )

Thanks again for all your help.