PDA

View Full Version : USB Interface using PIC



Tissy
- 13th March 2005, 17:45
Has anyone used a PIC to develop a USB Interface. From what i have read the 16C745 which has a USB interface onboard is not flashable. Is there another alternative, preferably easily obtainable in the UK.

I would, at this stage, like to use a Windows GUI (Via VB) to switch outputs on the PIC to HIGH and LOW as selected.

Can anyone help in this quest.

Many thanks,

Steve

OXIMBIT
- 13th March 2005, 18:04
The PIC18F4550 has a USB2 interface, it's brilliant!! I can use it behave like a Mouse or use the USB interface as virtual serial port. There are other devices in the USB family as well.

NL2TTL
- 23rd April 2005, 10:49
Do you have any example of code, that this pic is show as a virtual com in windows?

OXIMBIT
- 23rd April 2005, 12:24
Heres some code, It should all convert to Pbpro really easly



'
' General purpose USB CDC test program.
'
' Transmits data from several different buffers.
' And displays the contents of the USBIN buffer on the LCD
'
' Uses transmit and receive detection using the CARRY flag (STATUS.0)
'
' Communicate via the serial terminal set to the com port that the USB connects too.
'
OPTIMISER_LEVEL = 2 ' Enable the optimiser at level 2
Device = 18F4550 ' Choose a device with on-board full speed USB
XTAL = 48 ' Set the oscillator speed to 48MHz (using a 20MHz crystal)

REMINDERS = OFF ' Disable all reminders

LCD_DTPIN = PORTD.4 ' LCD data port attached to PORTD
LCD_RSPIN = PORTE.0 ' LCD RS pin attached to PORTE.0
LCD_ENPIN = PORTE.1 ' LCD EN pin attached to PORTE.1
LCD_INTERFACE = 4 ' 4-bit Interface
LCD_LINES = 2 ' 2 line LCD
LCD_TYPE = ALPHANUMERIC ' Use an Hitachi alphanumeric LCD

USB_DESCRIPTOR = "CDCDESC.INC" ' Point to the CDC DESCRIPTOR file (located in the INC\USB_18 folder)

Dim PP0 As Byte SYSTEM ' USBPOLL status return
Dim VAR1 As Word ' General purpose variable
Dim ARRAY1[20] As Byte ' Used to hold some output characters
Dim OUT_BUFFER As String * 20 ' Used to hold some output characters
Dim IN_BUFFER As String * 20 ' Used to hold some input characters

Symbol CARRY_FLAG = STATUS.0 ' High if microcontroller does not have control over the DP buffer
Symbol TRNIF = UIR.3 ' Low if USB Busy
'------------------------------------------------------------------------
' The main program loop starts here

DelayMS 200 ' Wait for things to stabilise
ALL_DIGITAL = True ' Set PORTA and PORTD to digital mode
Clear ' Clear all RAM before we start
Cls ' Clear the LCD

Repeat ' \
USBPoll ' Wait for the USB interface to become attached
Until PP0 = %00000110 ' /

StrN ARRAY1 = " ARRAY BUFFER\n\r" ' Fill the array with NULL terminated characters
OUT_BUFFER = " STRING BUFFER\n\r" ' Fill the string with NULL terminated characters

VAR1 = 0 ' Reset the counting variable
While 1 = 1 ' Create an infinite loop
Repeat ' Wait for USB input
USBIn 3, IN_BUFFER, Auto ' Poll the USB and Receive some data from endpoint 3
Until CARRY_FLAG = 0 ' Keep looking until data is able to be received

Print At 1,1,"USB BUFFER"
Print At 2,1,IN_BUFFER ' Display the contents of the USB buffer on line 2 of the LCD
Clear IN_BUFFER ' Then clear the buffer for the next time

__USBOUT_BUFFER = " USB BUFFER\n\r" ' Place characters directly into the USB TX buffer string
'
' Transmit from the USB's internal TX buffer String. NULL terminated.
Repeat
USBOut 3, __USBOUT_BUFFER, Auto ' Poll the USB and Transmit the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else
' Transmit from a NULL terminated array
Repeat
USBOut 3, ARRAY1, Auto ' Poll the USB and Transmit the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else
' Transmit from a NULL terminated String
Repeat
USBOut 3, OUT_BUFFER, Auto ' Poll the USB and Transmit the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else
' Transmit from a NULL terminated code memory String
Repeat
USBOut 3, TEXT_STRING, Auto ' Poll the USB and Transmit the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else
' Transmit a NULL terminated quoted string of characters
Repeat
USBOut 3, " QUOTED STRING\n\r", Auto ' Poll the USB and Transmit the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else
' Transmit 12 characters from a code memory String
Repeat
USBOut 3, TEXT_STRING, 12 ' Poll the USB and Transmit only 12 characters of the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else

__USBOUT_BUFFER = "\n\rVAR1 = " + Str$(Dec,VAR1) + "\n\r" ' Convert VAR1 into a string
' Transmit from the USB's internal TX buffer String. NULL terminated.
Repeat
USBOut 3, __USBOUT_BUFFER, Auto ' Poll the USB and Transmit the buffer from endpoint 3
Until CARRY_FLAG = 0 ' Keep trying if the microcontroller does not have control over the buffer
Repeat : Until TRNIF = 1 ' Wait for completion before transmitting anything else
Inc VAR1
Wend ' Go wait for the next buffer input


