All pastes #1565529 Raw Edit

MikroC HID Example

public text v1 · immutable
#1565529 ·published 2009-09-14 14:28 UTC
rendered paste body
/*
 * Project name:
     HIDtest2 (USB HID Read & Write Test)
 * Copyright:
     (c) MikroElektronika, 2005-2008
 * Revision History:
     20050502:
       - initial release;
 * 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. The character that user sends to PIC from the HID
     terminal will be re-sent back to user.
 * Test configuration:
     MCU:             PIC18F4550
                      http://ww1.microchip.com/downloads/en/DeviceDoc/39632D.pdf
     Dev.Board:       EasyPIC5
                      http://www.mikroe.com/en/tools/easypic5/
     Oscillator:      HS 8.000 MHz  (USB osc. is raised with PLL to 48.000MHz)
     Ext. Modules:    on-board USB-HID
                      http://www.mikroe.com/pdf/easypic5/easypic5_manual.pdf#page27
     SW:              mikroC PRO for PIC
                      http://www.mikroe.com/en/compilers/mikroc/pro/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 position
*/

/********************************************************
********************************************************/

#define BUFFER_SIZE 64

/*ALSO EDIT THIS PART OF USBdsc.c :

unsigned char const HID_INPUT_REPORT_BYTES      = 64; (previously 1)
unsigned char const HID_OUTPUT_REPORT_BYTES     = 64; (previously 1)

Data is now sent in blocks of 64 bytes, instead of just 1 !!

*********************************************************                                                  k
*********************************************************/


unsigned char userWR_buffer[BUFFER_SIZE], userRD_buffer[BUFFER_SIZE];
void Init_Main();

//**************************************************************************************************
// Main Program Routine
//**************************************************************************************************

void main() {
  unsigned char i;
  
  Init_Main();
  
  /*Enable the USB HID Module*/
  LATB0_bit = 1;
  HID_Enable(&userRD_buffer, &userWR_buffer);
  Delay_ms(1000);  Delay_ms(1000);
  LATB0_bit = 0;

  while (1) {
                                      //Connect a LED to RB1 to
    LATB1_bit = ~LATB1_bit;           //show something is going on
    
    /*HID_Read() Returns a value not zero when there's data
      to be read!
    */
    if (HID_Read()) {
        //Clear the Write buffer to be sure no data from
        //previous actions is still there.
        for (i = 0; i < BUFFER_SIZE ; i++) {
            userWR_buffer[i] = 0;
        }
        
        /* This part Toggles a LED connected to pin RB0
           if the proper command is received from PC.
           The command is defined by the first byte.
        */
        if (userRD_buffer[0] == 0x32) {
        LATB0_bit = !LATB0_bit;
        }
        
        /*This part sends back the status of RB0
          if the proper command is received from PC.
          Again, command defined by first byte.
          Although only one byte contains valid data,
          all 64 bytes are sent. Application on Host
          must use correct location of data.
        */
        if (userRD_buffer[0] == 0x81) {
        UserWR_buffer[1] = LATB0_bit;
        while (!HID_Write(&userWR_buffer, BUFFER_SIZE));
        }

        /*This part reads a 32-byte wide array, reverses
          it and sends it back to host. The first byte
          contains the command from PC, byte 2 to 33 contain
          data.
        */
        if (userRD_buffer[0] == 0x53) {
           for (i = 1 ; i < 33 ; i++ ) {
               userWR_buffer[i] = userRD_buffer[33 - i];
           }
        while (!HID_Write(&userWR_buffer, BUFFER_SIZE));
        }
    }
  }
  HID_Disable();
}


//**************************************************************************************************
// Main Interrupt Routine
//**************************************************************************************************
void interrupt()
{
  HID_InterruptProc();

}
//**************************************************************************************************

//**************************************************************************************************
// Initialization Routine
//**************************************************************************************************

void Init_Main()
{
        //--------------------------------------
        // Disable all interrupts
        //--------------------------------------
        INTCON = 0;                             // Disable GIE, PEIE, TMR0IE,INT0IE,RBIE
        INTCON2 = 0xF5;
        INTCON3 = 0xC0;
        RCON.IPEN = 0;                          // Disable Priority Levels on interrupts
        PIE1 = 0;
        PIE2 = 0;
        PIR1 = 0;
        PIR2 = 0;

        ADCON1 |= 0x0F;                         // Configure all ports with analog function as digital
        CMCON  |= 7;                            // Disable comparators
        //--------------------------------------
        // Ports Configuration
        //--------------------------------------
        TRISA = 0xFF;
        TRISB = 0x00;
        TRISC = 0xFF;
        TRISD = 0;
        TRISE = 0x07;

        LATA = 0;
        LATB = 0;
        LATC = 0;
        LATD = 0;
        LATE = 0;

        //--------------------------------------
        // Visual Confirmation PIC Is Stable
        //--------------------------------------
        LATB0_bit = 1;
        Delay_ms(500);
        LATB0_bit = 0;
        Delay_ms(500);
        LATB0_bit = 1;
        Delay_ms(500);
        LATB0_bit = 0;
        Delay_ms(500);
}
//**************************************************************************************************





//***********************************************o***************************************************