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
| //------------------------------------------------------------
//------------------------------------------------------------
// LCD module connections
sbit LCD_RS at RA0_bit; // PIN 22
sbit LCD_EN at RA1_bit; // PIN 23
sbit LCD_D4 at RA2_bit; // PIN 24
sbit LCD_D5 at RA3_bit; // PIN 25
sbit LCD_D6 at RA4_bit; // PIN 26
sbit LCD_D7 at RA5_bit; // PIN 27
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISA2_bit;
sbit LCD_D5_Direction at TRISA3_bit;
sbit LCD_D6_Direction at TRISA4_bit;
sbit LCD_D7_Direction at TRISA5_bit;
// H Bridge..
sbit enable1 at RC4_bit; //pin 23
sbit enable2 at RC5_bit; //pin 24
sbit input1 at RC6_bit; //pin 25
sbit input2 at RC7_bit; //pin 26
sbit input3 at Rd4_bit; //pin 27
sbit input4 at Rd5_bit; //pin 28
sbit enable1_dir at TRISC4_bit;
sbit enable2_dir at TRISC5_bit;
sbit input1_dir at TRISC6_bit;
sbit input2_dir at TRISC7_bit;
sbit input3_dir at TRISd4_bit;
sbit input4_dir at TRISd5_bit;
// End LCD module connections
unsigned const delay=50;
unsigned const scan_delay=5;
unsigned const press_delay=5;
void initialise();
void Test_Function();
void hexkeypad_scan();
void motor_initialse();
void motor_forward();
void motor_fw_left();
void motor_fw_right();
void motor_chk1();
void motor_chk2();
void motor_chk3();
void motor_chk4();
void motor_chk5();
void motor_chk6();
void motor_chk7();
void motor_chk8();
void main()
{
initialise();
//////////////
portb=0;
trisb=0x03;
anselh=0;
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
while(1)
{
/*motor_forward();
while (portb.rb0==1)
{
lcd_out(1,1,"right");
motor_fw_right();
}
while (portb.rb1==1)
{
lcd_out(1,1,"left");
motor_fw_left();
}
Lcd_Cmd(_LCD_CLEAR); // Clear display*/
motor_chk1();
Delay_ms(2500);
motor_chk2();
Delay_ms(2500);
motor_chk3();
Delay_ms(2500);
motor_chk4();
Delay_ms(2500);
}
}
void hexkeypad_scan()
{
PORTC=0X08;
Delay_ms(scan_delay);
while(portd.rd2==1)
{
porte=0x01;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("1"); // Write text in first row
}
while(portd.rd3==1)
{
porte=0x04;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("4"); // Write text in first row
}
while(portd.rd1==1)
{
porte=0x07;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("7"); // Write text in first row
}
while(portd.rd0==1)
{
porte=0x00;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("*"); // Write text in first row
}
PORTC=0X01;
Delay_ms(scan_delay);
while(portd.rd2==1)
{
porte=0x02;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("2"); // Write text in first row
}
while(portd.rd3==1)
{
porte=0x05;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("5"); // Write text in first row
}
while(portd.rd1==1)
{
porte=0x08;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("8"); // Write text in first row
}
while(portd.rd0==1)
{
porte=0x00;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("0"); // Write text in first row
}
PORTC=0X02;
Delay_ms(scan_delay);
while(portd.rd2==1)
{
porte=0x03;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("3"); // Write text in first row
}
while(portd.rd3==1)
{
porte=0x06;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("6"); // Write text in first row
}
while(portd.rd1==1)
{
porte=0x09;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("9"); // Write text in first row
}
while(portd.rd0==1)
{
porte=0x00;
Delay_ms(Press_Delay);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_out_cp("#"); // Write text in first row
}
}
void initialise()
{
OSCCON=0X71;
Delay_ms(100);
PORTA=0;
PORTB=0;
PORTC=0;
PORTD=0;
PORTE=0;
TRISA=0;
TRISB=0;
TRISC=0X00; // CONTROLLING FIRST FOUR BITS
TRISD=0X0F; // CONTROLLING FIRST FOUR BITS
TRISE=0;
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
motor_initialse();
Lcd_Init(); // Initialise the lcd library
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,3,"SCANING..."); // Write text in first row
}
void motor_initialse()
{
enable1_dir=0;
enable2_dir=0;
input1_dir=0;
input2_dir=0;
input3_dir=0;
input4_dir=0;
}
void motor_forward()
{
enable1=0;
enable2=0;
input1=1;
input2=0;
input3=1;
input4=0;
}
void motor_fw_left()
{
enable1=0;
enable2=0;
input1=1;
input2=0;
input3=0;
input4=0;
}
void motor_fw_right()
{
enable1=0;
enable2=0;
input1=0;
input2=0;
input3=1;
input4=0;
}
void motor_chk1()
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
LCD_out(1,1,"Function name");
LCD_out(2,1,"Motor chk 1");
enable1=0;
input1=1;
input2=1;
enable2=0;
input3=1;
input4=1;
}
void motor_chk2()
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
LCD_out(1,1,"Function name");
LCD_out(2,1,"Motor chk 2");
enable1=0;
input1=1;
input2=0;
enable2=0;
input3=1;
input4=0;
}
void motor_chk3()
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
LCD_out(1,1,"Function name");
LCD_out(2,1,"Motor chk 3");
enable1=0;
input1=0;
input2=1;
enable2=0;
input3=0;
input4=1;
}
void motor_chk4()
{
Lcd_Cmd(_LCD_CLEAR); // Clear display
LCD_out(1,1,"Function name");
LCD_out(2,1,"Motor chk 4");
enable1=0;
input1=0;
input2=0;
enable2=0;
input3=0;
input4=0;
} |