-
Notifications
You must be signed in to change notification settings - Fork 1
/
Final Receiver Code.c
546 lines (510 loc) · 13.7 KB
/
Final Receiver Code.c
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//ELEC 291 Project 2
//Receiver/Car controller
//Hannah Sawiuk, Adrian Viquez, Jake Osborne, Sajan Rajdev, Gregor Morrison
//********************************//
// Included Libraries //
//********************************//
#include <C8051F38x.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
//********************************//
// Defined Constants //
//********************************//
#define SYSCLK 48000000L // SYSCLK frequency in Hz
#define BAUDRATE 115200L // Baud rate of UART in bps
// ADC Pins //
#define backright_ADC LQFP32_MUX_P1_5
#define backleft_ADC LQFP32_MUX_P1_6
#define right_ADC LQFP32_MUX_P1_7
#define left_ADC LQFP32_MUX_P2_0
#define center_ADC LQFP32_MUX_P2_1
#define comm_ADC LQFP32_MUX_P1_4
// General Constants //
#define pwm_constant 50 //Base PWM value
#define rotate_constant 45 //Rotate PWM value
#define Ithresh_voltage 1 //Approaching intersection voltage value
#define sensitivity 0.1 //maintain_d voltage sensitivity factor
// MOSFET Pins //
#define OUT_L1 P2_2
#define OUT_L0 P2_3
#define OUT_R1 P2_5
#define OUT_R0 P2_4
#define Receive P1_1
//********************************//
// Function Prototypes //
//********************************//
void maintain_d (void);
void stop (void);
void forward (void);
void backward (void);
void rotate(void);
void turn_left(void);
void turn_right(void);
void bin2dec (void);
//********************************//
// Volatile Variables // note: type unsigned char or unsigned int only
//********************************//
// PWM //
volatile unsigned char pwm_count = 0;
volatile unsigned char pwmr = 0;
volatile unsigned char pwml = 0;
volatile unsigned char direction = 1; //1=forward, 0 = backwards
volatile unsigned char overflow_count = 0;
volatile unsigned char I_flag = 0; //1 = intersection detected, 0 = no intersection detected
volatile unsigned char pulse_count = 0;
volatile unsigned char decode_flag = 0;
/// ADC //
volatile float V_ADC[6];
volatile float dright = 0.0;
volatile float dleft = 0.0;
// Instructions //
volatile unsigned char action = 1;//stop
volatile unsigned char Instructions[3];
//********************************//
// Main Initializations //
//********************************//
char _c51_external_startup (void)
{
PCA0MD&=(~0x40); // DISABLE WDT: clear Watchdog Enable bit
VDM0CN=0x80; // enable VDD monitor
RSTSRC=0x02|0x04; // Enable reset on missing clock detector and VDD
// CLKSEL&=0b_1111_1000; // Not needed because CLKSEL==0 after reset
#if (SYSCLK == 12000000L)
//CLKSEL|=0b_0000_0000; // SYSCLK derived from the Internal High-Frequency Oscillator / 4
#elif (SYSCLK == 24000000L)
CLKSEL|=0b_0000_0010; // SYSCLK derived from the Internal High-Frequency Oscillator / 2.
#elif (SYSCLK == 48000000L)
CLKSEL|=0b_0000_0011; // SYSCLK derived from the Internal High-Frequency Oscillator / 1.
#else
#error SYSCLK must be either 12000000L, 24000000L, or 48000000L
#endif
OSCICN |= 0x03; // Configure internal oscillator for its maximum frequency
// Configure UART0
SCON0 = 0x10;
#if (SYSCLK/BAUDRATE/2L/256L < 1)
TH1 = 0x10000-((SYSCLK/BAUDRATE)/2L);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
#elif (SYSCLK/BAUDRATE/2L/256L < 4)
TH1 = 0x10000-(SYSCLK/BAUDRATE/2L/4L);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
#elif (SYSCLK/BAUDRATE/2L/256L < 12)
TH1 = 0x10000-(SYSCLK/BAUDRATE/2L/12L);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
#else
TH1 = 0x10000-(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
#endif
// Timer1 for Communication //
TL1 = TH1; // Init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI = 1; // Indicate TX0 ready
// Configure the pins used for square output
P2MDOUT|=0b_0000_1100;
P0MDOUT |= 0x10; // Enable UTX as push-pull output
XBR0 = 0x01; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1 = 0x40; // Enable crossbar and weak pull-ups
// Initialize timer 2 for periodic interrupt//
TMR2CN=0x00; // Stop Timer2; Clear TF2;
CKCON|=0b_0001_0000;
TMR2RL=(-(SYSCLK/(2*48))/(100L)); // Initialize reload value
TMR2=0xffff; // Set to reload immediately
ET2=1; // Enable Timer2 interrupts
TR2=1; // Start Timer2
//External INT1 and INT0 interrupt configuration
IT01CF|=0b_0001_1000; //(P0_1 for INT1 and P0_0 for INT0)->
TCON&=0b_1111_1101;
IT0 = 1;
EX0 = 1;
// Enable interrupts
EA=1;
return 0;
}
//********************************//
// Timer 3 Configuration //
//********************************//
void Timer3us(unsigned char us)
{
unsigned char i; // usec counter
// The input for Timer 3 is selected as SYSCLK by setting T3ML (bit 6) of CKCON:
CKCON|=0b_0100_0000;
TMR3RL = (-(SYSCLK)/1000000L); // Set Timer3 to overflow in 1us.
TMR3 = TMR3RL; // Initialize Timer3 for first overflow
TMR3CN = 0x04; // Sart Timer3 and clear overflow flag
for (i = 0; i < us; i++) // Count <us> overflows
{
while (!(TMR3CN & 0x80)); // Wait for overflow
TMR3CN &= ~(0x80); // Clear overflow indicator
// Check overflow of Timer/Counter 0
if (TF0==1)
{
TF0=0;
overflow_count++;
}
}
TMR3CN = 0 ; // Stop Timer3 and clear overflow flag
}
//********************************//
// WaitMilliSeconds //
//********************************//
void waitms (unsigned int ms)
{
unsigned int j;
for(j=ms; j!=0; j--)
{
Timer3us(249);
Timer3us(249);
Timer3us(249);
Timer3us(250);
}
}
//********************************//
// External interrupt (INT0) //
//********************************//
void External_Interrupt (void) interrupt 0 //P0_1 as trigger
{
//MSB first
if((pulse_count+1)==1)
{
waitms(30);
pulse_count++;
//This is the trigger
}
else if((pulse_count+1 > 1) && (pulse_count <=3))
{
decode_flag = 0; //don't decode instruction yet
waitms(80);
Instructions[pulse_count-1] = Receive;
pulse_count++;
}
else
{
waitms(80);
pulse_count = 0;
decode_flag = 1; //decode and read instruction
}
}
//********************************//
// Timer2 interrupt //
//********************************//
void Timer2_ISR (void) interrupt 5
{
TF2H = 0; // Clear Timer2 interrupt flag
pwm_count++;
if(pwm_count > 100)
pwm_count = 0;
if(direction == 1)//forward
{
OUT_R0 = 0;
OUT_R1 = pwm_count > (100-pwmr)?1:0;
OUT_L0 = 0;
OUT_L1 = pwm_count > (100-pwml)?1:0;
}
else if(direction == 0)//backward
{
OUT_R0 = pwm_count > (100-pwmr)?1:0;
OUT_R1 = 0;
OUT_L0 = pwm_count > (100-pwml)?1:0;
OUT_L1 = 0;
}
else if(direction == 2) //Left wheel forward, right wheel backwards
{
OUT_R0 = pwm_count > (100-pwmr)?1:0;
OUT_R1 = 0;
OUT_L0 = 0;
OUT_L1 = pwm_count > (100-pwml)?1:0;
}
else //Right wheel forward, left wheel backwards
{
OUT_R0 = 0;
OUT_R1 = pwm_count > (100-pwmr)?1:0;
OUT_L0 = pwm_count > (100-pwml)?1:0;
OUT_L1 = 0;
}
}
//********************************//
// Initialize ADC //
//********************************//
void InitADC (void)
{
// Init ADC
ADC0CF = 0xF8; // SAR clock = 31, Right-justified result
ADC0CN = 0b_1000_0000; // AD0EN=1, AD0TM=0
REF0CN = 0b_0000_1000; //Select VDD as the voltage reference for the converter
}
//********************************//
// Initialize ADC Pins //
//********************************//
void InitPinADC (unsigned char portno, unsigned char pinno)
{
unsigned char mask;
mask=1<<pinno;
switch (portno)
{
case 0:
P0MDIN &= (~mask); // Set pin as analog input
P0SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 1:
P1MDIN &= (~mask); // Set pin as analog input
P1SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 2:
P2MDIN &= (~mask); // Set pin as analog input
P2SKIP |= mask; // Skip Crossbar decoding for this pin
break;
case 3:
P3MDIN &= (~mask); // Set pin as analog input
P3SKIP |= mask; // Skip Crossbar decoding for this pin
break;
default:
break;
}
}
//********************************//
// ADC Value at Pin //
//********************************//
unsigned int ADC_at_Pin(unsigned char pin)
{
AMX0P = pin; // Select positive input from pin
AMX0N = LQFP32_MUX_GND; // GND is negative input (Single-ended Mode)
// Dummy conversion first to select new pin
AD0BUSY=1;
while (AD0BUSY); // Wait for dummy conversion to finish
// Convert voltage at the pin
AD0BUSY = 1;
while (AD0BUSY); // Wait for conversion to complete
return (ADC0L+(ADC0H*0x100));
}
//********************************//
// Voltage at Pin //
//********************************//
float Volts_at_Pin(unsigned char pin)
{
return ((ADC_at_Pin(pin)*3.30)/1024.0);
}
//********************************//
// Main Program //
//********************************//
void main (void)
{
int i = 0;
int j = 0;
InitPinADC(1,4); // Backright Inductor
InitPinADC(1,5); // Backright Inductor
InitPinADC(1,6); // Backleft inductor
InitPinADC(1,7); // Right inductor
InitPinADC(2,0); // Left Inductor
InitPinADC(2,1); // Center Inductor
InitADC(); // Initialize the ADC
for(i = 0; i < 3; i++) //initialize instructions and voltage arrays
Instructions[i]=0;
for(j = 0; j < 5; j++)
V_ADC[i]=0;
pulse_count = 0;
stop();
//main system run
while(1)
{
V_ADC[0]=Volts_at_Pin(right_ADC); //Right Inductor
V_ADC[1]=Volts_at_Pin(left_ADC); //Left Inductor
V_ADC[2]=Volts_at_Pin(center_ADC); //Center Inductor
V_ADC[3]=Volts_at_Pin(backright_ADC); //Backright Inductor
V_ADC[4]=Volts_at_Pin(backleft_ADC); //Backleft Inductor
V_ADC[5]=Volts_at_Pin(comm_ADC); //Backleft Inductor
if(decode_flag == 1) //if new instructions, decode and store into action
bin2dec();
if (direction == 1 || direction == 0) //only maintain distance when going forward or backward
maintain_d();
if(V_ADC[2] > Ithresh_voltage)
I_flag = 1;
else
I_flag = 0;
if( V_ADC[5] < sensitivity) //if the car is off track, stop
stop();
else
{
if(action == 1)
{
stop();
}
else if(action == 2)
{
if(I_flag == 1)
turn_right();
else
forward();
}
else if(action == 3)
{
if(I_flag == 1)
turn_left();
else
forward();
}
else if(action == 4)
{
forward();
}
else if(action == 5)
{
backward();
}
else if(action == 6)
{
rotate();
}
else
{
stop();
}
}
}
}
//********************************//
// Maintain Distance //
//********************************//
void maintain_d (void)
{
if (direction == 1) //forwards
{
dright = V_ADC[0]; //right
dleft = V_ADC[1]; //left
if (fabsf(dright-dleft) > sensitivity)//Constant adjusts sensitivity
{
if(dright > dleft)
{
pwmr = (unsigned char)(pwm_constant*fabsf(dleft/dright));
pwml = pwm_constant;
}
else
{
pwml = (unsigned char)(pwm_constant*fabsf(dright/dleft));
pwmr = pwm_constant;
}
}
else
{
pwmr = pwm_constant;
pwml = pwm_constant;
}
}
else if (direction == 0)
{
dright = V_ADC[3]; //backright
dleft = V_ADC[4]; //backleft
if (fabsf(dright-dleft) > sensitivity)//Constant adjusts sensitivity
{
if(dright > dleft)
{
pwmr = (unsigned char)(pwm_constant*fabsf(dleft/dright));
pwml = pwm_constant;
}
else
{
pwml = (unsigned char)(pwm_constant*fabsf(dright/dleft));
pwmr = pwm_constant;
}
}
else
{
pwmr = pwm_constant;
pwml = pwm_constant;
}
}
}
//********************************//
// Bin2Dec //
//********************************//
void bin2dec (void)
{
action = (unsigned char)((Instructions[2] + 2*Instructions[1] + 4*Instructions[0])-1);
}
//********************************//
// 1:Stop //
//********************************//
void stop (void)
{
pwml = 0;
pwmr = 0;
}
//***************************************//
// 2:Turn right at the next intersection //
//***************************************//
void turn_right(void)
{
direction = 3; //turn
stop();
waitms(100); //delay for short time
pwmr = pwm_constant;
pwml = 0;
while(fabsf(V_ADC[0]-V_ADC[1]) > sensitivity) //keep turning while checking voltages until fairly centered
{
V_ADC[0]=Volts_at_Pin(right_ADC); //Right Inductor -> negative input of the peak detector
V_ADC[1]=Volts_at_Pin(left_ADC); //Left Inductor
}
action = 4; //reset action
forward(); //then follow the wire
}
//***************************************//
// 3:Turn left at the next intersection //
//***************************************//
void turn_left(void)
{
direction = 2;
stop();
waitms(100);
pwml = pwm_constant;
pwmr = 0;
while(fabsf(V_ADC[0]-V_ADC[1]) > sensitivity)
{
V_ADC[0]=Volts_at_Pin(right_ADC); //Right Inductor -> negative input of the peak detector
V_ADC[1]=Volts_at_Pin(left_ADC); //Left Inductor
}
action = 4;
forward(); //go forward after
}
//********************************//
// 4:Forward //
//********************************//
void forward(void)
{
direction = 1;//forward
pwml = pwm_constant;
pwmr = pwm_constant;
maintain_d();
}
//********************************//
// 5:Backward //
//********************************//
void backward(void)
{
direction = 0;//backward
pwml = pwm_constant;
pwmr = pwm_constant;
maintain_d();
}
//********************************//
// 6:Rotate //
//********************************//
void rotate(void)
{
direction = 2;
pwml = rotate_constant;
pwmr = rotate_constant;
waitms(500);
while(fabsf(V_ADC[0]-V_ADC[1]) > sensitivity)
{
V_ADC[0]=Volts_at_Pin(right_ADC); //Right Inductor -> negative input of the peak detector
V_ADC[1]=Volts_at_Pin(left_ADC); //Left Inductor
}
stop();
action = 4;
forward(); //go forward after
}