-
Notifications
You must be signed in to change notification settings - Fork 0
/
BLUETOOTH BASED ROBOT CODE.txt
260 lines (203 loc) · 7.96 KB
/
BLUETOOTH BASED ROBOT CODE.txt
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
// PIC18F4520 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)
// CONFIG4L
#pragma config STVREN = OFF // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)
#include <stdio.h>
#include <stdlib.h>
#include <plib/usart.h>
#define rs LATEbits.LE0
#define rw LATEbits.LE1
#define en LATEbits.LE2
//LCD Data pins
#define lcdport LATD
#define _XTAL_FREQ 20000000
void lcd_init();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void my_delay(int mtime);
void my_delay(int mtime)
{
for(int i = 0; i<= mtime ; i++)
__delay_ms(1);
}
LCD_string(unsigned char *string)
{
while(*string)
lcddata(*string++);
}
void main(void) {
TRISB=0;
PORTBbits.RB1=1;
PORTBbits.RB2=1;
OSCCON=0x76; //Configure OSCON Register to use
//internal oscillator. Please check datasheet
TRISD=0;
LATD=0;
TRISEbits.RE0=0;
TRISEbits.RE1=0;
TRISEbits.RE2=0;
lcd_init();
LCD_string("Project Name") ;
lcdcmd(0x01);
LCD_string("Bluetooth Based");
lcdcmd(0xc0);
LCD_string("Android Robot");
my_delay(1000);
lcdcmd(0x01);
lcdcmd(0x80);
LCD_string("Project Members") ;
my_delay(500);
lcdcmd(0x01);
lcdcmd(0x80);
LCD_string("Sumit Rathi") ;
my_delay(500);
lcdcmd(0x01);
lcdcmd(0x80);
LCD_string("Shivam Pande") ;
my_delay(500);
lcdcmd(0x01);
lcdcmd(0x80);
LCD_string("Prathamesh") ;
my_delay(500);
lcdcmd(0x01);
lcdcmd(0x80);
char data;
unsigned char Txdata1[] = "Android Based Bluetooth Robot\r\n";
for (int x=0; x<=10; x++) my_delay(50); //Generate 500ms delay
unsigned char Txdata2[] = "Press F for Forward, L for Left,R for Right,B for Back and S for Stop \r\n";
// configure USART frequency is 8MHz, using high speed, asynchronous mode is used with 9600 baud and 8 data bits.
//Fosc / (16 * (spbrg + 1 ))
// spbrg = FOSC/(16 * baud) - 1
// = 8000000/(16 × 9600) - 1 = 52
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH &
USART_ADDEN_OFF,
129);
while(BusyUSART()); //Check if Usart is busy or not
putsUSART(Txdata1); //transmit the string1
for (int x=0; x<=20; x++) my_delay(50); //Generate 500ms delay
while(BusyUSART()); //Check if Usart is busy or not
putsUSART(Txdata2); //transmit the string2
my_delay(200);
while(1){
data = ReadUSART();
switch(data){
case '1':
LATBbits.LB4 = 1;
LATBbits.LB5 = 0;
LATBbits.LB6 = 1;
LATBbits.LB7 = 0;
lcdcmd(0x01);
LCD_string("FORWARD") ;
lcdcmd(0x80);
break;
case '2':LATBbits.LB4 = 1;
LATBbits.LB5 = 0;
LATBbits.LB6 = 0;
LATBbits.LB7 = 0;
lcdcmd(0x01);
LCD_string("RIGHT");
lcdcmd(0x80);
break;
case '3':LATBbits.LB4 = 0;
LATBbits.LB5 = 0;
LATBbits.LB6 = 1;
LATBbits.LB7 = 0;
lcdcmd(0x01);
LCD_string("LEFT") ;
lcdcmd(0x80);
break;
case '4':LATBbits.LB4 = 0;
LATBbits.LB5 = 1;
LATBbits.LB6 = 0;
LATBbits.LB7 = 1;
lcdcmd(0x01);
LCD_string("REVERSE") ;
lcdcmd(0x80);
break;
default:LATBbits.LB4 = 0;
LATBbits.LB5 = 0;
LATBbits.LB6 = 0;
LATBbits.LB7 = 0;
lcdcmd(0x01);
LCD_string("STOP") ;
lcdcmd(0x80);
break;
}
}
}
void lcd_init()
{
lcdcmd(0x38);
lcdcmd (0x0C);
lcdcmd(0x01);
lcdcmd(0x06);
lcdcmd(0x80);
}
void lcddata(unsigned char dataout)
{
lcdport=dataout;
rs=1;
rw=0;
en=1;
my_delay(10);
en=0;
}
void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout;
rs=0;
rw=0;
en=1;
my_delay(10);
en=0;
}