USB CDC Communications for Dummies!


Closed Thread
Results 1 to 40 of 105

Hybrid View

  1. #1

    Default Redefinition of VAR.

    Can sombody please give me a hint what i am do-ing wrong i keep getting the following error
    C:\PBP246\PIC18EXT.BAS ERROR Line 12: Redefinition of VAR.
    Here is the code i use


    Code:
    INCLUDE "MODEDEFS.BAS"
    INCLUDE "18F4550.BAS"		; USB descriptors for CDC demo
    
    DEFINE OSC 20				' Define crystal as 20Mhz
    
    'Define ACDIN parameters
    Define ADC_BITS 10 			' Set number of bits in result
    DEFINE ADC_CLOCK 3 			' Set clock source ( 3 = RC )
    DEFINE ADC_SAMPLEUS 50 		' Set sampling time to micro-seconds
    
    'This Part set PORTA 0-3 an analog inputs,4 as digital 
    TRISA = %00011111 			'set PORTA 0-5 as inputs
    ADCON1 = %00001010 			'ADC use vss and vdd as ref.
    ADCON2 = %10000111			'FRC (clock derived from A/D RC oscillator),Right justify output
    
    
    TRISD = %00001111			'Set port d 0-3 as inputs and 4-7 as outputs
    TRISB = %10011000
    
    '**************************************Variables*********************************************
    buffer	Var	Byte[16]
    cnt	Var	Byte
    LED	Var	PORTD.4
    
    USBInit
    Low LED		' LED off
    
    '**************************************Main Program******************************************
    ' Wait for USB input
    idleloop:
    	USBService	' Must service USB regularly
    	cnt = 1	' Specify input buffer size
    	USBIn 3, buffer, cnt, idleloop
    
    ' Message received
    	Toggle LED
    
    outloop:
    	USBService	' Must service USB regularly
    	USBOut 3, buffer, cnt, outloop
    
    	Goto idleloop	' Wait for next buffer
    
    
    END

  2. #2
    Join Date
    Aug 2005
    Posts
    57

    Default

    Comment out INCLUDE "18F4550.BAS and add the following files to the project folder.It compiled fine for me and connected to the pc usb,but your code only flashes an led.
    CDCDESC.ASM
    usb18.asm
    18F4550.BAS
    USBDESC.ASM
    usb18.inc
    usb18mem.asm

  3. #3

    Wink

    Thank you got it running, the problem seemed to be that my pic basic pro was installed in PBP246 and not PBP as one of the references needed it to be, Just fixing the path seemed to repaired everything

  4. #4
    Join Date
    Dec 2008
    Posts
    48

    Default USBIN Question

    Have a very basic question that I want to confirm before going forward...

    In the PBP manual, USBIN is described as:

    ++++++++++++++++++++++++++++++++++
    USBIN Endpoint, Buffer, Countvar, Label

    Get any available USB data for the Endpoint and places it in the Buffer. Buffer must be a byte array of suitable length to contain the data. Countvar will contain the number of bytes transferred to the buffer. Label will be jumped to if no data is available.
    ++++++++++++++++++++++++++++++++++

    It was my understanding that with or without data, that this function always went to Label. Then within Label we perform a test to see if Buffer has new data and route execution from there.

    However, the way that the PBP manual describes it is that if no data is input, then program execution jumps to Label.

    Which one is it? Does this function always go to Label after reading 8 bytes (or however many specified during HID config. and specified in CountVar) even if there was or was not any input? Or, does this function only jump to Label when no bytes are read from the bus?

  5. #5
    Join Date
    Jan 2009
    Posts
    6

    Default

    Do any of you have a working hex for a 2550 on a 20MHz crystal that I can try?

    I have tried everything I have found and still cannot get anything to work. I always get "Unknown Device" error no matter what.


    The vendors of the compilers really need to give more concise information on USB communications.

  6. #6
    Join Date
    Dec 2008
    Posts
    48

    Default

    I was consistently getting USB device recognition when I connected, but ended with an unknown device when I did not have the configuration directives set appropriately. Read the above thread between Darrel and myself. Or, you can put in a 4MHz chip and just use the code I posted a couple days ago...

  7. #7
    Join Date
    Jan 2009
    Posts
    6

    Default

    OK, I was finally able to get this working.

    I was using the USBTEST files above.

    I added the extra configs for the write protect stuff that was not in the code and VOILA!!!, it works now.

    It seems to be an issue with the PicKit2. It barks at you if those configs are not included, it usually isnt an issue as long as the important ones are defined. However in this case I assume since they were missing , the PicKit2 changed some of the needed configs to something incompatible for some reason.

    This should not be the issue, however it works. I removed the extra configs and it doesnt work, add them and its just fine!!!

    Now to figure out how to get data INTO the pic.

  8. #8
    Join Date
    Jan 2009
    Posts
    6

    Default

    Quote Originally Posted by kirtcathey View Post
    Have a very basic question that I want to confirm before going forward...

    In the PBP manual, USBIN is described as:

    ++++++++++++++++++++++++++++++++++
    USBIN Endpoint, Buffer, Countvar, Label

    Get any available USB data for the Endpoint and places it in the Buffer. Buffer must be a byte array of suitable length to contain the data. Countvar will contain the number of bytes transferred to the buffer. Label will be jumped to if no data is available.
    ++++++++++++++++++++++++++++++++++

    It was my understanding that with or without data, that this function always went to Label. Then within Label we perform a test to see if Buffer has new data and route execution from there.

    However, the way that the PBP manual describes it is that if no data is input, then program execution jumps to Label.

    Which one is it? Does this function always go to Label after reading 8 bytes (or however many specified during HID config. and specified in CountVar) even if there was or was not any input? Or, does this function only jump to Label when no bytes are read from the bus?
    I have some of the same questions. What IS and endpoint anyway???


    In my findings the above example from the manual is backwards. It will only jump to the label if the buffer is FULL. It seems to sit and wait until the specified number of bytes have been received.

  9. #9
    Join Date
    Feb 2009
    Posts
    12

    Default

    Note if using the USB bus power to power your pic remember that the max current your pic circuit can draw is 100ma! Anymore and you may blow your usb port! If in doubt stick in a fuse, or use external power.
    As far as I know a USB 2.0 device can draw upto 500mA once communication is established and the host grants the request for drawing more power, otherwise the device is only able to get 100mA from the host. I doubt that you will blow the USB port if you draw more then 100mA.

  10. #10
    Join Date
    Nov 2008
    Posts
    12

    Default USB to Serial

    Hi Guys

    I'm pretty new to all of this, but here's my attempt.

    I've attached a schematic of the device i am using.

    What i am attempting to make here is some sort of USB to Serial Port Converter.

    The code i loaded onto the chip is as follows:

    Code:
    '****************************************************************
    '*  Name    : UNTITLED.BAS                                      *
    '*  Author  : [select VIEW...EDITOR OPTIONS]                    *
    '*  Notice  : Copyright (c) 2009 [select VIEW...EDITOR OPTIONS] *
    '*          : All Rights Reserved                               *
    '*  Date    : 2009/01/02                                        *
    '*  Version : 1.0                                               *
    '*  Notes   :                                                   *
    '*          :                                                   *
    '****************************************************************
    @   __CONFIG	_CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
    @	__CONFIG	_CONFIG1H, _FOSC_HSPLL_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    '_CONFIG1H, _FOSC_XTPLL_XT_1H 
    @	__CONFIG	_CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L  & _BORV_28_2L  & _VREGEN_ON_2L
    @	__CONFIG	_CONFIG2H, _WDT_OFF_2H
    @	__CONFIG	_CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_ON_3H
    @	__CONFIG	_CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
    
    ' ****************************** Configure Hardware *********************************************
    trisb = %11111100
    trisc = %10111111       	' Set TX (PortC.6) to out, rest in
    spbrg = 25            		' Calculate ((CPU speed mhz/Baud)/16)- 1  25 = 115200 baud  EX:((48000000/115200)/16)-1
    rcsta = %10010000       	' Enable serial port and continuous receive
    txsta = %00100000       	' Enable transmit and asynchronous mode/brgh = 0 bit 2
    baudcon =%00001000			' Bit3 = 1: High speed usart 16 bit  
    
    '***************************** Variables ******************************************
    USBBufferTX Var Byte[1]
    USBBufferRX var Byte[1]
    USBBufferCount Var Byte
    SerBufferTX Var Byte
    SerBufferRX var Byte[1]
    BufferRX var Byte
    SerBufferCount Var Byte
    Indexer var byte
    Xmit_Start var bit									' Start serial data transmission
    Status_Bits var byte								' Status bits to Pc
    Data_Delay var word									' Delay for dataout
    Data_Out var word									' Data 
    
    Status_Bits = 0
    
    LED	Var	PORTB.1
    
    Define  OSC     48
    
    	USBInit
    	high led
    	pause 1000
    	Low LED		' LED off
        USBService
    
        for Indexer = 0 to 0
    	   USBBufferRX[Indexer]= 0
        next
    
        usbbuffercount = 1
        
        
    main:
        goSUB usb_rx
        goSUB com_rx
    goto main    
    
    	' Transmit USB Data     
    USB_TX:
    					' TX buffer size
    USBService 
    USBOut 3, serBufferrX, USBBufferCount, main 			' Transmit data		
    RETURN
    
    	' Receive USB Data 
    USB_RX: 
    USBService 											
    USBIn 3, USBBufferRX, USBBufferCount, exitsub 		' Receive data if available,then exit
    serbuffertx=usbbufferrx[0]
    goSUB com_tx
    return
    'GOSUB Main
    
    
    'Serial Receive
    
    COM_RX:
    hserin 1, exitsub, [bufferrx]
    serbufferrx[0]=bufferrx
    goSUB usb_TX
    goSUB main
    
    'Serial Transmit
    COM_TX:
    hserout [serbuffertx]
    RETURN
    
    exitsub:
    return
    Now this code compiles and i can connect via a hyperterminal to both the USB virtual and the physical Serial port. However if i enter anything in either of hyperterminals, it gives nothing and after a while of constant typing the hyperterminals start freezing on me. So i take it i've done something horrifically wrong.

    Any suggestions?
    Attached Images Attached Images  

Similar Threads

  1. Simple USB Comms Problem
    By awmt102 in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th January 2010, 21:17
  2. One USB keyboard to Two USB Ports
    By picnaut in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 11th June 2009, 01:04
  3. USB CDC help..
    By jchandir in forum USB
    Replies: 6
    Last Post: - 22nd November 2008, 22:23
  4. Replies: 4
    Last Post: - 5th November 2008, 17:21
  5. USB PIC without USB Connection
    By Tissy in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 26th December 2005, 18:39

Members who have read this thread : 2

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