/**************************************************************************************************
---------------------------------------------------------------------------------------------------
	Copyright (c) 2004, Jonathan Bagg
	All rights reserved.

	 Redistribution and use in source and binary forms, with or without modification, are permitted 
	 provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of 
	  conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of 
	  conditions and the following disclaimer in the documentation and/or other materials provided 
	  with the distribution.
    * Neither the name of Jonathan Bagg nor the names of its contributors may be used to 
	  endorse or promote products derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------------------------------
   Project name : Infidigm AVR Drivers
   Processor	: ATMega64, ATMega128
   File name    : timer1.c
---------------------------------------------------------------------------------------------------
   Modifications: 

   Revision 1.0  2004/06/11 	Bagg
   - Cleaned up for release
---------------------------------------------------------------------------------------------------
   Created      : 20 January 2004                Author(s) : Jonathan Bagg
---------------------------------------------------------------------------------------------------
   Timer/Counter1 Peripheral Driver, Input Capture 1 and PWM for Motors 1,2,3
---------------------------------------------------------------------------------------------------
**************************************************************************************************/

#include <avr/io.h> 
#include <avr/signal.h>
#include "timer1.h"

/**************************************************************************************************
*   INTERNAL GLOBAL VARIABLES
**************************************************************************************************/
static unsigned char icp1flag;
static unsigned int icp1temp;
static unsigned int icp1make;

/**************************************************************************************************
*   startTC1(); - See 'timer1.h' Header file for Description
**************************************************************************************************/
void startTC1(void)
{
 TCCR1A = _BV(COM1A1)|_BV(COM1B1)|_BV(COM1C1)|_BV(WGM11)|_BV(WGM10);		//Fast 10 bit PWM
 TCCR1B = _BV(WGM12) | TC1PRE;			//changeable prescaler & PWM
 OCR1A = 0x0000;						//set 0 pwm
 OCR1B = 0x0000;
 OCR1C = 0x0000;
 DDRB|= _BV(PB7)|_BV(PB6)|_BV(PB5);	//set pwm pins as outputs 
 TIMSK|= _BV(TOIE1)|_BV(TICIE1);		//enable ICP & TOV IRQs
}							

/**************************************************************************************************
*   Timer/Counter1 Input Capture ISR - See 'timer1.h' Header file for Description
**************************************************************************************************/
SIGNAL(SIG_INPUT_CAPTURE1) 
{
 unsigned int pulse;

 pulse = ICR1/TC1DIV;
 if (icp1flag) 
	width1 = pulse - icp1temp;
 else {
 	width1 = pulse + icp1make;
	icp1make = pulse;
 }
 icp1temp = pulse;
  //Dirrection Bit
 if (PINA & _BV(PA6)) {
  	width1*= -1;
 	count1--;
 }
 else 
	count1++;
 icp1flag = 1; 
}

/**************************************************************************************************
*   Timer/Counter1 Overflow ISR - See 'timer1.h' Header file for Description
**************************************************************************************************/
SIGNAL(SIG_OVERFLOW1) 
{
 if (icp1make < 30719) {
 	if (icp1flag) 
		icp1make = (1024/TC1DIV) - icp1make;
 	else 
		icp1make+= (1024/TC1DIV); 
 }
 icp1flag = 0 ;
}

