Moderadores: andre_luis, 51, guest2003, Renie
typedef struct
{
POV:4;
B4:1;
B5:1;
B6:1;
B7:1;
}byte_bits;
typedef union
{
unsigned char byte;
byte_bits bit;
}byte;
char read[64],write[64];
unsigned char temp,pov;
char x_axis,y_axis,throttle=0;
void interrupt(void)
{
HID_InterruptProc();
}
void main(void)
{
byte buttons;
buttons.byte=0;
//Initialize ports
TRISB=255; //Set port b to inputs - Port b samples all the digital switches
HID_Enable(read,write);
while(1)
{
/////////////////////////////////
//Joystick buttons
/////////////////////////////////
//Button 1
if (PORTB.RB0)
buttons.bit.B4=1;
else
buttons.bit.B4=0;
//Button 2
if (PORTB.RB1)
buttons.bit.B5=1;
else
buttons.bit.B5=0;
//Button 3
if (PORTB.RB2)
buttons.bit.B6=1;
else
buttons.bit.B6=0;
//Button 4
if (PORTB.RB3)
buttons.bit.B7=1;
else
buttons.bit.B7=0;
///////////////////////////////////
//POV Hat
///////////////////////////////////
pov=Adc_Read(0)>>2;
buttons.bit.POV=4; //idle
if (pov<=193 && pov>=176) //Left
buttons.bit.POV=3;
if (pov<=175 && pov>=143) //Down
buttons.bit.POV=2;
if (pov<=142 && pov>=61) //Right
buttons.bit.POV=1;
if (pov<=60 && pov>=0) //Up
buttons.bit.POV=0;
///////////////////////////////
//Joystick Axes
x_axis=(Adc_Read(1)>>2)-128; //Read only the top 8-bits of the ADC
y_axis=(Adc_Read(2)>>2)-128;
throttle=(Adc_Read(3)>>2)-128;
//////////////////////////////
//USB
write[0]=throttle;
write[1]=x_axis;
write[2]=y_axis;
write[3]=buttons.byte;
write[4]=0xFF;
while(!HID_Write(write,5));
}
}
/* cria um typedef para os bits em um byte */
typedef struct {
POV:4;
B4:1;
B5:1;
B6:1;
B7:1;
} byte_bits;
/* cria um typedef juntando os bits com um byte */
typedef union {
unsigned char byte;
byte_bits bit;
} byte;
char read[64]; /* buffer de recepcao */
char write[64]; /* buffer de transmissao */
unsigned char temp; /* temporario qq */
unsigned char pov; /* um potenciometro qq */
char x_axis; /* potenciometro do eixo x */
char y_axis; /* potenciometro do eixo y */
char throttle = 0; /* *outro* potenciometro aleatorio... tu tem um joystick fashion hein! */
/* deve ser tratamento de alguma interrupcao, nao conheco essa mcu */
void interrupt(void)
{
HID_InterruptProc();
}
/* programa principal */
void main(void)
{
/* cria uma variavel com o byte e os bits lah */
byte buttons;
/* zera todos os bits */
buttons.byte = 0;
/* inicializa porta B como entrada para switchs digitais */
TRISB = 255;
/* soh deus sabe! */
HID_Enable(read, write);
/* loop infinito comeca aqui */
while (1) {
/* testa os botaozinho nas portas RB e seta os bits de acordo */
/* testa botao 1 */
if (PORTB.RB0)
buttons.bit.B4 = 1;
else
buttons.bit.B4 = 0;
/* testa botao 2 */
if (PORTB.RB1)
buttons.bit.B5 = 1;
else
buttons.bit.B5 = 0;
/* testa botao 3 */
if (PORTB.RB2)
buttons.bit.B6 = 1;
else
buttons.bit.B6 = 0;
/* testa botao 4 */
if (PORTB.RB3)
buttons.bit.B7 = 1;
else
buttons.bit.B7 = 0;
/* le o AD, provavelmente de 10 bits e pega os 8 bits MSB */
pov = Adc_Read(0) >> 2;
/* separa o valor de 8 bits em 4 faixas diferentes e seta os bits POV, seguindo uma logica maluca qq */
buttons.bit.POV = 4; //idle
if (pov <= 193 && pov >= 176) //Left
buttons.bit.POV = 3;
if (pov <= 175 && pov >= 143) //Down
buttons.bit.POV = 2;
if (pov <= 142 && pov >= 61) //Right
buttons.bit.POV = 1;
if (pov <= 60 && pov >= 0) //Up
buttons.bit.POV = 0;
/* isso deve ser as coordenadas x/y do joystick, pegando 8 de 10 bits
e subtraindo 128 p/ tornar sinalizado (-127 a +127) */
x_axis = (Adc_Read(1) >> 2) - 128;
y_axis = (Adc_Read(2) >> 2) - 128;
throttle = (Adc_Read(3) >> 2) - 128;
/* monta uma mensagem de 5 bytes com os dados que capturou */
write[0] = throttle;
write[1] = x_axis;
write[2] = y_axis;
write[3] = buttons.byte;
write[4] = 0xFF;
/* envia a mensagem de 5 bytes... pela cara do loop, ele tenta varias vezes ateh conseguir mandar! */
while (!HID_Write(write, 5));
} /* loop infinito termina aqui */
}
/* testa botao 1 */
if (PORTB.RB0)
buttons.bit.B4 = 1;
else
buttons.bit.B4 = 0;
if (PORTB.RB0)
buttons |= 0B00010000; // seta o bit 4 da variavel
else
buttons &= 0B11101111; // reseta o bit 4 da variavel
fabim escreveu:ááátá tchelo.
Parte do main, sem problemas...
O q eu não estou conseguingo entender são aquelas 2 primeiras rotinas...
Mas pelo que eu observei no seu comentario.
Eu posso fazer tudo no main, sem criar aquelas frescurites né ?
Tipo só acessar o byte do array, que eu estou interessado... correto ?
Fabim
#define POV_MASK (0xf)
#define B4_MASK (1<<4)
#define B5_MASK (1<<5)
#define B6_MASK (1<<6)
#define B7_MASK (1<<7)
char read[64]; /* buffer de recepcao */
char write[64]; /* buffer de transmissao */
unsigned char buttons; /* variavel legal indicando o botao */
unsigned char temp; /* temporario qq */
unsigned char pov; /* um potenciometro qq */
char x_axis; /* potenciometro do eixo x */
char y_axis; /* potenciometro do eixo y */
char throttle = 0; /* *outro* potenciometro aleatorio... tu tem um joystick fashion hein! */
/* deve ser tratamento de alguma interrupcao, nao conheco essa mcu */
void interrupt(void)
{
HID_InterruptProc();
}
/* programa principal */
void main(void)
{
/* zera todos os bits */
buttons = 0;
/* inicializa porta B como entrada para switchs digitais */
TRISB = 255;
/* soh deus sabe! */
HID_Enable(read, write);
/* loop infinito comeca aqui */
while (1) {
/* testa os botaozinho nas portas RB e seta os bits de acordo */
/* testa botao 1 */
if (PORTB.RB0)
buttons |= B4_MASK;
else
buttons &= ~B4_MASK;
/* testa botao 2 */
if (PORTB.RB1)
buttons |= B5_MASK;
else
buttons &= ~B5_MASK;
/* testa botao 3 */
if (PORTB.RB2)
buttons |= B6_MASK;
else
buttons &= ~B6_MASK;
/* testa botao 4 */
if (PORTB.RB3)
buttons |= B7_MASK;
else
buttons &= ~B7_MASK;
/* le o AD, provavelmente de 10 bits e pega os 8 bits MSB */
pov = Adc_Read(0) >> 2;
/* separa o valor de 8 bits em 4 faixas diferentes e seta os bits POV, seguindo uma logica maluca qq */
buttons &= ~POV_MASK; /* limpa campo POV */
if (pov <= 193 && pov >= 176) //Left
buttons |= (POV_MASK & 3);
else if (pov <= 175 && pov >= 143) //Down
buttons |= (POV_MASK & 2);
else if (pov <= 142 && pov >= 61) //Right
buttons |= (POV_MASK & 1);
else if (pov <= 60 && pov >= 0) //Up
buttons |= (POV_MASK & 0);
else buttons |= (POV_MASK & 4); //idle
/* isso deve ser as coordenadas x/y do joystick, pegando 8 de 10 bits
e subtraindo 128 p/ tornar sinalizado (-127 a +127) */
x_axis = (Adc_Read(1) >> 2) - 128;
y_axis = (Adc_Read(2) >> 2) - 128;
throttle = (Adc_Read(3) >> 2) - 128;
/* monta uma mensagem de 5 bytes com os dados que capturou */
write[0] = throttle;
write[1] = x_axis;
write[2] = y_axis;
write[3] = buttons.byte;
write[4] = 0xFF;
/* envia a mensagem de 5 bytes... pela cara do loop, ele tenta varias vezes ateh conseguir mandar! */
while (!HID_Write(write, 5));
} /* loop infinito termina aqui */
}
Sergio38br escreveu:
- Código: Selecionar todos
if (PORTB.RB0)
buttons |= 0B00010000; // seta o bit 4 da variavel
else
buttons &= 0B11101111; // reseta o bit 4 da variavel
Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante