# 8051 Interrupts Question?

#### ra1ph

Joined Jan 5, 2010
31
Hi,
I'm looking for a bit of feedback on a problem that I'm working on:

The question is;
"Write an 8051 program that uses interrupts to implement an intruder alarm.
Let sensor be tied to the INTO(p3.2) pin, which is at 0 when an intruder steps on a special mat, and 1 when there is no pressure on the mat.
The program should write a 1 to P3.0 (ALARM) to turn on an alarm buzzer when an interrupt occurs. The program should keep the alarm on until there is no more pressure on the mat."

My attempted Answer is:

ORG 0000H

SETB EA ;SETTING Interupt enable registrar
SETB EX0
MOV TCON,#00H ;Recognising INT0 WHEN LOGIC lOW

MAIN_LOOP:

JMP $ORG 0003H ;external interupt 0 vector SETB P3.0 JNB P3.2,$
CLR P3.0
RET1

END

As usual any help greatly appreciated!

#### jimkeith

Joined Oct 26, 2011
540
First, your initialization cannot be done down in the interrupt address range because it will write over all kinds of stuff--do it like this:
The only other potential problem I see, is starting it with someone on the mat...
Hope this is helpful

ORG 0000h
jmp init ;get out of here

ORG 0003H ;external interupt 0 vector
SETB P3.0
JNB P3.2,$CLR P3.0 reti ;you had RET1 which may not work org 0030h ;safe place to put remainder of code--got burnt once putting code in the interrupt area init: ;initialization mov sp,#80h ;default 07h is too low for stack pointer--got burnt here too--prob not an issue with only 1 interrupt clr p3.0 ;clear alarm out SETB EA ;SETTING Interupt enable registrar SETB EX0 MOV TCON,#00H ;Recognising INT0 WHEN LOGIC lOW MAIN_LOOP: JMP$

Last edited:

#### ra1ph

Joined Jan 5, 2010
31
First, your initialization cannot be done down in the interrupt address range because it will write over all kinds of stuff--do it like this:
The only other potential problem I see, is starting it with someone on the mat...
Hope this is helpful

ORG 0000h
jmp init ;get out of here

ORG 0003H ;external interupt 0 vector
SETB P3.0
JNB P3.2,$CLR P3.0 reti ;you had RET1 which may not work org 0030h ;safe place to put remainder of code--got burnt once putting code in the interrupt area init: ;initialization mov sp,#80h ;default 07h is too low for stack pointer--got burnt here too--prob not an issue with only 1 interrupt clr p3.0 ;clear alarm out SETB EA ;SETTING Interupt enable registrar SETB EX0 MOV TCON,#00H ;Recognising INT0 WHEN LOGIC lOW MAIN_LOOP: JMP$

Thanks for this, I have an end of semester exam on the 8051 on Monday morning, so this is a great help. This question has come up in previous years. I'm glad I was not too far off the correct answer.

#### jimkeith

Joined Oct 26, 2011
540
assembler directives:
$mod51 ;put this at the beginning of your code to import mnemonics definitions and addresses or$mod52 for the 8052
I have always used cseg at xxxxh instead of org xxxxh Probably both will assemble OK.

I do not have an 8051 reference manual handy so check out which bit to set to make int0 edge triggered--if it is not edge triggered, it will lock up attempting to service a continuously low interrupt signal--another way to do it would be to disable the interrupt while servicing the interrupt and then re-enable it when exiting.

found it
setb it0 ;int0 is edge triggered--this is hidden in the tcon register or

mov tcon,#00000001b

Last edited:

#### ra1ph

Joined Jan 5, 2010
31
Thanks Jim for the follow up,

So the revised code is
$mod51 ORG 0000h ;we have not used cseg before jmp init ;get out of here ORG 0003H ;external interupt 0 vector SETB P3.0 JNB P3.2,$
CLR P3.0
reti ;you had RET1 which may not work

org 0030h ;safe place to put remainder of code--got burnt once putting code in the interrupt area

init: ;initialization
mov sp,#80h ;default 07h is too low for stack pointer--got burnt here too--prob not an issue with only 1 interrupt
clr p3.0 ;clear alarm out
SETB EA ;SETTING Interupt enable registrar
SETB EX0
MOV TCON,#01H ;Recognising INT0 when edge triggered

MAIN_LOOP:
JMP \$

Just a question on MOV TCON, #00000001b;

why should this be edged triggered and not set to 00000000b to be triggered when logic level is LOW?

PS. Weather here in Ireland is very mild around 10degC . Like yourselves we have had a very mild winter unlike last year when temperature reached -9DegC (16F)

#### jimkeith

Joined Oct 26, 2011
540
Level triggering is often intended for a short pulse--if the interrupt remains low, it will be continuously attempting to service the interrupt--to get around this, the interrupt is generally immediately turned off until the signal returns high

With edge triggering it can remain low and will not be re-triggered until it goes high and then low again.

In microcontrollers there are always multiple solutions to problems.