/****************************************************************************/
/*****************************************************************************
 *                      
 *      Project:        16x training
 *      Name:           mod166 
 *      Filename:       mod166.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:
 *
 *
 *****************************************************************************
 *
 *      Update:
 *        Date /Sgn.       Version  Changes made because of ...
 *      __-___-____/
 *
 *
 ****************************************************************************/
/****************************************************************************/


/****************************************************************************/
/***					System includes			                          ***/
/****************************************************************************/
#pragma MOD167 FIX167
#include <reg167.h>
#include <intrins.h>

/****************************************************************************/
/***					Defines & Constants                               ***/
/****************************************************************************/


/****************************************************************************/
/***        			 MODULVARIABLES                                   ***/
/****************************************************************************/
extern unsigned int counter;


/****************************************************************************/
/***					Functions					                      ***/
/****************************************************************************/

/*----------------------------------------------------------------------------
 *      
 *  Name:           init_t0
 *      
 *  Description:    initializing of timer0
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void  init_t0 (void)
{
	unsigned int   t;

	t = T01CON;

	/*
	 * T0 counts at fosc/32.
	 * T0REL is set for a period of 50ms (fosc = 40 MHz).
	 */
	T0REL  = 0xbdb;		/*(int)(10000l - 0x10000l * 25000 / 26214);*/
	T01CON = (t & 0xff00) | 0x0041;	 /* Sets: timer,  52,43ms period, start */
	T0IC   = 0x00;     	/* Sets: interrupt disable, lowest priority */

	/*
	 * Initializes CAPCOM Register 0.
	 */
	CC0   = 0x6000;
	CC0IC = 0x78;		/* PECC0 */
	CCM0  = 0x0006;	/* Mode 2, only 1 interrupt pro period;	Timer 0	*/
}


/*----------------------------------------------------------------------------
 *      
 *  Name:           init_t1
 *      
 *  Description:    initializing of timer1
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void  init_t1 (void)
{
	unsigned int   t;

	t = T01CON;

	/*
	 * T1 counts at fosc/16.
	 * T1REL is set for a period of 10ms (fosc = 40 MHz).
	 */
	T1REL  = 0x9E57;		/*(int)(0xFFFF - (20MHz * 10ms / 8);*/
	T01CON = (t & 0x00ff) | 0x4000;  /* Timer, 10ms period, start */
	T1IC   = 0x4f;     	/* T1 Interrupt enable, high priority */
}


/*----------------------------------------------------------------------------
 *      
 *  Name:           init_t6
 *      
 *  Description:    initializing of timer6
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void init_t6 (void){

    	T6CON  |= 3;    /* Timer input selection 625khz = 1.6us period */
    	CAPREL  = 625;  /* 625 * 1.6 us = 1 ms interruptfrequency      */
    	T6SR    = 1;    /* Timer reload mode bit set = reload CAPREL   */
    	T6UD    = 1;    /* Timer up-down control set = count down      */
    	T6IC   |= 0x44; /* int timer 6 ena, int lvl 1                  */
    	T6R     = 1;    /* Start timer 6                               */
}

/*----------------------------------------------------------------------------
 *      
 *  Name:           init_port
 *      
 *  Description:    initializing of port2
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void init_port(void){
	DP2 = 0xffff;
}


/*----------------------------------------------------------------------------
 *      
 *  Name:           motor_off
 *      
 *  Description:    clear port3 to switch motor off
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void motor_off (void){

	P3 = 0;
	counter++;
} /* motor_off */


/*----------------------------------------------------------------------------
 *      
 *  Name:           led_on
 *      
 *  Description:   	set leds with pattern
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   void
 *                  
 *--------------------------------------------------------------------------*/

void led_on (unsigned int pattern){

	P2 = pattern;
	counter++;
} /* led_on */


/*----------------------------------------------------------------------------
 *      
 *  Name:           func_3
 *      
 *  Description:    range var3
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   unsigned int
 *                  
 *--------------------------------------------------------------------------*/

unsigned int func_3 (unsigned int var3){

	if (var3 > 1000){
	  var3 -= 1000;
	  return (var3);
	}
	return (0);
} /* func_3 */


/*----------------------------------------------------------------------------
 *           	
 *  Name:           func_4
 *      
 *  Description:    range var4
 *                  
 *      
 *  Parameters:     
 *                  
 *  Return Value:   unsigned int
 *                  
 *--------------------------------------------------------------------------*/

unsigned int func_4 (unsigned int var4){

	if (var4 > 1000){
	  var4 -= 1000;
	  return (var4);
	}
	return (0);
} /* func_4 */
