PDA

View Full Version : USBDemo, something to learn USB a little bit



Pages : 1 [2]

sjm2000
- 14th September 2011, 08:56
Hello

I use a PIC18F2550 for CDC serial port emulation based on cdc_demo.bas:

USBInit

For input and output I use:
inloop:
icnt = inbuff
USBService
USBIn 3, inbuffer, icnt, inloop

outloop:
USBService
ocnt = outbuff
USBOut 3, outbuffer, ocnt, outloop

and it works really fine.


At start PIC tests 5V from PC-USB-Port to wait with 'USBInit'. Sometimes PC is rebooting w/o switch off, but equipmend with the PIC interface still running. Is it possible to let the PIC routine know that Windows VSP is gone and the 'USBInit' has to be done again?

Thanks to everybody who can help

NatureTech
- 4th October 2011, 09:03
Hello All,

I'm new in USB.Althought i have tried all the steps necessary steps in order to compile the USB CDC source,i'm not succeeding in doing so.The errors are this :

6032

What could be the problem?

Waiting for help

Regards.

Darrel Taylor
- 4th October 2011, 14:19
What could be the problem?
It could be that you are using PBP version 2.46 - 2.50C with MPLAB greater than 8.15a

Either upgrade PBP, or downgrade MPLAB.

If you have upgraded PBP, use the new USB files from the USB18 folder.

Demon
- 19th January 2012, 20:08
I'm able to use Steve's demo along with Darrel's include, but it only works when the MeLabs U2 USB programmer is connected. If I unplug the programmer, the 18F4550 is no longer recognized:



'************************************************* **************************
'* Name : USBLabX1_RH.pbp *
'* Author : Steve Monfette/Darrel Taylor/Demon *
'* Date : Jan 11 2012 *
'* Version : 3.0 *
'* Notes : This is a re-creation of mister-e's USBdemo for DT_HID *
'* : Meant to work with mister-e's GUI *
'* : Modified for Lab X1 *
'* Hardware : PIC 18F4550, 20mhz crystal *
'* : Lab X1 Experimental Board, 9.5V wall adapter *
'* : MeLabs U2 Programmer v4.32 *
'* Software : PIC Basic Pro v2.60C *
'* : MicroCode Studio Plus v2.2.1.1 *
'* : MPASM WIN Assembler v4.02 (mplab tools/MPASM Suite) *
'* PIC mods : plastic USB connector hot-glued onto top of PIC *
'* : D- wired to C4 (pin 23) directly to top of pins *
'* : D+ wired to C5 (pin 24) directly to top of pins *
'* : two 0.1uF ceramic caps in parallel across VUSB and VSS *
'************************************************* **************************
'--- if you use these, you must comment the ones in the .inc file ---
@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_46_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H
@ __CONFIG _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L

DEFINE OSC 48

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 2000 ' Set command delay time in microseconds
DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds

;--- Setup Interrupts ------------------------------------------------------
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; PBP Re-entry for external interrupt

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USB_Handler
INT_Handler INT_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
endasm

@ INT_ENABLE INT_INT ; enable external (INT) interrupts

;--- Setup USB -------------------------------------------------------------
INCLUDE "DT_HID260.pbp"

DEFINE USB_VENDORID 6017
DEFINE USB_PRODUCTID 2000
DEFINE USB_VERSION 1
DEFINE USB_VENDORNAME "Mr E/DT/RH"
DEFINE USB_PRODUCTNAME "USBLabX1"
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 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

;--- Setup ADC -------------------------------------------------------------
DEFINE ADC_BITS 8 ; Number of bits in ADCIN result

TRISA = %00000011 ' Set port A pins to 6 output, 2 input
TRISB = %00001111 ' Set port B pins to 4 output, 4 input
' B0 = External Interrupt
TRISC = %00000000 ' Set all port C pins to output
TRISD = %00000000 ' Set all port D pins to output
TRISE = %00000000 ' Set all port E pins to output

;--- Variables -------------------------------------------------------------
Value0 VAR WORD : Value0 = 0
Value1 VAR WORD : Value1 = 0
DUTY1 VAR WORD
DUTY2 VAR WORD
LED1 VAR PORTD.3
WS_CHAR VAR BYTE
WS_BYTE VAR BYTE
WS_LOOPW VAR WORD
;--- Initialize ------------------------------------------------------------
ADCON2.7 = 0 ; left justify (Change this if ADFM in diff register)
ADCON1 = %00001101 ; AN0 & AN1 analog
INTCON2 = %00000000 ; Pull-up resistors, falling edge

PORTB = %00000000 ; Set output on keypad to prevent floating pins


