#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
How about:
unsigned char rx_data; boud_rate();
#include<reg51.h> void baud_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) { #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) { baud_rate(); unsigned char rx_data; for(;;) { rx_data = serial_receive(); switch(rx_data) { case 0067892341 : open_door(); break; case 0045780034 : open_door(); break; default: dont_open(); } proper_delay(); } } errors rx_data:undefined identifier illigal octal digit
If you are going to pay someone $100 - do you then specifically write $00100?
If the compiler takes these numbers as octal - have you then taken a look at the language definition (lots and lots of books are available if you haven't access to the languge standard) to see how you in C define decimal, octal or hexadecimal numbers? Any book at least decent about describing the C language would tell you that 0x or 0X is the start of a hexadecimal number, and 0 (but not followed by X or x) is the start of an octal number.
Note that 10-digit decimal numbers spans 0 to 9,999,999,999. Do you think your C51 compiler supports that numeric range? So maybe you should consider using C strings and perform comparisons with strcmp() instead? You will not be able to use a switch statement, but at least you don't need to worry about the number of digits in each code compared to the numeric ranges supported by the target processor.
Another thing - with string comparison based on an array of known codes, you can break out your valid codes from the source code. With a switch statement, you do need to modify that switch statement whenever you need to change, add or remove a code.
OK, I'll lead you by the nose.
the reason that you are getting the rx_data: undefined identifier is that you are defining the variable after the call to baud_rate(). you can't define a variable after code in a code block.
your case statement is not going to work. rx_data is a single character, not a string of characters. You need to receive a number of characters, put them into an array, and then compare that array to your keys. a switch statement will not work for the array comparison.
Most of all, you need to pick up your C textbook.
OK, I'll lead you by the nose
did it hurt when you put the ring in?
... is that he REFUSES to read a C book before "writing" 'C'. His posts smell highly of "writing" 'C' after "studying" a couple of 'C' programs.
answer his most basic questions and he will be back tomorrow with "what is up on a resistor?"
I DO believe in helping posters, but the OPs questions show a fundamental lack of understanding the most basic aspects of programming.
Erik
thank you, completed the coding. . 250 numbers i have taken it in 2D array. and received no is another array and compared both using 2 for loops.
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
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...
"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.
View all questions in Keil forum