//********************************************************************
//*                                                                  *
//*  Project Name: Basic Hour Timer Controll                         *
//*  File Name: main.c                                               *
//*  Author: Raymond C. Moore III                                    *
//*  Date: Jan 7, 2005                                               *
//*  Version: 1.0                                                    *
//*  Microcontroller: ATMega8                                        *
//*  Purpose: To Turn on or off an external device according to a    *
//*           preset time.                                           *
//*                                                                  *
//*  Method: Using an external RTC 32768hz crystal setup with a      *
//*          prescaler so that it ticks once per second, update      *
//*          a 24 hour clock.  At each hour update, compare on       *
//*          and off times and set an I/O pin to the proper status.  *
//*                                                                  *
//*  Extras: Hour is output in binary on the Port-C pins 1 to 5.     *
//*              (Physical pins 24,25,26,27,28                       *
//*          Flashing heartbeat is on Port-C pin 0                   *
//*              (Physical pin 23)                                   *
//*          Port-D pin 0 set to input for reset button.             *
//*              (Physical pin 2)                                    *
//*          Port-D pin 1 set to input for hour increment.           *
//*              (Physical pin 3)                                    *
//*          Port-D pin 2 set to input for hour decrement.           *
//*              (Physical pin 4)                                    *
//*          32khz crystal goes across XTAL1 & XTAL2                 *
//*              (Physical pins 9 & 10)                              *
//*          Port-B pin 0 is turned on/off at the required times     *
//*              (Physical pin 14)                                   *
//*                                                                  *
//********************************************************************

#include "main.h"


int main(void)
{
	int	temp0,temp1;
	uint8_t d;

	for(temp0=0;temp0<0x0040;temp0++)		//delay, wait for external clock crystal to stabilize
	{
		for(temp1=0;temp1<0xFFFF;temp1++);
	}

	DDRB = 0x01;					//set port B pin 0 output
	PORTB = 0x00;					//make sure unit is turned off by default

	DDRC = 0xFF;					//set port C pins outputs
	PORTC = 0xFF;					//set port C outputs HIGH/on for diag display

	DDRD = 0x00;					//set port D pins as inputs
	PORTD = 0xFF;					//set port D pullup resistors on


	TIMSK &=~((1<<TOIE2)|(1<<OCIE2));		//Disable TC0 interrupt 
	ASSR |= (1<<AS2);				//set Timer/Counter0 to be asynchronous from the CPU clock
							//with a second external clock(32,768kHz)driving it. 
	TCNT2 = 0x00; 
	TCCR2 = 0x05;					//prescale the timer to be clock source / 128 to make it 
							//exactly 1 second for every overflow to occur 

	while(ASSR&0x07);				//Wait until TC0 is updated

	TIMSK |= (1<<TOIE2);				//set 8-bit Timer/Counter0 Overflow Interrupt Enable 
	sei(); 						//set the Global Interrupt Enable Bit 
	while(1)
	{

        for(temp0=0;temp0<0x0010;temp0++)               //little delay to make things not run so fast.
        {
                for(temp1=0;temp1<0xFFFF;temp1++);
        }

		d = PIND;				//read port D inputs
		if( (d & 1) == 0)			//PIND0 resets time to 00:00:00
		{
			t.hour = 0;
			t.minute = 0;
			t.second = 0;
		        PORTC = (t.hour * 0x02) | (t.second & 0x01);
		}

               if( (d & 2) == 0)			//PIND1 increments time by 1 hour
                {
                        t.hour++;
		        PORTC = (t.hour * 0x02) | (t.second & 0x01);
                }

               if( (d & 4) == 0)			//PIND2 decrements time by 1 hour
                {
                        t.hour--;
		        PORTC = (t.hour * 0x02) | (t.second & 0x01);
                }

	
		//while(ASSR&0x07);			//Wait until TC0 is updated 
	}
}



//interrupt [TIMER0_OVF_vect] void counter(void)	//overflow interrupt vector 
SIGNAL(SIG_OVERFLOW2)
{
 
	if (++t.second==60)				//keep track of time, date, month, and year 
	{ 
		t.second=0; 
		if (++t.minute==60) 
		{ 
			t.minute=0; 
			if (++t.hour==24) 
			{ 
				t.hour=0;
			}

		
			if (t.hour==turnon)		//turn on the external controll
			{
			PORTB = 0x01;			//turn on PortB Pin 0
			}

			if (t.hour==turnoff)		//turn off the external controll
			{
			PORTB = 0x00;			//turn off PortB Pin 0
			}
		}
	}
        PORTC = (t.hour * 0x02) | ((t.second & 0x01));

}




//      uint8_t b;

//      DDRA = 0xFF; //set port A pins as outputs
//      PORTA = 0xFF;  //set port A outputs HIGH

//      DDRB = 0x00; //set port B pins as inputs
//      PORTB = 0xFF; //set port B pullup resistors on

//              for( ; ; )        //loop forever
//              {
//              b = PINB; //read port B inputs
//              PORTA = b; //set port A to what port B shows
//              }


