Hi all, New to this forum in a posting perspective but it's been a great help in a lot of areas from the start of my 8051 classes this year and last year so for that thanks. Im coming near the end of my final year project and I'v come across a slight problem with my interrupt implementation. I have RTFMed about all I can find on interrupts from any number of sources before you tell me to Please read the manual. The problem is that my ISR is triggered fine the first time sets a flag goes back carries out a function ... due to it not being a good idea to have function calls within an ISR ... resets flag and then sits reading the clock again however when I try to interrupt again it will not carry out the ISR this next time.
I have : reset external interrupt flag; reset condition variable flag; even attempted to re-enable external interrupt (even though this is probably not required);
The code is a long one so Id rather not post the entire thing here if anyone wants to take a look at it who has a good idea as to what I'm doing wrong or if anyone wants to suggest a specific part to post to look at the ISR or the menu its being called from please contact me. I will however not be sending this code on to anyone who's just looking for something to copy as I've read the manual to get it this far. I'd also be very happy to have someone look over the code in general for bad programming implementation if they would ... I'm a student in this subject but I've a keen interest in the field.
Also I'm Irish but this website is picking me up in America for some reason ?
Regards Robbie.
Was a little silly to not include any code at all so here is my ISR ... my project is an RFID orienteering tag system and the elements in the array are correct for my usage before anyone points it out to me it is working its just that I cannot get it to retrigger the external interrupt after including (2) which is a small part of (3).
(1) //ISR void enter (void) interrupt 0 { while (1) { receive(); writecmd(0x99); if (card_id[2,3,4,5,6,7,8,9,10,11,12,13] == kite1[0,1,2,3,4,5,6,7,8,9,10,11]) { kite1_f = 1; delay(10000); IE = 0; return; } if (card_id[2,3,4,5,6,7,8,9,10,11,12,13] == kite2[0,1,2,3,4,5,6,7,8,9,10,11]) { kite2_f = 1; delay(10000); IE = 0; return; } if (card_id[2,3,4,5,6,7,8,9,10,11,12,13] == kite3[0,1,2,3,4,5,6,7,8,9,10,11]) { kite3_f = 1; delay(10000); IE = 0; return; } if (card_id[2,3,4,5,6,7,8,9,10,11,12,13] == kitef[0,1,2,3,4,5,6,7,8,9,10,11]) { kitef_f = 1; delay(10000); IE = 0; return; } else { writecmd(0x99); delay(4000000); writedata('R'); writedata('e'); writedata('t'); writedata('r'); writedata('y'); writedata('!'); return; } } } (2) if (kite1_f >= 1) { writecmd(0x99); writeline_lcd("Kite 1 scanned! "); split1[1]=c; split1[2]=e; split1[3]=f; delay(40000000); writecmd(0x99); writeline_lcd("Go To Kite 2 "); delay(10000); kite1_f = 0; }
while (1) it seems you are confusing an ISR as a task drop the while
The text of my post refers to the title of that post; viz,
card_id[2,3,4,5,6,7,8,9,10,11,12,13] == kitef[0,1,2,3,4,5,6,7,8,9,10,11]
What do you thing that does??
Sorry only saw what you were referring to in the heading there. What I'm doing there is comparing from the 2-13 element of the scanned array to the 0-11 elements of a predefined RFID tag I've debugged this part of my code over a week and it does in fact compare each element to its opposing element and do exactly as intended.
No, in fact - you're not.
The 'C' programming language does not have any such way to compare multiple array elements
Time to revisit the 'C' text book!
Hint: look up the comma operator...
Ah bugger ... Well I'm kind of confused then I'm not sure how the if statement is working then as its able to differentiate between about 15 different tags I'm using. With some tags the first number elements are exactly the same as others and with others they are completely different.
Ah ha ! Thanks for that, that could have been embarrassing. Ok i'll have to rewrite that just a tad :p. Any chance you can see any such silly mistakes in the ISR and my implementation of it taking that the ISR is working for a comparison of the final elements of the listed arrays. I want to get this part working before I tackle the problem of comparing the complete tag ID as it is the more important at the minute.
Thanks again so here is my resolution to the aforementioned error you pointed out:
unsigned char i = 0; receive(); writecmd(0x99); if (card_id[13] == kite1[13]) { for (i=2;i<14;i++) { check=card_id[i]-kite1[i]; errors = errors + check; } if (errors != 0) { writedata('E'); writedata('R'); writedata('R'); writedata('O'); writedata('R'); writedata('!'); } else { kite1_f = 1; delay(10000); IE = 0; } }
Can you see any problems with this ?
And any input on the ISR as a whole problem would be a big help.
Yes:
check=card_id[i]-kite1[i]; errors = errors + check;
'check' could be positive or negative - so errors could "cancel-out"...
You test fails as soon as any mismatch fails - so you just need to flag that a mismatch was found (and there's no need to continue the loop beyond that).
Also, you are checking at the same index in each array - which isn't what your original code intended...
What I mean is, you need to construct a test that will indicate "fail" if any mismatch is found.
Ah how did i not see that thanks! ... I'll start work on that now .... The system I'm using has a maximum of 12 tags that are all predefined I've used tags that all have the final array differing from one another (hence how I thought it was doing the original implementation) so this is the starting check to differentiate between each tag and the check is then ensuring that the user is not cheating by using a different tag not predefined for usage in the orienteering course.
Any thoughts on why my ISR is only being triggered once ?
If your ISR is expected to trig by an external-interrupt pin, then you shouldn't also have any "receive()" in that interrupt.
Create a UART ISR that trigs, picks up single characters and places in a queue.
Then have other code consume characters from this queue to try to figure out if you have a complete code. Obviously taking into account incomplete transfers where the UART doesn't get a complete number for some reason and might later restart a new transfer.
If your RFID unit sends digits and ends the stream with a newline character, then you could have your serial code set a flag when it sees the newline, and the main loop reacts to the flag, scans the received data and accepts/rejects.
Too much data before the newline? Throw away all characters until you either gets a longer pause or a newline character.
Long pause after digits but before a newline? So timeout the received data and wait for a new transfer.
But don't try to do too much in an interrupt handler.
receive(); what does that do?
Right so in reply to Andrew(hope you don't mind me using your first name) I have changed the check to this:
if (card_id[13] == kite1[13]) { for (i=2;i<14;i++) { if (card_id[i]!=kite1[i]) { writedata('E'); writedata('R'); writedata('R'); writedata('O'); writedata('R'); writedata('1'); return; } } kite1_f = 1; delay(10000); IE = 0; }
Hope thats a bit better, kind of quickly thrashing out code here maybe I should take some time with the problem ?
In reply to Mr Westmark : Hello there and thanks would I be correct in assuming when you are talking about placing in a queue you are talking about an array or is there some other construct for this implementation? I'll show you my current recieve(); function below but it appears that my function is currently working for the first external interrupt but then failing to trigger in the second instance however from my debugging I can see its not sticking inside the ISR just not recognising it. Also go easy on me when you see this recieve function as it was the first time I wrote anything for serial communication.
void receive() //Function to receive data serialy from RS232 { unsigned char k; for(k=0;k<14;k++) { while(RI==0); card_id[k]=SBUF; RI=0; } }
memcmp()
Hi eric I remember seeing your name before on these threads I think you helped a friend of mine Cian Young with his project too for certain reasons we didn't get the chance to do serial communications in our programmable electronics course and hence why the recieve function is a little butcheresque. The function is shown just above your post.
In all fairness, you did write "The 'C' programming language", not library.
Yes, that was deliberate.
That, and the "such" - meaning a direct comparison operator of multiple array elements.
I agree that memcmp() from the standard Library would seem like an ideal choice here.
In all fairness, I'm surprised nobody else picked this up.
;-)
Hmm would this take up more space to include or will my new error check suffice for my application ? I reworked it again after the potential for error cancellation.
Yeah im now lost tired and clueless.
i tried this as my last attempt I'm not even sure if this would work
void recieve(void) interrupt 4 { if (RI == 1) { RI = 0; card_id[s] = SBUF; s++; } }
like I said I haven't received any instruction on my course in interrupts so this is not going my way at all. I've tried a number of sites and books at this stage but im noot getting it at all.
Robert: in this code
You are very close. The way I would implement this is something like:
void recieve(void) interrupt4 { char c; if (RI==1) // a character recieve caused the interrupt { RI=0; c=SBUF0; // get the character } switch (state) { case WAITING_ID: { // // whatever you have to do to determine if this is an ID. state=WAIT_END; break; } case WAIT_END: { // whatever you have to do to determine if this is the end... if (end_of_packet) { state= DONE; } break; } case WAITING_START: { // whatever it takes to recognize that this is the start of the packet state=WAITING_ID; // prepare your buffer, and if this is based on a fixed length string, perhaps // initialize a counter and use it to determine when the packet ends. } } } elsewhere, you watch for state==DONE and then process the packet. When you are done, set state=WAITING_START; so that the state machine knows how to handle the next character. This code is skimpy on details, because I don't know how you recognize the start of the rfid read, and how you would recognize the id you want to extract, or recognize the end. If the packet is a fixed length, the simplest thing is to use a counter that starts at 0 when you recognize the start state, and decrement it, buffering the character until you find the ID, or else the end of the message. In a nutshell, this is a state machine that gets updated on each received character and reaches a DONE state after the full message is recognized. The main loop watches for the state to be tagged as DONE, then grabs the data, and resets the state to waiting for start (it might also reset the length etc.) This code as written probably won't run, and is certainly not a complete state machine for your application, but should give you an idea of what to do. The statemachine would recognize a start, buffer the data into a buffer until it recognizes an end. That can possibly be done by either a message count, or by analyzing the packet. I often implement protocols like this, and favor using a STX and EOT type protocol when I don't have binary data.
Thanks very much to everyone who gave me plenty to consider and so much food for thought I eventually found the problem with the ISR it turns out my code was working fine and I cannot believe no one saw the problem was that I was resetting IE instead of IE0. Now that was the most obvious problem however only today did notice it. So sometimes it is in fact the most simple solution that fixes these problems but look for complicated solutions and they will easily be found. Thanks again all, Robbie Dowdall.
Oh and a special thanks to Eric and Andrew for finding those errors that would have made me look a little thick :)
View all questions in Keil forum