
#include <reg51.h>
#include <generic.h>//this contains the functions for delays(sec, mili sec and micro sec)
#include<serial.h>

/*=============================================================================
SET FREQUENCY : 50Hz
Each half of the AC cycle lasts 0.01 sec. and the timer counts 0000h to FFFFh in this time.
We will control this time and switch on and off pin 1.0 that is the output.
We will take the ext int 0 negative edges as the start point.
/*=============================================================================*/
char present_value_L = 0x00, present_value_H = 0x00,count = 0x00,count_send = 0x00;
/*==============EXTERNAL INTERRUPT 0 SUBROUTINE================================*/
/*
When there is the Negative edge at the P3^2 the control jumps to the ISR
This routine first stops the timer, then stores the present timer register values
using present_value_L and present_value_H respectively.
Then reset the timer register and starts the timer.
*/
void ex0_isr (void) interrupt 0
{
  TR0 = 0;			//here i stop the timer
  present_value_L = TL0;	//store the present timer lower byte
  present_value_H = TH0;	//store the present timer higher byte
  count_send = count;		//store the number of overflows
  count = 0x00;			//resets the number of overflows
  TL0 = 0x00;			//reset the timer register lower byte
  TH0 = 0x00;	                //reset the timer register higher byte
  TR0 = 1;
}
/*====================TIMER INTERRUPT 0 SUBROUTINE==============================*/
/*
When the timer overflows it stops the trigger. I here stop the timer as well
so that it does not keep on counting.
*/
void timer0_ISR (void) interrupt 1
{
count++;//counts the number of times the counter has overflown
}
/*=============================================================================*/
void initialize_global_interrupt(void)
{
IE = 0x83;//10000011 global enable, ext int 0 ,timer int 0 and timer int 1 (for baud rate) enabled
}
/*=============================================================================*/
void initialize_timer_0(void)
{
TR0 = 0;
TMOD = 0x01;  					//00000001Set Mode(16-bit timer with reload)
TL0 = 0x00;						//Loading the initial value, lower Byte
TH0 = 0x00;						//Loading the initial value, Higher Byte
}
/*=============================================================================*/
void initialize_external_int_0(void)
{					
IT0 = 1;						//making the external interrupt edge trigerred
}
/*=============================================================================
/*=============================================================================*/
void main (void)
{
//Here we start the initialization of interrupts
initialize_timer_0();
initialize_external_int_0();
initialize_serial();
initialize_global_interrupt();
//dummy values sent
while(1)
  {
  TR0 = 0;			   //here we stop the timer
  EX0 = 0;			   //disable the external interrupt
  msec_wait(1);		   //just a wait
  SBUF = present_value_H;  //send the higher byte serially
  msec_wait(1);
  SBUF = present_value_L;  //send lower byte serially
  msec_wait(1);
  SBUF = count_send;	   //send the overflow value serially
  msec_wait(1);
  EX0 = 1;				   //enable ext. interrupt
  TR0 = 1;				   //start the timer
  sec_wait(5);			   //wait for 5 sec
  }
}
/*====================================*/

