mình giao tiếp gamepad ps2, tham khảo các bài viết trên mạng và dientuvietnam.net rồi mắc mạch và code như thế này nhưng thử nhiều lần chưa thấy tín hiệu . ai biết lỗi chỉ mình nhé, thanks
code:
#include <mega8.h>
// Declare your global variables here
#include <delay.h>
////////////////////////////////////////
#define DATA PINB.0
#define CMD PORTB.6
#define CLK PORTB.7
#define ATT PORTB.5
#define L1 1
#define L2 2
#define L3 3
#define L4 4
#define R1 5
#define R2 6
#define R3 7
#define R4 8
#define LL1 9
#define LL2 10
#define RR1 11
#define RR2 12
#define Start 14
#define Select 13
unsigned char rjoyx,rjoyy,ljoyx,ljoyy,analog,byte4,byte5;
//////////////////////
//Gamepad PS2
unsigned char access(unsigned char tbyte)
{
unsigned char rbyte=0;
unsigned int i;
CLK=1;
for(i=0;i<8;i++)
{
CMD=tbyte&0x01;
delay_us(10);
CLK=0;
delay_us(10);
rbyte=(rbyte>>1)|(DATA<<7);
CLK=1;
tbyte=tbyte>>1;
}
delay_us(20);
return rbyte;
}
void read_data(void)
{
rjoyx=rjoyy=ljoyx=ljoyy=128;
analog=128;
ATT=0;
access(0x01);
analog=access(0x42);
access(0);
byte4=access(0);
byte5=access(0);
if(analog==0x73||0x23||0X53)
{
rjoyx=access(0);
rjoyy=access(0);
ljoyx=access(0);
ljoyy=access(0);
}
if(analog==0x12)
{
rjoyx=access(0);
rjoyy=access(0);
}
ATT=1;
}
//////////////////////
unsigned char Button(unsigned char nut){
unsigned char KQ=0;
read_data();
switch(nut){
case 1: { if((byte4&0x10)==0) KQ=1; break; }
case 2: { if((byte4&0x20)==0) KQ=1; break; }
case 3: { if((byte4&0x40)==0) KQ=1; break; }
case 4: { if((byte4&0x80)==0) KQ=1; break; }
case 5: { if((byte5&0x10)==0) KQ=1; break; }
case 6: { if((byte5&0x20)==0) KQ=1; break; }
case 7: { if((byte5&0x40)==0) KQ=1; break; }
case 8: { if((byte5&0x80)==0) KQ=1; break; }
case 9: { if((byte5&0x04)==0) KQ=1; break; }
case 10: { if((byte5&0x01)==0) KQ=1; break; }
case 11: { if((byte5&0x08)==0) KQ=1; break; }
case 12: { if((byte5&0x02)==0) KQ=1; break; }
case 13: { if((byte4&0x01)==0) KQ=1; break; }
case 14: { if((byte4&0x08)==0) KQ=1; break; }
}
return KQ;
}
//////////////////////
void main(void)
{
PORTB=0b11100001;
DDRB= 0b11100000;
PORTC=0b00100000;
DDRC= 0b00100000;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
ATT=1;
CLK=1;
delay_ms(100);
while (1)
{
ATT=1;
CLK=1;
while(Button(L1)) PORTC.5=1;
while(Button(L2));
while(Button(L3));
while(Button(L4));
while(Button(R1)) PORTC.5=1;
while(Button(R2));
while(Button(R3));
while(Button(R4));
while(Button(LL1)) PORTC.5=1;
while(Button(LL2));
while(Button(RR1));
while(Button(RR2));
while(Button(Start)) PORTC.5=1;
while(Button(Select));
PORTC.5=0;
}
}
code:
#include <mega8.h>
// Declare your global variables here
#include <delay.h>
////////////////////////////////////////
#define DATA PINB.0
#define CMD PORTB.6
#define CLK PORTB.7
#define ATT PORTB.5
#define L1 1
#define L2 2
#define L3 3
#define L4 4
#define R1 5
#define R2 6
#define R3 7
#define R4 8
#define LL1 9
#define LL2 10
#define RR1 11
#define RR2 12
#define Start 14
#define Select 13
unsigned char rjoyx,rjoyy,ljoyx,ljoyy,analog,byte4,byte5;
//////////////////////
//Gamepad PS2
unsigned char access(unsigned char tbyte)
{
unsigned char rbyte=0;
unsigned int i;
CLK=1;
for(i=0;i<8;i++)
{
CMD=tbyte&0x01;
delay_us(10);
CLK=0;
delay_us(10);
rbyte=(rbyte>>1)|(DATA<<7);
CLK=1;
tbyte=tbyte>>1;
}
delay_us(20);
return rbyte;
}
void read_data(void)
{
rjoyx=rjoyy=ljoyx=ljoyy=128;
analog=128;
ATT=0;
access(0x01);
analog=access(0x42);
access(0);
byte4=access(0);
byte5=access(0);
if(analog==0x73||0x23||0X53)
{
rjoyx=access(0);
rjoyy=access(0);
ljoyx=access(0);
ljoyy=access(0);
}
if(analog==0x12)
{
rjoyx=access(0);
rjoyy=access(0);
}
ATT=1;
}
//////////////////////
unsigned char Button(unsigned char nut){
unsigned char KQ=0;
read_data();
switch(nut){
case 1: { if((byte4&0x10)==0) KQ=1; break; }
case 2: { if((byte4&0x20)==0) KQ=1; break; }
case 3: { if((byte4&0x40)==0) KQ=1; break; }
case 4: { if((byte4&0x80)==0) KQ=1; break; }
case 5: { if((byte5&0x10)==0) KQ=1; break; }
case 6: { if((byte5&0x20)==0) KQ=1; break; }
case 7: { if((byte5&0x40)==0) KQ=1; break; }
case 8: { if((byte5&0x80)==0) KQ=1; break; }
case 9: { if((byte5&0x04)==0) KQ=1; break; }
case 10: { if((byte5&0x01)==0) KQ=1; break; }
case 11: { if((byte5&0x08)==0) KQ=1; break; }
case 12: { if((byte5&0x02)==0) KQ=1; break; }
case 13: { if((byte4&0x01)==0) KQ=1; break; }
case 14: { if((byte4&0x08)==0) KQ=1; break; }
}
return KQ;
}
//////////////////////
void main(void)
{
PORTB=0b11100001;
DDRB= 0b11100000;
PORTC=0b00100000;
DDRC= 0b00100000;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
ATT=1;
CLK=1;
delay_ms(100);
while (1)
{
ATT=1;
CLK=1;
while(Button(L1)) PORTC.5=1;
while(Button(L2));
while(Button(L3));
while(Button(L4));
while(Button(R1)) PORTC.5=1;
while(Button(R2));
while(Button(R3));
while(Button(R4));
while(Button(LL1)) PORTC.5=1;
while(Button(LL2));
while(Button(RR1));
while(Button(RR2));
while(Button(Start)) PORTC.5=1;
while(Button(Select));
PORTC.5=0;
}
}