-
Notifications
You must be signed in to change notification settings - Fork 4
/
BussFunctions.c
448 lines (346 loc) · 7.69 KB
/
BussFunctions.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
/*
* SEND.c
*
* Created: 4/7/2014 1:18:39 PM
* Author: robsv107
*/
/*
* SendSensor.c
*
* Created: 4/7/2014 8:44:12 AM
* Author: robsv107
*/
#include <avr/io.h>
#include <inttypes.h>
/***********************MASTER Styr*****************************/
typedef int bool;
enum {false, true};
//Start bytes for transition
char front = 0b00000001;
char rightfront = 0b00000010;
char rightback = 0b00000011;
char leftfront = 0b00000100;
char leftback = 0b00000101;
char traveldist = 0b00000110;
char gyro = 0b00000111;
char RFID = 0b00001000;
char direction = 0b00001001;
char rightspeed = 0b00001010;
char leftspeed = 0b00001011;
char stop = 0x00; //Stop byte
char start_request = 0;
//Control signals
char right = 1;
char left = 2;
char turn = 3;
bool remoteControl = false; // Change to Port connected to switch
bool regulateright = false;
bool regulateleft = false;
bool regulateturn = false;
int time = 50;
unsigned char storedValues[11] = {0b11111111, 0b11001100, 0b00110011, 0b00000000, 0b00011100, 0b11100011, 0b11000111
, 0b00111000, 0b11111111, 0b11001100, 0b00110011};
/*********************************BUSSFUNKTIONER******************************/
void MasterInit(void)
{
/* Set MOSI and SCK output, alla others input*/
/* Ersätt DDR_SPI med den port "serie" som används ex DD_SPI -> DDRB
samt DD_MOSI och DD_SCK med specifik pinne ex DD_MOSI -> DDB5 */
DDRB = (1<<DDB3)|(1<<DDB4)|(1<<DDB5)|(1<<DDB7);
/* Enable SPI, Master, set clock rate fosc/64 and mode 3 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPI2X)|(1<<SPR1)|(1<<SPR0)|(1<<CPHA)|(1<<CPOL);
/* Set Slave select high */
PORTB = (1<<PORTB3)|(1<<PORTB4);
/* Enable External Interrupts */
sei();
}
void MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
//SPSR = (1<<SPIF);
}
void bussdelay()
{
for(int i = 0; i < time; i++){}
}
void transmit()
{
TransmitSensor(0);
TransmitComm();
}
void TransmitSensor(char invalue)
{
if(start_request == 1)
{
start_request = 0;
PORTB &= 0b11101111; // ss2 low
if(invalue == turn) // Starta flöde av gyrovärden
{
MasterTransmit(gyro);
bussdelay();
MasterTransmit(stop);
storedValues[6] = SPDR; // Gyro
}
else if(invalue == turnstop) // Stoppa flöde av gyrovärden
{
MasterTransmit(gyrostop);
}
else // Annars ta emot ett paket med övriga sensorvärden
{
MasterTransmit(RFID);
//First communication will contain crap on shift register
bussdelay();
MasterTransmit(traveldist); // Request front sensor
bussdelay();
storedValues[7] = SPDR; // SensorRFID
MasterTransmit(front); // Request front sensor
bussdelay();
storedValues[5] = SPDR; // Distance
MasterTransmit(rightfront);
bussdelay();
storedValues[0] = SPDR; // Front
MasterTransmit(rightback);
bussdelay();
storedValues[1] = SPDR; // Right front
MasterTransmit(leftfront);
bussdelay();
storedValues[2] = SPDR; // Right back
MasterTransmit(leftback);
bussdelay();
storedValues[3] = SPDR; // Left front
MasterTransmit(stop);
bussdelay();
storedValues[4] = SPDR; // Left back
}
PORTB ^= 0b00010000; // ss2 high
distance += storedValues[5];
posdistance += storedValues[5];
TCCR0B = 0b00000101; // Start timer
}
}
void TransmitComm()
{
if(start_request == 1)
{
start_request = 0;
PORTB &= 0b11110111; // ss1 low
bussdelay();
for(int i = 0; i < 11; i ++)
{
dummy = SPDR; // Dummy läsning för att cleara SPIF
MasterTransmit(storedValues[i]);
bussdelay();
}
PORTB ^= 0b00001000; // ss1 low
TCCR0B = 0b00000101; // Start timer
}
}
void transmit()
{
if(start_request == 1)
{
TransmitSensor(0);
TransmitComm();
TCCR0B = 0b00000101; // Start timer
}
}
void initiate_request_timer()
{
TIMSK0 = 0b00000100; //Enable interupt vid matchning med OCR1B TCCR1B =0x00;
TCNT0 = 0x00;
TCCR0B = 0x03; //Starta räknare, presscale 64.
OCR0BH = 0x01;
OCR0BL = 0x00; //RANDOM! När ska requesten triggas?
}
int main(void)
{
MasterInit();
while(1)
{
if(start_request == 1)
{
start_request = 0;
if(regulateright)
TransmitSensor(right);
else if(regulateleft)
TransmitSensor(left);
else if(regulateturn)
TransmitSensor(turn);
else
TransmitSensor(0x00);
}
TransmitComm(remoteControl);
}
return 0;
}
/*
* Recievecomm.c
*
* Created: 4/7/2014 5:08:19 PM
* Author: robsv107
*/
ISR(TIMER0_COMPB_vect)
{
TCNT0 = 0x00;
start_request = 1;
}
#include <avr/io.h>
#include <avr/interrupt.h>
/***********************SLAVE komm*****************************/
typedef int bool;
enum {false, true};
//Lables for transmition
char front = 0b00000001;
char rightfront = 0b00000010;
char rightback = 0b00000011;
char leftfront = 0b00000100;
char leftback = 0b00000101;
char traveldist = 0b00000110;
char gyro = 0b00000111;
char RFID = 0b00001000;
char direction = 0b00001001;
char rightspeed = 0b00001010;
char leftspeed = 0b00001011;
char stop = 0x00; //Stopbyte
char dummy;
bool remoteControl = false; // Change to Port connected to switch
unsigned char storedValues[11];
int index = 0; // index for recieved storedValue from buss
void SlaveInit(void)
{
/* Set MISO output, all others input */
DDRB = (1<<DDB6);
/* Enable SPI */
SPCR = (1<<SPE)|(1<<SPIE)|(1<<CPHA)|(1<<CPOL);
/* Enable external interrupts */
sei();
}
char SlaveRecieve(void)
{
/*Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return Data Register */
return SPDR;
}
/*******************************MAIN*******************************/
int main(void)
{
SlaveInit();
while(1)
{
if(remoteControl)
{
}
}
}
/*******************************INTERRUPTS*************************/
ISR(SPI_STC_vect) // Answer to call from Master
{
cli();
storedValues[indexvalue] = SPDR;
indexvalue ++;
if(indexvalue == 11)
indexvalue=0;
SPDR = 0;
sei();
}
/*
* Recieve.c
*
* Created: 4/7/2014 1:49:30 PM
* Author: robsv107
*/
#include <avr/io.h>
#include <avr/interrupt.h>
/***********************SLAVE Sensor*****************************/
typedef int bool;
enum {false, true};
//Start bytes for transmition
char front = 0b00000001;
char rightfront = 0b00000010;
char rightback = 0b00000011;
char leftfront = 0b00000100;
char leftback = 0b00000101;
char traveldist = 0b00000110;
char gyro = 0b00000111;
char RFID = 0b00001000;
char stop = 0x00; //Stopbyte
char selection;
unsigned char sortedValues[5] = {0b11001100, 0b00110011, 0b11100011
,0b00011100, 0b11000111};
unsigned char Distance = 0b00001100;
unsigned char sendGyro = 0b10101010;
bool isRFID = true;
void SlaveInit(void)
{
/* Set MISO output, all others input */
DDRB = (1<<DDB6);
/* Enable Global Interrupt */
sei();
/* Enable SPI and interrupts */
SPCR = (1<<SPE)|(1<<SPIE)|(1<<CPHA)|(1<<CPOL);
}
char SlaveRecieve(void)
{
/*Wait for reception complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return Data Register */
return SPDR;
}
/*************************INTERRUPTS********************/
ISR(SPI_STC_vector)
{
char selection = SPDR;
if(selection == front)
{
SPDR = sortedValues[0];
}
else if (selection == rightfront)
{
SPDR = sortedValues[1];
}
else if (selection == rightback)
{
SPDR = sortedValues[2];
}
else if (selection == leftfront)
{
SPDR = sortedValues[3];
}
else if (selection == leftback)
{
SPDR = sortedValues[4];
}
else if (selection == traveldist)
{
SPDR = Distance;
//Distance = 0;
}
else if (selection == gyro)
{
SPDR = sendGyro;
//sendGyro = 0;
}
else if (selection == RFID)
{
SPDR = isRFID;
}
else if (selection == stop)
{
// behöver förmodligen inte göra något här
}
}
/****************************MAIN**********************/
int main(void)
{
SlaveInit();
while(1)
{
}
}