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

    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

  2. #2
    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???

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default

    Get +5Vfrom USB itself maybe?

    Ioannis

  4. #4
    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

  5. #5
    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; - 8th December 2009 at 00:30.

  6. #6
    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

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


    Did you find this post helpful? Yes | No

    Default A couple of more questions re: USBDemo

    Darrel, first of all let me commend you on they way you created the USBdemo. It has been a god send to me to save time in getting my application to work with a USB interface. However, I have a couple of more questions:
    1) I notice that this declaration implies you must use BufferIn(X) where X is either 8.16, 32, or 64.
    "DEFINE USB_INSIZE 8 ; IN report is PIC to PC (8,16,32,64)"
    Is that true also with the BufferOut(X) per this statement?
    DEFINE USB_OUTSIZE 8 ; OUT report is PC to PIC
    I ask this question because I need 10 data elements per BufferOut transmission for my application and didn't know whether I could size the BufferOut as 10 or would have to use 16.

    2) In my application I need the PIC to read individual Bytes out of EEPROM and then send them as data to the PC via the USB interface. The data is stored in EEPROM during normal ops as 75 data sets, each data set consisting of 3 bytes. The PIC code reads each data set into a three element array, arrData(i,j,k), where i, j,k range from 0 to 74. My question is how to code the PIC to best fit this data into the BufferOut array to send each arrData(i,j,k) over the USB interface, at the rate of one per second. The data is in the PIC code in the form of:
    arrData(0,0,0)
    arrData(1,1,1
    arrData(2,2,2)
    |
    |
    arrData(74,74,74)
    I want each data set to show up at the PC as the three bytes contained in the array element. I guess my question is in essence how to convert each array element into the three BufferOut(X) bytes for transmission.

    3) I also notice that the PC's RX Window of my application is showing continuous receipt of a e garbagvalue shown as "p|?|6||", even when none of the PORTA pushbuttons are pressed. But when PORTA.4 button is pressed, the string value "USBDemo" is correctly received. However, when any of the other PORTA buttons are pressed it has no impact on the data stream and just shows another "p|?|6||". All other functions of the USBDemo interface work OK. It almost seems like there is a continuous button bounce going on in PORTA, but causing transmit of garbage values rather than the "USBDemo" string. I don't have a debug ability to see what is causing this. Any ideas?? If seeing my code would help you to assess this, let me know and I will somehow get it to you.
    Last edited by jellis00; - 11th January 2010 at 19:30.

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


    Did you find this post helpful? Yes | No

    Default

    Both USB_INSIZE and USB_OUTSIZE can only be 8,16,32 or 64.
    If you want to send 10-bytes per report, it needs to be 16.

    Question 2, I don't understand.
    If you have them in arrays, just copy them to the USBTXBuffer.

    And what looks like "garbage data" is the analog reading from the pots being sent periodically.
    <br>
    DT

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


    Did you find this post helpful? Yes | No

    Default Question re: DT_INTS

    Quote Originally Posted by Darrel Taylor View Post
    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>
    Darrel, I have discovered that when I am using INT2_INT interrupt source from DS1337 to the 18F4550 that I have to have the INTCON2 statement per below code...otherwise the interrupt doesn't work. When you said that I shouldn't mess with the INTCON register settings because DT_INTS takes care of them, did you mean only the GIE setting?? In fact, can you explain which of the INTCON register settings ARE NOT handled by DT_INTS, if more than just this one for INT2 falling edge??

    Code:
    '----------------------[ SETUP FOR INTERRUPTS ]-------------------------  
    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
    ' Per DT, DT_INTS already takes care of setting INTCON and RCON registers
    ' but this doesn't work unles INTCON2 set per below:
        'INTCON.7 = 1         ' Set Global Interrupt Enable bit
        INTCON2 = %00000000  ' Set INT2 for falling edge (Bit4-low)
                             ' on RTC's interrupt.
        'INTCON3 = %10010000  ' Set INT2 high priority (Bit7-high), enable INT2
                             ' (Bit4-high)

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


    Did you find this post helpful? Yes | No

    Default

    Enable bits (IE), Interrupt Flags (IF) or Priority bits (IP).
    Don't "mess" with them. That includes GIE and PEIE or GIEL.

    Many interrupt sources have registers that affect the specific peripheral.
    For instance ...
    • If you don't set TXSTA and RCSTA then you will never get proper interrupts from the USART.
    • If you don't set T1CON, you will never get interrupts from TMR1_INT.
    • And ...
      If you don't set the INTEDG bits, you may never get the correct interrupt from INT2_INT.

    You have to tell the hardware how to handle the signals.
    DT_INTS will handle the interrupts.
    <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, 08: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, 11:38
  3. Replies: 9
    Last Post: - 31st July 2008, 09:56
  4. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 01:55
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 02:07

Members who have read this thread : 1

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