// 1 to use a PIC with an internal USB Peripheral
// 0 to use a National USBN960x peripheral
#define __USB_PIC_PERIF__ 1
#define USB_HID_DEVICE TRUE
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_TX_SIZE 14 //max packet size of this endpoint
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_RX_SIZE 8
#include <18F2550.h>
#device adc=8
#fuses HSPLL,PLL5,USBDIV,CPUDIV1,VREGEN,NOWDT,NOPROTECT,NOLVP,NODEBUG
#fuses WDT128, BROWNOUT, BORV20, NOPUT, NOCPD, STVREN
#fuses NOWRT, NOWRTD, IESO, FCMEN, NOPBADEN, NOWRTC, NOWRTB
#fuses NOEBTR, NOEBTRB, NOCPB, MCLR, LPT1OSC, NOXINST
#use delay(clock=48000000)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors,stream=rs232)
#use fast_io(A)
#use fast_io(B)
/*
Variables de preprocesado para que la libreria usb del CCS se compile
de acuerdo a nuestra configuración usb, endpoints, etc.
*/
// 1 to use a PIC with an internal USB Peripheral
// 0 to use a National USBN960x peripheral
#define __USB_PIC_PERIF__ 1
#define USB_HID_DEVICE TRUE
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_TX_SIZE 14 //max packet size of this endpoint
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN bulk/interrupt transfers
#define USB_EP1_RX_SIZE 8
/*
Includes de la libreria CCS, el orden es:
#include <hardware_layer.h>
#include <custom_config.h>
#include <usb.c>
Hardware layer dependerá del pic que estemos usando, para nuestro caso pic18_usb.h
Custom config incluirá los descriptores usb de nuestra aplicación, que se negociarán
en el proceso de enumeración.
*/
#include <pic18_usb.h>
#include "usb_descriptors.h"
#include <usb.c>
/*
Definitions
*/
#define P_UP PIN_A0
#define P_RIGHT PIN_A1
#define P_BOTTOM PIN_A2
#define P_LEFT PIN_A3
#define P_BTN1 PIN_B7
#define P_BTN2 PIN_B6
#define P_BTN3 PIN_B5
#define P_BTN4 PIN_B4
#define P_BTN5 PIN_B3
#define P_BTN6 PIN_B2
#define P_START PIN_B1
#define P_COIN PIN_B0
#define N_IO 12
int8 pins[N_IO] = {
P_UP, P_RIGHT, P_BOTTOM, P_LEFT,
P_BTN1, P_BTN2,P_BTN3, P_BTN4,
P_BTN5, P_BTN6, P_START, P_COIN
};
int8 keys[N_IO] = {
0x52,0x4F,0x51,0x50,
0xE4,0xE6,0x2C,0xE5,
0x1D,0x1B,0x1E,0x22
};
#define DB_COUNTER 10
int8 debouncing[N_IO] = {
DB_COUNTER,DB_COUNTER,DB_COUNTER,DB_COUNTER,
DB_COUNTER,DB_COUNTER,DB_COUNTER,DB_COUNTER,
DB_COUNTER,DB_COUNTER,DB_COUNTER,DB_COUNTER
};
int16 old_state = 0x0000;
int16 current_state = 0x0000;
/*
Function prototypes
*/
void SetUp( void );
void SetUpIO( void );
void SendKey( char KEY );
void ReceiveLeds( void );
static int8 send_buffer[USB_EP1_TX_SIZE];
static int8 receive_buffer[USB_EP1_RX_SIZE];
void main( void )
{
int8 I;
int8 J;
int8 changed = FALSE;
SetUp();
usb_init();
while ( 1 )
{
usb_task();
if( usb_enumerated() )
{
// First check the state of the IO pins, and later test if there are changes
// and if there are send a packet with the current pressed keys
for( I = 0; I < N_IO; I++ )
{
if ( input( pins[I] ) )
{
bit_set( current_state, I );
}
else
{
bit_clear( current_state, I );
}
}
// Now check the changes
memset( send_buffer, 0, sizeof(send_buffer) );
changed = FALSE;
J = 0;
for( I = 0; I < N_IO; I++ )
{
if ( bit_test( current_state, I ) != bit_test( old_state, I ) )
{
if( debouncing[I] == 0 )
{
// This is neccessary because if the keys are depressed we need to send
// at least an empty keys report
changed = TRUE;
if( bit_test( current_state, I ) )
{
bit_set( old_state, I );
// Add the key to the send buffer
send_buffer[2+J] = keys[I];
J++;
}
else
{
bit_clear( old_state, I );
// If it is depressed do nothing
}
debouncing[I] = DB_COUNTER;
}
else
{
debouncing[I]--;
}
}
}
if( changed )
{
// There are keys on the buffer
usb_put_packet(1, send_buffer, sizeof(send_buffer), USB_DTS_TOGGLE);
}
if( usb_kbhit(1) )
{
usb_get_packet(1, receive_buffer, sizeof(receive_buffer));
}
}
}
}
void SetUp( void )
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_OFF);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
SetUpIO();
}
void SetUpIO( void )
{
set_tris_a(0xff);
set_tris_b(0xff);
}