We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
#include<reg51.h> void boud_rate() { SCON = 0x50; TMOD = 0x20; /* timer 1, mode 2, 8-bit reload */ TH1 = 0xFD; /* reload value for 2400 baud */ TR1 = 1; TI = 1; } char serial_receive() { char chr; /* variable to hold the new character */ while (RI != 1) {;} chr = SBUF; RI = 0; return(chr); } void main(void) { boud_rate(); unsigned char rx_data; for(;;) { rx_data = serial_receive(); switch(rx_data) { // case '1': open_door(); case 0067892341 : open_door(); break; // case '9': open_door(); case 0045780034 : open_door(); break; default: dont_open(); } proper_delay(); } }
errors
'rx_data':undefined identifier illigal octal digit
please tell me how to overcum these errors
If you zero-terminate the character arrays or have a one-dimensional array of strings, you could make use of strcmp() instead of your inner loop.
Another thing is that it is normally nicer to maintain code containing a one-dimensional array (vector) of strings, than to have a twodimensional array of characters.
Did you make sure to properly tag your array to get it stored in CODE?
do i have to use < pre > and < /pre > in my c code? when i dont use them i get syntax error at line 32. i tried to Please read the manual but no find it. is it in the manual?
One idea here could be to attempt something like "storing variable in code site:http://www.keil.com" using Google.
oh woe, oh woe
< pre > and < /pre > are formatting codes for THE FORUM, they are not 'C"
line 32 tell us nothing, we do not have your code, and if we did do not expect us to go 1, 2, 3, .... 32
Erik
All the problems in this thread can be effectively and completely resolved with a functioning brain.
Yes - the most important tool in software development is the one between the ears!
void serial_int (void) interrupt 4 { static char chr = '\0'; /* character buffer */ if (RI == 1) /* it was a receive interrupt */ { chr = SBUF; /* read the character into our local buffer */ RI = 0; /* clear the received interrupt flag */ TI = 1; /* signal that there's a new character to send */ } else if (TI == 1) /* otherwise, assume it was a transmit interrupt */ { TI = 0; /* clear the transmit interrupt flag */ if (chr != '\0') /* if there's something in the local buffer... */ { if (chr == '\r') chr = '\n'; /* convert to */ SBUF = chr; /* put the character into thetransmit buffer */ chr = '\0'; } } } int main() { unsigned char a[5][11]={{0,0,0,1,6,2,2,1,8,2,6},{0,2,3,1,5,6,7,3,6,4,5},{0,0,1,8,9,6,7,6,4,2,3},{0,2,5,8,9,6,4,7,2,3,0},{0,6,4,3,7,8,0,2,5,3,7}}; unsigned char c[10],i,j; char chr; lcd_init(); SCON = 0x50; /* mode 1, 8-bit uart, enable receiver */ TMOD = 0x20; /* timer 1, mode 2, 8-bit reload */ TH1 = 0xFD; /* reload value for 2400 baud */ ET0 = 0; /* we don't want this timer to make interrupts */ TR1 = 1; /* start the timer */ TI = 1; /* clear the buffer */ ES = 1; /* allow serial interrupts */ EA = 1; /* enable interrupts */ lcd_command(0x10); lcd_prinit(chr); c[10]=chr; for(j=0;j<5;j++) { i=1; if(a[j][i]==c[i]) { for(i=1;i<9;i++) { if(a[j][i]!=c[i]) break; } if(i==10) { // opn_dor=1; lcd_printxy(2,1,"yes"); } } // opn_dor=0; lcd_printxy(2,1,"NO"); } }
there are no errors and according to me the logic is correct... but still not getting the output(display-yes or no) please help me me not able to get the characters which are received in the interrupt program
So, are you _really_ sure your code is correct?
unsigned char c[10]; <= 10 elements
c[10]=chr; <= assigning to the 11th, element...
i=1; <= ignoring first character (zero) and start with second character (index 1). if(a[j][i]==c[i])
for(i=1;i<9;i++) { ... } if (i==10) { ... } <= how would i reach the value 10, when the loop ends if i reaches 9?
lcd_printxy(2,1,"NO"); <= printing "NO" even if you just printed "yes".
So - exactly how much did you verify your code? What was the result of the debugging? Did you really step through the code and check the values of the variables and how the execution jumped? Or did you just _assume_ what it would do?
Next thing - guess how much simpler your code would have been if you had stored the keys as strings:
char key[] = "00016221826"; char input_key[12] = ""; ... if (!strcmp(input_key,key)) { lcd_printxy(2,1,"yes"); }
Any special reason why you decided to ignore the suggestion to look at strcmp()?
By the way - when will you take care of the use of the "chr" variable?
You have one such variable in your interrupt handler. You seem to attempt some broken variant of echo.
You have another variable with same name (but totally different variable) in main(). But you never fill the 10 - or 11 - slots of your c[] array. So what will you then compare with the stored keys?
Wouldn't it be better if c[] was global, and the interrupt checked if the array was available or not? If available, then the interrupt handler adds a character at a time until you get the newline. If you get newline after exactly 11 digits, you set a flag that main() can process. And your interrupt needs to ignore all new characters received until main() is done. If you get the newline too early, then you failed somewhere and have to reset counter and start getting 11 new digits. If you get a 12th digit, then you also fail and have to wait for a newline to regain sync.
Same algorithm if using strings - but then you have to add yet another character ('\0') when you have all digits, to terminate the string.
the reason is-the data receiving through rs232 is not a string its a character. if u help me how to receive the string then I can follow ur method
the reason why i am starting the loop for i=1 is i dont want to compare the 1st character because when read it on hyper terminal it is showing $0062...... the rfreader is automatically including $, but $ is not present in the id-card. so I have taken an array of 11. I tried to take $ in the array of 11 numbers but it is giving syntax error. so i have replaced $ with 0 in the array i,e a[5][11]={{$,0,0,6,2.....},{$,0,4,6,7,8,.,.},.,.} is replaced with a[5][11]={{0,0,0,6,2,....},{0,0,4,6,7....},.,.,}. This is the reason I am taking i=1 in skipping 0($) as it is common for every no.
I am sorry for all other mistakes..
Yes; but a string is just a sequence of chatacters (plus a NUL terminator) - isn't it...?
BTW: Note how you've messed-up the thread by using the pre tags incorrectly...
in the interrupt handler i am receiving the characters in the variable 'chr'. how to use it in main to compare? do i need to take chr as a static global variable?
Wouldn't it be better if c[] was global, and the interrupt checked if the array was available or not? If available, then the interrupt handler adds a character at a time until you get the newline. I didnt get this point.... I am using c[] for received variables
using string compare(strcmp) we can compare with only one variable at a time ... how to compare with all other nos
Each strcmp() tests the input string with one of your stored keys. So if you have 5 keys, you need up to 5 strcmp() calls.
See any issues?
I did explain how to receive character-by-character and make a string of it.
k, but I am receiving characters in the interrupt handler, how to use those characters in main?
Was there something wrong with the solution I have already described?
"a[5][11]={{$,0,0,6,2.....},{$,0,4,6,7,8,.,.},.,.}"
Of course the above gives a syntax error.
Don't you realize the difference between characters and numbers?
0 is a number with value zero. You write it using a single digit. '0' is the ASCII character zero. It does not have the numeric value 0. It has the numeric value 0x30 = 48.
If you can see the codes on the hyperterminal, then the codes are sent out as printable text. So each character you receive on the serial port is a '0', '1', '2', ...
You really have to keep track of the difference betweeen 0 and '0' because you can't perform the comparison unless you have the same format on both sides of the comparison operator.
And you have to understand that the compiler can handle an array of numbers (as long as the numbers are within the range of the data type for the array) but numbers can't contain any $. And your arrays can just as well be filled with the characters '0', '1', '2', ...
C strings like "123" is an array of characters { '1', '2', '3', '\0' } which indicates what strcmp() would do when comparing a printable string of code digits as received from your RFID receiver with the strings you have stored as known keys.
Have you invested in a good book about C? Have you read it? Have you worked yourself through the exercises in the book?
C is not a language suitable for people who just try random code constructs.