[Programmation] Communication (PIC+ADC)<->PC en USB
Répondre à la discussion
Affichage des résultats 1 à 6 sur 6

Communication (PIC+ADC)<->PC en USB



  1. #1
    invited81c9f44

    Communication (PIC+ADC)<->PC en USB


    ------

    bonsoir tout le monde. je suis à présent sur un projet de conception d'une carte électronique d'acquisition des données montée autour du pic 18F4550. j'ai des difficultés à l'envoie des données à mon pc via usb et la reception correcte des données au pc. j'utilise MikroC comme compilateur. le pic de cette carte doit envoyer des données qu'il a lui même reçu d'un ADC externe à lui. svp aidez moi en me donnant des exemples de programme pic(.hex) dans ce type de situation. Je reste ouvert à toutes suggestions merci d'avance.

    cordialement.

    -----

  2. #2
    invited81c9f44

    Re : Communication PIC<->PC en USB

    PS l'ADC externe dont je parle est le MCP3001

  3. #3
    paulfjujo

    Re : Communication (PIC+ADC)<->PC en USB

    bonjour,


    recherche plutot sur le forum mikroelectronika
    uù il y a moultes exemples ...

    je t'en mets un ici, parmi tant d'autres
    je n'ai pas testé perso celui ci
    car je n'ai qu'un 18F2550 ( avec moins d epattes) mais bien testé aussi .
    qui utilise le HID terminal de mikroC ..

    Code:
    /*
     * Project name:
         HID_Read_Write_Interrupt (Testing the USB HID connection)
     * Copyright:
         (c) Mikroelektronika, 2011.
     * Revision History:
         20110929:
           - initial release (FJ);
     * Description:
         This example establishes connection with the HID terminal that is active
         on the PC. Upon connection establishment, the HID Device Name will appear
         in the respective window. After that software will wait for data and
         it will return received data back.
     * Test configuration:
         MCU:             PIC18F4550
                          http://ww1.microchip.com/downloads/en/DeviceDoc/39632D.pdf
         Dev.Board:       EasyPIC7 - ac:USB
                          http://www.mikroe.com/eng/products/view/757/easypic-v7-development-system/
         Oscillator:      HS, 8.000 MHz  (USB osc. is raised with PLL to 48.000MHz)
         Ext. Modules:    None.
         SW:              mikroC PRO for PIC
                          http://www.mikroe.com/eng/products/view/7/mikroc-pro-for-pic/
     * NOTES:
         (*) Be VERY careful about the configuration flags for the 18F4550 - there's
         so much place for mistake!
        - Place jumpers J12 in the right (USB) position.
     */
    
    unsigned char readbuff[64] absolute 0x510;   // Buffers should be in USB RAM, please consult datasheet
    unsigned char writebuff[64] absolute 0x550;
    
    char cnt;
    char kk;
    void USB_Init_Desc();
    const unsigned int USB_VENDOR_ID = 0x1234;
    const unsigned int USB_PRODUCT_ID = 0x1234;
    const char USB_SELF_POWER = 0xC0;            // Self powered 0xC0,  0x80 bus powered
    const char USB_MAX_POWER = 50;               // Bus power required in units of 2 mA
    const char HID_INPUT_REPORT_BYTES = 63;
    const char HID_OUTPUT_REPORT_BYTES = 63;
    const char USB_TRANSFER_TYPE = 0x03;         //0x03 Interrupt
    const char EP_IN_INTERVAL = 1;
    const char EP_OUT_INTERVAL = 1;
    const char USB_INTERRUPT = 1;
    const char USB_HID_EP = 1;
    const char USB_HID_RPT_SIZE = 57;
    
    /* Device Descriptor */
    const struct {
        char bLength;               // bLength         - Descriptor size in bytes (12h)
        char bDescriptorType;       // bDescriptorType - The constant DEVICE (01h)
        unsigned int bcdUSB;        // bcdUSB          - USB specification release number (BCD)
        char bDeviceClass;          // bDeviceClass    - Class Code
        char bDeviceSubClass;       // bDeviceSubClass - Subclass code
        char bDeviceProtocol;       // bDeviceProtocol - Protocol code
        char bMaxPacketSize0;       // bMaxPacketSize0 - Maximum packet size for endpoint 0
        unsigned int idVendor;      // idVendor        - Vendor ID
        unsigned int idProduct;     // idProduct       - Product ID
        unsigned int bcdDevice;     // bcdDevice       - Device release number (BCD)
        char iManufacturer;         // iManufacturer   - Index of string descriptor for the manufacturer
        char iProduct;              // iProduct        - Index of string descriptor for the product.
        char iSerialNumber;         // iSerialNumber   - Index of string descriptor for the serial number.
        char bNumConfigurations;    // bNumConfigurations - Number of possible configurations
    } device_dsc = {
          0x12,                   // bLength
          0x01,                   // bDescriptorType
          0x0200,                 // bcdUSB
          0x00,                   // bDeviceClass
          0x00,                   // bDeviceSubClass
          0x00,                   // bDeviceProtocol
          8,                      // bMaxPacketSize0
          USB_VENDOR_ID,          // idVendor
          USB_PRODUCT_ID,         // idProduct
          0x0001,                 // bcdDevice
          0x01,                   // iManufacturer
          0x02,                   // iProduct
          0x03,                   // iSerialNumber
          0x01                    // bNumConfigurations
      };
    
    /* Configuration 1 Descriptor */
    const char configDescriptor1[]= {
        // Configuration Descriptor
        0x09,                   // bLength             - Descriptor size in bytes
        0x02,                   // bDescriptorType     - The constant CONFIGURATION (02h)
        0x29,0x00,              // wTotalLength        - The number of bytes in the configuration descriptor and all of its subordinate descriptors
        1,                      // bNumInterfaces      - Number of interfaces in the configuration
        1,                      // bConfigurationValue - Identifier for Set Configuration and Get Configuration requests
        0,                      // iConfiguration      - Index of string descriptor for the configuration
        USB_SELF_POWER,         // bmAttributes        - Self/bus power and remote wakeup settings
        USB_MAX_POWER,          // bMaxPower           - Bus power required in units of 2 mA
    
        // Interface Descriptor
        0x09,                   // bLength - Descriptor size in bytes (09h)
        0x04,                   // bDescriptorType - The constant Interface (04h)
        0,                      // bInterfaceNumber - Number identifying this interface
        0,                      // bAlternateSetting - A number that identifies a descriptor with alternate settings for this bInterfaceNumber.
        2,                      // bNumEndpoint - Number of endpoints supported not counting endpoint zero
        0x03,                   // bInterfaceClass - Class code
        0,                      // bInterfaceSubclass - Subclass code
        0,                      // bInterfaceProtocol - Protocol code
        0,                      // iInterface - Interface string index
    
        // HID Class-Specific Descriptor
        0x09,                   // bLength - Descriptor size in bytes.
        0x21,                   // bDescriptorType - This descriptor's type: 21h to indicate the HID class.
        0x01,0x01,              // bcdHID - HID specification release number (BCD).
        0x00,                   // bCountryCode - Numeric expression identifying the country for localized hardware (BCD) or 00h.
        1,                      // bNumDescriptors - Number of subordinate report and physical descriptors.
        0x22,                   // bDescriptorType - The type of a class-specific descriptor that follows
        USB_HID_RPT_SIZE,0x00,  // wDescriptorLength - Total length of the descriptor identified above.
    
        // Endpoint Descriptor
        0x07,                   // bLength - Descriptor size in bytes (07h)
        0x05,                   // bDescriptorType - The constant Endpoint (05h)
        USB_HID_EP | 0x80,      // bEndpointAddress - Endpoint number and direction
        USB_TRANSFER_TYPE,      // bmAttributes - Transfer type and supplementary information
        0x40,0x00,              // wMaxPacketSize - Maximum packet size supported
        EP_IN_INTERVAL,         // bInterval - Service interval or NAK rate
    
        // Endpoint Descriptor
        0x07,                   // bLength - Descriptor size in bytes (07h)
        0x05,                   // bDescriptorType - The constant Endpoint (05h)
        USB_HID_EP,             // bEndpointAddress - Endpoint number and direction
        USB_TRANSFER_TYPE,      // bmAttributes - Transfer type and supplementary information
        0x40,0x00,              // wMaxPacketSize - Maximum packet size supported
        EP_OUT_INTERVAL         // bInterval - Service interval or NAK rate
    };
    
    const struct {
      char report[USB_HID_RPT_SIZE];
    }hid_rpt_desc =
      {
         {0x06, 0x00, 0xFF,       // Usage Page = 0xFF00 (Vendor Defined Page 1)
          0x09, 0x01,             // Usage (Vendor Usage 1)
          0xA1, 0x01,             // Collection (Application)
          0x85, 0x01,             // Report ID=1 (This is a global variable)
          0x15, 0x00,             // Logical Minimum (data bytes in the report may have minimum value = 0. This is a global variable)
          0x26, 0xFF, 0x00,       // Logical Maximum (data bytes in the report may have maximum value = 256. This is a global variable)
    
      // Input report(ID=1)
          0x19, 0x01,             // Usage Minimum
          0x29, 0xFF,             // Usage Maximum
          0x75, 0x08,             // Report Size: 8-bit field size
          0x95, 0x3F,             // Report Count (63 + 1 byte for the report ID)
          0x81, 0x02,             // Input (Data, Var, Abs)
    
      // Output report(ID=1)
          0x19, 0x01,             // Usage Minimum
          0x29, 0xFF,             // Usage Maximum
          0x75, 0x08,             // Report Size: 8-bit field size
          0x95, 0x3F,             // Report Count (63 + 1 byte for the report ID)
          0x91, 0x02,             // Output (Data, Var, Abs)
          
          
          0x85, 0x02,             // Report ID=1 (This is a global variable)
          
          // Input report(ID=2)
          0x19, 0x01,             // Usage Minimum
          0x29, 0x08,             // Usage Maximum
          0x75, 0x08,             // Report Size: 8-bit field size
          0x95, 0x01,             // Report Count (1 byte + 1 byte for the report ID)
          0x81, 0x02,             // Input (Data, Var, Abs)
          
          // Output report(ID=2)
          0x19, 0x01,             // Usage Minimum
          0x29, 0x08,             // Usage Maximum
          0x75, 0x08,             // Report Size: 8-bit field size
          0x95, 0x01,             // Report Count (1 byte + 1 byte for the report ID)
          0x91, 0x02,             // Output (Data, Var, Abs)
    
          0xC0},                   // End Collection
      };
    
    //Language code string descriptor
    const struct {
      char bLength;
      char bDscType;
      unsigned int string[1];
      } strd1 = {
          4,
          0x03,
          {0x0409}
        };
    
    
    //Manufacturer string descriptor
    const struct{
      char bLength;
      char bDscType;
      unsigned int string[13];
      }strd2={
        28,           //size of this descriptor string
        0x03,
        {'D','e','n','n','i','s',' ','L','e','p','i','n','e'}
      };
    
    //Product string descriptor
    const struct{
      char bLength;
      char bDscType;
      unsigned int string[10];
    }strd3={
        22,          //size of this descriptor string
        0x03,
        {'P','I','C','1','8','F','4','5','5','0'}
     };
     //Serial Number descriptor
    const struct{
      char bLength;
      char bDscType;
      unsigned int string[6];
    }strd4 = {
        14,          //size of this descriptor string
        0x03,
        {'A','B','C','1','2','3'} // Count as two bytes
     };
    
    
    
    //Array of configuration descriptors
    const char* USB_config_dsc_ptr[1];
    
    //Array of string descriptors
    const char* USB_string_dsc_ptr[3];
    
    void USB_Init_Desc(){
      USB_config_dsc_ptr[0] = &configDescriptor1;
      USB_string_dsc_ptr[0] = (const char*)&strd1;
      USB_string_dsc_ptr[1] = (const char*)&strd2;
      USB_string_dsc_ptr[2] = (const char*)&strd3;
      USB_string_dsc_ptr[3] = (const char*)&strd4;
      }
    void interrupt(){
       USB_Interrupt_Proc();                  // USB servicing is done inside the interrupt
    }
    
    void main(void){
      ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
      CMCON  |= 7;                            // Disable comparators
      TRISB=0;
      LATB=0;
      HID_Enable(&readbuff,&writebuff);       // Enable HID communication
    
     while(1){
        while(!HID_Read());                   // Wait for a report
        
        /* Report ID=1 function*/
        if (readbuff[0]==1){                  // If report ID is "1"
        for(cnt=0;cnt<64;cnt++){              // Then copy "text" report over
          writebuff[cnt]=readbuff[cnt];       // To the write buffer
          }
        while(!HID_Write(&writebuff,64));     // And send it back to the PC application
        }
        
         /* Report ID=2 function*/
        if (readbuff[0]==2){                 // If report ID is "2"
        LATB=readbuff[1];                    // Then send next byte to Port B as an "output control"
        writebuff[0]=2;                      // Set the report ID to "2"
        writebuff[1]=0;                      // Set an acknowledgment byte
        while(!HID_Write(&writebuff,2));     // And send an acknowledgment report back to the PC application
    }
    
    
      }
    
    
    }

  4. #4
    invited81c9f44

    Re : Communication (PIC+ADC)<->PC en USB

    Merci paulfjujo je me met toute suite au travail.

  5. A voir en vidéo sur Futura
  6. #5
    invited81c9f44

    Re : Communication (PIC+ADC)<->PC en USB

    Paulfjujo si vous avez un cas de figure pareil avec le Pic 18F2550 svp faites moi part de cela. Merci

  7. #6
    paulfjujo

    Re : Communication (PIC+ADC)<->PC en USB

    exemples autour de la carte Startusb PIC18F2550

Discussions similaires

  1. Power Line communication sur une ligne DC. Communication CAN
    Par invitee2723b34 dans le forum Électronique
    Réponses: 3
    Dernier message: 21/02/2012, 12h28
  2. Communication RF
    Par invitebee83688 dans le forum Électronique
    Réponses: 4
    Dernier message: 05/08/2011, 10h00
  3. Communication spi
    Par invite71317231 dans le forum Électronique
    Réponses: 5
    Dernier message: 22/01/2011, 22h11
  4. Communication GPS
    Par invitef97198f3 dans le forum Électronique
    Réponses: 7
    Dernier message: 10/02/2010, 15h10
  5. communication
    Par invite5a90430f dans le forum Orientation après le BAC
    Réponses: 3
    Dernier message: 11/08/2007, 15h38
Dans la rubrique Tech de Futura, découvrez nos comparatifs produits sur l'informatique et les technologies : imprimantes laser couleur, casques audio, chaises gamer...