PAUSE 2000 ' Wait for USB to initalize
LCDOUT $FE,1,"LabX1 18F4550 USB"
PORTD = 0 ' Clear LEDs from LCD operations
ARRAYWRITE USBTXBuffer, ["Lab X1 "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected

;--- The Main Loop ---------------------------------------------------------
Main:
FOR WS_LOOPW = 0 to 1000 ; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1
NEXT WS_LOOPW

ADCIN 0, Value0 ; Send A/D about once/sec
ADCIN 1, Value1
ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTD.3, 0, 0, 0, 0, 0]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected

GOTO Main
end

;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
HandleRX:
ARRAYREAD USBRXBuffer,[WS_BYTE, DUTY1.LowByte, DUTY1.HighByte, _
DUTY2.LowByte, DUTY2.HighByte]
LCDOUT $FE,$C0,BIN WS_BYTE
PORTD = 0 ' Clear LEDs from LCD operations
return

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED1
ARRAYWRITE USBTXBuffer, ["Button "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected
PORTB = 0 ' Set output on keypad to prevent floating pins
@ INT_RETURN


On the Lab X1:
- PWM logic is not active.
- buttons 1, 2, 3 and 4 on the keypad activate Port A2 LED.
- AN 0 and 1 work as expected.

On the PC:
- Port B is displayed on the LCD.

Any idea why Windows XP Home would need the USB programmer connected in order to recognize the PIC?

Everything works fine if I reconnect the programmer.

Robert

Darrel Taylor
- 19th January 2012, 20:25
Robert,

Check the ground from your USB connector.

Plugging in the programmer will give a common ground with the computer.
If there is no ground from your USB connector, it will disconnect when you unplug the U2.

Demon
- 19th January 2012, 21:55
Yup, that fixed that problem.

But if I leave the programmer connected, it garbles a few characters on the LCD?

Cleaned up code (replaced PAUSE for LOOP):



'************************************************* **************************
'* Name : USBLabX1_RH.pbp *
'* Author : Steve Monfette/Darrel Taylor/Demon *
'* Date : Jan 11 2012 *
'* Version : 3.0 *
'* Notes : This is a re-creation of mister-e's USBdemo for DT_HID *
'* : Meant to work with mister-e's GUI *
'* : Modified for Lab X1 *
'* Hardware : PIC 18F4550, 20mhz crystal *
'* : Lab X1 Experimental Board, 9.5V wall adapter *
'* : MeLabs U2 Programmer v4.32 *
'* Software : PIC Basic Pro v2.60C *
'* : MicroCode Studio Plus v2.2.1.1 *
'* : MPASM WIN Assembler v4.02 (mplab tools/MPASM Suite) *
'* PIC mods : plastic USB connector hot-glued onto top of PIC *
'* : D- wired to C4 (pin 23) directly to top of pins *
'* : D+ wired to C5 (pin 24) directly to top of pins *
'* : two 0.1uF ceramic caps in parallel across VUSB and VSS *
'************************************************* **************************
'--- if you use these, you must comment the ones in the .inc file ---
@ __CONFIG _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@ __CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H
@ __CONFIG _CONFIG3H, _CCP2MX_OFF_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L

DEFINE OSC 48
CLEAR

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 2000 ' Set command delay time in microseconds
DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds

;--- Setup Interrupts ------------------------------------------------------
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; PBP Re-entry for external interrupt

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler USB_Handler
INT_Handler INT_INT, _ToggleLED1, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
endasm

@ INT_ENABLE INT_INT ; enable external (INT) interrupts

;--- Setup USB -------------------------------------------------------------
INCLUDE "DT_HID260.pbp"

DEFINE USB_VENDORID 6017
DEFINE USB_PRODUCTID 2000
DEFINE USB_VERSION 1
DEFINE USB_VENDORNAME "Mr E/DT/RH"
DEFINE USB_PRODUCTNAME "USBLabX1"
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 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

;--- Setup ADC -------------------------------------------------------------
DEFINE ADC_BITS 8 ; Number of bits in ADCIN result

TRISA = %00000011 ' Set port A pins to 6 output, 2 input
TRISB = %00001111 ' Set port B pins to 4 output, 4 input
' B0 = External Interrupt
TRISC = %00000000 ' Set all port C pins to output
TRISD = %00000000 ' Set all port D pins to output
TRISE = %00000000 ' Set all port E pins to output

;--- Variables -------------------------------------------------------------
Value0 VAR WORD : Value0 = 0
Value1 VAR WORD : Value1 = 0
DUTY1 VAR WORD
DUTY2 VAR WORD
LED1 VAR PORTD.3
WS_CHAR VAR BYTE
WS_BYTE VAR BYTE
WS_LOOPW VAR WORD
;--- Initialize ------------------------------------------------------------
ADCON2.7 = 0 ; left justify (Change this if ADFM in diff register)
ADCON1 = %00001101 ; AN0 & AN1 analog
INTCON2 = %00000000 ; Pull-up resistors, falling edge

PORTB = %00000000 ; Set output on keypad to prevent floating pins

LCDOUT $FE,1 ' Initiate first command to LCD
PORTD = 0 ' Clear LEDs from LCD operations

FOR WS_LOOPW = 0 to 1000 ' Wait for LCD to initalize
PAUSE 1
NEXT WS_LOOPW
LCDOUT $FE,1,"LabX1 18F4550 USB"
PORTD = 0 ' Clear LEDs from LCD operations

FOR WS_LOOPW = 0 to 1000 ' Wait for USB to initalize
PAUSE 1
NEXT WS_LOOPW
ARRAYWRITE USBTXBuffer, ["Lab X1 "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected

;--- The Main Loop ---------------------------------------------------------
Main:
FOR WS_LOOPW = 0 to 1000 ; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1
NEXT WS_LOOPW

ADCIN 0, Value0 ; Send A/D about once/sec
ADCIN 1, Value1
ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTD.3, 0, 0, 0, 0, 0]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected

GOTO Main
end

;---- Receive incoming data PORTB LEDS and CCP PWM dutycycle ---------------
HandleRX:
ARRAYREAD USBRXBuffer,[WS_BYTE, DUTY1.LowByte, DUTY1.HighByte, _
DUTY2.LowByte, DUTY2.HighByte]
LCDOUT $FE,$C0,BIN WS_BYTE
PORTD = 0 ' Clear LEDs from LCD operations
return

'---[INT - interrupt handler]---------------------------------------------------
ToggleLED1:
TOGGLE LED1
ARRAYWRITE USBTXBuffer, ["Button "]
IF Plugged THEN GOSUB DoUSBOut ; only send when USB is connected
PORTB = 0 ' Set output on keypad to prevent floating pins
@ INT_RETURN


Sharing the USB NEG only seems to affect the LCD. If I unplug the programmer, Port B updates properly on the LCD, everything runs fine.

Uh oh, just realized I might have misunderstood. There's also a GROUND eyelet coming from the USB cable, is that the ground you were referring to? If so, would that go to VSS instead of the NEG?

Darrel Taylor
- 20th January 2012, 17:36
Pin 4 of the USB connector is Ground.
It and the Shield should be connected to VSS on the LAB-X1.

Have you removed the MAX232 chip?
You've set the RX pin to output, which is likely to cause high currents if the MAX232 is still there.

Demon
- 21st January 2012, 04:40
Pin 4 of the USB connector is Ground.
It and the Shield should be connected to VSS on the LAB-X1.
...

Tried both USB pin 4 (black wire) and the eyelet from the shielding connected to VSS, same garbles on LCD.

I used one of the VSS pins on the PIC, does it have to be a special VSS contact on the Lab X1?



...
Have you removed the MAX232 chip?
You've set the RX pin to output, which is likely to cause high currents if the MAX232 is still there.

I didn't remove the MAX232, but I installed headers on my Lab X1. The jumpers for RX and TX pins are disabled, same with VUSB pin.


EDIT: I didn't have the recommended capacitors so I used two 0.1uF ceramic caps in parallel across VUSB and VSS. Could that be a problem?

Charlie
- 11th February 2012, 14:36
I'm just getting started with USB. I've gone through the CDC demo and created my own variants with success (after a bit of teething pain) and now I'm trying to move on to HID. I've found the DT_HID260.zip file and I've been playing with the contents, but I'm not having any luck getting DT_HID260.pbp, DT_INTS-18.bas, ReEnterPBP-18.bas and my code to work together. I'm so close.... I think, but right now I can't even compile.

I've got 2 errors that have me completely stumped:
Illegal opcode (USB_INT)
Address label duplicated or different in second pass (INT_RETURN)

Any clues where to look? Many thanks...

Darrel Taylor
- 11th February 2012, 18:47
It sounds like you would also be getting an error that says it can't open include file DT_INTS-18.bas.

Did you put it in your PBP folder?

Charlie
- 11th February 2012, 19:30
Actually I just resolved it. It seems the line to include DT_HID260.pbp must come AFTER the INT_CREATE. I moved the include from the top of the file to this location and the errors went away.

Compiles no errors means it will work fine right? Well not exactly, but I'll chew on "why" by myself for a bit.

Thanks for the help!

Charlie
- 13th February 2012, 11:34
Well I though I'd leverage Darrel's heavy lifting and put the USB code (including the interrupt structure) together with the SD card code and make myself basically an overly complicated and expensive thumb drive, purely for my education. It seems, however, that the includes consume more than the 2K space for variables, and after about an hour of trying to make things less generic by eliminating variables wherever possible in favor of fixed values, I still don't have enough space. I'm also not sure if I'm even getting close - is there any way to have the compiler tell you how much you are over by, rather than simply "unable to fit"?

Demon
- 13th February 2012, 16:18
Charlie,

I had a similar problem once; so I copied the Includes in the code and removed all the variables I didn't need. That worked in my particular situation.

Robert

JBKerner
- 20th March 2012, 18:05
I'm just getting my feet wet with USB. I currently have PBP (2.60C). I'd like to get some recommendations, before I get too far into this project:

1) Is there any benefit to upgrading to PBP 3.0, specifically for my USB experiments?
2) I have very little experience with VB6, but some experience with VBA (in Excel). Back on post 248, Steve said, "...VB6 being a dead duck for ages now...". I _have_ VB6, but was wondering if there was a more current alternative (other than going out and trying to learn a completely new language, like C)

