USBDemo, something to learn USB a little bit


Closed Thread
Results 1 to 40 of 279

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    PORTB.0 is not used anywhere in the program (other than output from the Demo).
    PORTB.5-7 are only used if the USB Status LED lines are uncommented (I left them commented). And they can be assigned to any pin, they don't have to be PORTB.5-7

    CDC is USB, there's no RS232 connector.

    In excel, use the Data | Import External Data | Import Data menu item to just plop your CSV file right into your spreadsheet. Then when the data changes, use Data | Refresh Data to update everything automatically.
    <br>
    DT

  2. #2
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Question One more time

    Sorry to be such a pest, Darrell, but am now trying to adapt your USBdemo_DTID so that it will go into USB connection mode as an interrupt when the USB cable is connected, permitting me in the Interrupt Service Routine to execute some code that is only intended to be executed when the USB cable is connected.
    I thought I could make the simple changes to your code as shown in my attachment below, where I just used a USB_INT with the DT_INTS-18 include file to perform the interrupt to my label "USBhandler".
    This compiles and assembles OK and starts on power up, but when the USB cable is connected I get a WINDOWS error saying "USB Device Not Recognized". Am I missing something here on how to make this work as a USB connection interrupt??
    Code:
    '**************************************************************************
    '*  Name    : USBdemo_DTHID2.pbp                                          *
    '*  Author  : Modification of Steve Monfette/Darrel Taylor                *
    '*  Date    : 11/9/2009                                                   *
    '*  Version : 2.0                                                         *
    '*  Notes   : This is a re-creation of mister-e's USBdemo for DT_HID      *
    '*          : Meant to work with mister-e's GUI but with a USB interrupt  *
    '*          : service routine where some specific code will be executed   *
    '*          : only during USB connection.                                 *
    '**************************************************************************
    ;-- 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
    INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP high priority interrupts
    
    ASM
    INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
        INT_Handler        USB_INT,    _USBhandler,   PBP,  yes
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    @    INT_ENABLE   USB_INT    ; enable external (INT) interrupts
    
    ;--- Setup USB -------------------------------------------------------------
    INCLUDE "DT_HID260.pbp"
    
    DEFINE USB_VENDORID    6017
    DEFINE USB_PRODUCTID   2000
    DEFINE USB_VERSION     1
    DEFINE USB_VENDORNAME  "Mister E/DT"
    DEFINE USB_PRODUCTNAME "USBDemo"
    DEFINE USB_SERIAL      "001"
    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:   
        'Normally in sleep mode waiting for interrupts
        Toggle PORTE.2      ' Test LED flashing during Main loop
    GOTO Main
    
    '----------------Interrupt Service Routines Start Here--------------------
    USBhandler:
        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, ["USB Demo"]
                    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
        'Code to be processed only during USB connection.
            TOGGLE PortE.3      ' Test LED that USB interrupt occured
    @ INT_RETURN
    
    ;--- 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

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    DT_HID already uses the USB interrupt.
    If you override it, then USB can't happen anymore.

    Besides, your routine is not suitable for interrupts anyway.
    There's 1000 loops with pauses and gosubs ... wouldn't work.

    There is already a way to determine if the cable is plugged in or not, and that is handled by DT_HID.

    When the cable is plugged in, and the USB is connected to the PC and has fully enumerated. The Plugged bit will be 1. When not plugged, it's 0.

    So just put your routine in the Main loop, and only run it IF Plugged = 1.

    Don't forget to restore the INT_Handler ...
    Code:
    ASM
    INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
            INT_Handler   USB_Handler
        endm
        INT_CREATE               ; Creates the interrupt processor
    endasm
    Also, DO NOT enter sleep mode when using USB.
    <br>
    DT

  4. #4
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Unhappy Bad News!

    Quote Originally Posted by Darrel Taylor View Post

    Also, DO NOT enter sleep mode when using USB.
    <br>
    So what your saying is I can't have the MCU in SLEEP mode and wake it up with an intterrupt when the USB cable is connected? Unfortunately, that is exactly what we wanted to do to save battery power on our application. It is only going to have someone plug a USB cable into it once per month and we wanted to have the MCU sleep except for about 1 minute at midnight each night and also when it then sees a USB cable plug-in once per month. Keeping it awake all the time to accomadate a possible USB connection will not meet our battery life requirements.
    Is there any way to save power consumption by the embedded processing application when a USB cable is not plugged in???

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,825


    Did you find this post helpful? Yes | No

    Default

    Get +5Vfrom USB itself maybe?

    Ioannis

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    You can.
    But before you put it to sleep, you'll need to put the USB module in suspend mode UCON.1, which can only be done reliably when unplugged.

    This disables the USB voltage regulator and stops the clock to the SIE, which will give the lowest current consumption.

    The USB routines are not setup to handle a resume.
    So your best bet it probably to connect the +5V from the USB cable to one of the INT pins with a pull-down resistor.

    When the cable gets plugged in, it will generate an interrupt, at which point you would execute an @ RESET to reset the processor and initialize the USB again.

    HTH
    DT

  7. #7
    Join Date
    Mar 2009
    Location
    Colorado
    Posts
    378


    Did you find this post helpful? Yes | No

    Default Made changes per your suggestions...now an assembly error

    :(
    Quote Originally Posted by Darrel Taylor View Post
    The USB routines are not setup to handle a resume.
    So your best bet it probably to connect the +5V from the USB cable to one of the INT pins with a pull-down resistor.
    When the cable gets plugged in, it will generate an interrupt, at which point you would execute an @ RESET to reset the processor and initialize the USB again.
    HTH
    OK, so I made changes to my code per your suggestions (see partial code below) including an IF Plugged =1 test in the Main loop to pole for USB connection and go to my USB servicing procedure when connected. I didn't do anything to get SLEEP mode yet because I want to make sure the interrupts are working first in this code.
    This code compiles, but creates a single error during assembly that says
    "Error[101]c:\PathToAsmfile.asm 1132:ERROR:(INT_ENABLE priority state not found)". So I can't get it to run yet with this error.
    I'm not sure what this error is telling me...the required statement for @ INT_ENABLE INT2_INT is in the code. Does it also require an INT_ENABLE USB_HANDLER statement for the USB interrupt??
    Code:
    Include "Modedefs.Bas"
    INCLUDE "DT_INTS-18.bas"    ' Base Interrupt System
    INCLUDE "ReEnterPBP-18.bas" ' Include if using PBP high priority interrupts
    INCLUDE "ALLDIGITAL.pbp"    ' Sets all registers for digital ops.
    
    DEFINE OSC 48
    
    ;--- 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 ; PortB resets as digital
       __CONFIG    _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
    ENDASM
    
    ' Initialize Hardware 
    ' ===================
        Spare_1         VAR PORTB.7   ' PGD for ICSP & Spare I/O for normal ops
        Spare_2         VAR PORTB.6   ' PGC for ICSP & Spare I/O for normal ops 
        'Set registers
          TRISA = 0             ' PORTA all outputs for LCD use              
          TRISB =%00011000      ' RB4 & RB3 set as RTC Alarm1 & Alarm2 inputs 
          TRISC = 0      
          TRISD = 0 
          TRISE = 0
       
    '------------SETUP FOR USING DS1337 Real Time Clock (RTC)--------------- 
    '*********************************************************************** 
    ' Setup Hardware for uart
    ' =======================
        DEFINE HSER_BAUD 115200
        DEFINE HSER_RCSTA 90h
        DEFINE HSER_TXSTA 24h
        DEFINE HSER_CLROERR 1
    
    ' Aliased Variables for CLock 
    ' =========================== 
        Alarm1      VAR PORTB.4     ' Alarm1 input from DS1337 INTA
        Alarm2      VAR PORTB.3     ' Alarm2 input from DS1337 INTB
        SCL         VAR PORTB.1     ' I2C clock pin 
        SDA         VAR PORTB.0     ' I2C data pin 
        RTC         CON %11010000   ' RTC device write address(byte addressing)
        ' This is a list of allowable clock contrl settings, one which must be
        ' setup in the SetTimeAndDate subroutine as intended.                   
        'contrl     CON %00000011   ' Starts the oscillator, sets the SQW/OUT 
                                    ' to 1 Hz, enables INTA interrupt from A1F
                                    ' or from A2F.  WORKS as intended!!
        'contrl     CON %00000111   ' Starts oscillator, enables INTA interrupt 
                                    ' from A1F or INTB from A2F. WORKS OK! 
                                    ' Both interrupts staggered 30 secs apart.
     
    ' RTC Address definitions
    ' ======================== 
        SecReg      CON $00   ' seconds address (00 - 59) 
                              ' MSB of SecReg must be set to a 0 to enable RTC 
        MinReg      CON $01   ' minutes address (00 - 59) 
        HourReg     CON $02   ' hours address (01 - 12) or (00 - 23) 
        DayReg      CON $03   ' day address (1 - 7) 
        DateReg     CON $04   ' date address (01 - 28/29, 30, 31) 
        MonthReg    CON $05   ' month address (01 - 12) 
        YearReg     CON $06   ' year address (00 - 99) 
    
    ' Alarm 1 Address definitions 
    ' ===========================
        Alm1sec     CON $07   ' Alarm 1 seconds address (00 - 59) 
        Alm1min     CON $08   ' Alarm 1 minutes address (00 - 59)
        Alm1hr      CON $09   ' Alarm 1 hours address (01 - 12) or (00 - 23) 
        Alm1Day     CON $0A   ' Alarm 1 day address (1 - 7)
    
    ' Alarm 2 Address definitions 
    ' ===========================
        Alm2min     CON $0B   ' Alarm 2 minutes address (00 - 59)
        Alm2hr      CON $0C   ' Alarm 2 hours address (01 - 12) or (00 - 23) 
        Alm2Day     CON $0D   ' Alarm 2 day address (1 - 7)
    
    ' Alias of Clock register addresses
    ' =================================
       ContReg     CON $0E         ' CONTROL register address 
       StatusReg   CON $0F         ' STATUS register address
     
    ' Clock Variables
    ' ================
        sec         VAR BYTE  ' seconds 
        MINs        VAR BYTE  ' minutes 
        hr          VAR BYTE  ' hours 
        day         VAR BYTE  ' day 
        date        VAR BYTE  ' date 
        mon         VAR BYTE  ' month 
        yr          VAR BYTE  ' year 
    
    ' ALARM1 VARIABLES
    ' ================
        A1sec       VAR BYTE  ' seconds 
        A1MINs      VAR BYTE  ' minutes 
        A1hr        VAR BYTE  ' hours 
        A1day       VAR BYTE  ' day 
        
    ' ALARM2 VARIABLES
    ' ================
        A2MINs      VAR BYTE  ' minutes
        A2hr        VAR BYTE  ' hours
        A2day       VAR BYTE  ' day
                               
    GoSub SetTimeAndDate        ' Setup current time & alarm settings 
    
    '*----------------SETUP FOR USING USB INTERFACE------------------------- 
    '*********************************************************************** 
    INCLUDE "DT_HID260.pbp"
    
    DEFINE USB_VENDORID    6017
    DEFINE USB_PRODUCTID   2000
    DEFINE USB_VERSION     1
    DEFINE USB_VENDORNAME  "Mister E/DT"
    DEFINE USB_PRODUCTNAME "USBDemo"
    DEFINE USB_SERIAL      "001"
    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
    
    '------------------------SETTINGS FOR INTERRUPTS------------------------ 
    '*********************************************************************** 
    'Set interrupt related registers
        RCON.7 = 1     ' Set Interrupt Priority Enable bit (Bit7=IPEN) 
                             ' to enable priority levels on interrupts
        INTCON.7 = 1         ' Set Global Interrupt Enable bit
        INTCON.6 = 1         ' Set PEIE to enable low priority periph interrupts
        INTCON2 = %10001000  ' Enable PORTB Pull-ups (Bit7-high), set INTEG2
                             ' for falling edge (Bit4-low) on RTC's interrupt.
        INTCON3 = %10001000  ' Set INT2IP high priority (Bit7-high), 
                             ' enable INT2IE for interrupt(Bit4-high)
        RTC_INT_FLG  VAR INTCON3.1 'Alias for RB4 INTA interrupt from RTC
    'Setup external interrupt macros
    ASM
    INT_LIST  macro    ; IntSource,         Label,  Type, ResetFlag?
            INT_Handler   USB_Handler
            INT_Handler   INT2_INT,        _Alarm,   PBP,  yes
        endm
        INT_CREATE                ; Creates the interrupt processor
    ENDASM
    @    INT_ENABLE   INT2_INT     ; enable external (INT) interrupts
    
    '  -----------------------BEGIN MAIN PROGRAM LOOP----------------------- 
    '***********************************************************************  
    Main:
        Pause 1000
        IF Plugged = 1 THEN
            HIGH PORTC.6 
            GOTO USB  ' USB cable is plugged-in
        ENDIF
        ' Else, do other things if not connected
        ' If Alarm interrupt, read current time & ALARM settings from DS1337
        ' Perform range measurements if Midnight
        LOW PORTC.6
    USB:
        'Process data from EEPROM to PC via USB interface        
        
        GoTo Main                       ' Endless Loop 
    End     ' Safety measure to insure program stops if reaches here 
    
    '--------------------( Begin Interrupt Handler )------------------------ 
    '*********************************************************************** 
    Alarm:
           HIGH PORTC.7      ' Test LED if Interupt
           '  Put code here to service the Alarm interrupt
           LOW PORTC.7     
        ' Resume Main Program 
    @ INT_RETURN
    End     ' Safety measure to insure program stops if reaches here
    '------------------{ End of Interrupt Handler }---------------------------
    
    ' START LIST OF SUBROUTINES
    '**************************
    Last edited by jellis00; - 7th December 2009 at 23:30.

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Put the Interrupt definitions before the HID stuff.

    That's the way it was originally. But it got reversed in your program.

    Also, don't mess with all the INTCON and RCON registers.
    DT_INTS handles all those for you.
    If you change them, it can interfere with the interrupt system.
    <br>
    DT

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. How to receive stream of bytes using PIC USART
    By unifoxz in forum mel PIC BASIC Pro
    Replies: 34
    Last Post: - 20th June 2009, 10:38
  3. Replies: 9
    Last Post: - 31st July 2008, 08:56
  4. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 3

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

Tags for this Thread

Posting Permissions

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