Author Topic: Interfacing Sony PS2 controller to AtMega16 [Help!!!]  (Read 520 times)

0 Members and 1 Guest are viewing this topic.

Offline robocellTopic starter

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
Interfacing Sony PS2 controller to AtMega16 [Help!!!]
« on: December 26, 2013, 08:26:53 AM »
Hello, guys!!! :)
    I have a project , in which I wish to control bot movements, by PS2 controller.
I have written a code , but failed!!  :'( :'( :'(  Please help me!!!

Your help is much appreciated. Thank you very much..!!!  :)


Code: [Select]
#include<avr/io.h>
#include"delayavr.h"
#include"lcdavr.h"
#include<avr/sfr_defs.h>
#include<avr/interrupt.h>

#define psclk 2 //PA2
#define psdata 3 //PA3
#define psack 4 //PA4
#define pscmd 5 //PA5
#define psatt 6 //PA6


short int command(short int cmd);

int main()
{
lcd_init();
DDRA|=_BV(PA2); //clk o/p

DDRA&=~(_BV(PA3)); //data i/p
PORTA|=_BV(PA3); //enable pul up register

DDRA&=~(_BV(PA4)); //ack i/p
PORTA|=_BV(PA4); //enable pul up register

DDRA|=_BV(PA5); //cmd o/p
DDRA|=_BV(PA6); //att o/p


sei(); //enable interrupt
//timerInit();
short int chk_ana=0;

while(chk_ana!=0X73)
{

//enter in confi mode

PORTA|=_BV(pscmd);
PORTA|=_BV(psclk);
PORTA&=~(_BV(psatt));

command(0x01);
command(0x43);
command(0x00);
command(0x01); //enter confi mode
command(0x00);

PORTA|=_BV(pscmd);
delayms(1);
PORTA|=_BV(psatt);
delayms(10);
//configuring in analog mode

PORTA|=_BV(pscmd);
PORTA|=_BV(psclk);
PORTA&=~(_BV(psatt));

command(0x01);
command(0x44);
command(0x00);
command(0x01);
command(0x03);
command(0x00);
command(0x00);
command(0x00);
command(0x00);

PORTA|=_BV(pscmd);
delayms(1);
PORTA|=_BV(psatt);
delayms(10);
 
// exit confi mode

PORTA|=_BV(pscmd);
PORTA|=_BV(psclk);
PORTA&=~(_BV(psatt));

command(0x01);
command(0x43);
command(0x00);
command(0x00);
command(0x5A);
command(0x5A);
command(0x5A);
command(0x5A);
command(0x5A);

PORTA|=_BV(pscmd);
delayms(1);
PORTA|=_BV(psatt);
delayms(10);
  //checking analog mode

PORTA|=_BV(pscmd);
PORTA|=_BV(psclk);
PORTA&=~(_BV(psatt));

command(0x01);
chk_ana=command(0x42); //poling
command(0x00);
command(0x00);
command(0x00);
command(0x00);
command(0x00);
command(0x00);
command(0x00);

PORTA|=_BV(pscmd);
delayms(1);
PORTA|=_BV(psatt);
delayms(10);


}// while not analog
   
short int cmd1,d0,d1,d2,d3;
while(1)
{

   
PORTA|=_BV(pscmd);
PORTA|=_BV(psclk);
PORTA&=~(_BV(psatt));

command(0x01);
cmd1=command(0x42); //poling
command(0x00);
d0=command(0x00);
d1=command(0x00);
d2=command(0x00);
d3=command(0x00);
command(0x00);
command(0x00);

PORTA|=_BV(pscmd);
delayms(1);
PORTA|=_BV(psatt);
delayms(10);
   
    lcd_string("d0 d1 d2 d3 d4");
lcd_gotoxy(1,0);
lcd_num(cmd1);
delayms(500);
lcd_num(d0);
delayms(500);
    lcd_num(d1);
delayms(500);
    lcd_num(d2);
delayms(500);
    lcd_num(d3);
delayms(500);
    lcd_gotoxy(0,0);
}



}

short int command(short int cmd)
{
int i;
short int data=0x00;

for(i=0;i<8;i++)
{
if(cmd & _BV(i))
{
PORTA|=_BV(pscmd); //send 1
}
else
{
PORTA&=~(_BV(pscmd)); //send 0
}

PORTA&=~(_BV(psclk)); //clk low

delayus(20);

///read in data
if(PINA & _BV(psdata))
{
data|=_BV(i); //send 1
}

PORTA|=_BV(psclk); //clk high
}

PORTA|=_BV(pscmd); //cmd high

delayus(20);
return data;
}

 


Get Your Ad Here

data_list