PDA

View Full Version : PIC USB to VB.NET



gadelhas
- 31st May 2011, 00:21
Hi everyone;
Some time ago, i search this forum for a program in VB.net to comunicate with the USB program that mister_E and Darrel posted in this Thread ( http://www.picbasic.co.uk/forum/showthread.php?t=5418&p=80434#post80434 ), however i could not found any source code in VB.Net, only in VB6 made by mister_e, so i decided to do it myself.

I used the PBP code from Darrel Tayler, and converted the mister_e VB6 program to VB.Net.

Please note that all the credits should go to Darrel Tayler and mister_e, i just convert the program to VB.Net, nothing more!

Here is the PIC16F4550 code;


'************************************************* **************************
'* Name : USB.pbp *
'* Author : Darrel Taylor, changed by Gadelhas *
'* Date : 23-01-2011 *
'* Version : 1.0 *
'* Notes : This is a re-creation of mister-e's USBdemo for DT_HID *
'* : Meant to work with mister-e's GUI *
'************************************************* **************************
;--- if you un-comment these, you must comment the ones in the .inc file ---
ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_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_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM
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 "Hugo Oliveira"
DEFINE USB_PRODUCTNAME "Demo USB"
DEFINE USB_SERIAL "001Hugo"
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
TRISB = 0
OUTPUT PORTC.1
OUTPUT PORTC.2

ADCON2.7 = 0 ; left justify (Change this if ADFM in diff register)
ADCON1 = %1101 ; 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, ["HugoDem1"]
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


Attached is the code for PIC18F4550
I the next post is the VB.net Program
Hope it can help someone!

gadelhas
- 31st May 2011, 00:24
Here is the VB Program;

The image of the program;

5588
And here is the code;



PublicClass Form1
' vendor and product IDs
PrivateConst VendorID AsShort = 6017 'Replace with your device's
PrivateConst ProductID AsShort = 2000 'product and vendor IDs

' read and write buffers
PrivateConst BufferInSize AsShort = 8 'Size of the data buffer coming IN to the PC
PrivateConst BufferOutSize AsShort = 8 'Size of the data buffer going OUT from the PC
Dim BufferIn(BufferInSize) AsByte'Received data will be stored here - the first byte in the array is unused
Dim BufferOut(BufferOutSize) AsByte'Transmitted data is stored here - the first item in the array must be 0
Dim PORTB(8) AsInteger
' ************************************************** **************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'************************************************* ****************
PrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
' do not remove!
ConnectToHID(Me)
Pictureoff.Visible = True
Pictureon.Visible = False
TrackBar1.Enabled = False
TrackBar2.Enabled = False
Label8.Text = "AN0"
Label8.Text = "AN1"
Label10.Text = "CCP1"
Label11.Text = "CCP2"
PictureBox1.Visible = False
PictureBox3.Visible = False
PictureBox5.Visible = False
PictureBox7.Visible = False
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
CheckBox6.Enabled = False
CheckBox7.Enabled = False
CheckBox8.Enabled = False
StatusLB2.Text = "Dispositivo USB Desconectado"
EndSub
'************************************************* ****************
' disconnect from the HID controller...
'************************************************* ****************
PrivateSub Form1_FormClosed(ByVal sender AsObject, ByVal e As System.Windows.Forms.FormClosedEventArgs) HandlesMe.FormClosed
Splash.Close()
DisconnectFromHID()
EndSub
'************************************************* ****************
' a HID device has been plugged in...
'************************************************* ****************
PublicSub OnPlugged(ByVal pHandle AsInteger)
Dim VendorName AsString = "00000000000000000000"
Dim serial AsString = "00000000000000000000"
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
' ** YOUR CODE HERE **)
hidGetVendorName(pHandle, VendorName, 20)
hidGetSerialNumber(pHandle, serial, 20)
Label1.Text = "Vendor Name: " & VendorName
Label2.Text = "Product Name: " & ProductName
Label3.Text = "Vendor ID: " & VendorID
Label4.Text = "Product ID: " & ProductID
Label5.Text = "Serial: " & serial
Pictureoff.Visible = False
Pictureon.Visible = True
TrackBar1.Enabled = True
TrackBar2.Enabled = True
Label8.Text = Val(BufferIn(1))
Label8.Text = Val(BufferIn(2))
Label10.Text = TrackBar1.Value
Label11.Text = TrackBar2.Value
CheckBox1.Enabled = True
CheckBox2.Enabled = True
CheckBox3.Enabled = True
CheckBox4.Enabled = True
CheckBox5.Enabled = True
CheckBox6.Enabled = True
CheckBox7.Enabled = True
CheckBox8.Enabled = True
StatusLB2.Text = "Dispositivo USB Conectado"
EndIf
EndSub
'************************************************* ****************
' a HID device has been unplugged...
'************************************************* ****************
PublicSub OnUnplugged(ByVal pHandle AsInteger)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
' ** YOUR CODE HERE **
Pictureoff.Visible = True
Pictureon.Visible = False
TrackBar1.Enabled = False
TrackBar2.Enabled = False
Label8.Text = "AN0"
Label8.Text = "AN1"
Label10.Text = "CCP1"
Label11.Text = "CCP2"
PictureBox1.Visible = False
PictureBox3.Visible = False
PictureBox5.Visible = False
PictureBox7.Visible = False
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox3.Enabled = False
CheckBox4.Enabled = False
CheckBox5.Enabled = False
CheckBox6.Enabled = False
CheckBox7.Enabled = False
CheckBox8.Enabled = False
StatusLB2.Text = "Dispositivo USB Desconectado"
EndIf
EndSub
'************************************************* ****************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'************************************************* ****************
PublicSub OnChanged()
' get the handle of the device we are interested in, then set
' its read notify flag to true - this ensures you get a read
' notification message when there is some data to read...
Dim pHandle AsInteger
pHandle = hidGetHandle(VendorID, ProductID)
hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
EndSub
'************************************************* ****************
' on read event...
'************************************************* ****************
PublicSub OnRead(ByVal pHandle AsInteger)
' read the data (don't forget, pass the whole array)...
Dim index AsByte
If hidRead(pHandle, BufferIn(0)) Then
' ** YOUR CODE HERE **
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontroller...
If BufferIn(7) = 0 And BufferIn(8) = 0 Then
'
' incomming ADC reading and PORTA status
' --------------------------------------
ProgressBar1.Value = BufferIn(1) 'Display ADC results to
ProgressBar2.Value = BufferIn(2) 'the progress bars
'
Label8.Text = Val(BufferIn(1)) 'Display Progress bars
Label9.Text = Val(BufferIn(2)) 'Values
'
PictureBox1.Visible = BufferIn(3) 'Display PORTA
PictureBox3.Visible = BufferIn(4) 'status to
PictureBox5.Visible = BufferIn(5) 'the according
PictureBox7.Visible = BufferIn(6) 'LEDs
Else
'
' Incomming data is a text string
' -------------------------------
For index = 1 To BufferInSize ' pass the whole array but skip (0)
If BufferIn(index) <> 0 Then' valid data?
txtreception.AppendText(Chr(BufferIn(index)))
EndIf
Next
txtreception.Text += vbNewLine
txtreception.Select(txtreception.Text.Length - 1, 0)
txtreception.ScrollToCaret()
EndIf

