-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
108 lines (100 loc) · 2.2 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <xc.h> //XC compiler header
#pragma config LVP = OFF
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#define _XTAL_FREQ 2000000 //Crystal Oscillator Frequency
#define IRR RC4
#define PB RC5
#define IRE RB0
#define Alarm RB1
#define LED1 RB2
#define LED2 RB3
#define Servo RB5
#define Green RB6
#define Red RB7
int THRESHOLD = 3;
int mode = 1;
int count = 0;
void servoRotate90() //90 Degree - reset motion
{
Alarm = 1;
unsigned int i;
for(i=0;i<50;i++)
{
Servo = 1;
__delay_us(1370); //1200
Servo = 0;
__delay_us(18800); //18800
}
Alarm = 1;
}
void servoRotate180() //180 Degree - downward motion
{
unsigned int i;
for(i=0;i<50;i++)
{
Servo = 1;
__delay_us(4000); //2000
Servo = 0;
__delay_us(16000);
}
}
void main(void){
TRISB = 0x00;
TRISC = 0xFF;
TRISE = 0;
TRISC5 = 1;
PORTB = 0x00;
IRE = 1;
Green = 1;
while(1){
if (PB == 0){
mode++;
if (mode == 3){
mode = 1;
}
while (PB == 0){}
}
if (mode == 1){
LED1 = 1;
LED2 = 0;
count = 0;
if (IRR == 0){
while(IRR == 0){}
Red = 1;
Green = 0;
//__delay_ms(1000);
servoRotate90();
__delay_ms(250);
servoRotate180();
__delay_ms(1000);
Red = 0;
Green = 1;
__delay_ms(1500);
}
}
if (mode == 2){
LED2 = 1;
LED1 = 0;
if (IRR == 0){
while (IRR == 0){}
count++;
if (count >= THRESHOLD){
Red = 1;
Green = 0;
// __delay_ms(1000);
servoRotate90();
__delay_ms(250);
servoRotate180();
__delay_ms(1000);
Red = 0;
Green = 1;
count = 0;
}
__delay_ms(1500);
}
}
}
return;
}