nested interrupts in 8051 helpppp!!!!!

Thread Starter

qrious

Joined Sep 16, 2009
22
i am trying to write a program for at89c4051 uc which uses nested interrupts but its not working at all. My aim is to control some leds and relays using extrnal hardware interrupt pins ..when int0 is there then say mains supply is present and when int1 gets activated then mains supply is not there and depending on the interrupt certain leds and relays need 2 b swtchd on and off.

is it possible to forever nest the interrupts in an infinite loop? viz. when int0 is working i hav chnged the priority to set int1 high so dt int0 can b interrupted( which i vry much need) and in the isr for int1 i hav chnged priority again to set int0 high so that when suddenly mains supply comes back anothr set of instructions is executed( i need 1 interrupt to be able to interrupt the other)



the code is as follows.................



org 00h

ljmp main


org 30h
main:

mov ie,#85h ; to enable ea and int0 and int1


mov p1,#06h ; port 1.1 and p1.2=i/p and p1.0-p1.7 hav o/p devices(led and relay)

mov sp,#50h ; just felt like defining sp


d: nop ; main routine loops indefinitely
sjmp d


org 0003h ; isr0
ljmp bat0
reti



org 0013h ; isr1
ljmp bat1
reti




org 0ccc3h ; isr0

bat0: mov ip,#04h ; set the int1 interrupt high priority
mov ie,#85h ; enabled interrupts again

pop 03h ; to prevent stack from overflowing
pop 05h


clr p1.6 ; clear the led/relay which wud be set by isr1
setb p1.5 ; swtch on led/relay for isr0

l1: mov r0,#255 ; delay routine
l2: mov r2,#255
l3: mov r4,#255
l4: mov r5,#255


djnz r4,l4
djnz r2,l3
djnz r0,l2

nop
reti


org 0ffc0h ;isr1
bat1:
mov ip,#03h ; change interrupt priority 2 make int0 high
mov ie,#85h ; enabled interrupts again
pop 03h ; prevent stack ovrflow
pop 05h
setb p1.6 ; set led/relay for isr1
clr p1.5 ; clear devices set by isr0


; need a delay again for 30 mins at this point, not attaching code here

reti


end

this code shud let me set up infinite nested loop structure, evrytime 1 isr is executing it shud waiting for some time 4 another interrupt 2 happen if it does then the corrsponding isr shud run and if during that isr again while its waiting in the delay routine the other interrupt shud happen then jump to isr for that interrupt and so oon...but its not working....i am simulating in proteus and evrytine the program seems to be stuck in the delay part. even if the other interupt happens still until the currnt isr ends its full delay routine it is not jumping to the other isr, not acknowledging interrupt. i dnt kno how this happens only after the delay is over does the execution strt frm the other isr. why isnt the control being transferred the instant the othr interrupt happnes???????????? why is the delay routine always being executed fully??
do i need the reti instructions in the isr's ??? if i do not put reti instruction in the isr codes' and instead put an infinite loop there evn then during the excution of the loop the other interrupt is not being acknowledged...in that it nevr gets acknowledged plzzzzzzzzzzzzzzzzzzzzz helppppp me with this rubbish someone!!!!!! im a nooob
 

beenthere

Joined Apr 20, 2004
15,819
In general, you must complete any task called by one interrupt and return from that interrupt before you can accept another. Otherwise, your code gets lost and the stack will roll over and die.
 

Thread Starter

qrious

Joined Sep 16, 2009
22
I don't get you sorry, i pop the stack every time an isr runs...how does the stack overflow then? i really cannot understand i can't understand whats wrong with this program...moreover the program counter seems to roll over evry time it reaches the end statement in proteus and again reloads with 00h ...how is this possible?
 

beenthere

Joined Apr 20, 2004
15,819
is it possible to forever nest the interrupts in an infinite loop? viz. when int0 is working i hav chnged the priority to set int1 high so dt int0 can b interrupted( which i vry much need) and in the isr for int1 i hav chnged priority again to set int0 high so that when suddenly mains supply comes back anothr set of instructions is executed( i need 1 interrupt to be able to interrupt the other)
See my first reply. If the loop execution is slow, you can have interrupts coming faster than the uC can keep up with.

For predictable program execution, you may only handle one interrupt at a time.
 

Thread Starter

qrious

Joined Sep 16, 2009
22
you know, i think im finally getting what you say here..bcoz earlier when the delay routine was smaller the interrupts were working fine, int0 could interrupt int1 and vice versa..but when i increased the delay then interrupts are acknowledged only after the delay is over...now this part is really confusing coz i thought the uC is supposed to jump to an isr the instant it is interrupted, it is checking the interrupt pins every clock/machine cycle isnt it?...how is the slowness of the loop affecting the response time to the interrupt?..moreover, as i was tracing through..i found that the ie1 & ie0 flags are set the moment i apply the respective interrupts but the execution is still taking place after the delay...this is all so confusing....you think i the structure i want can be implemented in any other 8 bit uC or should i switch to a 16 bit uC?
 

RiJoRI

Joined Aug 15, 2007
536
You're going to have to study the manual very hard.

First, why use interrupts at all? You should be able to do this by just polling two inputs.
Second, the shorter the ISR, the better. They should probably set/clear a flag, and the main code should test this flag.
Third, interrupts usually disable all interrupts when they vector to the ISR in order to prevent nesting interrupts.

--Rich
 

Thread Starter

qrious

Joined Sep 16, 2009
22
yeah well i thought nesting interrupts would be fun..never tried it before...now however i dont think its fun at all...irritating to say the least...i've resorted to polling..thanks for the help guys!
 
Top