AtomSoft
Well-Known Member
Hey guys i have people ask me now and then how to get data from PSP JoyStick... Where here is some pic code i made. I will draw a schematic in 10 minutes and also a small video and pictures of exact pinout... I hope you will appreciate and enjoy this info... At the end ill create a PDF with code and images for easy portability...
Code:
/* *****************************************************************************
; *
; Filename: PSP JOYSTICK CONTROL (PIC18F2525)(40MHZ)(10MHZ x 4PLL) *
; Date: FEB, 20 - 2010 *
; File Version: 001 *
; *
; Author: Jason Lopez *
; Company: AtomSoft *
; *
;***************************************************************************** */
#include <p18f2525.h>
#include <delays.h>
#include <string.h>
#pragma config WDT = OFF, LVP = OFF, OSC = HSPLL
#define PSPY TRISAbits.TRISA0
#define PSPX TRISAbits.TRISA1
#define YPIN 0
#define XPIN 1
/************************************
Prototypes
*************************************/
void main(void);
void SetupADC(char PIN);
unsigned int GetADC(char PIN);
void SendToPC(unsigned rom char *string);
void SetupUART(void);
/************************************
Variables and Defines
*************************************/
unsigned int XMAIN = 0;
unsigned int YMAIN = 0;
/************************************
Main
*************************************/
void main(void){
volatile unsigned int YDATA = 0;
volatile unsigned int XDATA = 0;
SetupUART();
PSPY = 1;
PSPX = 1;
Delay10KTCYx(100);
XMAIN = GetADC(XPIN);
Delay10KTCYx(100);
YMAIN = GetADC(YPIN);
while(1){
XDATA = GetADC(XPIN);
YDATA = GetADC(YPIN);
if(XDATA < (XMAIN - 230)) {
SendToPC((unsigned rom char*)"RIGHT");
}
if(XDATA > (XMAIN + 257)) {
SendToPC((unsigned rom char*)"LEFT ");
}
if(YDATA < (YMAIN - 257)) {
SendToPC((unsigned rom char*)"UP ");
}
if(YDATA > (YMAIN + 257)) {
SendToPC((unsigned rom char*)"DOWN ");
}
}
}
void SetupADC(char PIN){
ADCON0 = PIN<<2;
ADCON1 = 0x0D;
ADCON2 = 0b10001101;
ADCON0bits.ADON = 1;
}
unsigned int GetADC(char PIN){
SetupADC(PIN);
Delay10TCYx(100);
ADCON0bits.GO = 1; //Start the conversion
while(ADCON0bits.DONE); //Wait until it’s done
return ADRES;
}
void SendToPC(unsigned rom char *string){
Delay10TCYx(100);
while(*string){
while(!PIR1bits.TXIF) //wait until TXIF is high
continue;
TXREG = *string++; //put byte into Transmit Register
}
}
void SetupUART(void){
TRISC = 0b10000000;
TXSTAbits.SYNC = 0;
BAUDCTLbits.BRG16 = 0;
TXSTAbits.BRGH = 1;
SPBRG = 129; //19200bps for 40 Mhz
RCONbits.IPEN = 1;
TXSTAbits.TXEN = 1; //ENABLE TX
RCSTAbits.CREN = 1; //ENABLE RX
RCSTAbits.SPEN = 1; //ENABLE SERIAL PORT and PIN Config
PIR1 = 0x00;
IPR1 = 0b00101000;
Delay10TCYx(100);
}
Attachments
Last edited: