Controlando motor  joystick PS2 -  CONTROLLING DC MOTOR WITH JOYSTICK PS2
Autor: Ronaldo | Publicado: 2013-05-22T18:40:00-07:00


CONTROLE DE 4 MOTORES DC COM JOYSTICK PS2
4 &nbsp; &nbsp; &nbsp; DC MOTOR CONTROLLING &nbsp;BY &nbsp;JOYSTICK PS2








Este projeto controla 4 motores DC com joystick PS2, utilizando duas pontes H&nbsp;L298&nbsp;através de&nbsp;&nbsp;ATMEGA162.&nbsp;Inicialmente o joystick é inicializado para modo analógico e travar o botão analógico.&nbsp;O software lê constantemente os dados do joystick e armazena em 6 bytes organizados de dig_1 a dig_6.Os bytes dig_1 e dig_2 contém a leitura do botões do joystick. Estes botões são utilizados para controlar os 4 motores através das duas pontes H.L298.&nbsp;Os bytes dig_3 a dig_6 contem as leitura das sticks analógicos, onde o valor 0x00 do stick, indica a posição toda abaixo,&nbsp;&nbsp;o valor 0x80 indica o botão solto no meio&nbsp;e 0xFF&nbsp;indica a posição toda para cima. Quando o joystick não está em modo analógico com o led vermelho&nbsp;apagado, estes botões analógicos correspondem a:Analógico esquerdo =&gt; esquerda, direita, cima e baixo&nbsp;Analógico direito =&gt; quadrado, círculo, triângulo e X.&nbsp;O mapa do botões está listado abaixo.&nbsp;Recomendo visitar as paginas abaixo, pois dão uma visão mais detalhada de todos comando e sinais envolvidos.&nbsp;usei a interface SPI para comunicar-se com o joystick.&nbsp;

Pode ser usado controle também sem fios.

usei clock do atmega162 de &nbsp;8MHz.

English:

This project controls 4 motors with dc ps2 joystick with two bridges &nbsp;L298h and &nbsp;ATMEGA162. the joystick is initialized to analog mode and lock&nbsp;analogic&nbsp;button . The software constantly reads data from&nbsp;joystick&nbsp; and stores in 6 bytes, which are the dig_1 &nbsp;to dig_6. The dig_1 and&nbsp;dig_2&nbsp;and contains &nbsp;reading the joystick buttons. These buttons are used to control 4 motors through two bridges H L298. The bytes dig_3 to &nbsp;dig_6 contens reading the analog buttons, and ranges 0x00 to 0xFF, where 0x00 indicates &nbsp;the LOW/LEFT position,&nbsp;0x80 indicates &nbsp;the &nbsp;MIDDLE and&nbsp;&nbsp;0xFF indicates &nbsp;the UP/RIGHT. &nbsp;When &nbsp;red led is off, the&nbsp;&nbsp; LEFT STICK is same control the digital buttons &nbsp;left, right, up, and RIGHT STICK &nbsp;same&nbsp;digital buttons&nbsp;square, circle, triangle and x. &nbsp;


The map of the buttons are listed below. I recommend visiting the links below, &nbsp;they give a more detailed overview of all command and joystick signals. This version uses the SPI inteface to communicate with the joystick.

wireless&nbsp;control can be used .

Use the clock of the 8MHz,&nbsp;at 16MHz, initialization fails sometimes, and not turn the red led analog mode.
Controle em modo analógico, led vermelho ligado
Analog control with Red LED on


 BYTE    CMND    DATA

  01     0x01    idle
  02     0x42    0x73
  03     idle    0x5A    Bit0 Bit1 Bit2 Bit3 Bit4 Bit5 Bit6 Bit7
  04     idle    data    SLCT JOYR JOYL STRT UP   RGHT DOWN LEFT
  05     idle    data    L2   R2   L1   R1   /\   O    X    |_|
  06     idle    data    Right Joy 0x00 = Left  0xFF = Right
  07     idle    data    Right Joy 0x00 = Up    0xFF = Down
  08     idle    data    Left Joy  0x00 = Left  0xFF = Right
  09     idle    data    Left Joy  0x00 = Up    0xFF = Down





links de referencia &nbsp;/ &nbsp;reference links
http://store.curiousinventor.com/guides/PS2

http://www.gamesx.com/controldata/psxcont/psxcont.htm

http://www.avrfreaks.net/index.php?name=PNphpBB2&amp;file=viewtopic&amp;p=1053985

Circuíto completo.



Programa em C para codevisionavr
source code to codevisionavr


//******************************************************
// sistema de leitura do joystick playstation 2
//read joy ps2 avr atmega162 a 16MHzz, divide clock 16/2= 8MHz
/// usa a SPI para le bytes do controle
// programa &nbsp;para analógico e trava botao mode
// use spi interface and lock button mode
//******************************************************

#include &lt;mega162.h&gt;
#include &lt;delay.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define SSD 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Delay for how long SS stays high before going low again for the next byte trransfer
#define SS &nbsp;PORTB.4 &nbsp; &nbsp; // Slave/Chip Select line
#define cs1 &nbsp; PORTB.2
#define cs2 &nbsp; PORTB.3

#define left &nbsp; &nbsp; &nbsp;PORTA.0
#define right &nbsp; &nbsp;PORTA.1
#define up &nbsp; &nbsp; &nbsp; PORTA.2
#define down &nbsp;PORTA.3
#define tri &nbsp; &nbsp; &nbsp; PORTA.4
#define axi &nbsp; &nbsp; &nbsp;PORTA.5
#define qua &nbsp; &nbsp; PORTA.6
#define cir &nbsp; &nbsp; &nbsp; PORTA.7


unsigned char dummy,modo,status,dig_1,dig_2,dig_3,dig_4,dig_5,dig_6; &nbsp; &nbsp;// For dummy reads
typedef unsigned char byte;
byte SPI_TxRx(byte data);


//******************************************
&nbsp;// &nbsp;inicializa joystick modo analogico e trava botao modo
// &nbsp;init joystick and lock mode button
&nbsp;//*****************************************


&nbsp; void config_ana(void)
&nbsp; { &nbsp; modo=1;
&nbsp; &nbsp; &nbsp; while(modo != 0x73){
&nbsp; &nbsp; &nbsp; &nbsp; // entra em modo configuracao 
&nbsp; &nbsp; &nbsp; &nbsp; SS = 0;
&nbsp; &nbsp; &nbsp; &nbsp; dummy = SPI_TxRx(0x01); &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; modo = SPI_TxRx(0x42);
&nbsp; &nbsp; &nbsp; &nbsp; dummy = SPI_TxRx(0x00);
&nbsp; &nbsp; &nbsp; &nbsp; dummy = SPI_TxRx(0xff);
&nbsp; &nbsp; &nbsp; &nbsp; dummy = SPI_TxRx(0xff);
&nbsp; &nbsp; &nbsp; &nbsp; SS=1;
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; delay_ms(1); 
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; // ENTRA MODO CONFIGURACAO - CONFIG &nbsp;MODE
&nbsp; &nbsp; &nbsp; &nbsp; SS = 0;
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x01); &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x43); &nbsp;// LE STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x01); &nbsp;// LE STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; SS = 1;
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; delay_ms(1); 
&nbsp; &nbsp; &nbsp; &nbsp; // modo analogico -&nbsp;analog mode
&nbsp; &nbsp; &nbsp; &nbsp; SS = 0;
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x01); &nbsp;// &nbsp;VAZIO
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x44); &nbsp;// LE STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x01); &nbsp;// LE STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x03); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; SS = 1;
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;delay_ms(1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// sai do modo de configuracao - exit config
&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; SS = 0;
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x01); &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x43); &nbsp;// LE STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x00); &nbsp;// LE STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x5A); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x5A); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x5A); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x5A); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x5A); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; SS = 1;
&nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; delay_ms(1);
&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; 
&nbsp; }

&nbsp;//************************************
&nbsp;// &nbsp;inicializa a interface SPI serial
// init interface SPI serial
&nbsp;//************************************
&nbsp; &nbsp; 
void SPI_Master_Init(void)
{
&nbsp; &nbsp; DDRB = 0xbf; &nbsp; &nbsp;// Set SCK, MOSI, SS as output &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; SS = 1; &nbsp; &nbsp; &nbsp; &nbsp; // Initialize Slave Select as High
&nbsp; &nbsp; SPCR = 0x7f; &nbsp; &nbsp;// LSB 1st, Master, SCK Idle High, SCK Rising Edge Clocks Data,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // SCK = 125 kHz @ fosc = 8 MHz, SPI Enabled
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // SPI Interrupt Disabled 
&nbsp; 
&nbsp; &nbsp; SPSR = 0x00; &nbsp; &nbsp;// Make sure Clock x 2 is off
&nbsp; &nbsp; //SPCR=0x7B;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
}


&nbsp;//************************************
&nbsp;// &nbsp;le dados do joystck e salva 8 bytes
// read 8 &nbsp;bytes from joystick
&nbsp;//************************************

