-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·334 lines (308 loc) · 6.98 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
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
/* Main.c file generated by New Project wizard
* Author : Ajit Jadhav
* Created: Thu Mar 15 2016
* Processor: PIC18F46K22
* Compiler: MPLAB C18
*/
/*
|--------------------------------------|
| Peripheral - Function - PIC Pin |
|--------------------------------------|
| Ultrasonic - Trig - RA4 |
| - ECHO - RB0 |
|--------------------------------------|
| PIR - OUT - AN1 |
|--------------------------------------|
| LDR - OUT - AN0 |
|--------------------------------------|
| LED* - INPUT - DACOUT |
|--------------------------------------|
| LCD - RS - RE0 |
| - RW - RE1 |
| - E - RE2 |
| - D4 - RD0 |
| - D5 - RD1 |
| - D6 - RD2 |
| - D7 - RD3 |
|--------------------------------------|
| Indicator - INPUT - RB5 |
| LED |
|--------------------------------------|
*LED (Power LED) is interfaced to PIC using mosfet.
DACOUT pin is connected to mosfet gate.
*/
#include <p18f46k22.h>
#include <delays.h>
#include <stdio.h>
#include "config.h"
#include "lcd.h"
// Ultrasonic Module pin definations
#define US_TRIG_DIR TRISAbits.TRISA4
#define US_TRIG LATAbits.LATA4
#define US_ECHO_DIR TRISBbits.TRISB0
#define US_ECHO PORTBbits.RB0
// LED Pin
#define LED_DIR TRISBbits.TRISB5
#define LED LATBbits.LATB5
// LCD Line Buffers
char line1[16];
char line2[16];
unsigned int t;
// Results
unsigned int regL,regH,resADC,resPIR,resUS,resLDR,tmr_2 = 0;
// Timer
int sec=0,mn=0,hr=0;
// Flags
short f1,f2,f3;
void lcdinit();
void lcdstring(unsigned char *);
// Timer 2 Interrupt
#pragma interrupt chk_isr
void chk_isr(void)
{
if(PIR1bits.TMR2IF == 1)
{
tmr_2++;
PIR1bits.TMR2IF = 0;
if(tmr_2 >= 250)
{
sec++;
tmr_2 = 0;
}
}
}
#pragma code My_Prio_Int=0x0008
void My_Prio_Int(void)
{
_asm
goto chk_isr
_endasm
}
#pragma code
// Millisecond Delay for 16MHz Crystal
void msDelay(unsigned int i)
{
for(; i>0; i--)
Delay1KTCYx(16);
}
// Microsecond Delay
void usDelay(unsigned int i)
{
for(; i>0; i--)
{
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
}
// ADC Initialization with Channel
void initADC(unsigned char ch)
{
ADCON0 = 0x00;
if(ch < 28)
ADCON0 |= (ch << 2);
ADCON2 = 0xAD;
ADCON0bits.ADON = 1;
}
// ADC Conversion
void readADC(void)
{
msDelay(1);
ADCON0bits.GO_DONE = 1;
msDelay(1);
while(ADCON0bits.GO_DONE);
regL = ADRESL;
regH = ADRESH;
resADC = ((int) (regH<<8)|regL);
}
// Initializing Ultrasonic with Timer 0 16bit mode and
// Weak-Pullup resistor enabled on RB0
void initUltrasonic(void)
{
T0CON = 0x01;
US_ECHO_DIR = 1;
US_TRIG_DIR = 0;
US_ECHO = 1;
WPUBbits.WPUB0 = 1;
}
// Reading distance from US module in cm. using timer0
void readUltrasonic(void)
{
TMR0H = 0;
TMR0L = 0;
US_TRIG = 1;
Delay10TCYx(16); // 10 uSec Delay for Trig pulse
US_TRIG = 0;
while(!US_ECHO);
T0CONbits.TMR0ON = 1;
while(US_ECHO);
T0CONbits.TMR0ON = 0;
regL = TMR0L;
regH = TMR0H;
resUS = (regH << 8) | regL;
resUS = (int) (((resUS*0.00000025)*34000)/2);
}
// Reading PIR status using ADC on channel AN1
void readPIR(void)
{
initADC(1);
readADC();
resPIR = resADC;
if(resPIR > 500)
resPIR = 1;
else
resPIR = 0;
}
// LED indicator
void eventL(void)
{
LED = 1;
Delay10KTCYx(10);
LED = 0;
Delay10KTCYx(10);
LED = 1;
Delay10KTCYx(10);
LED = 0;
Delay10KTCYx(10);
}
// LED indication for PIR true
void pirIndicate(void)
{
LED = 1;
Delay10KTCYx(20);
LED = 0;
Delay10KTCYx(20);
}
// Reading LDR on ADC channel AN0
void readLDR(void)
{
initADC(0);
readADC();
resLDR = (int) resADC/100;
}
// Setting Brightness of LED on DACOUT pin using 5bit DAC
void setBrightness(void)
{
readLDR();
if(resLDR <= 9 && resLDR > 8)
VREFCON2 = 23;
if(resLDR <= 8 && resLDR > 7)
VREFCON2 = 23;
if(resLDR <= 7 && resLDR > 6)
VREFCON2 = 21;
if(resLDR <= 6 && resLDR > 5)
VREFCON2 = 20;
if(resLDR <= 5 && resLDR > 4)
VREFCON2 = 19;
if(resLDR <= 4 && resLDR > 3)
VREFCON2 = 18;
if(resLDR <= 3 && resLDR > 2)
VREFCON2 = 17;
}
// Initial brightness control testing
void initBrightness()
{
for(t=17;t<24;t++)
{
VREFCON2 = t;
msDelay(1200);
}
for(t=24;t>16;t--)
{
VREFCON2 = t;
msDelay(1200);
}
}
// Main routine
void main(void) {
OSCCON = 0x7C; // Internal Oscillator 16MHz Enabled
OSCTUNE = 0x5F;
OSCCON2bits.PLLRDY = 1;
VREFCON1 = 0xE0;
T2CON = 0x7B;
INTCONbits.GIE = 1; // Global, peripheral & timer 2 interrupt enable
INTCONbits.PEIE = 1;
PIE1bits.TMR2IE = 1;
PR2 = 0xff; // period registor 2 with max value
VREFCON2 = 0; // zero brightness
LED_DIR = 0; // LED pin on output
LED = 1;
lcdinit(); // Initialize LCD in 4bit mode, ultrasonic and brightness
lcdcmd(0x01);
initUltrasonic();
initBrightness();
LED = 0;
while(1)
{
readPIR();
if(resPIR)
{
pirIndicate();
readUltrasonic();
f1 = 0;
f2 = 1;
while(resUS < 100 && resUS > 10)
{
T2CONbits.TMR2ON = 1;
setBrightness();
if(f2)
{
lcdcmd(0x80);
sprintf(line1," Welcome ! ");
lcdstring(line1);
sprintf(line2," Chaitanya. ");
lcdcmd(0xc0);
lcdstring(line2);
msDelay(1000);
f2 = 0;
}
if(sec >= 60)
{
mn++;
sec = 0;
}
if(mn >= 60)
{
hr++;
mn = 0;
}
sprintf(line1," Study Time ");
sprintf(line2," %i:%i:%i ",hr,mn,sec);
lcdcmd(0x80);
lcdstring(line1);
lcdcmd(0xc0);
lcdstring(line2);
eventL();
eventL();
readUltrasonic();
f1 = 1;
}
T2CONbits.TMR2ON = 0;
if(f1)
{
sprintf(line1," Thank You ! ");
sprintf(line2," Good Bye...! ");
lcdcmd(0x80);
lcdstring(line1);
lcdcmd(0xc0);
lcdstring(line2);
msDelay(3000);
VREFCON2 = 0;
}
lcdcmd(0x01);
}
}
}