'****************************************************************
Device = 18F1320
Declare Bootloader = off
'THIS CONFIG IS FOR THE INTERNAL 8 MHZ OSCILLATOR, AND MCLRE OFF
Config_Start
OSC = HS'INTIO2 ; Internal RC, OSC1 as RA7, OSC2 as RA6
FSCM = OFF ; Fail-Safe Clock Monitor disabled
IESO = OFF ; Internal External Switch Over mode disabled
PWRT = On ; Enabled
BOR = OFF ; Disabled
BORV = 27 ; 4.2V
WDT = OFF ; Disabled
WDTPS = 64 ; 1:64
MCLRE = off ; Disabled
STVR = OFF ; Disabled
LVP = OFF ; Disabled
Debug = OFF ; Disabled
CP0 = OFF ; Disabled
CP1 = OFF ; Disabled
CPB = OFF ; Disabled
CPD = OFF ; Disabled
WRT0 = OFF ; Disabled
WRT1 = OFF ; Disabled
WRTB = OFF ; Disabled
WRTC = OFF ; Disabled
WRTD = OFF ; Disabled
EBTR0 = OFF ; Disabled
EBTR1 = OFF ; Disabled
EBTRB = OFF ; Disabled
Config_End
' ____ _____
'Board Volts>-|RA0 RB3|->beeper
' Lcd<-|RA1 RB2|->PS2_CLK
' Video LOS>-|RA4 RA7|-Xtal
' MCLR>-|RA5 RA6|-Xtal
' Gnd>-|Vss 18F Vdd|-<+5
' -|RA2 1320 RB7|-<PS2_DAT
' -|RA3 RB6|->PS2_CMD
' TX/RX Sens<-|RB0 RB5|->PS2_ATT
' TX<-|RB1 RB4|-<RX
All_Digital = TRUE ' We only want digital functions
PortB_Pullups = off
' CMCON0 = %00000111 ' Only if you don't trust "ALL_DIGITAL"
' ADCON0 = 0 ' Only if you don't trust "ALL_DIGITAL"
' ANSEL = %00000000 ' Only if you don't trust "ALL_DIGITAL"
' INTCON.7 = 0 ' Only if you don't trust "ALL_DIGITAL"
' INTCON2.7 = 1 'disable B pullups
' WPU.2 = 1 ' Only if you don't trust "ALL_DIGITAL"
' OSCCON = %01110010 ' Internal OSC 8MHz
' OSCTUNE = %00000000 ' Fine tune the timing of the pulses, "0" uses default
Xtal = 10 'Proton clock setting (just for calculations)
init:
Disable
'OSCCON = %0111000 ' Internal x MHz osc %01100000
TRISA = %11111101
TRISB = %10010000
'Serial LCD:
Symbol lcd = PORTA.1
Symbol bds = 32 ' 19200 188'4800 bds
Symbol TX_bds = 32'84
'Ps2 controller:
Symbol dat = PORTB.7 'need a pull up' Play Station game controller connections.
Symbol cmd = PORTB.6
Symbol att = PORTB.5
Symbol clk = PORTB.2
'ack is not plugged
'beeper:
Symbol bip = PORTB.3
'rs485:
Symbol TX = PORTB.1
Symbol RX = PORTB.4
Symbol Tx_Rx_Sens = PORTB.0
Dim psxmode As Byte ' Variable definitions.
Dim psx_dummy As Byte
Dim psxbutton_1 As Byte ' Variable definitions.
Dim psxbutton_2 As Byte
Dim psxjoystick_1 As Dword'Byte
Dim psxjoystick_2 As Dword'Byte
Dim psxjoystick_3 As Dword'Byte
Dim psxjoystick_4 As Dword'Byte
Dim psxjoystick_1_Cal As Dword
Dim psxjoystick_2_Cal As Dword
Dim psxjoystick_3_Cal As Dword
Dim psxjoystick_4_Cal As Dword
Dim Center As Byte
' Dim voie1 As Byte
' Dim voie2 As Byte
' Dim voieA As Byte
' Dim voieB As Byte
' Dim voieC As Byte
' Dim voieD As Byte
Dim S_motor As Byte
Dim B_motor As Byte
Dim up As Bit
Dim down As Bit
Dim right As Bit
Dim left As Bit
Dim selec As Bit
Dim start As Bit
Dim L_stick As Bit
Dim R_stick As Bit
Dim cross As Bit
Dim trian As Bit
Dim round As Bit
Dim squar As Bit
Dim M_f_r As Dword
Dim M_f_l As Dword
Dim M_r_l As Dword
Dim M_r_r As Dword
DelayMS 1000
SerOut lcd,bds,[$A0]
DelayMS 100
SerOut lcd,bds,[$A3,$01]',$A5,$02]
SerOut lcd,bds,[$A1,1,0]'place cursor on x y
SerOut lcd,bds,[$A2,"PS2 Controller",$00] 'elcd
DelayMS 1000
SerOut lcd,bds,[$A3,$01]',$A5,$02]
'gosub Esc 'init the servos or esc
'software starts here
main:
GoSub config_mode 'run config mode at startup
'gosub init_dualshock 'doesn't work!?
GoSub analog_lock 'set the pad to analog mode with analog button lock
'GoSub digital_lock
GoSub exit_config_mode 'all settings ok ,exit the config mode
DelayMS 200
GoSub config_mode 'run config mode at startup
'gosub init_dualshock 'doesn't work!?
GoSub vibration_en 'set analog vibration to enable (default is probably disable)
GoSub exit_config_mode 'all settings ok ,exit the config mode
DelayMS 200
GoSub get_buttons_sticks 'get all datas from the pad
psxjoystick_1_Cal = 0
psxjoystick_2_Cal = 0
psxjoystick_3_Cal = 0
psxjoystick_4_Cal = 0
GoSub calibration
Sound bip, [50,60,70,20,85,120]
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
loop: 'main loop
GoSub get_buttons_sticks 'get all datas from the pad
'DelayMS 500 'wait before vibrate both motors
'debuging on lcd
'vibrate big motor
If psxbutton_2 = $FE Then
S_motor = $00 '$01
B_motor = $FF
GoSub vibration_run 'vibrate motors (see file ps2cmd01.txt to change the speed)
EndIf
'vibrate small motor
If psxbutton_1 = $FE Then
S_motor = $FF '$01
B_motor = $00
GoSub vibration_run 'vibrate motors (see file ps2cmd01.txt to change the speed)
EndIf
'display datas from get_buttons_sticks subroutine on a serial lcd (alcd from selectronic)
'GoSub louky_mix
GoSub louky_mix 'get the mixes and sort out the 6 motors values
High Tx_Rx_Sens
'send the motors vlues mixed all together + buttons @ 19200 'M_f_r = Y - X - Z
'M_f_l = Y + X + Z
'M_r_l = -Y + X - Z
'M_r_r = -Y - X + Z
SerOut TX,TX_bds,["@","A",Dec3 M_f_r,"B",Dec3 M_r_l,"C",Dec3 M_f_l ,"D",Dec3 M_r_r,"E",Dec3 psxjoystick_4,"F", psxbutton_1,"G", psxbutton_2,"*",$0A,$0D]
'send the joystick an buttons values for debug @19200bds
'SerOut TX,bds,["@","A",DEC3 psxjoystick_1,"B",DEC3 psxjoystick_2,"C",DEC3 psxjoystick_3,"D",DEC3 psxjoystick_4,"E",psxbutton_1,"F",psxbutton_2,"*"] 'when going the opposite way turn right to go back
'Debug with ELCD On this uc board:
'////////////////////////////////////////////
'SerOut lcd,bds,[$A1,0,0,$A2,Bin8 psxbutton_1,",",Bin8 psxbutton_2,$00,$A1,0,1,$A2,Dec3 psxjoystick_1,",",Dec3 psxjoystick_2,",",Dec3 psxjoystick_3,",",Dec3 psxjoystick_4,$00]
'////////////////////////////////////////////
'SerOut lcd,bds,[$A1,0,0,$A2,DEC3 M_f_l,":",DEC3 M_f_r,$00,$A1,0,1,$A2,DEC3 M_r_l ,":",DEC3 M_r_r," ",DEC3 psxjoystick_4,$00]
'SerOut lcd,bds,[$A1,9,0,$A2,DEC3 psxjoystick_1,":",DEC3 psxjoystick_2,$00,$A1,9,1,$A2,DEC3 psxjoystick_3,":",DEC3 psxjoystick_4,$00]
'DelayMS 10
Low Tx_Rx_Sens 'ready to receive
'SerOut lcd,bds,[$A1,0,0,$A2,BIN8 psxbutton_1,",",BIN8 psxbutton_2,$00,$A1,0,1,$A2,DEC3 psxjoystick_1,",",DEC3 psxjoystick_2,",",DEC3 psxjoystick_3,",",DEC3 psxjoystick_4,$00]
GoTo loop 'again and again
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
calibration:
Center = 128 'cause not divided by two yet
'needs togo To get sticks subroutine first
psxjoystick_1_Cal = psxjoystick_1
psxjoystick_2_Cal = psxjoystick_2
psxjoystick_3_Cal = psxjoystick_3
psxjoystick_4_Cal = psxjoystick_4
'serOut lcd,bds,[$A1,0,0,$A2,DEC3 psxjoystick_1_cal,":",DEC3 psxjoystick_2_cal,$00,$A1,0,1,$A2,DEC3 psxjoystick_3_cal,":",DEC3 psxjoystick_4_cal,$00]
psxjoystick_1_Cal = psxjoystick_1_Cal - Center
psxjoystick_2_Cal = psxjoystick_2_Cal - Center
psxjoystick_3_Cal = psxjoystick_3_Cal - Center
psxjoystick_4_Cal = psxjoystick_4_Cal - Center
'SerOut lcd,bds,[$A1,9,0,$A2,sDEC3 psxjoystick_1_Cal,":",sDEC3 psxjoystick_2_Cal,$00,$A1,9,1,$A2,sDEC3 psxjoystick_3_Cal,":",sDEC3 psxjoystick_4_Cal,$00]
Return
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
louky_mix:
'joystick comes from 0 to 255
'we want it from 1 to 128
'rezero with true joystick values
psxjoystick_1 = psxjoystick_1 - psxjoystick_1_Cal
psxjoystick_2 = psxjoystick_2 - psxjoystick_2_Cal
psxjoystick_3 = psxjoystick_3 - psxjoystick_3_Cal
psxjoystick_4 = psxjoystick_4 - psxjoystick_4_Cal
'limit Z and X mix from 1 to 255
If psxjoystick_1 > 178 Then psxjoystick_1 = 178
If psxjoystick_1 < 78 Then psxjoystick_1 = 78
If psxjoystick_3 > 188 Then psxjoystick_3 = 188
If psxjoystick_3 < 68 Then psxjoystick_3 = 68
'SerOut lcd,bds,[$A1,0,0,$A2,DEC3 psxjoystick_1,":",DEC3 psxjoystick_2,$00,$A1,0,1,$A2,DEC3 psxjoystick_3,":",DEC3 psxjoystick_4,$00]
'dead band and stretching
''''''''''''''''''''''''
Select psxjoystick_1
Case 0 To 111
psxjoystick_1 = psxjoystick_1 + 22
Case 106 To 150
psxjoystick_1 = 128
Case 145 To 255
psxjoystick_1 = psxjoystick_1 - 22
'CASE ELSE ' Otherwise...
' psxjoystick_1 = 128
EndSelect
''''''''''''''''''''''
Select psxjoystick_2
Case 0 To 111
psxjoystick_2 = psxjoystick_2 + 17
Case 111 To 145
psxjoystick_2 = 128
Case 145 To 255
psxjoystick_2 = psxjoystick_2 - 17
'Case Else ' Otherwise...
' psxjoystick_2 = 128
EndSelect
''''''''''''''''''''''''''''
Select psxjoystick_3
Case 0 To 111
psxjoystick_3 = psxjoystick_3 + 22
Case 106 To 150
psxjoystick_3 = 128
Case 145 To 255
psxjoystick_3 = psxjoystick_3 - 22
'Case Else ' Otherwise...
' psxjoystick_3 = 128
EndSelect
''''''''''''''''''''''''
Select psxjoystick_4
Case 0 To 111
psxjoystick_4 = psxjoystick_4 + 17
Case 111 To 145
psxjoystick_4 = 128
Case 145 To 255
psxjoystick_4 = psxjoystick_4 - 17
'Case Else ' Otherwise...
' psxjoystick_1 = 128
EndSelect
'SerOut lcd,bds,[$A1,9,0,$A2,DEC3 psxjoystick_1,":",DEC3 psxjoystick_2,$00,$A1,9,1,$A2,DEC3 psxjoystick_3,":",DEC3 psxjoystick_4,$00]
' dive psxjoystick_4 throttle (Y) psxjoystick_2
'HHHHHHHHH HHHHHHHHH
'H H H H
'H O H Rudder (X) psxjoystick_3 H O H travelling (Z) psxjoystick_1
'H H H H
'HHHHHHHHH HHHHHHHHH
'pour les moteurs :
'M_f_r = avant droit , sens positif propulse vers avant gauche
'M_f_l = avant gauche , sens positif propulse vers avant droit
'M_r_l = arriere gauche , sens positif propulse vers avant droit
'M_r_r = arriere droit , sens positif propulse vers avant gauche
'( les moteurs avant tirent vers l'avant, les arrières, vers l'arrière ! )
'pour les mixages je pensais à un truc trés simple du genre :
'M_f_r = Y - X - Z
'M_f_l = Y + X + Z
'M_r_l = -Y + X - Z
'M_r_r = -Y - X + Z
'Ensuite il suffit de tronquer les valeurs comme pour l'exemple du p'tipi.
'Pour améliorer, il est aussi possible de prévoir deux coeff de pondération :
'un pour X et Y ( commun au 2 axes ) et un pour Z, pour affiner les réactions en fonction des tests
''''''''''works''''''''''''''''''''''''''''''''''
M_f_r = (psxjoystick_1-128)
M_f_r = M_f_r + (psxjoystick_3-128)
M_f_r = M_f_r - (psxjoystick_2-128)
M_f_l = -(psxjoystick_1-128)
M_f_l = M_f_l - (psxjoystick_3-128)
M_f_l = M_f_l - (psxjoystick_2-128)
M_r_r = -(psxjoystick_1-128)
M_r_r = M_r_r + (psxjoystick_3-128)
M_r_r = M_r_r - (psxjoystick_2-128)
M_r_l = (psxjoystick_1-128)
M_r_l = M_r_l - (psxjoystick_3-128)
M_r_l = M_r_l - (psxjoystick_2-128)
''''''''''''''''''''''''''''''''''''''''''''''''''''
'offset
M_f_r = M_f_r + 128
M_r_l = M_r_l + 128
M_f_l = M_f_l + 128
M_r_r = M_r_r + 128
'scale to get 1 to 128
M_f_r = M_f_r/2
M_r_l = M_r_l/2
M_f_l = M_f_l/2
M_r_r = M_r_r/2
psxjoystick_4 = psxjoystick_4/2
'protection
If M_f_r >=128 Then M_f_r = 128
If M_f_r <=1 Then M_f_r = 1
If M_r_l >=128 Then M_r_l = 128
If M_r_l <=1 Then M_r_l = 1
If M_f_l >=128 Then M_f_l = 128
If M_f_l <=1 Then M_f_l = 1
If M_r_r >=128 Then M_r_r = 128
If M_r_r <=1 Then M_r_r = 1
If psxjoystick_4 >=128 Then psxjoystick_4 = 128
If psxjoystick_4 <=1 Then psxjoystick_4 = 1
'
'deadband:
'If 71 > M_f_r And 57 < M_f_r Then M_f_r = Center
'If 71 > M_r_l And 57 < M_r_l Then M_r_l = Center
'If 71 > M_f_l And 57 < M_f_l Then M_f_l = Center
'If 71 > M_r_r And 57 < M_r_r Then M_r_r = Center
'debuging on lcd
'SerOut lcd,bds,[$A1,0,0,$A2,SDEC3 M_f_r,",",SDEC3 M_f_l," ",$00,$A1,0,1,$A2,SDEC3 M_r_r,",",SDEC3 M_r_l," ",$00]
Return
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
get_buttons_sticks:
' The first shiftin gets the mode. (73) (my pic is not fast enaught to get the first byte send by the pad it gets next byte 5A instead)
' The next 2 shiftins gets the pushbutton data.
' The next 4 shiftins gets the analog joystick data.
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$42] ' Initiate request for data from PSX controller.
SHIn dat,clk,lsbpost_h,[psxmode,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
'SerOut lcd,bds,[$A1,0,1,hex psxdata[3],",",hex psxdata[5],",",hex psxdata[1]] 'see what is changing...
'SerOut lcd,bds,[$A1,0,2,hex psxdata[4],",",hex psxdata[6],",",hex psxdata[2]]
config_mode:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$43,$00,$01,$00] 'simply send config_mode data on command line
SHIn dat,clk,lsbpost_h,[psxmode,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
init_dualshock:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$4D,$00,$00,$01] 'simply send init_dualshock data on command line
SHIn dat,clk,lsbpost_h,[psxmode,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
vibration_en:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$4D,$00,$00,$01]
'SHIn dat,clk,lsbpost_h,[psxmode,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
vibration_run:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$42,$00,S_motor,B_motor]'instead od $FF >,psxjoystick_1]
'SHOut cmd,clk,lsbfirst_h,[$01,$42,$00,$FF,$FF]
'look's like we don't need to get data in the config mode so the line underneath is not necessary!
SHIn dat,clk,lsbpost_h,[psx_dummy,psx_dummy,psx_dummy,psx_dummy]',psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
analog_lock:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$44,$00,$01,$03] '$03 lock the mode $01 (analog)
SHIn dat,clk,lsbpost_h,[psxmode,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
digital_lock:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$44,$00,$00,$03]
'SHIn dat,clk,lsbpost_h,[,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
exit_config_mode:
Low att
SHOut cmd,clk,lsbfirst_h,[$01,$43,$00,$00,$5A]
SHIn dat,clk,lsbpost_h,[psxmode,psxbutton_1,psxbutton_2,psxjoystick_1,psxjoystick_2,psxjoystick_3,psxjoystick_4]
High att
Return
End
Esc:
'init the servos or esc
SerOut TX,16780 ,["T","A",Dec 64,13,"T","B",Dec 64,13,"T","C",Dec 64,13,"T","D",Dec 64,13]
SerOut TX,16780 ,["T","A",Dec 64,13,"T","B",Dec 64,13,"T","C",Dec 64,13,"T","D",Dec 64,13]
DelayMS 5000
SerOut TX,16780 ,["T","A",Dec 1,13,"T","B",Dec 1,13,"T","C",Dec 1,13,"T","D",Dec 1,13]
DelayMS 2000
SerOut TX,16780 ,["T","A",Dec 64,13,"T","B",Dec 64,13,"T","C",Dec 64,13,"T","D",Dec 64,13]
Return