Some time ago, i searched 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=14945 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 Taylor, and converted the mister_e VB6 program to VB.Net.
Please note that all the credits should go to Darrel Taylor and mister_e, i just convert the program to VB.Net, nothing more!
Here is the PIC16F4550 code;
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
And the VB.NET 2008 source code;
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 End Class
You can find both VB.Net program and firmware attached.
Hope it helps someaone!!!
Re: CAN header and EXT asm question
Interface board with SPI controlled MCP 2515 and line driver to use with non CAN enabled PIC's
retepsnikrep Yesterday, 08:34https://www.aliexpress.com/item/1005006850683509.html
Line driver to use with CAN enabled PIC's....