Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Multiple interrupts

Status
Not open for further replies.

premkumar9

Member
In my project where I use 89S52 uC, I use UART for serial communication and T0 timer for generating a time delay. Programming is done in C. When I tried to generate 1 minute delay, it works correctly in the circuit only if serial communication is disabled (ES=0). Can anybody pls suggest where could be the problem?
 
Not without some real information. How are you generating a 1 minute delay? Are you doing a static loop for the delay or using a timer? How precise does your 1 minute timer need to be? Posting the code you have might help.
 
Not without some real information. How are you generating a 1 minute delay? Are you doing a static loop for the delay or using a timer? How precise does your 1 minute timer need to be? Posting the code you have might help.

Delay is generated using the timer. Need not be very accurate. Using 16 bit counter mode and 14 counts taken as one second approximately (11.0596 MHz Clock). Working alright if serial commn is disables by making ES=0; My doubt is whether any special care required when both interrupts are active.
 
You'll have to use different timers, or change the way your delay routine runs, enabling the UART probably changes the way the timer behaves as the timer is required for UART operation. When you enable your UART code you probably change the time prescaler and interupt values so that your timer interupt never executes, or at least suddenly takes 10 hours not 1 minutes to execute.

Determine what turning the UART on does to the interupt modes and you'll have to adjust the code you're using for the timing timer to work with however the timer is configured after the UART is turned on. You may have to software poll the timer for the 1 minute delay instead of using an interrupt.
 
You'll have to use different timers, or change the way your delay routine runs, enabling the UART probably changes the way the timer behaves as the timer is required for UART operation. When you enable your UART code you probably change the time prescaler and interupt values so that your timer interupt never executes, or at least suddenly takes 10 hours not 1 minutes to execute.

Determine what turning the UART on does to the interupt modes and you'll have to adjust the code you're using for the timing timer to work with however the timer is configured after the UART is turned on. You may have to software poll the timer for the 1 minute delay instead of using an interrupt.
1. I am using different timers (T0 for delay and T2 for UART)
2. My main loop takes different time duration since random serial communication is happening in the loop.That is why I didn't try polling.
 
Perhaps the timer interrupt is occurring when the UART code is running? If the code that handles your UART code prevents interrupts from occurring and clears the interupt flags when it returns it may be clearing the Timer0 interrupt flag which means the interrupt is never triggered, for good UART timing the UART code obviously has to take precedence so disable the interrupts, but BEFORE you clear the interupt flags and return from the ISR routine poll the interupt register and manually trigger the timer ISR routine after the UART code is done servicing it's ISR. I'm not sure of the details of the interupt handler of the chip you're using but I hope you understand what I'm trying to say and it gives you something more to look at how your code might be interacting.
 
Perhaps the timer interrupt is occurring when the UART code is running?
Yes. It is.
If the code that handles your UART code prevents interrupts from occurring and clears the interupt flags when it returns it may be clearing the Timer0 interrupt flag which means the interrupt is never triggered, for good UART timing the UART code obviously has to take precedence so disable the interrupts, but BEFORE you clear the interupt flags and return from the ISR routine poll the interupt register and manually trigger the timer ISR routine after the UART code is done servicing it's ISR. I'm not sure of the details of the interupt handler of the chip you're using but I hope you understand what I'm trying to say and it gives you something more to look at how your code might be interacting.
I use the KEIL tools for programming in C and have very limited expertise in programming. I understood your point. I think such things are usually taken care of by the compiler. One more thing to mention is that I use the same T0 interrupt for flashing an LED. It is observed that during UART communication the flashing stops (may be T0 interrupt getting disabled) and after that flashing resumes. Doesn't it mean that T0 interrupt is getting enabled again after the UART ISR is over.
 
Is the UART code an interrupt driven software UART or a true hardware UART with a buffer? Post your code it might help understand what's going on a little better.
 
Is the UART code an interrupt driven software UART or a true hardware UART with a buffer? Post your code it might help understand what's going on a little better.
It is a hardware UART (a transition at RXD pin takes to ISR) and received is byte is stored in SBUF register and sets RI flag.
Actually both UART and T0 interrupt are working. When I program to start some activity after 1 minute, it doesn't work correctly but instead of 1 min it works after about 20 minutes. I tried by changing the time. Still the same.
Sending the full code may be very confusing . But I doubt whether partial code will be of any use.
So what I expect is that if and expert like you could give some hint on where to look for, I will make an attempt and try to narrow down the issue.
 
