Code:
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:
Code:
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.
Bookmarks