Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

need help in coding of micro-controller

Status
Not open for further replies.

saba.asif

Newbie level 2
Newbie level 2
Joined
May 15, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
47
this is my coding in which i am comparing 2 strings. coding is for micro-controller. one string is already stored in code and second string is taken by user. i don't know why but it is not comparing the strings. please someone correct my mistake in the coding.


Code C - [expand]
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
#include<reg51.h>
#include<string.h>
#define all_on P2=0x0f            
#define all_off P2=0x00
#define dataport P0
#define cmdport P1
#define output P2
 
char y;int k;
sbit rs=cmdport^0;           
sbit rw=cmdport^1;
sbit en=cmdport^2;
sbit tv=output^0;
sbit fan=output^1;
sbit light=output^2;
sbit ac=output^3;
 
char compare1[4]="4211";         //  TV ON
char compare2[4]="4212";         //  FAN ON
char compare3[4]="4213";         //  LIGHT ON
char compare4[4]="4214";         //  AC ON
char compare5[4]="4215";         //  TV OFF
char compare6[4]="4216";         //  FAN OFF
char compare7[4]="4217";         //  LIGHT OFF
char compare8[4]="4218";         //  AC OFF 
char compare9[4]="4111";         //  ALL ON 
char compare0[4]="4119";     //  ALL OFF   
char input[4];               //input string      
 
void delay(int time)
{ 
 unsigned int i,j;
 for(i=0;i<time;i++)
 for(j=0;j<1275;j++);
 }    // delay funtion
 
void lcddata(char item)
{
 dataport=item;
 rs=1,rw=0,en=1;
 delay(5),en=0;
 }            // funtion to send data to lcd
 
void print(char cmd, char *str)
{
 dataport=cmd;
 rs=0,rw=0,en=1;
 delay(5),en=0;       // funtion to send cmd and print data to lcd
 while(*str!='\0')
 {
  lcddata(*str);
  str++;
 }
 }
 
void LCD_clear()
{
 delay(50);
 print(0x01,0);
}
                                          // funtion to clear lcd
void LCD_Init()                                                                   // funtion to LCD initialize
{
 print(0x38,0);
 print(0x06,0);
 print(0x0e,0);                                       // print introduction on lcd
 print(0x80,"  A PROJECT BY  ");
 print(0xc0,"  BSEE   ");
 LCD_clear();
 print(0x80,"DTMF CONTROLLED ");
 print(0xc0,"HOME AUTOMATION ");
 LCD_clear();
 print(0x80,"  SYSTEM READY  ");
 }
 
void serial_init()
{
TMOD=0X20;
SCON=0X50;
TH1=0XFD;
TR1=1;
}                             // funtion to initialize serial communication
 
void main()                                                                       // main program 
{
 all_off;
 serial_init();
 LCD_Init();                                                  // initiaze lcd and serial communication 
while(1)
{
 print(0xC0,"ENTER CODE ");                                                       // enter a string of length 4 by using for loop
 for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  }              
 
 if(strcmp(compare1, input)==0)                                               // compare input strings with stored strings
 {
 LCD_clear();
 print(0x80,"     TV ON     ");
 tv=1;
 }  
 print(0xC0,"ENTER CODE "); 
 for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare2, input)==0)
{LCD_clear();print(0x80,"    FAN ON     ");fan=1;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare3, input)==0)
{LCD_clear();print(0x80,"   LIGHT ON    ");light=1;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare4, input)==0)
{LCD_clear();print(0x80,"     AC ON     ");ac=1;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare5, input)==0)
{LCD_clear();print(0x80,"     TV OFF    ");tv=0;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare6, input)==0)
{LCD_clear();print(0x80,"    FAN OFF    ");fan=0;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare7, input)==0)
{LCD_clear();print(0x80,"   LIGHT OFF   ");light=0;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare8, input)==0)
{LCD_clear();print(0x80,"     AC OFF    ");ac=0;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare9, input)==0)
{LCD_clear();print(0x80,"    ALL ON     ");all_on;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
 if(strcmp(compare0, input)==0)
{LCD_clear();print(0x80,"    ALL OFF    ");all_off;}
print(0xC0,"ENTER CODE ");  
for(k=0;k<4;k++)
 { 
  while(RI==0);
  y=SBUF;
  input[k]=y;
  lcddata(input[k]);
  RI=0;
  delay(40);
  } 
}
}

 
Last edited by a moderator:

strcmp() and similar functions are working with C strings and expect a terminating 0 character. To work with strcmp() your strings must be defined with a length of 5 bytes. Likewise a terminating 0 must be appended when reading in strings character by character.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top