Berikut adalah program bascom AVR untuk percobaan joystick yang baru saja saya selesaikan, merupakan tugas kuliah praktek antar muka mikroprosesor, berikut programnya :
'----------------------------------------------------------
'Nama File : JoystickPS2.Bas
'Ditulis oleh : Dori Saka Pradito
'
'Support : - Sony PSX Handcontroller
' - Hardware SPI Function in BASCOM
' - External crystal 11.0592 MHz
'
'PS2 pinout dapat dilihat dibawah ini :
' _____________________
'Pin 1 ->| o o o | o o o | o o o |
' \___________________/
'
'Untuk ATMEGA 8535
'Keterangan :
'Pin 1 = Data (PSX PAD data line output) - PORTB.1
'Pin 2 = Command (PSX PAD data line input - PORTB.5
'Pin 3 = N/C no connection
'Pin 4 = GND obvious
'Pin 5 = VCC +5VDC obvious
'Pin 6 = ATT (Select) - PORTB.2
'Pin 7 = Clock Line (SCK) - PORTB.7
'Pin 8 = N/C no connection
'Pin 9 = ACK no connection
'
'----------------------------------------------------------
$regfile = "m8535.dat"
$crystal = 11059200
$baud = 9600
'----------------------------------------------------------
'Simbol-simbol pada controller
Deflcdchar 0 , 4 , 4 , 10 , 10 , 27 , 17 , 17 , 31 'Segitiga
Deflcdchar 1 , 31 , 17 , 17 , 17 , 17 , 17 , 17 , 31 ' Kotak
Deflcdchar 2 , 4 , 14 , 21 , 4 , 4 , 4 , 4 , 4 ' Up
Deflcdchar 3 , 4 , 4 , 4 , 4 , 4 , 21 , 14 , 4 ' Down
Deflcdchar 4 , 32 , 32 , 4 , 2 , 31 , 2 , 4 , 32 ' Right
Deflcdchar 5 , 32 , 32 , 4 , 8 , 31 , 8 , 4 , 32 ' Left
'----------------------------------------------------------
'LCD 16x2
'Menggunakan PORTC
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3 , Db6 = Portc.4
Config Lcdpin = Pin , Db7 = Portc.5 , Rs = Portc.0 , E = Portc.1
Cursor Noblink Off : Cls
'----------------------------------------------------------
'SPI
Config Spi = Hard , Interrupt = Off , Data Order = Lsb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 128
Spiinit
Config Portb.1 = Output
Pad_att_line Alias Portb.1
Dim Cmdstart As Byte : Cmdstart = &H01
Dim Cmdstatus As Byte : Cmdstatus = &H42
Dim Joy_present As Byte , Joy_mode As Byte
Dim Joy_byte1 As Byte , Joy_byte2 As Byte
Dim Joy_kanan_x As Byte , Joy_kanan_y As Byte
Dim Joy_kiri_x As Byte , Joy_kiri_y As Byte
'----------------------------------------------------------
Key_select Alias Joy_byte1.0
Key_joyr Alias Joy_byte1.1
Key_joy1 Alias Joy_byte1.2
Key_start Alias Joy_byte1.3
Key_up Alias Joy_byte1.4
Key_right Alias Joy_byte1.5
Key_down Alias Joy_byte1.6
Key_left Alias Joy_byte1.7
Key_l2 Alias Joy_byte2.0
Key_r2 Alias Joy_byte2.1
Key_l1 Alias Joy_byte2.2
Key_r1 Alias Joy_byte2.3
Key_triangle Alias Joy_byte2.4
Key_circle Alias Joy_byte2.5
Key_x Alias Joy_byte2.6
Key_square Alias Joy_byte2.7
Config Portd.7 = Output
Config Portd.6 = Output
Config Portd.5 = Output
Config Portd.4 = Output
'----------------------------------------------------------
Cls
Lcd "Tes Joystick PS2"
Lowerline
Lcd "-----SUKSES-----"
Wait 3
Cls
Lcd "--DORI & JIMI-- "
Wait 3
Cls
Do
Reset Pad_att_line : Waitus 100
Spiout Cmdstart , 1
Joy_mode = Spimove(cmdstatus)
Spiin Joy_present , 1
Spiin Joy_byte1 , 1
Spiin Joy_byte2 , 1
Spiin Joy_kanan_x , 1
Spiin Joy_kanan_y , 1
Spiin Joy_kiri_x , 1
Spiin Joy_kiri_y , 1
Set Pad_att_line : Waitus 100
If Joy_byte1.3 = 0 Then
Locate 1 , 1
Lcd "Start "
Print "Start "
Portd.4 = 0
Portd.5 = 1
Portd.6 = 0
Portd.7 = 0
Waitus 500
Elseif Joy_byte1.0 = 0 Then
Locate 1 , 1
Lcd "Select "
Print "Select "
Portd.4 = 0
Portd.5 = 1
Portd.6 = 0
Portd.7 = 0
Waitus 500
Elseif Joy_byte1.1 = 0 Then
Locate 1 , 1
Lcd "Joystick kanan "
Locate 2 , 1
Lcd "X = " ; Joy_kanan_x ; "Y = " ; Joy_kanan_y
Print "Joystick kanan "
Elseif Joy_byte1.2 = 0 Then
Locate 1 , 1
Lcd "Joystick kiri "
Locate 2 , 1
Lcd "X = " ; Joy_kiri_x ; "Y = " ; Joy_kiri_y
Print "Joystick kiri "
Elseif Joy_byte1.4 = 0 Then
Locate 1 , 1
Lcd Chr(2) ; " Panah keatas "
Print "Panah keatas "
Portd.4 = 0
Portd.5 = 1
Portd.6 = 0
Portd.7 = 0
Waitus 500
Elseif Key_down = 0 Then
Locate 1 , 1
Lcd Chr(3) ; " Panah kebawah "
Print "Panah kebawah "
Portd.4 = 0
Portd.5 = 1
Portd.6 = 0
Portd.7 = 0
Waitus 500
Elseif Key_right = 0 Then
Locate 1 , 1
Lcd Chr(4) ; " Panah kekanan"
Print "Panah kekanan "
Portd.4 = 0
Portd.5 = 1
Portd.6 = 0
Portd.7 = 0
Waitus 500
Elseif Key_left = 0 Then
Locate 1 , 1
Lcd Chr(5) ; " Panah kekiri "
Print "Panah kekiri "
Portd.4 = 0
Portd.5 = 1
Portd.6 = 0
Portd.7 = 0
Waitus 500
Elseif Key_triangle = 0 Then
Locate 1 , 1
Lcd Chr(0) ; " Segitiga "
Print "Segitiga "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 0
Portd.7 = 1
Waitus 500
Elseif Key_x = 0 Then
Locate 1 , 1
Lcd "Tombol X "
Print "Tombol X "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 0
Portd.7 = 1
Waitus 500
Elseif Key_square = 0 Then
Locate 1 , 1
Lcd Chr(1) ; " Kotak "
Print "Kotak "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 0
Portd.7 = 1
Waitus 500
Elseif Key_circle = 0 Then
Locate 1 , 1
Lcd "O Lingkaran "
Print "Lingkaran "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 0
Portd.7 = 1
Waitus 500
Elseif Key_l1 = 0 Then
Locate 1 , 1
Lcd "L1 "
Print "L1 "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 1
Portd.7 = 0
Waitus 500
Elseif Key_l2 = 0 Then
Locate 1 , 1
Lcd "L2 "
Print "L2 "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 1
Portd.7 = 0
Waitus 500
Elseif Key_r1 = 0 Then
Locate 1 , 1
Lcd "R1 "
Print "R1 "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 1
Portd.7 = 0
Waitus 500
Elseif Key_r2 = 0 Then
Locate 1 , 1
Lcd "R2 "
Print "R2 "
Portd.4 = 0
Portd.5 = 0
Portd.6 = 1
Portd.7 = 0
Elseif Pad_att_line = 1 Then
Locate 1 , 1
Lcd "Tidak ada respon"
Print "Tidak ada respon"
Portd.4 = 1
Portd.5 = 0
Portd.6 = 0
Portd.7 = 0
End If
Waitus 100
Loop
'----------------------------------------------------------
Pada program diatas sudah bisa terhubung dengan komputer, dengan menggunakan rangkaian serial MAX232, bisa ditampilkan dengan hyperterminal ataupun menggunakan aplikasi Visual Basic 6.0 pada komputer.
Untuk pengembangan, jika ada yang bisa membantu untuk menambahkan program pembacaan nilai X dan Y pada joystick mohon beri komentar, karena pada program diatas belum saya masukkan untuk keadaan Red Mode.