/********************************************************************/
/*  Kierroslukumittari                                              */
/*  13.10.1998  J.Koskinen Tietomyrsky Oy                           */
/********************************************************************/

#include <8051reg.h>
#include <8051bit.h>
#include <2051int.h>

#define TRUE  1
#define FALSE 0

register unsigned char  run, int0done;
register unsigned char  int0count, int0count0; 


/********************************************************************/
/* init-funktio, joka tekee kaikki tarvittavat alustustoimenpiteet  */
/********************************************************************/

#define TLOAD_H 0x3c		    // 65536 - 50000 =  15536   (=3cb0h)
#define TLOAD_L 0xb0
#define TICKS_PER_HALF_SECOND 10
int init()
{
  RCAP2H = TLOAD_H;		      // 65536 - 50000 =  15536  (=3cb0h) 
  RCAP2L = TLOAD_L;		      // T2 laskee 15536...65535 (50000 askelta)
  T2CON  = 0x04;		        // T2 16 bit autoreload, T2 käyntiin 
  setbit(IP.5)			        // set T2 interrupt to high priority 
  setbit(IE.5)			        // Salli Timer 2 keskeytys
  setbit(IE.0)			        // Salli external 0 keskeytys

  int0count0 = int0count = 0;
  int0done = FALSE;
  enable();			            // Kaikki keskeytykset ovat mahdollisia 
}



/********************************************************************/
/* Timer 2:n keskeytyspalveluohjelma                                */
/* Timer 2 on 16-bittisessä automaattilataustilassa, keskeytys      */
/* tapahtuu 50 ms:n välein. 10 * 50 ms = 0,5 s                      */
/********************************************************************/

static register unsigned char tick;

INTERRUPT(_TF2_) timerint2()
{
  clrbit(T2CON.7);                // TF2  keskeytyksen kuittausbitti

  if(++tick > TICKS_PER_HALF_SECOND)
  {
    tick = 0;

    int0count0 = int0count;
    int0count = 0;
    int0done = TRUE;
  }
}


/********************************************************************/
/* lisätään keskeytyslaskuria                                       */
/********************************************************************/

INTERRUPT(_IE0_) extint0()
{
  int0count++;			              /* update count of interrupts */
}


/********************************************************************/
/* pääohjelma                                                       */
/********************************************************************/

main()
{

  init();			                    // tee tarvittavat alustukset

  while(1) 
  {
    if (int0done == TRUE)         // päivitä näyttö
    {
      int0done = FALSE;

      P1 = 0xFF;                  // sammuta kaikki LEDit
      P3 = 0xFF;

      if (int0count0 > 117)
        clrbit(P3.0);
      if (int0count0 > 108)
        clrbit(P3.1);
      if (int0count0 > 100)
        clrbit(P3.3);
      if (int0count0 > 92)
        clrbit(P3.4);
      if (int0count0 > 83)
        clrbit(P3.5);
      if (int0count0 > 75)
        clrbit(P3.7);
      if (int0count0 > 67)
        clrbit(P1.0);
      if (int0count0 > 58)
        clrbit(P1.1);
      if (int0count0 > 50)
        clrbit(P1.2);
      if (int0count0 > 42)
        clrbit(P1.3);
      if (int0count0 > 33)
        clrbit(P1.4);
      if (int0count0 > 25)
        clrbit(P1.5);
      if (int0count0 > 17)
        clrbit(P1.6);
      if (int0count0 > 8)
        clrbit(P1.7);
    } 
    else 
    {
                                  // tehdään jotain muuta
    }
  }
}


