I have the following code which is supposed to count the number of interrupts on pin P3.2 (external interrupt) and display it on UART. But this code is doing exactly opposite of it i.e. it's running when the interrupt doesn't occur (count gets incremented when there is no interrupt and count stops when there is an interrupt). Any advice on it on where I'm going wrong ? I'm using P89LV52RD2 micro controller. Thanks in advance!

#include&lt;reg51.h&gt;
#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

/* function prototypes */
void delay(unsigned int ms);
void putc1( chr);
void windspeed_read( void);
void initialize(void);
void record_wind(unsigned char c);
void puts1(char* p);
void send_data();


// global variables declared
unsigned char buf[1]={0},*ptr;
int c=0,i=0; 



/* delay function to create a delay of 1 sec */
void delay(const unsigned int ms)
{
 unsigned int x;
 unsigned int y;
 for (x = 0; x &lt; ms; x++)
 {
 for (y = 0; y &lt;= 113; y++)
 ;
 }
}



void puts1(char* p)
{
 char *temp = p;          /*temp pointer so that the actual pointer is not displaced */
 while(*temp != 0x00)
 {
  putc1(*temp);  
  temp++;
 } 
}



/* function to detect the wind speed */
void windspeed_read( void ) interrupt 0
{
EA=0;
c = c+1;
sprintf(buf,"%d",c);
EA=1;
}



/*init the TIMER and etc*/
void initialize()
{
 SCON  = 0x50;   /*SCON: mode 1, 8-bit UART, enable receive      */ 
 TMOD |= 0x20;   /*TMOD: timer 1, mode 2, 8-bit                  */
 TH1   = 0xFD;   /*TH1:  for 9600 baud                           */
 TR1   = 1;      /*TR1:  timer 1 run                             */ 
}



/* to print the character in UART and serial window*/
void putc1(chr)
{
    initialize();
  SBUF = chr;
  while(TI==0);            /*Wait until the character is completely sent */
  TI=0;                   /*Reset the flag */
}



/* main function */
void main()
{
initialize();
IE = 0x81;
EX0 = 1;
while(1)
{
send_data();
delay(100000);
}
}

void send_data()
{

sprintf(buf,"%d",c);
ptr = buf;
P2 = buf[0];
puts1(ptr);
}


Here is the link for the data sheet of microcontroller I'm using.


Question: Simple static integer stack
Answer: First of all, your code has a bug: You did not follow the rule of three.
Code such as myStack1 = myStack2; will cause the pointer to be deleted twice - which is undefined behavior.



int storedElements;


This seems kind of misleading. This variable doesn't hold the stored elements. It holds the number of stored elements. Perhaps storedElementsCount or something like that would be better?



myStaticIntStack( int aNumber );


Why did you call the variable aNumber here? aNumber is a terrible name. It tells me essentially nothing. Later you used stackSize which is a FAR better name. Why didn't you use it here too?

And, by the way, consider using size_t to store sizes and capacities instead of ints. This would apply to stackSize and storedElements.



myStaticIntStack::myStaticIntStack()
{
    this-&gt;stackSize = 1;
    this-&gt;elements = new int(0);
    this-&gt;storedElements = 0;
}


Seriously? The default behavior for the stack is to create a stack with maximum size 1? That is kind of... worthless. It's OK to allow this in the myStaticIntStack(int) constructor, but as a default it just seems odd.

Consider not even allowing a default constructor.



myStaticIntStack::myStaticIntStack( int stackSize )
{
    this-&gt;stackSize = stackSize;
    this-&gt;elements = new int[ stackSize ];
    this-&gt;storedElements = 0;
}


Can the stackSize be zero? Can it be negative? Consider adding an assertion.
By the way, if you make stackSize be a size_t, the negative case becomes impossible, since size_t is unsigned. But you'll still need to handle the "stackSize == 0" case.

A size of 0 might be acceptable. If that is the case, add a comment stating that and why it is so.

Consider declaring this constructor explicit.



myStaticIntStack::~myStaticIntStack()
{
    if( this-&gt;elements != NULL )
    {
        if( stackSize &gt; 1 )
            delete[] this-&gt;elements;
        else
            delete this-&gt;elements;
    }

}


Why do you need a NULL check? Are you expecting the destructor to be called with elements == NULL? If this merely defensive programming and this case is never supposed to happen, then leave it as an assertion.

Also, that whole delete vs delete[] is weird. What if I use the constructor myStaticIntStack(1)? Then you'll be deleteing something created with new[].

I'd change the code (from the constructors) so that new[] - not new - is always used and, therefore, delete[] is always the right thing to do in the destructor.



void myStaticIntStack::push( int newElement )
{
    if( this-&gt;storedElements == this-&gt;stackSize )
        cout &lt;&lt; "Stack is full, you must POP an element before PUSHing a new one!" &lt;&lt; endl;
    else
    {
        this-&gt;elements[ (this-&gt;stackSize - 1) - this-&gt;storedElements ] = newElement;
        this-&gt;storedElements++;
    }
}


This is improper error handling that violates the single responsibility principle.
Your function should either throw an exception, merely assert the condition or return an error code. Printing to the command line should be done elsewhere - probably outside this class.



this-&gt;elements[ (this-&gt;stackSize - 1) - this-&gt;storedElements ] = newElement;


I'm pretty sure you could simplify this. For a stackSize=16, the first position is at index 15. Why 15? Why not 0? That'd simplify the code to just elements[storedElements] = newElement;. Just remember to also fix the pop/peek code.

Just because the stack is Last-in-first-out doesn't mean you have to fill the last indexes in the internal array first. That's an implementation detail.




  it's confusing that pop() returns a value even if Stack is empty.


Then don't  return a value if the Stack is empty. Throw an exception or abort with an assertion. Problem solved. Next.
As I stated before, your couts are in the wrong place. If you fix that the "Pop return" problem might just naturally go away.

If you decide to keep it like that (return -1), then consider making -1 a named constant since magic numbers are bad.

return ERROR_STACK_IS_EMPTY;




int myStaticIntStack::peek()


Same thing about cout.
Also, peek() doesn't change anything, does it? Then consider making it const.

int myStaticIntStack::peek() const




Consider splitting your myStaticIntStack in two files: myStaticIntStack.hpp and myStaticIntStack.cpp.
Your main function would be in a third file.



Some extra thoughts:


I suspect the "better way" might be to just use a vector internally. This would painlessly solve your "rule of three" problem. Bonus points since it'd make it easier to "resize" the stack after being created if you ever wanted to add that feature.
Personally, I'd uppercase the first letter of the class name, but that's just personal preference. Nothing wrong with your particular style.
Consider adding a bool empty() function, that checks if the stack is empty.
Consider adding a bool full() function, that checks if the stack is full.
Those two functions above could make your error detection code easier to understand. If your assignment forbids adding extra functions, consider adding them as private functions.
Consider adding a int size() function, that returns storedElements.
Consider adding a int capacity() function, that returns stackSize
Normally, a class like this would be implemented as a template since it's useful to have stacks for more than just integers. Not sure if you've learned about templates yet.


If you follow QuentinUK's suggestion of removing the this-&gt;, beware. There is one case that might cause you trouble.
In the constructor, this-&gt;stackSize = stackSize; is correct, but stackSize = stackSize; would not be.
To work around this issue, you could use something like this:

/*optionally add "explicit" here*/ myStaticIntStack::myStaticIntStack( int stackSize ) :
    stackSize(stackSize)
{
    elements = new int[ stackSize ];
    storedElements = 0;
}


This is all I can think of right now. Hope this helps.