-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCarLock.c
108 lines (88 loc) · 1.58 KB
/
CarLock.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
// Car Lock for PIC16F84A
// With EXT OSC 4Mhz
static int time = 0;
static int x = 0;
static int locked = 0;
static int unlocked = 0;
const int interval = 20;
const float inTimeSec = 0.65;
const int inTime = inTimeSec * 1000;
const float outTimeSec = 3.7;
const int outTime = outTimeSec * 1000;
const int inTimeCheck = inTime / interval;
const int outTimeCheck = outTime / interval;
int current = 0;
int enabled = 0;
int pressed = 0;
void lock()
{
x = 0;
PORTB.B5 = 1;
locked = 1;
PORTB.B6 = 0;
unlocked = 0;
enabled = 0;
current = 1;
// pressed = 1;
}
void unlock()
{
x = 0;
PORTB.B6 = 1;
unlocked = 1;
PORTB.B5 = 0;
locked = 0;
enabled = 0;
current = 2;
// pressed = 1;
}
void main()
{
TRISB = 0b00000110;
do
{
if (PORTB.B1 == 1 && PORTB.B2 == 1 && enabled == 1 && current != 1) // Check Lock
{
time++;
if (time >= inTimeCheck)
{
time = 0;
lock();
}
}
else if (PORTB.B1 == 0 && PORTB.B2 == 0 && enabled == 1 && current != 2) // Check Unlock
{
time++;
if (time >= inTimeCheck)
{
time = 0;
unlock();
}
}
else if (locked == 1 || unlocked == 1)
{
x++;
if (x >= outTimeCheck)
{
locked = 0;
unlocked = 0;
PORTB = 0x00;
x = 0;
}
}
else if (PORTB.B1 == 0 && PORTB.B2 == 1)
{
pressed = 0;
current = 0;
time = 0;
PORTB = 0x00;
}
// else
// {
// PORTB = 0x00;
// time = 0;
// }
Delay_ms(interval);
enabled = 1;
} while (1);
}