EndIf
EndSub
PublicSub WriteSomeData()
' Use to send data to the USB bus.
' data must be store in BufferOut array
'
' Actual structure:
' -----------------
' BufferOut(0) = Report id => always 0
' BufferOut(1) = PORTB ' first data item, etc etc
' BufferOut(2) = HPWM slider1 MSB
' BufferOut(3) = HPWM slider1 LSB
' BufferOut(4) = HPWM slider2 MSB
' BufferOut(5) = HPWM slider2 LSB
' BufferOut(6) = n/a = 0
' BufferOut(7) = n/a = 0
' BufferOut(8) = n/a = 0
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(6) = 0
BufferOut(7) = 0
BufferOut(8) = 0
hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
EndSub

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
txtreception.Clear()
EndSub
PrivateSub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
BufferOut(2) = (TrackBar1.Value) And 255 ' keep only 8 LSB
BufferOut(3) = Int(TrackBar1.Value / 256) ' keep only 8 MSB
Label10.Text = TrackBar1.Value ' display it
WriteSomeData() ' send it
EndSub
PrivateSub TrackBar2_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar2.Scroll
BufferOut(4) = (TrackBar2.Value) And 255 ' keep only 8 LSB
BufferOut(5) = Int(TrackBar2.Value / 256) ' keep only 8 MSB
Label11.Text = TrackBar2.Value ' display it
WriteSomeData() ' send it
EndSub
PublicSub ActualizaPORTAB()
Dim i AsInteger
BufferOut(1) = 0
If CheckBox1.Checked = TrueThen
PORTB(0) = 1
Else
PORTB(0) = 0
EndIf
If CheckBox2.Checked = TrueThen
PORTB(1) = 1
Else
PORTB(1) = 0
EndIf
If CheckBox3.Checked = TrueThen
PORTB(2) = 1
Else
PORTB(2) = 0
EndIf
If CheckBox4.Checked = TrueThen
PORTB(3) = 1
Else
PORTB(3) = 0
EndIf
If CheckBox5.Checked = TrueThen
PORTB(4) = 1
Else
PORTB(4) = 0
EndIf
If CheckBox6.Checked = TrueThen
PORTB(5) = 1
Else
PORTB(5) = 0
EndIf
If CheckBox7.Checked = TrueThen
PORTB(6) = 1
Else
PORTB(6) = 0
EndIf
If CheckBox8.Checked = TrueThen
PORTB(7) = 1
Else
PORTB(7) = 0
EndIf
For i = 0 To 7
BufferOut(1) = BufferOut(1) + (PORTB(i) * (2 ^ i))
Next
WriteSomeData()
EndSub
PrivateSub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
ActualizaPORTAB()
EndSub
PrivateSub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
StatusLB3.Text = DateAndTime.Now.ToString
EndSub
EndClass


Attached is the hole Project in VB.Net 2008

mister_e
- 31st May 2011, 00:37
Nice work, will be useful for many, that's for sure!

I've already posted a snip for vb.net here awhile back. Amr Nekhit did a VB.NET template based on my work
http://helmpcb.com/software/usb-hid-template-for-visual-basic-2005

gadelhas
- 31st May 2011, 00:40
Nice work, will be useful for many, that's for sure!

I've already posted a snip for vb.net here awhile back. Amr Nekhitdid a VB.NET template based on my work
http://helmpcb.com/software/usb-hid-template-for-visual-basic-2005

Thank you mister_e

I see that post, when i was searching, however since it was made in VB2005, i decided to this this one in VB2008, not to much but some things change.

mister_e
- 31st May 2011, 00:44
Time goes by way too fast :D

gadelhas
- 31st May 2011, 00:48
Time goes by way too fast :D

You are right... Now i have to do it in VB2010.... or perhaps in one future new version, like VB2012........ hummm i don't think so!!!

mister_e
- 31st May 2011, 01:00
People need to do something by themself sometimes huh? ;)

cncmachineguy
- 31st May 2011, 01:43
Thank you both. I guess you are off the hook now steve :). I will have to digest this (mostly the vb side) but I do THANK YOU both.!!!

mister_e
- 31st May 2011, 03:56
In the beginning earth was empty

Then someone says

5594

May the force be with you

cncmachineguy
- 31st May 2011, 05:52
So does HID fall in the catagory of "pure USB"?

mister_e
- 31st May 2011, 06:00
HID is a generic USB class, does not require specific driver, could be considered as pure USB... at least the most simple way to develop painless USB stuff.

I still dream Melabs would consider porting Microchip USB framework so PBP user would access other USB Class such as Audio, MIDI and such. We already have mouse & Keyboard (joystick maybe?!?)... why they've stopped there?

Darrel Taylor
- 31st May 2011, 23:11
Who stopped where?
The Microchip USB "framework" is already in PBP.

You can make your own descriptor files and code for all kinds of stuff.
The mouse, HID and CDC examples are there to show you how it's done.

Would you expect DMX, MODBUS or NMEA 2000 to be included just because there's an HSEROUT command?

mister_e
- 1st June 2011, 00:06
I know that Darrel, I can even make my own compiler if I would like to. That's not the whole point. Seems pretty obvious you don't have a clue of what's all in the Microchip Framework then.

LCDOUT & GLCD a load ask because they can't, It's somehow easy when you take time to eat/digest the datasheet...