&nbsp; void le_joy (void)
&nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // &nbsp;if (entrada==0)
&nbsp; &nbsp; &nbsp; &nbsp;// &nbsp; &nbsp;config_ana(); &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; // Obtain digital Values
&nbsp; &nbsp; &nbsp; &nbsp; SS = 0;
&nbsp; &nbsp; &nbsp; &nbsp; dummy &nbsp; &nbsp;= SPI_TxRx(0x01); &nbsp;//
&nbsp; &nbsp; &nbsp; &nbsp; modo &nbsp; &nbsp; = SPI_TxRx(0x42); &nbsp;// LÊ /READ STATUS 53 OU 73
&nbsp; &nbsp; &nbsp; &nbsp; status &nbsp; = SPI_TxRx(0x00); &nbsp;// LE 5A
&nbsp; &nbsp; &nbsp; &nbsp; dig_1 &nbsp; &nbsp;= SPI_TxRx(0xff); &nbsp;// was 0x00 botao 1
&nbsp; &nbsp; &nbsp; &nbsp; dig_2 &nbsp; &nbsp;= SPI_TxRx(0xff); &nbsp;// was 0x00 botao 2
&nbsp; &nbsp; &nbsp; &nbsp; dig_3 &nbsp; &nbsp;= SPI_TxRx(0xff); &nbsp;// was 0x00 esq &nbsp;up
&nbsp; &nbsp; &nbsp; &nbsp; dig_4 &nbsp; &nbsp;= SPI_TxRx(0xff); &nbsp;// was 0x00 esq &nbsp;down
&nbsp; &nbsp; &nbsp; &nbsp; dig_5 &nbsp; &nbsp;= SPI_TxRx(0xff); &nbsp;// was 0x00 dir &nbsp;up
&nbsp; &nbsp; &nbsp; &nbsp; dig_6 &nbsp; &nbsp;= SPI_TxRx(0xff); &nbsp;// was 0x00 dir &nbsp;down
&nbsp; &nbsp; &nbsp; &nbsp; SS = 1;
&nbsp; &nbsp;}
&nbsp; 


//***********************************************
// &nbsp;escreve e le byte na SPI e retorna em SPDR
// &nbsp;send byte to SPI and read SPRD
//**********************************************
&nbsp; 

byte SPI_TxRx(byte data)
{ 
&nbsp; &nbsp; delay_us(SSD); &nbsp; &nbsp; 
&nbsp; &nbsp; SPDR = data;
&nbsp; &nbsp; while(!(SPSR &amp; 0x80)); &nbsp;// Wait for end of transmission
&nbsp; &nbsp; return SPDR;
}




//**************************************************
// &nbsp;Le bytes dig_1 e dig_2 trata botao pressionado
// Read dig1 and dig 2&nbsp;checks which button was pressed
//**************************************************

&nbsp;void le_chaves(void)

{

&nbsp; up &nbsp; &nbsp; &nbsp; = &nbsp;((dig_2 &gt;&gt; 4) &amp; (0x01));
&nbsp; right &nbsp; &nbsp;= &nbsp;((dig_2 &gt;&gt; 5) &amp; (0x01));
&nbsp; down &nbsp;= &nbsp;((dig_2 &gt;&gt; 6) &amp; (0x01));
&nbsp; left &nbsp; &nbsp; &nbsp;= &nbsp;((dig_2 &gt;&gt; 7) &amp; (0x01));
&nbsp; tri &nbsp; &nbsp; &nbsp; = &nbsp;((dig_1 &gt;&gt; 4) &amp; (0x01));
&nbsp; cir &nbsp; &nbsp; &nbsp;= &nbsp;((dig_1 &gt;&gt; 5) &amp; (0x01));
&nbsp; axi &nbsp; &nbsp; = &nbsp;((dig_1 &gt;&gt; 6) &amp; (0x01));
&nbsp; qua &nbsp; &nbsp;= &nbsp;((dig_1 &gt;&gt; 7) &amp; (0x01));
&nbsp; &nbsp; &nbsp; 
}


&nbsp;void led(void)

&nbsp;{
&nbsp; char i,y;
&nbsp; y=0x90;

&nbsp; for (i=0;i&lt;7;i++)
&nbsp;{
&nbsp; PORTA=y;
&nbsp; y=y&gt;&gt;1;
&nbsp; delay_ms(500);
&nbsp; }
&nbsp;}

void main(void)
{
#pragma optsize-
CLKPR=0x80;
CLKPR=0x01; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // dividir o clck por 2 = 16mhz/2=8MHz
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
PORTA=0x00;
DDRA=0xFF;
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x86;
UBRR0H=0x00;
UBRR0L=0x33;
&nbsp; 

SPI_Master_Init(); &nbsp;// Initialize SPI as Master and set SS High and Direction of Pins
printf("teste leitura do playstation\r\n");
delay_ms(500);
config_ana();
//led(); 

&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; while(1)
&nbsp; &nbsp; { 

&nbsp; &nbsp; &nbsp;le_joy();
&nbsp; &nbsp; &nbsp;le_chaves();
&nbsp; &nbsp; &nbsp;}
&nbsp; &nbsp; return;
}