forked from seanamtalay/ECE375
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ece375-L8_RX.asm
345 lines (283 loc) · 9.48 KB
/
ece375-L8_RX.asm
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
;***********************************************************
;*
;* Lab 8 RX - ROBOT
;*
;*
;***********************************************************
;*
;* Author: Samuel Jia Khai Lee & Namtalay Laorattanavech
;* Date: 3/13/2017
;*
;***********************************************************
.include "m128def.inc" ; Include definition file
;***********************************************************
;* Internal Register Definitions and Constants
;***********************************************************
.def mpr = r16 ; Multi-Purpose Register
.def mpr2 = r17
.def waitcnt = r18 ; Wait Loop Counter
.def ilcnt = r19 ; Inner Loop Counter
.def olcnt = r20 ; Outer Loop Counter
.def speed = r21
.def speed_level = r22
.equ WTime = 100 ; Time to wait in wait loop
.equ WskrR = 0 ; Right Whisker Input Bit
.equ WskrL = 1 ; Left Whisker Input Bit
.equ EngEnR = 4 ; Right Engine Enable Bit
.equ EngEnL = 7 ; Left Engine Enable Bit
.equ EngDirR = 5 ; Right Engine Direction Bit
.equ EngDirL = 6 ; Left Engine Direction Bit
.equ BotAddress = 0b11111111
;/////////////////////////////////////////////////////////////
;These macros are the values to make the TekBot Move.
;/////////////////////////////////////////////////////////////
.equ MovFwd = (1<<EngDirR|1<<EngDirL) ;0b01100000 Move Forward Action Code
.equ MovBck = $00 ;0b00000000 Move Backward Action Code
.equ TurnR = (1<<EngDirL) ;0b01000000 Turn Right Action Code
.equ TurnL = (1<<EngDirR) ;0b00100000 Turn Left Action Code
.equ Halt = (1<<EngEnR|1<<EngEnL) ;0b10010000 Halt Action Code
;***********************************************************
;* Start of Code Segment
;***********************************************************
.cseg ; Beginning of code segment
;***********************************************************
;* Interrupt Vectors
;***********************************************************
.org $0000 ; Beginning of IVs
rjmp INIT ; Reset interrupt
;Should have Interrupt vectors for:
;- Left whisker
;- Right whisker
;- USART receive
.org $0002
rcall HitRight
reti
.org $0004
rcall HitLeft
reti
.org $0003C
rcall USART_Receive
reti
.org $0046 ; End of Interrupt Vectors
;***********************************************************
;* Program Initialization
;***********************************************************
INIT:
;Stack Pointer (VERY IMPORTANT!!!!)
ldi mpr, high(RAMEND)
out SPH, mpr
ldi mpr, low(RAMEND)
out SPL, mpr
;I/O Ports
ldi mpr, $FF ;Set Port B Data Direction Regisiter
out DDRB, mpr
ldi mpr, $00 ;Initialize Port B Data Register
out PORTB, mpr
ldi mpr, $00 ;Set Port D Data Direction Register
out DDRD, mpr
ldi mpr, $FF ;Initialize Port D Data Register
out PORTD, mpr
;USART1
ldi mpr, (1<<U2X1)
sts UCSR1A, mpr
;Set baudrate at 2400bps
ldi mpr, high(416) ; Load high byte of 0x0340
sts UBRR1H, mpr ; UBRR0H in extended I/O space
ldi mpr, low(416) ; Load low byte of 0x0340
sts UBRR1L, mpr
;Enable receiver and enable receive interrupts
ldi mpr, (1<<RXEN1 | 1<<TXEN1 | 1<<RXCIE1)
sts UCSR1B, mpr
;Set frame format: 8 data bits, 2 stop bits
ldi mpr, (0<<UMSEL1 | 1<<USBS1 | 1<<UCSZ11 | 1<<UCSZ10)
sts UCSR1C, mpr ; UCSR0C in extended I/O space
;External Interrupts
;Set the External Interrupt Mask
ldi mpr, (1<<INT0) | (1<<INT1)
out EIMSK, mpr
;Set the Interrupt Sense Control to falling edge detection
ldi mpr, (1<<ISC01) | (0<<ISC00) | (1<<ISC11) | (0<<ISC10)
sts EICRA, mpr ;Use sts, EICRA in extended I/O space
; Configure 8-bit Timer/Counters
ldi mpr, 0b01010000 ; Fast PWM w/ toggle
out TCCR0, mpr ;
ldi mpr, 0b01010000
out TCCR2, mpr ;
;default state
ldi speed_level, 0 ;Initialize Speed level and clock
out OCR0, speed_level
out OCR2, speed_level
ldi speed, 0 ;Initialize Speed
andi mpr, $F0 ;
or mpr, speed ;
out PORTB, mpr ;
; Initialize Fwd Movement
ldi mpr, MovFwd
out PORTB, mpr
sei
;Other
;***********************************************************
;* Main Program
;***********************************************************
MAIN:
rjmp MAIN
;***********************************************************
;* Functions and Subroutines
;***********************************************************
;----------------------------------------------------------------
; Sub: USART_Receive
; Desc: Receive USART Command from Transmitter
;----------------------------------------------------------------
USART_Receive:
lds mpr, UDR1 ;Read Bot Address and compare
ldi mpr2, BotAddress ;if Bot Address is incorrect, then return interrupt
cpse mpr, mpr2 ;aka do nothing, else continue
ret
ldi waitcnt, 55
rcall Wait
lds mpr, UDR1 ;Read Action Code and Print it
out PORTB, mpr ;Print Action Code
ldi waitcnt, WTime
rcall Wait
cpi mpr, (1<<6|1<<4) ;Check Speed Min, Speed Max, Speed Down, Speed Up bits
breq INPUT0
cpi mpr, ($80|1<<(EngDirL-1))
breq INPUT1
cpi mpr, (1<<(EngDirR-1)|1<<(EngDirL-1))
breq INPUT2
cpi mpr,($80|1<<(EngDirL))
breq INPUT3
rcall UPLOAD ;Update Leds
ldi mpr, (1<<INT0 | 1<<INT1) ;Clean Queue
out EIFR, mpr
ldi waitcnt, 160
rcall Wait
ret
;SPEED UP, SPEED DOWN, SPEED MIN, SPEED MAX FUNCTIONS FROM LAB 7
INPUT0:;min speed
ldi speed_level, 0 ;Min Speed
ldi speed, 0
out OCR0, speed_level
out OCR2, speed_level
rcall UPLOAD
ret
INPUT1:;max speed
ldi speed_level, 15 ;Max Speed
ldi speed, $F
out OCR0, speed_level
out OCR2, speed_level
rcall UPLOAD
ret
INPUT2:;-speed
cpi speed_level,0 ;Check if Min speed, else dec speed and speed level
breq INPUT0
dec speed
dec speed_level
out OCR0, speed_level
out OCR2, speed_level
rcall UPLOAD
ret
INPUT3:;+speed
cpi speed_level,15 ;Check if Max Speed, else inc speed and speed level
breq INPUT1
ldi mpr, 1
inc speed
inc speed_level
out OCR0, speed_level
out OCR2, speed_level
rcall UPLOAD
ret
;######################
UPLOAD:
ldi mpr, Halt ;Update Leds
or mpr, speed
out PORTB, mpr
ret
;----------------------------------------------------------------
; Sub: HitRight
; Desc: Handles functionality of the TekBot when the right whisker
; is triggered.
;----------------------------------------------------------------
HitRight:
push mpr ; Save mpr register
push waitcnt ; Save wait register
in mpr, SREG ; Save program state
push mpr ;
; Move Backwards for a second
ldi mpr, MovBck ; Load Move Backward command
out PORTB, mpr ; Send command to port
ldi waitcnt, WTime ; Wait for 1 second
rcall Wait ; Call wait function
; Turn left for a second
ldi mpr, TurnL ; Load Turn Left Command
out PORTB, mpr ; Send command to port
ldi waitcnt, WTime ; Wait for 1 second
rcall Wait ; Call wait function
ldi mpr,(1<<INT0 | 1<<INT1) ; clean the queue
out EIFR,mpr
; Move Forward again
ldi mpr, MovFwd ; Load Move Forward command
out PORTB, mpr ; Send command to port
ldi mpr, (1<<INT0 | 1<<INT1) ;Clean Queue
out EIFR, mpr
pop mpr ; Restore program state
out SREG, mpr
pop waitcnt ; Restore wait register
pop mpr ; Restore mpr
ret ; Return from subroutine
;----------------------------------------------------------------
; Sub: HitLeft
; Desc: Handles functionality of the TekBot when the left whisker
; is triggered.
;----------------------------------------------------------------
HitLeft:
push mpr ; Save mpr register
push waitcnt ; Save wait register
in mpr, SREG ; Save program state
push mpr ;
; Move Backwards for a second
ldi mpr, MovBck ; Load Move Backward command
out PORTB, mpr ; Send command to port
ldi waitcnt, WTime ; Wait for 1 second
rcall Wait ; Call wait function
; Turn right for a second
ldi mpr, TurnR ; Load Turn Left Command
out PORTB, mpr ; Send command to port
ldi waitcnt, WTime ; Wait for 1 second
rcall Wait ; Call wait function
ldi mpr,(1<<INT0 | 1<<INT1) ; clean the queue
out EIFR,mpr
; Move Forward again
ldi mpr, MovFwd ; Load Move Forward command
out PORTB, mpr ; Send command to port
ldi mpr, (1<<INT0 | 1<<INT1) ;Clean Queue
out EIFR, mpr
pop mpr ; Restore program state
out SREG, mpr ;
pop waitcnt ; Restore wait register
pop mpr ; Restore mpr
ret ; Return from subroutine
;----------------------------------------------------------------
; Sub: Wait
; Desc: A wait loop that is 16 + 159975*waitcnt cycles or roughly
; waitcnt*10ms. Just initialize wait for the specific amount
; of time in 10ms intervals. Here is the general eqaution
; for the number of clock cycles in the wait loop:
; ((3 * ilcnt + 3) * olcnt + 3) * waitcnt + 13 + call
;----------------------------------------------------------------
Wait:
push waitcnt ; Save wait register
push ilcnt ; Save ilcnt register
push olcnt ; Save olcnt register
Loop: ldi olcnt, 224 ; load olcnt register
OLoop: ldi ilcnt, 237 ; load ilcnt register
ILoop: dec ilcnt ; decrement ilcnt
brne ILoop ; Continue Inner Loop
dec olcnt ; decrement olcnt
brne OLoop ; Continue Outer Loop
dec waitcnt ; Decrement wait
brne Loop ; Continue Wait loop
pop olcnt ; Restore olcnt register
pop ilcnt ; Restore ilcnt register
pop waitcnt ; Restore wait register
ret ; Return from subroutine