But still there's a MAJOR difference between simple serial/parrallel stuff on various baudrate VS USB Class & Descriptors... kinda black magic for A LOAD of people.

Arduino, Atmel have other class already made in their free compiler, I would expect Melabs be open minded to do it for their paid compiler.

It's a simple suggestion, for the real few time I ask something, don't be so defensive buddy... I love you :)

dhouston
- 1st June 2011, 01:03
Arduino, Atmel have other class already made in their free compiler, I would expect Melabs be open minded to do it for their paid compiler.Well, life's too short (especially, in my case) to argue with either you or Darrell who both seem to know this subject far better than I do but, I was reading the installation instructions for the Arduino and learned that neither Linux nor OSX need any drivers to connect with its USB link (Windows only needs an .inf file). It shows up in Windows as a COM port so I assume it appears as a serial port in the others as well. However, the Arduino is using a rather powerful Atmel ATMega8U2 32-pin MCU just for USB->serial so it's understandable (at least to a dim-witted geezer like me) that Microchip might not be able to accomplish quite the same thing with a single MCU.

mister_e
- 1st June 2011, 01:08
FALSE, you can create any USB device with ANY usb capable of MCU. You just need to use Generic classes, and this is just all about the USB descriptor.

Should you need higher speed & specific feature, you develop your own USB driver... and for that, you need to do it for ANY OS platform...

dhouston
- 1st June 2011, 02:12
I wasn't really talking about the OS end but about the embedded end where Arduino dedicates an MCU to USB->serial while Microchip does not..

Darrel Taylor
- 1st June 2011, 02:16
Well mister-e,

Looking at the Arduino language reference ...
http://arduino.cc/en/Reference/HomePage?from=Reference.Extended
I don't even see any native USB commands.

I don't look for Arduino stuff, so maybe you have a better link to native commands.
I have seen a USB library for it. But much like you or I would write routines for PBP, it was user created.

And the only MIDI usage I can find, is thru an FTDI chip ...
A far cry from a compiler command.

As for the Microchip USB "Framework" ...
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2680&dDocName=en537044

I am quite familiar with it, and I'm sure you know that it only has a MIDI Demo application.
You've already emailed me regarding that program and indicated it wasn't very good and you wanted melabs to make a better one.

MIDI is just that, an application.
It's not a compiler command.

No matter what melabs could generate for a MIDI application, somebody would want more.
It's a loosing proposition that is better left to the programmers writing the application.

With that said...
It's well within the capabilities of PicBasic Pro to write a MIDI application.
It's just up to you to do it.

mister_e
- 1st June 2011, 02:35
I don't talk only about MIDI, oh well forget that then, I guess my suggestion are more to give Melabs idea more than to help myself, bah forget it... Since that e-mail, I've fixed their mistakes, rolled my own and everything's fine... no worries.

NEXT!

Darrel Taylor
- 1st June 2011, 02:44
... Since that e-mail, I've fixed their mistakes, rolled my own and everything's fine... no worries.

NEXT!
Great!

Wanna share?
Someone I know was looking for a free MIDI program.

mister_e
- 1st June 2011, 02:56
MPLAB is free, C18 has free version, Microchip framework is free and there's a MIDI-DEMO in... or he just need to roll his own based on MIDI Demo like I did, after all

It's well within the capabilities of PicBasic Pro to write a MIDI application.
It's just up to you to do it. :D

... or be patient 'till the public release of Or-BIT IDE, but from what I know, it's ain't going to be free and still not 100% of all freebies inside.

mariacho
- 13th July 2011, 12:12
Hi,
I would like to put this firmware in my pic18f4550, but it doesn't compile. If you have some hex file for the same code pls post it. I want to give it a run on my pic.

And for another matter at all, I want to use the previous pic with a wireless router (D-Link DI-524UP) and I want to know if there is any change necessary. Also, I think that the USB Print Server will complicate my life a bit. If anyone tried this before and has any experience with it, please share. The entire project is about a small boat controlled from laptop via the wireless router, and includes two DC motors (2 PWM signals) and one stepper. Any opinion will do. Tx a lot.

gadelhas
- 13th July 2011, 12:42
Hi,
I would like to put this firmware in my pic18f4550, but it doesn't compile. If you have some hex file for the same code pls post it. I want to give it a run on my pic.

And for another matter at all, I want to use the previous pic with a wireless router (D-Link DI-524UP) and I want to know if there is any change necessary. Also, I think that the USB Print Server will complicate my life a bit. If anyone tried this before and has any experience with it, please share. The entire project is about a small boat controlled from laptop via the wireless router, and includes two DC motors (2 PWM signals) and one stepper. Any opinion will do. Tx a lot.



Crystal Ball, please tell me why the code doesn't compile with this user?

mariacho
- 14th July 2011, 17:40
i got:
error[180] usb18mem.asm116: RES directive cannot reserve odd number of bytes in PIC18 absolute mode
warning [230] usb18mem.asm116: no memory has been reserved by this instruction

gadelhas
- 14th July 2011, 21:41
i got:
error[180] usb18mem.asm116: RES directive cannot reserve odd number of bytes in PIC18 absolute mode
warning [230] usb18mem.asm116: no memory has been reserved by this instruction

Post your code.

What is your PBP version? Also list the files that you have in your project directory.

I works fine here without any error and warning.

mariacho
- 14th July 2011, 23:16
PBP 2.60, micro code studio plus 3.0.0.5
folder content: hd_hid260.pbp, PiCUSB.pbp and all files from c:PBP/usb18

-- code removed --

thank you for your help

gadelhas
- 14th July 2011, 23:27
The code that you posted is not the same as i post in the first post! The code that you post is the code to be included in the code that i post.
Please copy past the code form the first post and try it!

mariacho
- 15th July 2011, 00:36
i do copy-paste the first post code and includein the same folder the files from firmware.zip and all the files from usb18 folder
compile and result: c:\pic\new\dt_hid260.pbp error line 25> bad BANK number

