title: 8051 c interrupts
body: <p>I'm using C to write a program on an 8051 microcontroller. The compiler I'm using is Keil Microvision. I'm stuck and having trouble figuring out what is missing from my code. I know it's very basic code I just can't figure out what I'm supposed to do.</p>
  
  <p>So pretty much what I am doing is taking sending a sentence out to the user and having them answer yes or no through the serial port and I used a serial interrupt. That part works fine. If I get a no from the person I want to generate a square wave 5kHz by a timer interrupt. I want this square wave to be controlled by an external interrupt turning it on and off when the external interrupt on pin P3.2 is either on or off. 
  Here is all my code</p>
  
  <pre><code>#include &lt;REG52.H&gt;
  #include &lt;stdio.h&gt;
  sbit WAVE = P1 ^ 7;
  #define BIT(x) (1 &lt;&lt; (x))
  
  void timer0() interrupt 1  // timer is controlling square wave timer 0
  {
    WAVE = ~WAVE;
  }
  
  void interrupt0() interrupt 0
  {
    IE ^= BIT(1);
  }
  
  void serial0() interrupt 4
  {
    unsigned char x;
    unsigned int i, z;
    unsigned char yes[] = " YES ";
    unsigned char no[] = " NO ";
    unsigned char nvalid[] = " NOT VALID TRY AGAIN ";
  
    while (RI == 1) {
  
      x = SBUF;
      RI = 0;
  
      if (z &lt; 1) {
        if (x == 'n') {
          for (i = 0; i &lt; 4; i++) {
            SBUF = no[i];
            while (TI == 0) ;  //wait for transmit
            TI = 0;
            z++;
          }
        }
      } else {
        return;
      }
  
      if (x == 'y') {
        for (i = 0; i &lt; 5; i++) {
          SBUF = yes[i];
          while (TI == 0) ;
          TI = 0;
        }
      } else if (x != 'n') {
        for (i = 0; i &lt; 21; i++) {
          SBUF = nvalid[i];
          while (TI == 0) ;
          TI = 0;
        }
      }
  
      TI = 0;
      return;
    }
  }
  
  void main()
  {
    TMOD = 0x20;
    TH1 = 0xF6;    //baud rate
    SCON = 0x50;
    TH0 = 0xA4;
    IE = 0x93;    //enable interrupts
    IP = 0x10;    // propriety to serial interrupt
    TR1 = 1;    //start timer 1
    TR0 = 1;    //clear timer 0
    TI = 1;
    printf("Hello, Are you okay? Press y for yes and n for no ");
    while (1) ;
  } 
  </code></pre>
  
  <p>The part I'm having trouble with is these two interrupt from the previous code</p>
  
  <pre><code>void timer0() interrupt 1 // timer is controlling square wave timer 0
  { 
      WAVE=~WAVE;
  }
  
  void interrupt0() interrupt 0
  {
    IE ^= BIT(1);
  } 
  </code></pre>
  
  <p>Any hints in the right direction would be greatly appreciated! Thanks. Sorry about formatting</p>