1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
$regfile = "m8def.dat"
'8MHz = highest internal frequency of the ATMega8
$crystal = 8000000
'configuring the ADC, its resolution is 10bit per channel
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc
'pins for the output of the X-axis
Config Portb.1 = Output
Config Portb.2 = Output
'pins for the output of the y-axis
Config Portb.3 = Output
Config Portb.4 = Output
'variables in which the 10bit value of the ADC will be stored
Dim X As Integer , Y As Integer
'init X axis. the value is divided by 2 because otherwise it's too big
X = Getadc(1)
Shift X , Right , 2 , Signed
'init Y-axis
Y = Getadc(2)
Shift Y , Right , 2 , Signed
'variables to store the old X and Y values for comparison in the next round
Dim Oldx As Integer , Oldy As Integer
Oldx = X
Oldy = Y
'rotating the xwheel and ywheel bytes 1 step to the left or the right is the
'same as turning the optical wheels in the N64 stick 1 step clockwise or
'counter-clockwise.
'by fixating on two bits of these bytes (let's say xwheel.0 and xwheel.1) and
'then rotating the byte 1 step to the left or the right, you'll get the new
'gray code on the these two bits (e.g. xwheel.0=A , xwheel.1=B)
Dim Xwheel As Byte , Ywheel As Byte
Xwheel = &B11001100
Ywheel = &B11001100
Dim I As Byte
'in these two variables we're storing the number of steps we have to process for each axis
Dim Xsteps As Integer , Ysteps As Integer
'main loop:
'-------------
Do
'get and store X-value; resolution/2
X = Getadc(1)
Shift X , Right , 2 , Signed
'get and store X-value; resolution/2
Y = Getadc(2)
Shift Y , Right , 2 , Signed
'calculate the number of steps we have to process
Xsteps = X
Xsteps = Xsteps - Oldx
Ysteps = Y
Ysteps = Ysteps - Oldy
'stay in while-loop until all steps of the X- and Y-axis are processed
While Xsteps <> 0 Or Ysteps <> 0
'steps of the x-axis
If Xsteps > 0 Then
Rotate Xwheel , Left , 1
Xsteps = Xsteps - 1
Elseif Xsteps < 0 Then
Rotate Xwheel , Right , 1
Xsteps = Xsteps + 1
End If
'steps of the y-axis
If Ysteps > 0 Then
Rotate Ywheel , Right , 1
Ysteps = Ysteps - 1
Elseif Ysteps < 0 Then
Rotate Ywheel , Left , 1
Ysteps = Ysteps + 1
End If
'write the new gray codes for both axis in I
I.1 = Xwheel.1
I.2 = Xwheel.2
I.3 = Ywheel.1
I.4 = Ywheel.2
'...and then write I to port B
Portb = I
'we have to wait a little bit for processing the next steps because otherwise
'it would be too fast and the IC in the N64 controller would skip some steps
Waitus 10
Wend
'store the values of both axis for comparison in the next round
Oldx = X
Oldy = Y
Loop
End
|