Using the External Interrupt 1

Hi there, I am trying to get a signal to the P89LPC932 Micro Controller by using the External interrupt 1 (P1.4). I never tried this before and I am also a beginner programming. Can anyone help me figuring out why this program does not work. The interrupt is supposed to be edge triggered and every time an interrupt occurs a counter (NPulses) is incremented til it gets to 1000 pulses. then it goes back to zero. I think my problem is configuring the timers registers, but I am not too sure.

Here is the code.
Thank you,

============================================
#include <Reg932.h>
#include <stdio.h>

void init(void);
void brkrst_init(void);
void Pulses_recieved (void);
unsigned long NPulses; //amount of pulses received
unsigned int Glass;
void ToggleC1Input (void);
sbit Pulse = P1^4;
int Glassid, count;

void Main(void)
{
P2 = 0x00;
NPulses = 0;
Glass = 10;
Glassid = 0;
count = 0;
init();
brkrst_init();
Pulses_recieved();
}

void init(void)
{
P1M1 = 0xFF; // Input only
P1M2 = 0x00;
P2M1 = 0x00; // push pull output
P2M2 = 0xFF;
ES = 1; EA = 1;
}

void brkrst_init(void) // This function allows ISP entry through the UART break detect
{
AUXR1 |= 0x40; // enable reset on break detect, pg. 102
SCON = 0x50; // select the BRG as UART baud rate source, pg. 60
SSTAT = 0x00;
//TCON = 0x04;
BRGR0 = 0x70; // 9600 BAUD at 11.0592 MHz. Hex 0470= 1136 decimal
BRGR1 = 0x04;
BRGCON = 0x03; // enable BRG, pg. 59
}

void UART(void) interrupt 4
{
RI = 0;
}

void Pulses_recieved(void)
{
while(1)
{
TCON = 0x40;
IEN0 = 0x04;
EA = EA | 1;
}
}

void ExtPulses (void) interrupt 2
{
NPulses++;
if (NPulses == 1000)
{
NPulses = 0;
Glassid++;
P2 = Glassid;
}
}
=============================================

Parents
  • Hmm, seems I miss something or ...?

    1. to configure INT1 interrupt to falling edge you should set IT1 bit of TCON then set EX1 and EA of IEN0.
    You do TCON = 0x40; <- it runs timer 1 as well as configured INT1 to low level triggered interrupt, that's wrong because your INT1 ISR does not support such mode of interrupt. You should:
    - either configure TCON for falling edge detector (by set bit IT1)
    - or expand your ISR with code for waiting till low level signal has been gone from pin P1.4 and then clear bit IE1 manualy -- you know, this bit is cleared with hardware only if IT1 set (for falling edge mode). Otherwise you will go to ISR again and again (dead interrupt loop).

    2. Same task may be done with timer 0 or 1 configured as counter of external events (on T0 or T1 pin). Read more about C/T bit of TMOD register. This way may be more useful - you will get only one interrupt at time when counter has been done.

    Good days!

Reply
  • Hmm, seems I miss something or ...?

    1. to configure INT1 interrupt to falling edge you should set IT1 bit of TCON then set EX1 and EA of IEN0.
    You do TCON = 0x40; <- it runs timer 1 as well as configured INT1 to low level triggered interrupt, that's wrong because your INT1 ISR does not support such mode of interrupt. You should:
    - either configure TCON for falling edge detector (by set bit IT1)
    - or expand your ISR with code for waiting till low level signal has been gone from pin P1.4 and then clear bit IE1 manualy -- you know, this bit is cleared with hardware only if IT1 set (for falling edge mode). Otherwise you will go to ISR again and again (dead interrupt loop).

    2. Same task may be done with timer 0 or 1 configured as counter of external events (on T0 or T1 pin). Read more about C/T bit of TMOD register. This way may be more useful - you will get only one interrupt at time when counter has been done.

    Good days!

Children
More questions in this forum