Keyboard - C: Programa en C USB PIC18F
Keyboard - C: Programa en C USB PIC18F
//// ////
//// Joystick.c ////
//// Joystick 8 ejes 24 botones ////
//// Autor: Alejandro ////
//// ////
//// 02/05/2010 ////
//// ////
/////////////////////////////////////////////////////////////////////////
#include <18F2550.h>
#device ADC = 10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use fast_io(A)
#use fast_io(b)
#use fast_io(c)
#include <pic18_usb.h>
#include <PPM_USB_HID.h> //USB Configuration and Device descriptors
for this UBS device
#include <usb.c> //handles usb setup tokens and get
descriptor reports
#include <ctype.h>
/////////////////////////////////////////////////////////////////////////////
//
// Defines y Zarandajas
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//
// RAM
//
/////////////////////////////////////////////////////////////////////////////
//int8 connected;
int8 enumerated;
int8 rx_msg[USB_EP1_RX_SIZE];
int8 tx_msg[8]={2,0,0,0,0,0,0,0};
//0:Modifier
//1:Reserved
//2:Key1
//3:Key2
//4:Key3
//5:Key4
//6:Key5
//7:Key6
char NextChar='0';
int1 hay_dato=0;
int1 fLeer=0;
int1 fTX=0;
//int8 i;
int8 Btn1a8 = 0;
int8 Btn9a16 = 0;
int8 Btn17a24 = 0;
int8 Btn25a32 = 0;
int8 Btn33a40 = 0;
//Registros enviados por USB 40 Botones
/////////////////////////////////////////////////////////////////////////////
//
// usb_debug_task()
//
// When called periodically, displays debugging information over serial
// to display enumeration and connection states. Also lights LED2 and LED3
// based upon enumeration and connection status.
//
/////////////////////////////////////////////////////////////////////////////
void usb_debug_task(void) {
enumerated=usb_enumerated();
if(enumerated){
//LED_ON(LED1);
//LED_OFF(LED3);
}
else{
//LED_ON(LED3);
//LED_OFF(LED1);
}
}
/////////////////////////////////////////////////////////////////////////////
//
// usb_keyboard_task()
//
// Sends a packet of keyboard data. The protocol was specified in the HID
// report descriptor (see usb_desc_kbmouse.h), and is:
// tx_msg[0] = HID report id (2)
// tx_msg[1] = modifier (an 8bit bitmap of shift, tab, alt keypress)
// tx_msg[2] = const 0
// tx_msg[3:7] = an array of held down keys. a=4, b=5, etc.
// if msg[2:7]={0} then no keys are held down
//
// rx_msg[1] = HID report id (2)
// rx_msg[0] = 5bit bitmap of led status
//
/////////////////////////////////////////////////////////////////////////////
int8 ic;
if(isAlpha(c)){
ic=c-'a'+4;
}
else{
if(c=='0'){
ic=39;
}
else{
ic=c-'1'+30;
}
}
return(ic);
}
void usb_keyboard_task(void) {
if(hay_dato==1){
hay_dato=0;
tx_msg[2]=0;
tx_msg[3]=char_2_usb_kbd_code(NextChar);
usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
++NextChar;
if(NextChar>'z'){
NextChar='0';
}
if(NextChar>'9'&&NextChar<'a'){
NextChar='a';
}
LED_OFF(LED2);
delay_ms(5);
}else{
if(fLeer){
fLeer=0;
lectura_teclado(tx_msg);
fTx=1;
}
else if(fTx){
fTx=0;
usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
delay_ms(10);
}
else{
tx_msg[2]=0;
tx_msg[3]=0;
usb_put_packet(1,tx_msg,sizeof(tx_msg),USB_DTS_TOGGLE);
delay_ms(5);
LED_ON(LED1);
delay_ms(5);
LED_OFF(LED1);
delay_ms(5);
}
}
}
/////////////////////////////////////////////////////////////////////////////
//
// usb_rx_task()
//
// Listens to EP1 for any incoming packets. The only report ID that is
// configurd to send us data is 2 (keyboard LED status, see above)
//
/////////////////////////////////////////////////////////////////////////////
void usb_rx_task(void){
if (usb_kbhit(1)){
usb_get_packet(1, rx_msg, sizeof(rx_msg));
}
}
/////////////////////////////////////////////////////////////////////////////
//
// recibe por interrupci�n ext
//
/////////////////////////////////////////////////////////////////////////////
#int_ext
void ext_handler(void){
if(hay_dato==0){
hay_dato=1;
//LED_ON(LED2);
}
}
#int_RTCC
void RTCC_isr() {
if(fTx==0)
fleer=1;
}
void main() {
hay_dato=0;
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_128|RTCC_8_BIT);
set_tris_a(0b11100000);
set_tris_b(0b11111111);
set_tris_c(0b00110111);
port_b_pullups(false);
LED_OFF(LED1);
//LED_OFF(LED2);
//LED_OFF(LED3);
usb_init_cs();
//ext_int_edge(0,L_TO_H);
//enable_interrupts(int_ext);
enable_interrupts(INT_TIMER0);
delay_ms(500);
enable_interrupts(global);
LED_ON(LED1);
delay_ms(500);
LED_OFF(LED1);
delay_ms(500);
LED_ON(LED1);
delay_ms(500);
LED_OFF(LED1);
delay_ms(500);
LED_ON(LED1);
delay_ms(500);
LED_OFF(LED1);
delay_ms(500);
LED_ON(LED1);
delay_ms(500);
LED_OFF(LED1);
delay_ms(500);
LED_ON(LED1);
delay_ms(500);
LED_OFF(LED1);
delay_ms(500);
while (TRUE) {
usb_task();
usb_debug_task();
if (usb_enumerated()) {
usb_keyboard_task();
usb_rx_task();
delay_ms(5);
}
}
}
int8 temp=0;
int8 temp1=0;
output_high(pin_c7);
output_low(pin_c6);
output_low(pin_a4);
//output_low(pin_c6);
//output_high(pin_c7);
delay_us(10);
temp= input_b();
temp1=input_c();
temp=temp & 0b11110001;
temp1=(temp1 & 0b00000111)*2;
btn1a8 = temp | temp1;
delay_us(10);
output_low(pin_c7);
output_high(pin_c6);
output_low(pin_a4);
//output_high(pin_c6);
//output_low(pin_c7);
delay_us(10);
temp= input_b();
temp1=input_c();
temp=temp & 0b11110001;
temp1=(temp1 & 0b00000111)*2;
btn9a16 = temp | temp1;
delay_us(10);
output_low(pin_c7);
output_low(pin_c6);
output_high(pin_a4);
//output_low(pin_c6);
//output_low(pin_c7);
delay_us(10);
temp= input_b();
temp1=input_c();
temp=temp & 0b11110001;
temp1=(temp1 & 0b00000111)*2;
btn17a24 = temp | temp1;
delay_us(10);
/*
output_low(pin_c0);
output_high(pin_c1);
output_low(pin_c2);
output_low(pin_c6);
output_low(pin_c7);
delay_us(10);
btn25a32 = input_d();
delay_us(10);
output_high(pin_c0);
output_low(pin_c1);
output_low(pin_c2);
output_low(pin_c6);
output_low(pin_c7);
delay_us(10);
btn33a40 = input_d();
delay_us(10);
*/
output_low(pin_c0);
output_low(pin_c1);
output_low(pin_c2);
//output_low(pin_c6);
//output_low(pin_c7);
delay_us(10);
if(Btn1){
cadena[3]=4; //'a'
/*
while(1){
LED_ON(LED1);
delay_ms(500);
LED_OFF(LED1);
delay_ms(500);
}
*/
return;
}
else if(Btn2){
cadena[3]=5; //'b'
return;
}
else if(Btn3){
cadena[3]=7; //'d'
return;
}
else if(Btn4){
cadena[3] = 8; //'e'
return;
}
else if(Btn5){
cadena[3] = 9; //'f'
return;
}
else if(Btn6){
cadena[3] = 10; //'g'
return;
}
else if(Btn7){
cadena[3]=11; //'h'
return;
}
else if(Btn8){
cadena[3]=12; //'i'
return;
}
else if(Btn9){
cadena[3]=13; //'j'
return;
}
else if(Btn10){
cadena[3]=14; //'k'
return;
}
else if(Btn11){
cadena[3]=15; //'l'
return;
}
else if(Btn12){
cadena[3]=16; //'m'
return;
}
else if(Btn13){
cadena[3]=17; //'n'
return;
}
else if(Btn14){
cadena[3]=18; //'o'
return;
}
else if(Btn15){
cadena[3]=19; //'p'
return;
}
else if(Btn16){
cadena[3]=20; //'q'
return;
}
else if(Btn17){
cadena[3]=21; //'r'
return;
}
else if(Btn18){
cadena[3]=22; //'s'
return;
}
else if(Btn19){
cadena[3]=23; //'t'
return;
}
else if(Btn20){
cadena[3]=24; //'u'
return;
}
else if(Btn21){
cadena[3]=25; //'v'
return;
}
else if(Btn22){
cadena[3]=26; //'w'
return;
}
else if(Btn23){
cadena[3]=27; //'x'
return;
}
else if(Btn24){
cadena[3]=28; //'y'
return;
}
/*
else if(Btn25){
cadena[3]=29; //'z'
return;
}
else if(Btn26){
cadena[3]=39; //'0'
return;
}
else if(Btn27){
cadena[3]=30; //'1'
return;
}
else if(Btn28){
cadena[3]=31; //'2'
return;
}
else if(Btn29){
cadena[3]=32; //'3'
return;
}
else if(Btn30){
cadena[3]=33; //'4'
return;
}
else if(Btn31){
cadena[3]=34; //'5'
return;
}
else if(Btn32){
cadena[3]=35; //'6'
return;
}
else if(Btn33){
cadena[3]=36; //'7'
return;
}
else if(Btn34){
cadena[3]=37; //'8'
return;
}
else if(Btn35){
cadena[3]=38; //'9'
return;
}
else if(Btn36){
cadena[3]=54; //','
return;
}
else if(Btn37){
cadena[3]=55; //'.'
return;
}
else if(Btn37){
cadena[3]=45; //'-'
return;
}
else if(Btn38){
cadena[3]=47; //'{'
return;
}
else if(Btn39){
cadena[3]=48; //'}'
return;
}
else if(Btn40){
cadena[3]=49; //'|'
return;
}
*/
else{
cadena[3]=00; //Borra
}
return;
}