PIC USB to VB.NET


Results 1 to 40 of 54

Threaded View

  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default PIC USB to VB.NET

    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/show...0434#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;
    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!
    Attached Files Attached Files
    Thanks and Regards;
    Gadelhas

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts