/******************************************************************************
*                                                                             *
*              Copyright (C),  Philips Semiconductors SLH                     *
*                                                                             *
*  All rights are reserved. Reproduction in whole or in part is prohibited    *
*           without the written consent of the copyright owner.               *
*                                                                             *
*  Philips reserves the right to make changes without notice at any time.     *
* Philips makes no warranty, expressed, implied or statutory, including but   *
* not limited to any implied warranty of merchantibility or fitness for any   *
*   particular purpose, or that the use will not infringe any third party     *
* patent, copyright or trademark. Philips must not be liable for any loss or  *
*                      damage arising from its use.                           *
*                                                                             *
*******************************************************************************
*                                                                             *
*       Project: 336355/105 - Control SW Library for HTRC110/HTRC120/HITAG2   *
*                                                                             *
*       File: RC_BASE.C                                                       *
*       Author:  St. de Zeeuw                                                 *
*                                                                             *
*******************************************************************************
*      DATE      *          CHANGES DONE                        *   BY        *
*******************************************************************************
*                *                                              *             *
*  Dec 04,1997   * Start implementation                         *   de Zeeuw  *
*  Dec 10,1997   * Version 1.0, tested Version                  *   de Zeeuw  *
*  Apr 27,1998   * Version 2.0, tested Version                  *   de Zeeuw  *
*                *                                              *             *
*******************************************************************************/

#include "portab.h"
#include "reg51.h"
#include "htrc110.h"


#pragma regparms
 

void Send_HTRC110_Cmd (UBYTE RcCmd)
/******************************************************************************
*                                                                             *
* Description:                                                                *
* The function sends the command bitstream to the HTRC110. 3- and 8-bit       *
* commands are distinguished                                                  *
*                                                                             *
* Parameters:                                                                 *
* RcCmd: 3/8 bit compounded command to the HTRC110                            *
*                                                                             *
* Return: none                                                                *
*                                                                             *
******************************************************************************/
{
   UBYTE counter;

   DIN  = LOW;    /* Initialization of serial interface */
   SCLK  =LOW;
   SCLK = HIGH;
   DIN  = HIGH;
   SCLK  =LOW;

   if( (RcCmd & 0xC0) == 0xC0 ) counter = 3;	 /*  this is a 3-bit command */
   else                         counter = 8;	 /*... resp. a 8-bit command */
	
	do
	{
	    if( RcCmd & 0x80 )    /* decide whether 0 or 1 has to be sent*/
                   DIN=HIGH;
            else
                   DIN=LOW;
  
            SCLK = HIGH;            /* make this bit valid for the HTRC110*/
	    SCLK = LOW;
	    RcCmd  <<= 1;	    /* next bit */
	}
	while (--counter);
   DIN = LOW;
}



UBYTE Receive_HTRC110_Data( void )
/******************************************************************************
*                                                                             *
* Description:                                                                *
* Data transmitted from the HTRC110 are polled and returned.        		      *
*                                                                             *
* Parameters: none                                                            *
* Return: 																	                  *
* 8-bit response from the HTRC110 according to its specification.             *
*                                                                             *
******************************************************************************/
{
	UBYTE InputByte=0, counter=8;

	do	        	  /* poll DOUT, while SCLK is high */
	{
           InputByte <<= 1;
           SCLK = HIGH;                 /* activate DOUT*/
           if ( DOUT ) InputByte|=0x01; /* and store the information*/
           SCLK = LOW;
	}
	while (--counter);    /* has to be done 8 times */
	return InputByte;
}