sorry i`m not so good at this (trying to learn)

gadelhas
- 15th July 2011, 00:53
And wich code are you compiling? In MicroCode Studio you do not compile the DT_HID260.pbp, you need to compile, the one with the code from the first post. Also, are you compiling the code for the correct Chip 18F4550? Is it possible for you to put a screenshot of the Microcode Studio after you compile with the erros?

jmgelba
- 3rd September 2011, 01:30
Hi,

I used the code in this thread on the VB side, and used parts of and incorporated it into my own code on the PIC side. I can communicate and send data perfectly to VB but I cannot get data out of VB into the PIC.

I need to change a slider and have that value (0-255) sent to the PIC so it can save it to EEPROM and later use it. My code on the PIC side works perfectly if I increase or decrease that value with push buttons but I cannot get the byte into the PIC.

Lets say I want to send that value out to the PIC and store it in byte IOUT. What is the program structure in VB?

I am using VB 2010, pbp 2.60 and a 18F2550.

What info do I need to post next to make this easier to figure out?

gadelhas
- 5th September 2011, 15:24
Hi,

I used the code in this thread on the VB side, and used parts of and incorporated it into my own code on the PIC side. I can communicate and send data perfectly to VB but I cannot get data out of VB into the PIC.

I need to change a slider and have that value (0-255) sent to the PIC so it can save it to EEPROM and later use it. My code on the PIC side works perfectly if I increase or decrease that value with push buttons but I cannot get the byte into the PIC.

Lets say I want to send that value out to the PIC and store it in byte IOUT. What is the program structure in VB?

I am using VB 2010, pbp 2.60 and a 18F2550.

What info do I need to post next to make this easier to figure out?


Hi;

have you tried with the code for the PIC post here. Since the code published here works well, i think there is something wrong with your code, so you should try with the pic code of this thread, or you should post your code to see what is wrong.

jmgelba
- 6th September 2011, 20:34
Hmm. Ok, seem to have some data coming out but its when I press buttons and not when I move the slider bar :) Its all wrong lol!
I'll post code later.

jmgelba
- 7th September 2011, 02:11
Ok so I stripped out some of the stuff I dont need and with the addition of a few things this is the VB side:


Public Class Form1

' vendor and product IDs
Private Const VendorID As Short = 6017 'Replace with your device's
Private Const ProductID As Short = 2000 'product and vendor IDs


' read and write buffers
Private Const BufferInSize As Short = 16 'Size of the data buffer coming IN to the PC
Private Const BufferOutSize As Short = 16 'Size of the data buffer going OUT from the PC
Dim BufferIn(BufferInSize) As Byte 'Received data will be stored here - the first byte in the array is unused
Dim BufferOut(BufferOutSize) As Byte 'Transmitted data is stored here - the first item in the array must be 0

Dim PORTC(8) As Integer

Dim IOUT1 As Byte
' ************************************************** **************
' when the form loads, connect to the HID controller - pass
' the form window handle so that you can receive notification
' events...
'************************************************* ****************
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' do not remove!
ConnectToHID(Me)
Pictureoff.Visible = True
Pictureon.Visible = False
Label8.Text = "PCB Temp COUNT"
Label8.Text = "AN1"
CheckBox1.Enabled = False
CheckBox2.Enabled = False
StatusLB2.Text = "USB DEVICE DISCONNECTED"
End Sub

'************************************************* ****************
' disconnect from the HID controller...
'************************************************* ****************
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Splash.Close()
DisconnectFromHID()
End Sub

'************************************************* ****************
' a HID device has been plugged in...
'************************************************* ****************
Public Sub OnPlugged(ByVal pHandle As Integer)
Dim VendorName As String = "00000000000000000000"
Dim serial As String = "00000000000000000000"

If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
' ** YOUR CODE HERE **)
hidGetVendorName(pHandle, VendorName, 20)
hidGetSerialNumber(pHandle, serial, 20)

Label1.Text = "Vendor Name: " & VendorName
Label2.Text = "Product Name: " & ProductName
Label3.Text = "Vendor ID: " & VendorID
Label4.Text = "Product ID: " & ProductID
Label5.Text = "Serial: " & serial
Pictureoff.Visible = False
Pictureon.Visible = True
Label8.Text = Val(BufferIn(1))
Label8.Text = Val(BufferIn(2))
CheckBox1.Enabled = True
CheckBox2.Enabled = True
StatusLB2.Text = "USB DEVICE CONNECTED"
End If
End Sub

'************************************************* ****************
' a HID device has been unplugged...
'************************************************* ****************
Public Sub OnUnplugged(ByVal pHandle As Integer)
If hidGetVendorID(pHandle) = VendorID And hidGetProductID(pHandle) = ProductID Then
hidSetReadNotify(hidGetHandle(VendorID, ProductID), False)
' ** YOUR CODE HERE **
Pictureoff.Visible = True
Pictureon.Visible = False
Label8.Text = "DEG C"
Label8.Text = "AN1"
CheckBox1.Enabled = False
CheckBox2.Enabled = False
CheckBox4.Enabled = False
StatusLB2.Text = "USB DEVICE DISCONNECTED"
End If
End Sub

'************************************************* ****************
' controller changed notification - called
' after ALL HID devices are plugged or unplugged
'************************************************* ****************
Public Sub OnChanged()
' get the handle of the device we are interested in, then set
' its read notify flag to true - this ensures you get a read
' notification message when there is some data to read...
Dim pHandle As Integer
pHandle = hidGetHandle(VendorID, ProductID)
hidSetReadNotify(hidGetHandle(VendorID, ProductID), True)
End Sub

'************************************************* ****************
' on read event...
'************************************************* ****************
Public Sub OnRead(ByVal pHandle As Integer)
' read the data (don't forget, pass the whole array)...
Dim index As Byte

If hidRead(pHandle, BufferIn(0)) Then
' ** YOUR CODE HERE **
' first byte is the report ID, e.g. BufferIn(0)
' the other bytes are the data from the microcontroller...

If BufferIn(7) = 0 And BufferIn(8) = 0 Then
'
' incomming ADC reading and PORTA status
' --------------------------------------
ProgressBar1.Value = BufferIn(1) 'Display ADC results to
ProgressBar2.Value = BufferIn(2) 'the progress bars
'
ProgressBar3.Value = BufferIn(1) 'Display ADC results to
ProgressBar4.Value = BufferIn(3) 'the progress bars

Label8.Text = Val(BufferIn(1)) 'Display Progress bars
Label9.Text = Val(BufferIn(2)) 'Values
Label18.Text = Val(BufferIn(3)) 'Value
'
Else
'
' Incomming data is a text string
' -------------------------------
For index = 1 To BufferInSize ' pass the whole array but skip (0)
If BufferIn(index) <> 0 Then ' valid data?
txtreception.AppendText(Chr(BufferIn(index)))
End If
Next
txtreception.Text += vbNewLine
txtreception.Select(txtreception.Text.Length - 1, 0)
txtreception.ScrollToCaret()
End If


End If
End Sub
Public Sub WriteSomeData()
' Use to send data to the USB bus.
' data must be store in BufferOut array
'
' Actual structure:
' -----------------
' BufferOut(0) = Report id => always 0
' BufferOut(1) = PORTB ' first data item, etc etc
' BufferOut(2) = HPWM slider1 MSB
' BufferOut(3) = IOUT
' BufferOut(4) = HPWM slider2 MSB
' BufferOut(5) = HPWM slider2 LSB
' BufferOut(6) = n/a = 0
' BufferOut(7) = n/a = 0
' BufferOut(8) = n/a = 0
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(6) = 0
BufferOut(7) = 0
BufferOut(8) = 0

hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
txtreception.Clear()
End Sub


Public Sub ActualizaPORTC()
Dim i As Integer

BufferOut(1) = 0

If CheckBox1.Checked = True Then
PORTC(6) = 1
Else
PORTC(6) = 0
End If

If CheckBox2.Checked = True Then
PORTC(1) = 1
Else
PORTC(1) = 0
End If

For i = 0 To 1
BufferOut(1) = BufferOut(1) + (PORTC(i) * (2 ^ i))
Next

WriteSomeData()
End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
ActualizaPORTAB()
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
ActualizaPORTAB()
End Sub

'Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' ActualizaPORTAB()
'End Sub

'Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' ActualizaPORTAB()
'End Sub

'Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' ActualizaPORTAB()
'End Sub

'Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' ActualizaPORTAB()
'End Sub

'Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' ActualizaPORTAB()
'End Sub

'Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
' ActualizaPORTAB()
'End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
StatusLB3.Text = DateAndTime.Now.ToString
End Sub

Private Sub StatusLB1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusLB1.Click

End Sub

Private Sub ProgressBar1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProgressBar1.Click

End Sub
Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

End Sub

Private Sub Label7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label7.Click

End Sub

Private Sub GroupBox2_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox2.Enter

End Sub

Private Sub Label10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label10.Click

End Sub

Private Sub Label12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label12.Click

End Sub

End Class

And this is the PIC side:




CLEAR

include "modedefs.bas"

Define OSC 48
DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 3 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 2 ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2000' Set command delay time in us
DEFINE LCD_DATAUS 50 ' Set data delay time in us
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50

;--- 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 "Regal Electronics"
DEFINE USB_PRODUCTNAME "Light Lynk - USB"
DEFINE USB_SERIAL "0001"
DEFINE USB_INSIZE 16 ; IN report is PIC to PC (8,16,32,64)
DEFINE USB_OUTSIZE 16 ; 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 PORTC,6 ; " " data being sent to PC
DEFINE USB_RXLED PORTB,6 ; " " data being received from PC

;--- Variables -------------------------------------------------------------
Old_PORTC VAR BYTE
New_PORTC VAR BYTE


ADCON1 = %00001011
ADCON2 = %10000111

TRISA = %00001111
TRISB = %00000000
TRISC = %00000111
CMCON = 7
PORTA.5 = 0

LEDTEMP var word
PCBTEMP var word
LEDVOLT var word
LEDCOUNT var word
CURRENT var word
LEDCURRENT var word
LCUR0 var word
LCUR1 var word

ADJUST var word

tb0 VAR WORD
tb1 VAR WORD
tb2 VAR WORD

LED0 var word
LED1 var word
LED2 var word

LEDV0 var word
LEDV1 var word

IOUT VAR byte
IOUT1 VAR BYTE
IOUT1 = 5

JMG VAR WORD
JMG1 VAR WORD
JMG2 VAR WORD
JMG3 VAR WORD
JMG4 VAR WORD
POT0 var word
SCK var PORTB.0 ' Alias PORTB.0 to SCK (serial data clock)
SDO var PORTC.7 ' Alias PORTC.7 to SDO (serial data out)
CS var PORTB.1 ' Alias PORTB.1 to C_EN (chip enable)

HIGH CS 'Chip enable high to start
high sck 'Mode 1,1 SCK idles high

pot0.Highbyte = %00000000 'POT0 address in hex - easier for me to see which pot selected
pot0.lowbyte = 0 'POT0 value in DEC - initialize pot - can be any value

READ 10, pot0.lowbyte

low CS ' Make chip active
pause 1 ' Delay after making chip active per data sheet - only need 60nS
shiftout sdo,sck,5,[pot0\16] ' Mode 1,1 in MCP4261 DS / Mode 5 PBP - clock idles high, MSB shift out first
high CS ' MCP4261 DS recommends toggling /CS between commands
pause 1 ' Delay keeps from banging CS pin


ADJUST = 0
PORTC.6 = 0

Pause 1500
LCDOUT $FE,$C0, " YJ5 LED DRIVER "
pause 2000

;--- The Main Loop ---------------------------------------------------------
Main:
ARRAYWRITE USBTXBuffer, ["Random text here"]
gosub WaitToSend
FOR X = 0 to 1000 ; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
PAUSE 1

New_PORTC = PORTC ; Check PORTC pins
IF New_PORTC != Old_PORTC THEN
Old_PORTC = New_PORTC
ARRAYWRITE USBTXBuffer, ["Text Here!"]
GOSUB WaitToSend
ARRAYWRITE USBTXBuffer, [PCBTEMP, LEDTEMP, IOUT1, _
PORTC.6, PORTA.4, PORTA.5,0,0]
GOSUB WaitToSend
ENDIF
NEXT X

'ADCIN 3, PCBTEMP ; Send A/D about once/sec
'ADCIN 2, LEDTEMP
ARRAYWRITE USBTXBuffer, [tb0, LEDTEMP, IOUT1, _
PORTC.6, PORTA.4, PORTA.5,0,0]
GOSUB SendIfReady

If PORTE.3 = 1 THEN MENU
if PORTC.0 = 1 then SELEC
if PORTC.1 = 1 then UP
if PORTC.2 = 1 then DOWN

ADCIN 3,PCBTEMP
tb0 = PCBTEMP * 33 /92
tb1 = tb0*721
tb2 = Div32 255
tb2 = tb2 - 300
tb0 = tb2/10
tb1 = tb2//10

ADCIN 2,LEDTEMP
LED0 = LEDTEMP * 33 /92
LED1 = LED0*721
LED2 = Div32 255
LED2 = LED2 - 264
LED0 = LED2/10
LED1 = LED2//10

PAUSE 200

LCDOUT $FE,1,"PCB ",DEC2 tb0,".",DEC1 tb1,"C ",DEC4 IOUT1
LCDOUT $FE,$C0,"LED ",DEC2 LED0,".",DEC1 LED1,"C ", DEC4 LEDTEMP

ADCIN 1, LEDCOUNT
LEDVOLT = LEDCOUNT * 49 /100
LEDV0 = LEDVOLT/10
LEDV1 = LEDVOLT//100

ADCIN 0, CURRENT
LEDCURRENT = CURRENT * 50 /10
LCUR0 = LEDCURRENT/10
LCUR1 = LEDCURRENT//100

PAUSE 2000

If PORTE.3 = 1 THEN MENU
if PORTC.0 = 1 then SELEC
if PORTC.1 = 1 then UP
if PORTC.2 = 1 then DOWN

LCDOUT $FE,1, "LED V ", DEC2 LEDV0, ".", DEC1 LEDV1,"V ", DEC4 LEDCOUNT
LCDOUT $FE,$C0,"LED C ", DEC2 LCUR0, ".", DEC1 LCUR1,"A ", DEC4 CURRENT

pause 2000

If PORTE.3 = 1 THEN MENU
if PORTC.0 = 1 then SELEC
if PORTC.1 = 1 then UP
if PORTC.2 = 1 then DOWN

goto main

MENU:
'LCDOUT $FE, 1, "MENU" : LCDOUT $FE, $C0,"PRESSED"
PORTC.6 = 1
pause 300
PORTC.6 = 0
'LCDOUT $fe,1
goto setup

SELEC:
LCDOUT $FE, 1, "SELECT" : LCDOUT $FE, $C0,"PRESSED"
PORTC.6 = 1
pause 300
PORTC.6 = 0
LCDOUT $fe,1
goto main

UP:
LCDOUT $FE, 1, "UP" : LCDOUT $FE, $C0,"PRESSED"
PORTC.6 = 1
pause 300
PORTC.6 = 0
LCDOUT $fe,1
goto main

DOWN:
LCDOUT $FE, 1, "DOWN" : LCDOUT $FE, $C0,"PRESSED"
PORTC.6 = 1
pause 300
PORTC.6 = 0
LCDOUT $fe,1
goto main

SETUP:
LCDOUT $FE, 1, "SET LED CURRENT"
LCDOUT $FE, $C0, DEC3 IOUT
gosub HandleRX
IF PORTC.1 = 1 THEN
IOUT = IOUT + 1
PAUSE 75
ENDIF
IF PORTC.2 = 1 THEN
IOUT = IOUT - 1
PAUSE 100
ENDIF
pot0.lowbyte = IOUT
low CS ' Make chip active
pause 1 ' Delay after making chip active per data sheet - only need 60nS
shiftout sdo,sck,5,[pot0\16] ' Mode 1,1 in MCP4261 DS / Mode 5 PBP - clock idles high, MSB shift out first
high CS ' MCP4261 DS recommends toggling /CS between commands
pause 1 ' Delay keeps from banging CS pin


WRITE 10, IOUT
IF PORTC.0 = 1 THEN main
PAUSE 75
'IF PORTE.3 = 1 THEN VERSION
GOTO SETUP

VERSION:
pause 100
LCDOUT $FE, 1, "FIRMWARE 1.0"
LCDOUT $FE, $C0, "PCBA 1.0 SN 0001"
If PORTC.0 = 1 THEN SETUP
Goto VERSION

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,[JMG, JMG1, IOUT1, PORTC.6, JMG3, JMG4]
IOUT1 = USBRXBuffer(3)

return



All I want to do is send some AD values and a couple of Bytes containing temperatures to the PC, and set a slider in VB and send the 0 - 255 value back to IOUT1 byte in PBP and also turn on or off portc.6 in VB.

jmgelba
- 7th September 2011, 18:46
The rx LED lights up as I click on buttons on the pc, so data must be getting to the pic, but I dont see it turning on or off the port pin as it should. So not sure what data is being sent or whats happening to it once it gets tot he PIC.

Also, I dont see the rx LED lighting when I move the slider bar so there has to be an issue on the VB side.

Can anyone help me out please? I'm a learn by example guy. I've figured out a few errors and got my self from some problems but I'm stuck now.

jmgelba
- 8th September 2011, 18:12
It looks like there are 8 bytes. The first byte has to be 0 as to the last 2. So theroetically i should be able to just do something like this:

BufferOut(0) = 0
BufferOut(1) = 100
BufferOut(2) = 200
BufferOut(3) = 300
BufferOut(4) = 400
BufferOut(5) = 500
BufferOut(6) = 600
BufferOut(7) = 0
BufferOut(8) = 0

On the PIC end I should be able to use ARRAYWRITE [byte1, byte2, byte3, byte4, byte5, byte6, byte7, byte8]

Should this work?

gadelhas
- 8th September 2011, 20:58
HI;

To send data from PIC to PC, you must do something like this ( Allready posted in the code of the post #1 )


ARRAYWRITE USBTXBuffer, [Value0, Value1, PORTA.2,PORTA.3, PORTA.4, PORTA.5,0,0]
You don't have to worry about the first byte be equal to 0, only ont the PC to PIC side.

If you want to send from PC to PIC, you must do something like this ( Allready posted in the code of the post #1 )



PublicSub WriteSomeData()
' Use to send data to the USB bus.
' data must be store in BufferOut array
'
' Actual structure:
' -----------------
BufferOut(0) = 0 ' first by is always the report ID
BufferOut(1) = 127 ' value from 1º slider
BufferOut(2) = 255 ' value from 2º slider
BufferOut(3) = 10
BufferOut(4) = 100
BufferOut(5) = 178
BufferOut(6) = 0
BufferOut(7) = 0
BufferOut(8) = 0
hidWriteEx(VendorID, ProductID, BufferOut(0)) ' send it
EndSub

jmgelba
- 9th September 2011, 00:26
Are the values in BufferOut(1) through BufferOut(5) decimal? Are they bytes or words?

How do I receive these values and put them into VAR BYTES?

Do I always need to send the 8 values of BufferOut or can I send say just BufferOut(3)?

Can I send BufferOut(1) = "text here" for example?



Thank you!!

gadelhas
- 9th September 2011, 00:48
Are the values in BufferOut(1) through BufferOut(5) decimal? Are they bytes or words?

How do I receive these values and put them into VAR BYTES?

Do I always need to send the 8 values of BufferOut or can I send say just BufferOut(3)?

Can I send BufferOut(1) = "text here" for example?



Thank you!!

Hi, you are not reading the code that i posted.
If you read the code carfully you see that in VB the BufferOut Array is declared asbyte, so you can only send bytes.

Dim BufferOut(BufferOutSize) AsByte

On the PIC side you receive the bytes on the USBRXBuffer array, and then you do what you want with those bytes.

ARRAYREAD USBRXBuffer,[PORTB, DUTY1.LowByte, DUTY1.HighByte, DUTY2.LowByte, DUTY2.HighByte]

On the VB side you receive tte bytes on BufferIn array, and then you do what you want with thise bytes.

Dim BufferIn(BufferInSize) AsByte

Since the Define of the USB_INSIZE and USB_OUTSIZE are equal to 8, you can only send and receive 8 bytes. You can change this, to multiples values, like 16, 32, 64. YOu need always to send all th ebytes that are defined. You you Define 8, you must send 8 bytes, if you define 16 you must send 16 bytes. For instance, if you have only 10 bytes to send, you must define you USB_sizes equal to 16, and complete the array with 0's.


Can I send BufferOut(1) = "text here" for example?
You cannot do that, because it excedes the number of bytes. BufferOut(1) is one byte only. You cand o just this, BufferOut(1)="t", or
BufferOut = "0abcdefgh".

I think that you should read both PIC and VB code that i posted and review review some principles of the PBP manual.

jmgelba
- 9th September 2011, 01:36
Ok, then I think I've found a problem. My buffer size is set to 16 in and out but I'm only rx/tx'ing 8.

When receiving empty bytes, how do I deal with them? Say I only ever need to send 3 bytes, the left overs will always be empty. Do I just assign unused VAR Bytes to those incoming bytes?

mister_e
- 9th September 2011, 08:53
You just need to parse/test them on both side. Treat USB like any proper serial communication and you should be fine. Your milleage may vary though.

jmgelba
- 9th September 2011, 15:58
Thanks guys. I think I've found my issues.

One more question for now though, is the data within the byte transmitted msb first or lsb first from VB?

gadelhas
- 9th September 2011, 16:11
Thanks guys. I think I've found my issues.

One more question for now though, is the data within the byte transmitted msb first or lsb first from VB?


The answer to your question is in the code in post #1 and #2.

Wirecut
- 4th April 2013, 11:15
Hi.

I have tried to compile the source code that is present on the first page of this thread and I obtain the following error message:



Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 301 : Overwriting previous address contents (0000)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 301 : Overwriting previous address contents (0001)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 302 : Overwriting previous address contents (0000)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 302 : Overwriting previous address contents (0001)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 303 : Overwriting previous address contents (0002)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 303 : Overwriting previous address contents (0003)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 304 : Overwriting previous address contents (0002)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 304 : Overwriting previous address contents (0003)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 305 : Overwriting previous address contents (0004)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 305 : Overwriting previous address contents (0005)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 306 : Overwriting previous address contents (0006)
Error[118] C:\PIC\MEL\PBP3061\18F4550\USB\PICUSB\PICUSB.ASM 306 : Overwriting previous address contents (0007)

the content of the file into the working directory is:



24/01/2011 02:45 22.213 DT_HID260.pbp
04/11/2009 11:41 28.721 DT_INTS-18.bas
04/04/2013 10:03 7.665 firmware.zip
04/04/2013 11:52 84.061 PICUSB.ASM
04/04/2013 11:52 1.368 PiCUSB.ERR
04/04/2013 11:52 1.112.440 PiCUSB.LST
04/04/2013 11:52 10.855 PICUSB.MAC
04/04/2013 11:52 4.917 PiCUSB.pbp
21/06/2012 03:06 96.199 usb_dev.asm
21/06/2012 03:06 6.290 usb_dev.inc
21/06/2012 03:06 26.203 usb_hid.asm
21/06/2012 03:06 6.378 usb_hid.inc
21/06/2012 03:06 6.403 usb_mem.asm

I'm using:
Win 7 Ultimate
A 18F4550 that will be run on the PICDEM FSUSB
PBP 3.0.6.1
MCS 5.0.0.3

I have the trial version (15 gg left - I'm waiting to solve some administrative trouble before buy the upgrade)

Attached please find the Directory contents.

Any suggestion will be more appreciate.

Ciao

Leo

Demon
- 4th April 2013, 14:36
Search for OVERWRITING, you get a ton of threads. Start with this one:

http://www.picbasic.co.uk/forum/showthread.php?t=11577&highlight=overwriting

Robert

Wirecut
- 7th April 2013, 00:49
Hi,

I have commented all of that:

;--- if you un-comment these, you must comment the ones in the .inc file ---
'ASM ; 18F2550/4550, 8mhz crystal
' commented all to run , otherwize some error arise
' __CONFIG _CONFIG1L, _PLLDIV_2_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_ON_2H & _WDTPS_512_2H
' __CONFIG _CONFIG3H, _PBADEN_OFF_3H
' __CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
'ENDASM


and all begin to work.

But there are some conceptual doubt that I have explained on a different thread:

http://www.picbasic.co.uk/forum/showthread.php?t=17877

Wirecut
- 7th April 2013, 00:55
Hi,

thanks to the suggestion of Henrik Ollson on the thread:

http://www.picbasic.co.uk/forum/showthread.php?t=17877&p=119740#post119740

I have substitute the ASM/ENDASM to #CONFIG/#ENDCONFIG as:

#CONFIG ; 18F4550, 20mhz crystal
__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_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
#ENDCONFIG
and now "automagically" all works! GREATH!!!! :biggrin:

nicjo
- 4th May 2013, 02:04
Hi all,

I'm trying to use TMR0 however USB does not seem to communicate. I saw in past threads that TMR0 was used for USB polling, I've been examining all files related DT-INTS, DT-HID and soforth, TMR0 does not appear, so I don't know why using this timer would stop things. I'm not using DT-INTS on overflow just monitoring the int flag. By itsself i.e. no USB this little routine works fine. I'm outputing a 50msec (mark) pulse on portB then a second delay of around 300 msec (space). Goal is to adjust the space to vary stepper motor speed.

advice appreciated

Cheers

John

nicjo
- 4th May 2013, 02:14
Heres some of the code, this all works fine using simple delay or other timer

Main:
; Check for incomming USB data while waiting
@ ON_USBRX_GOSUB _HandleRX
lcdout $FE, 1, dec Size
pause 2000
if portB.0=1 OR Run=1 then
high porta.3
T0CON.7=1 'start TMR0
mark:
if not match01 then goto mark 'match01 var INTCON.2 '50msec delay
T0CON.7=0 'stop timer
match01=0 'clear interupt flag
INTCON.5=0 'set interupt enable to 0
low porta.3
if plugged then ARRAYWRITE USBTXBuffer, [Size,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] ;send position
GOSUB WaitToSend
time=time+1
lcdout $FE, 1, dec time 'use to check whats happening here
TMR0H=$C9 '300msec
TMR0L=$10
T0CON.7=1 'start TMR0
space:
if not match01 then goto space
T0CON.7=0 'stop timer
match01=0 'clear interupt flag
INTCON.5=0 'set interupt enable to 0
TMR0H=$F6 '50msec pulse
TMR0L=$D8
endif
goto Main

jmgelba
- 5th August 2014, 19:26
Getting back to USB basics after a 3 year absence. I cant seem to get the code in the first post to run on a 18F2550. I get: ERROR Line 25: Bad BANK number. (DT_HID260.pbp) Apparently this issue didn't exist the first time I ran the code all that time ago.

Any pointers?

mark_s
- 14th September 2016, 22:41
Thanks for posting Gadelhas,

Been wanting to try USB for a long time. But it seems everything changes rapidly. Windows, Visual Studio, .net etc. This was a popular subject a few years back. So most of the examples seem to be VB6 or in this example VS 2008

Can anyone tell me if the code in posts 1 and 2 can be used with Visual Studio Community 2015? I was able to download VS 2008 sp1 ISO, which may be good enough?

Also can't seem to find where to download EasyHID, maybe its out of date. HIDmaker FS looks great, but $399 is hard to justify for casual use.

Thanks
Mark

gadelhas
- 15th September 2016, 01:15
Thanks for posting Gadelhas,

Been wanting to try USB for a long time. But it seems everything changes rapidly. Windows, Visual Studio, .net etc. This was a popular subject a few years back. So most of the examples seem to be VB6 or in this example VS 2008

Can anyone tell me if the code in posts 1 and 2 can be used with Visual Studio Community 2015? I was able to download VS 2008 sp1 ISO, which may be good enough?

Also can't seem to find where to download EasyHID, maybe its out of date. HIDmaker FS looks great, but $399 is hard to justify for casual use.

Thanks
Mark

It's been a long time since i don't use this project. It was made in VB.net 2008, so it should be easy to use with VS 2015.

At his moment i really don't have the time to port the code to VS2015, lost of projects at the same time, but i'll put it on my list.

I not even know if EasyHID still exists, but i never used it, its to expensive.

Also let me know what are you trying to do with USB.

mark_s
- 15th September 2016, 18:55
Thanks for answering. I don't have a specific project in mind, just looking for a templet to build on. Your example is perfect, a small app written in VB to read and write to a pic's ports and modules.

I worry a little, starting projects based on VB6, mister-e and DT's great work. Some of this material is 10 years old now and they are no longer here to answer or modify these routines.

Found this site which sells a DLL for Visual Studio that looks very interesting. It can be used with VS 2015 and examples in MikroC. I have Mikrobasic so this may be the best path. The only reason I have not purchased it, the examples and videos are not in English, so I need a little more time to study.

http://www.usbhidnetclass.com/

Regards
Mark

gadelhas
- 15th September 2016, 22:19
Thanks for answering. I don't have a specific project in mind, just looking for a templet to build on. Your example is perfect, a small app written in VB to read and write to a pic's ports and modules.

I worry a little, starting projects based on VB6, mister-e and DT's great work. Some of this material is 10 years old now and they are no longer here to answer or modify these routines.

Found this site which sells a DLL for Visual Studio that looks very interesting. It can be used with VS 2015 and examples in MikroC. I have Mikrobasic so this may be the best path. The only reason I have not purchased it, the examples and videos are not in English, so I need a little more time to study.

http://www.usbhidnetclass.com/

Regards
Mark

Hi mark_s;

I'm aware of that dll, but i didn't use it.
You can probably use this to try out, but only in C and C#
http://libstock.mikroe.com/projects/view/233/easy-windform-usb-hid-net-with-easypic-v7-and-visual-c-express-2010-and-visual-basic-net-button-ad-and-leds
http://libstock.mikroe.com/projects/view/663/visual-c-net-easy-usb-component-for-all-kits-mikroe-development

Nowadays i only program in C ( embedded) or C#/python/Java (desktop app).
I few months ago i did an application with MicroC and PIC18F4550 to emulate a HID Keyboard. In this way you don't even need the desktop app.
The worst part is the descriptor, but after that it works great.

What language you usually work? PicBasic, MikroBasic, Visual Basica, C#?

mark_s
- 15th September 2016, 22:44
Hi,

I program in PBP also Mikrobasic for pic, dspic, and pic32. I'm going to learn visual basic. I have not programmed in C or C#, but have learned how to read and understand code. These days most code examples are in C so it's hard to avoid.

Thanks for the links, some of these projects are by the guy who sells the DLL.

Regards

rsocor01
- 10th May 2017, 05:27
Hi,

Does anybody have a new version of this code for PBP3 and VB.NET 13, 15, or 17? I tried the VB NET code in the first post and it gave me all kind of errors. Does anybody knows where I can get this code?

Thanks,

Robert