The code that is running your UART is obviously also altering the other timer. Perhaps it's a retriction of the timer's on your chip? I'm not sure of your specific chip but the problem is going to lie in the setup of the UART itself, something it is changing is what is causing the problem, change the time to like 1 second (so you notice right away if it stops working) and starting commenting out sections of your UART code until it starts to work again.

Perhaps the UART code is tampering with registers that the timer routine requires?
 
Last edited:
Hi, 20 minutes!? that is really very long. Are you sure you are clearing the RI flag? Cause as i remember you need to clear it manually. What you were describing earlier(no time duration) was a classic case of an interrupt occurring while another one is being serviced. If you are clearing the RI flag, than post the code( the skeleton only and not the WHOLE code). There shouldn't be THAT much delay.
 
UART communication

The code that is running your UART is obviously also altering the other timer. Perhaps it's a retriction of the timer's on your chip? I'm not sure of your specific chip but the problem is going to lie in the setup of the UART itself, something it is changing is what is causing the problem, change the time to like 1 second (so you notice right away if it stops working) and starting commenting out sections of your UART code until it starts to work again.

Perhaps the UART code is tampering with registers that the timer routine requires?

I would like to mention another observation also for your valuable comments.
I tried communication between u1 and u2 in the following sequence.
1) u1 sends a request (one character) to u2 and waits for the response.
2)On receiving the request, u2 sends 3 bytes of data to u1.
3)u1 receives these data and stores them as an array.

In step 2, I check the reception by checking a flag which is set in the ISR on receiving 3 bytes.
After sending each request from u1, u1 will wait till the above flag is set.

Here the program works only if I add about 300ms delay between each sending of request.
Could you please give any hint on why this delay becomes necessary?
 
It might be waiting for a stop bit that it never receives, verify you have the same frame format on both, or something in the code might be messing up a bit from the Interupts going (possible register corruption) If an interrupt routine uses a register that the running code is using without backing it up at the start and restoring it at the end of the interupt all sorts of unpredictable things can happen.
 
Multiple active interrupts require prioritization and setting them to occur only as frequently as necessary. Do only what's necessary in any interrupt: keep the code short.

The ~1 minute timer should be set to interrupt as few as possible during each span.
The serial port should interrupt once/byte received (or buffer full, if you can) or on tx empty.
Disable timer interrupts when servicing the serial port.

Aside from simple mistakes of altering stuff used by the other routines while in an interrupt, nesting interrupts is a common source of troubles. Even experienced coders do it only when Necessary. Happy Coding... <<<)))
 
Multiple active interrupts require prioritization and setting them to occur only as frequently as necessary. Do only what's necessary in any interrupt: keep the code short.

The ~1 minute timer should be set to interrupt as few as possible during each span.
The serial port should interrupt once/byte received (or buffer full, if you can) or on tx empty.
Disable timer interrupts when servicing the serial port.
In fact I was using multiple interrupts in u2. Now I disabled and tried. Still the issue persists.

Aside from simple mistakes of altering stuff used by the other routines while in an interrupt,
I use a bit which is set to high inside the ISR after 3 bytes are received. In the main program I change that bit (which is global) to 0 before sending the request and wait it to become 1 after sending the request(to confirm reception). Anything wrong in this method? (As mentioned earlier, this method works only if there is an additional delay provided)
 
It might be waiting for a stop bit that it never receives, verify you have the same frame format on both, or something in the code might be messing up a bit from the Interupts going (possible register corruption) If an interrupt routine uses a register that the running code is using without backing it up at the start and restoring it at the end of the interupt all sorts of unpredictable things can happen.
Sorry. The additional delay between sending requests was required because of a mistake in my coding in the u2 side. Now it works without that extra delay.
 
My sequence is as below
Send request to u2.
Wait till all bytes are received as response OR 1 second (time out) whichever is earlier.
Then send the second request and repeat the above sequence.
The received bytes are stored in an array

Here one problem I faced is that some times, the array is not getting updated even though sending and receiving continues ( I used LED indications to check that).
It seems that this problem changes ( not sure if it is completely solved and will not come again) if I add additional delay between every sending of request.

If this problem comes in the actual working condition (in the field), it will be a very serious issue.

What could be the reasons?
Is there any way to take care of such situations in the coding (like timing out)?
Your valuable suggestions ill be highly appreciated
 
Could you be sending and receiving at the same time and both interupt routines aren't able to process in a timely manner?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top