0% found this document useful (0 votes)
106 views

Keyboard - C: Programa en C USB PIC18F

The document is source code for a joystick with 8 axes and 24 buttons that communicates with a computer over USB HID. It defines pins, variables, functions for reading the joystick, converting button presses to key codes, and sending reports over USB. The code initializes USB, sets up pins and interrupts, then enters a main loop that handles USB communication and reads the joystick on a timer interrupt to send button presses as keyboard keys.

Uploaded by

Luis Gonzales
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
106 views

Keyboard - C: Programa en C USB PIC18F

The document is source code for a joystick with 8 axes and 24 buttons that communicates with a computer over USB HID. It defines pins, variables, functions for reading the joystick, converting button presses to key codes, and sending reports over USB. The code initializes USB, sets up pins and interrupts, then enters a main loop that handles USB communication and reads the joystick on a timer interrupt to send button presses as keyboard keys.

Uploaded by

Luis Gonzales
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

/////////////////////////////////////////////////////////////////////////

//// ////
//// 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)

#DEFINE USB_HID_DEVICE TRUE

#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN


bulk/interrupt transfers
#define USB_EP1_TX_SIZE 8

#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT //turn on EP1 for IN


bulk/interrupt transfers
#define USB_EP1_RX_SIZE 8

#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
//
/////////////////////////////////////////////////////////////////////////////

#define LED1 PIN_A3


#define LED2 PIN_A1
#define LED3 PIN_A2

#define LED_ON output_high


#define LED_OFF output_low

#define PIN_SOURCE PIN_B1

/////////////////////////////////////////////////////////////////////////////
//
// 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

#bit Btn1 = btn1a8.0


#bit Btn2 = btn1a8.1
#bit Btn3 = btn1a8.2
#bit Btn4 = btn1a8.3
#bit Btn5 = btn1a8.4
#bit Btn6 = btn1a8.5
#bit Btn7 = btn1a8.6
#bit Btn8 = btn1a8.7

#bit Btn9 = btn9a16.0


#bit Btn10 = btn9a16.1
#bit Btn11 = btn9a16.2
#bit Btn12 = btn9a16.3
#bit Btn13 = btn9a16.4
#bit Btn14 = btn9a16.5
#bit Btn15 = btn9a16.6
#bit Btn16 = btn9a16.7

#bit Btn17 = btn17a24.0


#bit Btn18 = btn17a24.1
#bit Btn19 = btn17a24.2
#bit Btn20 = btn17a24.3
#bit Btn21 = btn17a24.4
#bit Btn22 = btn17a24.5
#bit Btn23 = btn17a24.6
#bit Btn24 = btn17a24.7

#bit Btn25 = btn25a32.0


#bit Btn26 = btn25a32.1
#bit Btn27 = btn25a32.2
#bit Btn28 = btn25a32.3
#bit Btn29 = btn25a32.4
#bit Btn30 = btn25a32.5
#bit Btn31 = btn25a32.6
#bit Btn32 = btn25a32.7

#bit Btn33 = btn33a40.0


#bit Btn34 = btn33a40.1
#bit Btn35 = btn33a40.2
#bit Btn36 = btn33a40.3
#bit Btn37 = btn33a40.4
#bit Btn38 = btn33a40.5
#bit Btn39 = btn33a40.6
#bit Btn40 = btn33a40.7

void lectura_teclado(char *cadena);

/////////////////////////////////////////////////////////////////////////////
//
// 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 char_2_usb_kbd_code(char c){

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) {

//static char Char_Tx;

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);
}
}
}

void lectura_teclado(char *cadena){

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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy