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.
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; } }
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.
Two things.
You have some paths of your ISR that do IE=0. But you also have paths that only do "return". Every single path out of your ISR must make sure that you leave the processor in a state where it will be able to react to a new interrupt.
Next thing.
And ISR should enter, do a very quick job and then leave.
You have an ISR that calls receive(). And receive() may take forever since it will not end until a fixed number of serial characters are received. But what if there are no characters to receive?
Rework it so you have zero delays and zero loops waiting for any events inside all of your interrupt handlers.
Yes - an array is a good primitive for creating a queue. Just that if you read up on "round robin" or "circular buffer", you'll see examples of how an array is used with one insert pointer and one read-out pointer, allowing an interrupt handler to insert received data, while the main loop reads out and processes data.
Such a buffer also works for outgoing data - you can insert multiple characters into a send buffert, and have a serial interupt handler pick up the characters one-by-one as the UART gets ready for more data. Your code that prints multiple characters in the interrupt handler will have to wait for the actual transmit speed of the UART since you emit multiple characters and then busyloops waiting for the ready flag from the UART.
void enter (void) interrupt 0 { receive();
first interrupt 0 is the EI0 interrupt second you go to recieve even if the interrupt is transmit
this need some clarification
Ok my reader has a output pin that triggers when an RFID tag is received, I'm using this to tell the 89s52 to go and carry out recieve to see what is on the UART when this happens. Im only using this reader to recieve and have no transmit connection present only intended on simplex communication. I hope that answers your question.
Hmmm I feel slightly out of my depth here now.
Ok my reader has a output pin that triggers when an RFID tag is received, I'm using this to tell the 89s52 to go and carry out recieve to see what is on the UART when this happens. WHY?, why not just read the data coming down instead of mickey-mousing the read KISS
Im only using this reader to recieve and have no transmit connection present only intended on simplex communication.
you STILL need (if you do not do the right thing and move the read to the UART interrupt) to test for RI or you will willi-nilly fetch 'bytes' that may or may not be read from the seral bus.
you are waaaay overcomplicating the issue
Ah ha ha damn so I've overcomplicated things again .... back to the drawing board so serial interrupt it is then.
Also Willy-nilly is that a technical term?
Hmm this could potentially remove all requirement for external interrupt if I can do a quick tag check after the serial interrupt is carried out set the required flag for the required tag and then that will return to the menu to carry out the function for the specified flag. ?
Or would I better to have external and serial interrupts active with the serial to recieve the data and then the external will be triggered to set a flag high and this flag will then enable a tag check within the menu function.?
I'm expecting my computer to slap me at this stage.. Ok I'm sure i'm not far away from being annoying at this stage but can an array be included in this serial interrupt or should I be staying away from that and trying to get each byte be read in by the UART exit the interrupt save said byte before the UART gets triggered for the next byte .... Confused is not the word right now
Have you googled for source code for a ring buffer yet?
Where your UART interrupt handler checks if there is a received character, inserts it into the buffer, and updates the write index.
The main loop sees that the read index doesn't match the write index so picks up the next character and does whatever is needed.
But then again - a UART interrupt handler could keep an index of RFID byte position and perform a digit comparison for that index position on the fly. And if all digits has been received set a "OK" flag.
Or if a difference is found set a fail flag and then wait to synchronize for a new RFID code.
Thanks for all the help everyone I don't understand how the FIFO ring buffer is working it's completely alien to me but I'm gonna have to call time on this code as I've a final report to complete on the project.. Maybe I'll be able to get back to it after I finish college but for the minute it's gonna have to sit on the back burner. Anyone who wants mention in the report just say as I have to reference my sources. Thanks again.