TEXT_STRING: CData " CODE MEMORY BUFFER\n\r",0

NL2TTL
- 23rd April 2005, 16:21
oke thanx for the info i give it a try only i'm suppriced about how windows will detect this device

Tissy
- 24th April 2005, 20:31
Having now got my USB PICs for development (all of the 18F USB Series), are there any examples of how to switch the outputs of the PIC HIGH or LOW as driven by the PC.

I'm looking at an application where by the front end is developed using VB, but then the control is by the PIC. So VB sends a command to the PIC to make a desired PORT HIGH or LOW.

It seems fairly simple in its concept, but i can't find any examples of this on the WEB / Forums.

Any help / pointers would be appriciated.

Thanks.

Steve.

OXIMBIT
- 24th April 2005, 21:14
Aren't there any examples included with the compiler?

Tissy
- 25th April 2005, 17:54
Not as far as im aware, but i may be wrong. They just seem to be about complex data transfer opposed to simple switching commands.

Of course if you are aware of any, then please correct me if i'm wrong.

I'm using PicBasic Pro.

Many thanks,

Steve

OXIMBIT
- 25th April 2005, 18:29
The Compiler I'm using has many demos including one to Implement the functionality of a Jan Axelson demo which flashes Leds at the Pic end in response to inputs made at the PC. And I just remembered one where as I move my mouse pointer of a picture of a Prototype board the Led's light up on the real board.

I can only suggest you contact Melabs for more help.

Tissy
- 25th April 2005, 19:16
Sounds like what exactly after. Two questions then, what Compiler are you using and can you send these demos to me on email? I've sent you a PM with my email address.

Many thanks,

Steve

OXIMBIT
- 25th April 2005, 21:09
Steve I have sent you a PM with some example code.

Tim

NL2TTL
- 26th April 2005, 07:37
DOes any of you have a schematic of how to connect the PIC18F2455 to the usb?

NL2TTL
- 26th April 2005, 08:58
And does somebody have a example prog that is realy for pbp FOR A pic18f2455

OXIMBIT
- 26th April 2005, 09:10
Have you tried looking in the manual?

NL2TTL
- 26th April 2005, 09:14
Yes i tried i try to compile the example code:

USBCDC but can't get it work, i want to use a PIC2455.

This is the PBP code:

' USB sample program for PIC18F4550 CDC serial port emulation

' Compilation of this program requires that specific support files be
' available in the source directory. You may also need to modify the
' file USBDESC.ASM so that the proper descriptor files are included. For
' detailed information, see the file PBP\USB18\USB.TXT.

buffer Var Byte[16]
cnt Var Byte

LED Var PORTB.0


Define OSC 48


USBInit
Low LED ' LED off

' Wait for USB input
idleloop:
USBService ' Must service USB regularly
cnt = 16 ' Specify input buffer size
USBIn 3, buffer, cnt, idleloop

' Message received
Toggle LED

buffer[0] = "H"
buffer[1] = "e"
buffer[2] = "l"
buffer[3] = "l"
buffer[4] = "o"
buffer[5] = " "
buffer[6] = "W"
buffer[7] = "o"
buffer[8] = "r"
buffer[9] = "l"
buffer[10] = "d"
buffer[11] = 13
buffer[12] = 10
buffer[13] = 0

outloop:
USBService ' Must service USB regularly
USBOut 3, buffer, 14, outloop

Goto idleloop ' Wait for next buffer

NL2TTL
- 26th April 2005, 09:41
Getting further, the PBP goes compiling now only thany he asked for the MPASM compiler so i installed this compiler made the path settings.

Then he start the MPASM only it gives the error.

Error in parameter "18F4550"

NL2TTL
- 26th April 2005, 13:54
Another step closer,

Windows detect a USB device but an unknown, and installing the "PBPCDC"
drivers have no effect. sombody?

NL2TTL
- 27th April 2005, 13:13
Windows detects the USB PIC now,

only now i need a working description.asm file. Because the standard description (demo pbp files) don't set every setting in windows.

Like manufacture, product and more.

Tgq
- 27th April 2005, 17:45
Having now got my USB PICs for development (all of the 18F USB Series), are there any examples of how to switch the outputs of the PIC HIGH or LOW as driven by the PC.

look at this :)
http://www.alecmcnamara.freeserve.co.uk/piccalc/
you find HIDcomm component from Microchip for really easy VB development

robbenaz
- 22nd May 2006, 14:55
i face the error message

"ERROR: Macro USBINIT? not found in macro file"

how to solve this problem

soni

BigWumpus
- 22nd May 2006, 15:02
if you are german, look at http://www.sprut.de there are the basic for USB & PIC18Fxx55 !

mister_e
- 22nd May 2006, 16:04
i face the error message

"ERROR: Macro USBINIT? not found in macro file"

how to solve this problem

soni

Tell us wich PIC you're using and post your code here.