#ifndef KEYBOARD_C #define KEYBOARD_C /** INCLUDES *******************************************************/ #include #include "./USB/usb.h" #include "HardwareProfile.h" #include "./USB/usb_function_hid.h" /** CONFIGURATION **************************************************/ // PIC18F14K50 #pragma config CPUDIV = NOCLKDIV ,USBDIV = OFF, FOSC = HS #pragma config PLLEN = OFF, PCLKEN = ON, HFOFST = OFF, DEBUG = OFF #pragma config PWRTEN = ON, BOREN = OFF, BORV = 30, MCLRE = ON #pragma config FCMEN = OFF, IESO = OFF, WDTEN = OFF, WDTPS = 1, LVP = OFF #pragma config XINST = OFF, STVREN = ON, BBSIZ = OFF #pragma config CP0 = OFF, CP1 = OFF, CPB = OFF, CPD = OFF #pragma config WRT0 = OFF, WRT1 = OFF, WRTC = OFF, WRTB = OFF, WRTD = OFF #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTRB = OFF /** VARIABLES ******************************************************/ #pragma udata BYTE old_sw1,old_sw2; USB_HANDLE lastINTransmission; USB_HANDLE lastOUTTransmission; /** PRIVATE PROTOTYPES *********************************************/ static void InitializeSystem(void); void ProcessIO(void); void UserInit(void); void Keyboard(void); BOOL Switch1IsPressed(void); BOOL Switch2IsPressed(void); void USBCBSendResume(void); void USBHIDCBSetReportComplete(void); /** VECTOR REMAPPING ***********************************************/ #if defined(USB_INTERRUPT) #pragma udata void YourHighPriorityISRCode(); void YourLowPriorityISRCode(); #define REMAPPED_RESET_VECTOR_ADDRESS 0x00 #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08 #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18 #pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS void Remapped_High_ISR (void) { _asm goto YourHighPriorityISRCode _endasm } #pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS void Remapped_Low_ISR (void) { _asm goto YourLowPriorityISRCode _endasm } #pragma code #pragma interrupt YourHighPriorityISRCode void YourHighPriorityISRCode() { USBDeviceTasks(); } #pragma interruptlow YourLowPriorityISRCode void YourLowPriorityISRCode() { } #endif /** DECLARATIONS ***************************************************/ #pragma code /******************************************************************** * Function: void main(void) *******************************************************************/ void main(void) { InitializeSystem(); #if defined(USB_INTERRUPT) USBDeviceAttach(); #endif while(1) { #if defined(USB_POLLING) // Check bus status and service USB interrupts. USBDeviceTasks(); #endif // Application-specific tasks. // Application related code may be added here, or in the ProcessIO() function. ProcessIO(); } } /******************************************************************** * Function: static void InitializeSystem(void) *******************************************************************/ static void InitializeSystem(void) { UserInit(); #if defined(USE_USB_BUS_SENSE_IO) tris_usb_bus_sense = INPUT_PIN; // See HardwareProfile.h #endif #if defined(USE_SELF_POWER_SENSE_IO) tris_self_power = INPUT_PIN; // See HardwareProfile.h #endif USBDeviceInit(); //usb_device.c. Initializes USB module SFRs and firmware variables to known states. } /****************************************************************************** * Function: void UserInit(void) *****************************************************************************/ void UserInit(void) { /* 入出力ポート設定 */ TRISA = 0x00; TRISB = 0x00; TRISC = 0xf0; ADCON0bits.ADON = 0; //AD OFF //Initialize all of the PUSH buttons & LED mInitAllSwitches(); old_sw1 = sw1; old_sw2 = sw2; led01 = 0; //LED ON led02 = 0; //initialize the variable holding the handle for the last // transmission lastINTransmission = 0; lastOUTTransmission = 0; } /******************************************************************** * Function: void ProcessIO(void) *******************************************************************/ void ProcessIO(void) { // User Application USB tasks if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) return; //Call the function that behaves like a keyboard Keyboard(); } void Keyboard(void) { static unsigned char key = 4; //Check if the IN endpoint is not busy, and if it isn't check if we want to send //keystroke data to the host. if(!HIDTxHandleBusy(lastINTransmission)) { if(Switch2IsPressed()) //SW2 ON? { //Load the HID buffer hid_report_in[0] = 0; //R-G,A,S,C, L-G,A,S,C if ( sw1 == 0 ) { //SW1 ON? hid_report_in[0] = 0x02; //R-G,A,S,C, L-G,A,S,C } hid_report_in[1] = 0; //0x00 hid_report_in[2] = key++; //key 1 hid_report_in[3] = 0; //key 2 hid_report_in[4] = 0; //key 3 hid_report_in[5] = 0; //key 4 hid_report_in[6] = 0; //key 5 hid_report_in[7] = 0; //key 6 //Send the 8 byte packet over USB to the host. lastINTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08); if(key == 40) { key = 4; } } else { //Load the HID buffer hid_report_in[0] = 0; hid_report_in[1] = 0; hid_report_in[2] = 0; //Indicate no character pressed hid_report_in[3] = 0; hid_report_in[4] = 0; hid_report_in[5] = 0; hid_report_in[6] = 0; hid_report_in[7] = 0; //Send the 8 byte packet over USB to the host. lastINTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x08); } } //Check if any data was sent from the PC to the keyboard device. Report descriptor allows //host to send 1 byte of data. Bits 0-4 are LED states, bits 5-7 are unused pad bits. //The host can potentially send this OUT report data through the HID OUT endpoint (EP1 OUT), //or, alternatively, the host may try to send LED state information by sending a //SET_REPORT control transfer on EP0. See the USBHIDCBSetReportHandler() function. if(!HIDRxHandleBusy(lastOUTTransmission)) { lastOUTTransmission = HIDRxPacket(HID_EP,(BYTE*)&hid_report_out,1); //Data is in the OutBuffer[0]. //Num Lock LED state is in Bit0. if(hid_report_out[0] & 0x01) //Make LED1 and LED2 match Num Lock state. { led02 = 0; //LED2 On; } else { led02 = 1; //LED2 Off } } return; } /****************************************************************************** * Function: BOOL Switch1IsPressed(void) *****************************************************************************/ BOOL Switch1IsPressed(void) { if(sw1 != old_sw1) { old_sw1 = sw1; // Save new value if(sw1 == 0) // If pressed return TRUE; // Was pressed } return FALSE; // Was not pressed } /****************************************************************************** * Function: BOOL Switch2IsPressed(void) *****************************************************************************/ BOOL Switch2IsPressed(void) { if(sw2 != old_sw2) { old_sw2 = sw2; // Save new value if(sw2 == 0) // If pressed return TRUE; // Was pressed } return FALSE; // Was not pressed } // ****************************************************************************************************** // ************** USB Callback Functions **************************************************************** // ******************************************************************************************************