-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAUTOVAC final code.c
458 lines (374 loc) · 11.6 KB
/
AUTOVAC final 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
#include "TM4C123.h" // Device header
#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/pwm.h"
uint32_t d1 ,d2,d3;
uint32_t duty = 250; //1.5ms pulse width
void delay_Microsecond(uint32_t time);
void Timer0A_init(void);
uint32_t Timer0A_Handler(void);
void Timer0B_init(void);
uint32_t Timer0B_Handler(void);
void Timer2A_Handler(void);
void IntGlobalEnable(void);
void Timer2A_init(void);
void Timer2B_init(void);
uint32_t Timer2B_Handler(void);
void delayMs(int n);
uint32_t d1 = 0;
uint32_t d2 = 0;
uint32_t d3 = 0;
int pw = 0;
int pw1 =0;
const double _16MHz_1clock = 62.5e-9; /*Value of 1clock cycle in nanoseconds*/
const uint32_t MULTIPLIER = 5882; /*Derived from speed of sound*/
#define ECHO 0x40 //PB6
#define ECHO1 0x80//PB7
#define ECHO2 0x02 // PB0
#define TRIGGER 0x10 //PA4(OUTPUT)
#define TRIGGER1 0x40 //PA 6
#define TRIGGER2 0x08 // PA3
#define BLUE_LED 0x04//PF2 onboard Blue LED
#define RED_LED 0x02 //PF1 onboard RED LED
#define GREEN_LED 0x08//PF3
uint32_t highEdge2,lowEdge2;
uint32_t highEdge,lowEdge;
uint32_t highEdge1,lowEdge1;
uint32_t ddistance;
uint32_t ddistance1;/*Distance in centimeters*/
uint32_t ddistance2;
uint32_t counter =0;
uint32_t Timer0A_Handler(void)
{
GPIOA->DATA &=~TRIGGER;
delay_Microsecond(12);
GPIOA->DATA |=ECHO|TRIGGER;
delay_Microsecond(12);
GPIOA->DATA &=~TRIGGER;
TIMER0->ICR =4;
while((TIMER0->RIS & 4)==0){}; //Wait till captured
highEdge = TIMER0->TAR;
TIMER0->ICR =4; //clear timer capture flag
while((TIMER0->RIS & 4) ==0){};
lowEdge = TIMER0->TAR;
ddistance = lowEdge -highEdge;
ddistance = _16MHz_1clock *(double) MULTIPLIER *(double)ddistance;
return ddistance;
}
uint32_t Timer0B_Handler(void) {
GPIOA->DATA &=~TRIGGER1;
delay_Microsecond(12);
GPIOA->DATA |= ECHO1|TRIGGER1;
delay_Microsecond(12);
GPIOA->DATA &=~TRIGGER1;
TIMER0->ICR |=0x400;//clears the timer before read
while((TIMER0->RIS & 0x400)==0){}; //Wait till captured
highEdge1 = TIMER0->TBR;//reads the value from timer
TIMER0->ICR |=0x400; //clear timer capture flag
while((TIMER0->RIS & 0x400) ==0){};
lowEdge1 = TIMER0->TBR;
ddistance1 = lowEdge1 -highEdge1;
ddistance1 = _16MHz_1clock *(double) MULTIPLIER *(double)ddistance1;
/*
DISTANCE THRESHOLD
Trigger interrupt;...int0.start();
*/
return ddistance1;
}
uint32_t Timer2B_Handler(void) {
GPIOA->DATA &=~TRIGGER2;
delay_Microsecond(12);
GPIOA->DATA |= ECHO2|TRIGGER2;
delay_Microsecond(12);
GPIOA->DATA &=~TRIGGER2;
TIMER2->ICR |=0x400;//clears the timer before read
while((TIMER2->RIS & 0x400)==0){}; //Wait till captured
highEdge2 = TIMER2->TBR;//reads the value from timer
TIMER2->ICR |=0x400; //clear timer capture flag
while((TIMER2->RIS & 0x400) ==0){};
lowEdge2 = TIMER2->TBR;
ddistance2 = lowEdge2 -highEdge2;
ddistance2 = _16MHz_1clock *(double) MULTIPLIER *(double)ddistance2;
/*
DISTANCE THRESHOLD
Trigger interrupt;...int0.start();
*/
return ddistance2;
}
void Timer2A_Handler(void)
{
TIMER2_ICR_R|= 0x001;
d1 = ddistance;
d2 = ddistance1;
if(d1 < 15&& d2 < 15)
{
GPIOF->DATA |=BLUE_LED;
GPIOF->DATA |=RED_LED;
//GPIOD->DIR |= 0x0F;
GPIOD->DATA &=~ 0x04;
GPIOE->DATA &=~ 0x04;
GPIOD->DATA |= 0x08;
GPIOE->DATA |= 0x08;
for (pw1 = 100; pw1 <3999; pw1 += 600)
{
PWM1->_2_CMPB = pw1;
delayMs(50);
}
for (pw = 100; pw < 3999; pw += 600)
{
PWM1->_3_CMPB = pw;
delayMs(50);
}
GPIOD->DATA |= 0x04;
GPIOD->DATA &=~ 0x08;
GPIOE->DATA &=~ 0x0C;
for (pw = 100; pw < 3999; pw += 150)
{
PWM1->_3_CMPB = pw;
delayMs(50);
}
GPIOD->DATA &=~ 0x04;
}
else
{
GPIOF->DATA &=~BLUE_LED;
GPIOF->DATA &=~RED_LED;
}
d1 = Timer0A_Handler();
if(d1 < 15)
{
GPIOF->DATA |=BLUE_LED;
//GPIOD->DIR |= 0x0F;
GPIOD->DATA |= 0x04;
GPIOD->DATA &=~ 0x08;
GPIOE->DATA &=~ 0x0C;
for (pw = 100; pw < 3999; pw += 275)
{
PWM1->_3_CMPB = pw;
delayMs(50);
}
GPIOD->DATA &=~ 0x04;
}
else
{
GPIOF->DATA &=~BLUE_LED;
}
d2 = Timer0B_Handler();
if(d2 < 15)
{
GPIOF->DATA |=RED_LED;
//GPIOD->DIR |= 0x0F;
GPIOE->DATA |= 0x04;
GPIOD->DATA &=~ 0x0C;
GPIOE->DATA &=~ 0x08;
for (pw1 = 100; pw1 <3999; pw1 += 275)
{
PWM1->_2_CMPA = pw1;
delayMs(50);
}
GPIOE->DATA |= 0x04;
}
else
GPIOF->DATA &=~RED_LED;
//GPIOD->DATA |= 0x8;
//GPIOE->DATA |= 0x8;
// GPIOD->DATA |= 0x44;
d3 = Timer2B_Handler();
if(d3 > 10)
{
// GPIOF->DATA |=GREEN_LED;
//GPIOD->DIR |= 0x0F;
GPIOD->DATA &=~ 0x04;
GPIOE->DATA &=~ 0x04;
GPIOD->DATA |= 0x08;
GPIOE->DATA |= 0x08;
for (pw1 = 100; pw1 <3999; pw1 += 600)
{
PWM1->_2_CMPB = pw1;
delayMs(50);
}
for (pw = 100; pw < 3999; pw += 600)
{
PWM1->_3_CMPB = pw;
delayMs(50);
}
GPIOD->DATA |= 0x04;
GPIOD->DATA &=~ 0x08;
GPIOE->DATA &=~ 0x0C;
for (pw = 100; pw < 3999; pw += 150)
{
PWM1->_3_CMPB = pw;
delayMs(50);
}
// GPIOD->DATA &=~ 0x04;
GPIOD->DATA |= 0x04;
GPIOE->DATA |= 0x04;
}
else
{
// GPIOD->DATA |= 0x04;
//GPIOE->DATA |= 0x04;
}
GPIOD->DATA |= 0x04;
GPIOE->DATA |= 0x04;
}
void IntGlobalEnable(void)
{
__asm(" cpsie i\n");
}
int main(void){
//SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
SYSCTL->RCGCPWM |= 0x02; // enable clock to PWM1
SYSCTL->RCGCGPIO |= 0x20; // enable clock to GPIOF
SYSCTL->RCGCGPIO |= 0x10; // enable clock to GPIOE
SYSCTL->RCGCGPIO |= 0x08; // enable clock to GPIOd
GPIO_PORTF_LOCK_R = 0x4C4F434B;
GPIO_PORTF_CR_R |= 0x01;
SYSCTL->RCC &= ~0x00100000; // use system clock for PWM
PWM1->_3_CTL = 0; // disable PWM1_3 during configuration
PWM1->_3_GENB = 0x0000080C; // output high when load and low when match
PWM1->_3_LOAD = 3999; // 4 KHz
PWM1->_3_CTL = 0x08; // enable PWM1_3
PWM1->INVERT |= 0x80; // positive pulse
PWM1->INVERT |= 0x10;
PWM1->ENABLE |= 0x80; // enable PWM1
PWM1->_2_CTL = 0; // disable PWM1_3 during configuration
PWM1->_2_GENA = 0x0000080C; // output high when load and low when match
PWM1->_2_LOAD = 3999; // 4 KHz
PWM1->_2_CTL = 0x04; // enable PWM1_2
PWM1->ENABLE |= 0x10; // enable PWM1
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_4,duty);
PWMPulseWidthSet(PWM1_BASE, PWM_OUT_7,duty);
GPIO_PORTF_LOCK_R = 0x4C4F434B;
GPIO_PORTF_CR_R |= 0x01;
GPIOF->DIR |= 0x09; // set PORTF 3 pins as output (LED) pin
GPIOF->DEN |= 0x09; // set PORTF 3 pins as digital pins
GPIOF->AFSEL |= 0x09; // enable alternate function
GPIOF->PCTL &= ~0x000F00F; // clear PORTF 3 alternate function
GPIOF->PCTL |= 0x0005005; // set PORTF 3 alternate funtion to PWM
//GPIOE->DIR |= 0x10; // set PORTF 3 pins as output (LED) pin
//GPIOE->DEN |= 0x10; // set PORTF 3 pins as digital pins
//GPIOE->AFSEL |= 0x10; // enable alternate function
// GPIOE->PCTL &= ~0xF000F; // clear PORTF 3 alternate function
//GPIOE->PCTL |= 0x50005; // set PORTF 3 alternate funtion to PWM
GPIOD->DIR |= 0x0C;
GPIOD->DEN |= 0x0C; // PORTB 3 as digital pins
// set PORTB 3 as output
GPIOD->DATA |= 0x04; // enable PORTB 3
GPIOE->DIR |= 0x0C;
GPIOE->DEN |= 0x0C; // PORTB 3 as digital pins
// set PORTB 3 as output
GPIOE->DATA |= 0x04; // enable PORTB 3
SYSCTL->RCGCGPIO |=(1U<<0); //Enable clock for PORTA
// SYSCTL->RCGCGPIO |=(1U<<5); //Enable clock for PORTF
GPIOA->DIR |=TRIGGER;
GPIOA->DIR |=TRIGGER1;
GPIOA->DIR |=TRIGGER2;
GPIOF->DIR |=BLUE_LED;
GPIOF->DIR |=RED_LED;
GPIOA->DEN |=(ECHO)|(TRIGGER);
GPIOA->DEN |= (ECHO1)|(TRIGGER1);
GPIOA->DEN |= (ECHO2)|(TRIGGER2);
GPIOF->DEN |= BLUE_LED;
GPIOF->DEN |=RED_LED;
GPIOF->DIR |=GREEN_LED;
Timer0A_init();
Timer0B_init();
Timer0A_Handler();
Timer0B_Handler();
Timer2A_init();
Timer2B_init();
IntGlobalEnable();
while(1)
{
GPIOD->DATA |= 0x04;
GPIOE->DATA |= 0x04;
}
}
void delay_Microsecond(uint32_t time)
{
int i;
SYSCTL->RCGCTIMER |=(1U<<1);
TIMER1->CTL=0;
TIMER1->CFG=0x04;
TIMER1->TAMR=0x02;
TIMER1->TAILR= 16-1;
TIMER1->ICR =0x1;
TIMER1->CTL |=0x01;
for(i=0;i<time;i++){
while((TIMER1->RIS & 0x1)==0)
TIMER1->ICR = 0x1;
}
}
void Timer0A_init(void)
{
SYSCTL->RCGCTIMER |=(1U<<0);
SYSCTL->RCGCGPIO |=(1U<<1); //PortB
GPIOB->DIR &=~ECHO;
GPIOB->DEN |=ECHO;
GPIOB->AFSEL |=ECHO;
GPIOB->PCTL &=~0x0F000000;
GPIOB->PCTL |= 0x07000000;
NVIC_PRI4_R &= ~0xE0000000;
TIMER0->CTL &=~0x0001; // TAen is disabled
TIMER0->CFG =4; //16 bit timer
TIMER0->TAMR = 0x17; // Timer count up TACDIR Edge time mode TACMR CAPTURE MODE TAMR
TIMER0->CTL |=0x00C; // bOTH EDGES ARE ENABLE
TIMER0->CTL |=0x0001; // TAen enable to configure CFG
}
void Timer0B_init(void)
{
// SYSCTL->RCGCTIMER |=(1U<<0);
// SYSCTL->RCGCGPIO |=(1U<<1); //PortB
GPIOB->DIR &=~ECHO1;
GPIOB->DEN |=ECHO1;
GPIOB->AFSEL |=ECHO1;
GPIOB->PCTL &=~0xF0000000;
GPIOB->PCTL |= 0x70000000;
NVIC_PRI5_R |=0x00000020;
TIMER0->CTL &=~0x100; // Tben is disabled
TIMER0->CFG =4; //16 bit timer
TIMER0->TBMR = 0x17; // Timer count up TACDIR Edge time mode TACMR CAPTURE MODE TBMR
TIMER0->CTL |=0xC00; // bOTH EDGES ARE ENABLE
TIMER0->CTL |=0x0100; // TBen enable to configure CFG
}
void Timer2A_init(void)
{
SYSCTL->RCGCTIMER |=(1U<<2);
// NVIC_PRI4_R &= ~0xE0000000;
TIMER2->CTL &=~0x001; // TAen is disabed
TIMER2->CFG =4; //16 bit timer
TIMER2->TAMR = 0x02; // Timer PERIODIC
TIMER2_TAILR_R = 7999999;
NVIC_PRI5_R |=0x20000000;
NVIC_EN0_R |= 0x00800000; // Enabling Timer2A
TIMER2_IMR_R|= 0x001; //arming Timer2A
// TIMER0->CTL |=0xC00; // bOTH EDGES ARE ENABLE
TIMER2->CTL |=0x0001; // TAen enable to configure CFG
}
void Timer2B_init(void)
{
// SYSCTL->RCGCTIMER |=(1U<<0);
// SYSCTL->RCGCGPIO |=(1U<<1); //PortB
GPIOB->DIR &=~ECHO2;
GPIOB->DEN |=ECHO2;
GPIOB->AFSEL |=ECHO2;
GPIOB->PCTL &=~0x00000F0;
GPIOB->PCTL |= 0x0000070;
//NVIC_PRI6_R |=0x0000060;
TIMER2->CTL &=~0x100; // Tben is disabled
TIMER2->CFG =4; //16 bit timer
TIMER2->TBMR = 0x17; // Timer count up TACDIR Edge time mode TACMR CAPTURE MODE TBMR
TIMER2->CTL |=0xC00; // bOTH EDGES ARE ENABLE
TIMER2->CTL |=0x0100; // TBen enable to configure CFG
}
void delayMs(int n)
{
int i, j;
for(i = 0 ; i < n; i++)
for(j = 0; j < 3180; j++)
{} // do nothing for 1 ms
}