Any tips would be greately appreciated!

Jeff

gadelhas
- 20th March 2012, 18:33
[QUOTE=JBKerner;112633Steve said, "...VB6 being a dead duck for ages now...". I _have_ VB6, but was wondering if there was a more current alternative (other than going out and trying to learn a completely new language, like C)
[/QUOTE]

VB.NET;

http://www.picbasic.co.uk/forum/content.php?r=376-PIC-USB-to-VB.NET

Demon
- 21st March 2012, 02:49
Jeff, read this thread:
http://www.picbasic.co.uk/forum/showthread.php?t=16234

It's exactly about this topic.

Robert

hyilmaz
- 18th November 2012, 22:10
hi,

i use USBDemo.HEX file. i send it to my PIC using USB PIC programmer (not boot loader). i use;

18f4550
4 MHZ crystal (with 22pF capacitors)
3 parallel 100nF (not polarized) capacitors on pin18
10k resistor on mclr
100nF capacitor between VCC and GND

my pc doesn't recognise the device. nothing happens when i connect PIC to my pc

what is wrong. i couldn't work it. i am workin on it for months and i am going crazy.:mad:

thanks

UnaKRon
- 19th November 2012, 14:48
...well looking back it appears you can use a 4mhz. I used a 20mhz crystal when I was able to get it working.

Do you have a schematic.

hyilmaz
- 19th November 2012, 18:41
6735

this is my scheme. i tried it using 20MHZ and 15pF caps. what is wrong with it?

UnaKRon
- 19th November 2012, 19:44
First thing I do when I'm having trouble is either hook up some Status LEDs or a Serial output to debug the pic. I've had trouble with a program or two..and more than once something was causing a reset. Do you have a compiler so you can add in some debugging code?

I believe i did 22pf with my 20mhz...which is more than required by the datasheet. :/ I'm not seeing anything right off :/
Have you verified the usb cable is providing power and works with another device?

hyilmaz
- 19th November 2012, 22:41
i use pic programmer's usb cable. cable works. can anyone send me a simple pcb layout of a working usb project and hex file. i want to improve myself about pic and usb but i couldn't get result for weeks. thanks

mister_e
- 20th November 2012, 00:12
How do you dump the .hex file into your PIC, i mean how do you program your PIC?

Are you sure your device programmer software program the configuration fuses properly?

Those .HEX files providen here are there since 6 years or so, they're working.

dhouston
- 20th November 2012, 01:00
Could this be adapted to http://www.sureelectronics.net/goods.php?id=1140

Dwayne
- 20th November 2012, 16:30
Hello Steve and Darrel,

You mangy old farts, how are you all doing? Looks like all as been well for both of you. Its been a while since we have chatted.

PIC's working ok? Haven't done any programming for a number of years on those chips. I probably should get back into them.

Dwayne

mister_e
- 22nd November 2012, 01:34
So cool to see you back here Dwayne :)

As long as I wake up, it's a good day ;)

HenrikOlsson
- 22nd November 2012, 06:11
Could this be adapted to http://www.sureelectronics.net/goods.php?id=1140
Dave,
That board is based on a PIC24 device. PBP support the 10, 12, 16 and 18 series of PICs - so the answer is no.
Rumour has it that there's a PBP compiler for PIC24 under development though so perhaps some time in the future.

/Henrik.

dhouston
- 22nd November 2012, 11:43
That board is based on a PIC24 device. PBP support the 10, 12, 16 and 18 series of PICs - so the answer is no.


Thanks, Henrik.

hyilmaz
- 10th March 2013, 13:48
DataToSend's array size is 8. i want to make it 16. i mean i want to send 16 variable at the same time. not 8. can anybody help me?

Demon
- 1st May 2014, 00:38
...
Also note: This is for PBP 2.60 only ...

Someone found a workaround for PBP 3:

http://www.picbasic.co.uk/forum/showthread.php?t=19176&p=126599#post126599

Robert