/****************************************************************************/
/*****************************************************************************
 *                      
 *      Project:        16x training
 *      Name:           function f_clock
 *      Filename:       clock.c
 *      System:         IBM PC/AT
 *      Compiler:       C166 V3.11
 *      Date:           02/98
 *      Author:         Siegbert Schaufelberger
 *      Rights:         hitex-systementwicklung GmbH        
 *                      Greschbachstr. 12
 *                      76229  Karlsruhe
 *                                                         
 *****************************************************************************
 *
 *      Implementation description:
 *
 *             clock programm for the 16x Training           
 *             counting of clock                 
 *             floating point calculation            
 *                                                           
 *             Procedures :                                  
 *                     inc_clock                             
 *                     radius_diff                           
 *                     convert_clock                         
 *
 *****************************************************************************
 *
 *      Update:
 *        Date /Sgn.       Version  Changes made because of ...
 *      __-___-____/
 *
 ****************************************************************************/

/****************************************************************************/
/***					System includes			                          ***/
/****************************************************************************/

#pragma MOD167 FIX167
#include <intrins.h>
#include <string.h>
	/* #include <math.h>	*/


/****************************************************************************/
/***					Defines & Constants                               ***/
/****************************************************************************/

 #define pi  3.141592654

                    
/****************************************************************************/
/***					Externals					                      ***/
/****************************************************************************/


 /* reference in module isr */

	extern struct time 	{
		unsigned char millisec;
		unsigned char second;
		unsigned char minute;
		unsigned char hour;
						} clock;


/****************************************************************************/
/***					Publics						                      ***/
/****************************************************************************/



/****************************************************************************/
/***					Functions					                      ***/
/****************************************************************************/

/*----------------------------------------------------------------------------
 *      
 *  Name:  			inc_clock 
 *      
 *  Description:	Increment routine for clock occurence all 10 ms
 *                  						  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void inc_clock () 
{
  _nop_ (); 
	if (clock.millisec == 99) 
	{
		clock.millisec = 0;
		if (clock.second == 59)
		{
			clock.second = 0;
			if (clock.minute == 59) 
			{
				clock.minute = 0;
				if (clock.hour == 23) 
				{
					clock.hour = 0;
				}

				else
				{
					clock.hour++;
				}			
			}
			else 
			{
				clock.minute++;
			}
		}
		else 
		{
			clock.second++;
		}
	}
	else 
	{
		clock.millisec++;
	}			
}


/*----------------------------------------------------------------------------
 *      
 *  Name:  			radius_diff 
 *      
 *  Description:	Calculate the change of the radius of a circle, if the
 *                  circumference enlarged						  
 *      
 *  Parameters:     int radius, int add_circumference
 *                  
 *  Return Value:	int radius_diff  
 *                  
 *--------------------------------------------------------------------------*/

int radius_diff (int radius, int add_circumference)
{

    float circumference;
    float radius_new;
    int radius_diff;

    circumference  = 2 * pi * (float) radius; 
    circumference += (float) add_circumference;

    radius_new = circumference / (2 * pi);

    radius_diff = (int)radius_new - radius;
    return (radius_diff);
}

/*----------------------------------------------------------------------------
 *      
 *  Name:  			convert_clock 
 *      
 *  Description:	Convert function clock.second is used as input for
 *					floatingpoint calculations  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void convert_clock ()
{
	int radius;
	int add_circumference;
	int diff_radius;

	struct time x_clock;

	memcpy (& x_clock, & clock, sizeof(clock));

	radius = x_clock.second * 100;
	add_circumference = 50;
	diff_radius = radius_diff (radius, add_circumference);
	_nop_ (); 
}

/*----------------------------------------------------------------------------
 *      
 *  Name:  			f_clock 
 *      
 *  Description:	Procedure for increment of clock. It is called every
 *                  10 millisecond. The value of clock.second is used for a						  
 *      			floatingpoint calculation.
 *
 *  Parameters:     
 *                  
 *  Return Value:   
 *                  
 *--------------------------------------------------------------------------*/

void f_clock () 
{
	inc_clock ();
  	_nop_ (); 
	convert_clock ();
}
