//----------------------------------------------------------------------------
// HACCOUNT                                                   19960306 CHG
// 
// 
//
// HACBUS:
//   Device type: 0x08             ; HACCOUNT type (same as HACPWR)
//   Address    : 0x01..0x0F       ; Address 1..15
//   Commands   : 0x00             ; Poll
//          Resp: 0x80 LSB MSB     ; Number of pulses since last poll
//
//
//
//----------------------------------------------------------------------------
#include <reg2051.h>
#include <stdio.h>
#include "..\HACNET\HACNET.H"

//----------------------------------------------------------------------------
// Constants...
//----------------------------------------------------------------------------
#define TRUE   1
#define FALSE  0

#define MyDeviceType 0x08 // My device type on HACNET...
#define RxSize 1+3        // Maximum size of RxBuffer

//----------------------------------------------------------------------------
// I/O definitions...
//----------------------------------------------------------------------------
#define LED P12
#define SignalA P32 // Signal from monitor
#define SignalB P33 // is connected
#define SignalC P34 // to all 4 inputs
#define SignalD P35 

//----------------------------------------------------------------------------
// Variables...
//----------------------------------------------------------------------------
unsigned char RxBuffer[RxSize];       // RxBuffer
extern bit RxAvail;                   // Message available in RX Buffer
extern bit Resend;                    // Resend last response
unsigned int CountValue=0; 

//----------------------------------------------------------------------------
// wait for a specific time in 10 mSec
// Based on a 11.059 MHz crystal
//----------------------------------------------------------------------------
void Delay(unsigned char t) {
  unsigned int i;
  while (t--) for(i=0;i<774; i++);
}

//----------------------------------------------------------------------------
// Timer 0 interrupt service function
//----------------------------------------------------------------------------
void timer0(void) interrupt 1 using 2 {
static unsigned char Count; 
  // Check if signal still active
  if (SignalA==0) {
    TH0  = 0xFC; // set timer period for timer 0
    TL0  = 0x66; // to 1 mSec
    Count++;
  // If signal dissappear, zero set everything and start looking for a new edge
  } else {
    Count= 0;   // Start from zero counts
    TR0  = 0;   // Stop timer
    IE0  = 0;   // Remove any pending int's on external int0
    EX0  = 1;   // External interrupt 0 enable
  }
  // If we have seen signal active for more than 10 msec, count it as a signal
  if (Count>10) {
    CountValue++; // We have a valid signal (t>10 mSec)
    Count= 0;   // Start from zero counts
    TR0  = 0;   // Stop timer
    IE0  = 0;   // Remove any pending int's on external int0
    EX0  = 1;   // External interrupt 0 enable
  }
}

//----------------------------------------------------------------------------
// Low detected on signal input, prepeare for a new measurement 
//----------------------------------------------------------------------------
void Int0(void) interrupt 0 using 2 {
  LED  = !LED; // Flip the LED so we can something happen
  TH0  = 0xFC; // set timer period for timer 0
  TL0  = 0x66; // to 1 mSec
  TR0  = 1;    // Start 1 mSec timer
  EX0  = 0;    // Disable external interrupt 0
  IE0  = 0;    // Remove any pending int's on external int0
}


//----------------------------------------------------------------------------
// Main part
//----------------------------------------------------------------------------
void main() {
  unsigned char Rsp[5];                       // Buffer for responses
  unsigned char MyAddress;                    // My Address on HACNET
  Delay(50);
 //----------------------------------------------------------------------------
 // Initalize HACNET Driver
 //----------------------------------------------------------------------------
  MyAddress=InitHACNET(MyDeviceType, RxSize);

 //---------------------------------
 // Initialize outputs...
 //---------------------------------

 //------------------------------
 // Configure onchip resources...
 //------------------------------
  EX0   = 1;    // External interrupt 0 enable
  IT0   = 1;    // External interrupt 0 Edge sensitive
  TMOD |= 0x01; // TMOD: timer 0, mode 1, 16 timer
  TH0   = 0xFC; // set timer period for timer 0
  TL0   = 0x66; // to 1 mSec
  ET0   = 1;    // Enable Timer 0 interrupts
  EA=1;         // Global int enable

  //----------------------------------------------------------------------------
  // Forever
  //----------------------------------------------------------------------------

  while(1) {
    //----------------------------------------------------------------------------
    // Diagnostic mode, Address is 0x00
    //----------------------------------------------------------------------------
    if (MyAddress==0) {
      Delay(20);
      LED=!LED;
    } else {
    //----------------------------------------------------------------------------
    // Normal mode
    //----------------------------------------------------------------------------
      // Check if we have to resend last response...
      if (Resend) {
        Resend=FALSE;
        HacTransmit(Rsp, 0xFF);
      }
      if (RxAvail) { // New telegram from our Master...
        switch (RxBuffer[0]) {
          case 0:
            Rsp[0]=0x80; // Response code
            if (CountValue) {
              EA=0; // Disable update of CountValue by INT
              Rsp[1]=0;
              Rsp[2]=0;
              Rsp[3]=CountValue;
              Rsp[4]=CountValue >> 8;
              CountValue=0;
              EA=1; // Enable update of CountValue by INT
              HacTransmit(Rsp, 5);
            } else
              HacTransmit(Rsp, 1);
            break;
        }
        RxAvail=0;
      }
    }
  }
}
