www.pudn.com > Bit1611_demo_code.rar > BITEK.C


/* ********************************************************************** 
 
         Copyright (c) 2002-2006 Beyond Innovation Technology Co., Ltd 
 
        All rights are reserved. Reproduction in whole or in parts is 
    prohibited without the prior written consent of the copyright owner. 
   ---------------------------------------------------------------------- 
 
    Module: BITEK.C - BITEKbus. 
 
    Purpose: Implementation of BITEK module. 
 
    Version: 0.01                                   07:27PM  2005/05/13 
 
    Compiler: Keil 8051 C Compiler v8.01 
 
    Reference: 
    [1] BIT1611B Datasheet Version 1.0, 2005-11-10, Beyond Innovation Technology 
 
   ---------------------------------------------------------------------- 
    Modification: 
 
    R0.01 07:27PM  2005/05/13 Jeffrey Chang 
    Reason: 
        1. Original. 
    Solution: 
 
   ********************************************************************** */ 
 
#define _BITEK_C_ 
 
 
/* ------------------------------------ 
    Header Files 
   ------------------------------------ */ 
#include  
#include "bitek.h" 
 
/* ------------------------------------ 
    Macro Definitions 
   ------------------------------------ */ 
#if (VP_IF_CFG == VP_IF_BITEK) 
 
    #define NOP_24  { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();} 
    #define NOP_20  { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } 
    #define NOP_18  { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } 
    #define NOP_15  { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();  } 
    #define NOP_12  { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();   } 
    #define NOP_10  { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_();                     } 
    #define NOP_3   { _nop_(); _nop_(); _nop_(); } 
    #define NOP_2   { _nop_(); _nop_(); } 
    #define NOP_1   { _nop_(); } 
    #define NOP_0   {  } 
 
    #define     LO_PULSE                NOP_1 
    #define     HI_PULSE                NOP_1 
 
 
    #define     BITEK_GET_SDAT_HIGH     (   BITEK_ioSDAT == HIGH    ) 
 
    #define     BITEK_SET_SCLK(HiLo)    {   BITEK_ioSCLK = (HiLo) ? HIGH : LOW; } 
    #define     BITEK_SET_SDAT(HiLo)    {   BITEK_ioSDAT = (HiLo) ? HIGH : LOW; } 
 
#endif 
 
/* ------------------------------------ 
    Type Definitions 
   ------------------------------------ */ 
 
 
/* ------------------------------------ 
    Variables Definitions 
   ------------------------------------ */ 
#if (VP_IF_CFG == VP_IF_BITEK) 
 
    sbit BITEK_ioSCLK   = P1 ^ 6; 
    sbit BITEK_ioSDAT   = P1 ^ 7; 
#endif 
 
static UB8 bData; 
 
/* ------------------------------------ 
    Function Prototypes 
   ------------------------------------ */ 
#if (VP_IF_CFG == VP_IF_BITEK) 
    static BOOL BITEK_GetACK(void); 
    static UB8  BITEK_RxData(void); 
    static void BITEK_Start(void); 
    static void BITEK_Stop(void); 
 
    #if (BITEK_SET_ACK) 
    static void BITEK_SetACK(void); 
    #endif 
 
    static void BITEK_TxData(UB8 bData); 
#endif 
 
 
#if (VP_IF_CFG == VP_IF_BITEK) 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_GetACK - 
    Purpose: . 
    Passed: None. 
    Returns: LOW if OK 
    Notes: 
    1) The acknowledge-related clock pulse is generated by the master. 
    2) The transmitter (Tx) release the SDA line (HIGH) during the 
       acknowledge clock pulse. 
    3) The receiver (Rx) must pull down the SDA line during the 
       acknowledge clock pulse so that it remains stable LOW during 
       the HIGH period of this clock pulse. 
  ------------------------------------------------------------------- */ 
BOOL BITEK_GetACK (void) 
{ 
    BOOL    fResult; 
 
 
    BITEK_SET_SDAT(HIGH); 
    HI_PULSE; 
 
    BITEK_SET_SCLK(HIGH); 
    HI_PULSE; 
 
    fResult = BITEK_GET_SDAT_HIGH; 
    HI_PULSE; 
 
    BITEK_SET_SCLK(LOW); 
    LO_PULSE; 
 
    return( fResult ); 
} /* BITEK_GetACK */ 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_Init - (VP_IF_BITEK) 
    Purpose: To initialize the BITEK module. 
    Passed: None. 
    Returns: None. 
    Notes: 
  ------------------------------------------------------------------- */ 
void BITEK_Init (void) 
{ 
    BITEK_SET_SDAT(HIGH); 
    BITEK_SET_SCLK(HIGH); 
} /* BITEK_Init */ 
 
 
#if (BITEK_RX_BURST) 
/* ------------------------------------------------------------------- 
    Name: BITEK_RxBurst - 
    Purpose: To receive bulk data from BITEK slave device. 
    Passed: 
        UB8     bSLA    = BITEK slave address 
        UW16    wREG    = BITEK register address 
        UB8     bCNT    = The number of data which will be received 
            bCNT: 1..255. (Excludes slave and register addresses) 
        UB8     *pbDATA = The pointer which points to the first DATA item 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_RxBurst ( 
UB8     bSLA,               /* BITEK slave address */ 
UW16    wREG,               /* BITEK register address */ 
UB8     bCNT,               /* The number of data which will be transmitted */ 
UB8     *pbDATA             /* The pointer which points to the first DATA item */ 
) 
{ 
    UB8     bIdx; 
 
 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData((wREG << 1) | 0x01); 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Read 
       -------------------------------- */ 
    for (bIdx = (bCNT - 1); bIdx; bIdx--) 
    { 
        *pbDATA++ = BITEK_RxData(); 
 
        BITEK_SetACK();                    
    } /* for */ 
 
    // Reading last one without ACK ! 
    *pbDATA = BITEK_RxData(); 
 
 
    BITEK_Stop(); 
} /* BITEK_RxBurst */ 
#endif 
 
 
#if (BITEK_RX_BYTE) 
/* ------------------------------------------------------------------- 
    Name: BITEK_RxByte - 
    Purpose: To receive one byte data from BiTEKbus slave device. 
    Passed: 
        UB8     bSLA    = BITEK slave address. 
        UW16    wREG    = BITEK register address. 
    Returns: One byte received data. 
    Notes: 
   ------------------------------------------------------------------- */ 
UB8 BITEK_RxByte ( 
UB8     bSLA,           /* BITEK slave address */ 
UW16    wREG            /* BITEK register address */ 
) 
{ 
    UB8    bDATA; 
 
 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData((wREG << 1) | 0x01);           // Read Operation ! 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Read Data 
       -------------------------------- */ 
    // Reading last one without ACK ! 
    bDATA = BITEK_RxData(); 
 
 
    BITEK_Stop(); 
 
    return( bDATA ); 
} /* BITEK_RxByte */ 
#endif 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_RxData - 
    Purpose: 
        To do BITEK parallel/serial conversion for reception. 
    Passed: None. 
    Returns: 
    Notes: 
  ------------------------------------------------------------------- */ 
UB8 BITEK_RxData (void) 
{ 
    UB8     bResult; 
    UB8     bMask; 
 
 
    bResult = 0x00; 
 
    BITEK_SET_SDAT(HIGH); 
 
 
    /* MSB is read first */ 
    for (bMask = 0x80; bMask; bMask >>= 1) 
    { 
        BITEK_SET_SCLK(HIGH); 
        HI_PULSE; 
 
        if ( BITEK_GET_SDAT_HIGH ) 
            bResult |= bMask; 
 
        BITEK_SET_SCLK(LOW); 
        LO_PULSE; 
    } /* for */ 
 
    return( bResult ); 
} /* BITEK_RxData */ 
 
 
 
#if (BITEK_RX_WORD) 
/* ------------------------------------------------------------------- 
    Name: BITEK_RxWord - 
    Purpose: To receive one WORD data from BITEK slave device. 
    Passed: 
        UB8     bSLA    = BITEK slave address 
        UW16    wREG    = BITEK register address 
    Returns: One word received data. 
    Notes: 
   ------------------------------------------------------------------- */ 
UW16 BITEK_RxWord ( 
UB8     bSLA,               /* BITEK slave address */ 
UW16    wREG                /* BITEK register address */ 
) 
{ 
    UW16    wDATA; 
 
 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData((wREG << 1) | 0x01); 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Read Data 
       -------------------------------- */ 
    // LSB Data 
    wDATA = BITEK_RxData();                 /* Low byte */ 
    BITEK_SetACK(); 
 
    // Reading last one without ACK ! 
    wDATA = (BITEK_RxData() << 8) + wDATA; 
 
 
    BITEK_Stop(); 
 
    return( wDATA ); 
} /* BITEK_RxWord */ 
#endif 
 
 
#if (BITEK_SET_ACK) 
/* ------------------------------------------------------------------- 
    Name: BITEK_SetACK - 
    Purpose: . 
    Passed: None. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_SetACK (void) 
{ 
    BITEK_SET_SDAT(LOW); 
 
    BITEK_SET_SCLK(HIGH); 
    HI_PULSE; 
 
    BITEK_SET_SCLK(LOW); 
    LO_PULSE; 
 
} /* BITEK_SetACK */ 
#endif 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_Start - START condition (SDAT falling edge). 
    Purpose: . 
    Passed: None. 
    Returns: None. 
    Notes: 
    Reference: 
   ------------------------------------------------------------------- */ 
void BITEK_Start (void) 
{ 
 
    BITEK_SET_SDAT(HIGH); 
 
    BITEK_SET_SCLK(HIGH); 
 
    HI_PULSE; 
    HI_PULSE; 
    HI_PULSE; 
 
    BITEK_SET_SDAT(LOW); 
    LO_PULSE; 
    LO_PULSE; 
 
    BITEK_SET_SCLK(LOW); 
    LO_PULSE; 
} /* BITEK_Start */ 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_Stop - STOP condition (SDAT rising edge). 
    Purpose: . 
    Passed: None. 
    Returns: None. 
    Notes: 
    Reference: 
  ------------------------------------------------------------------- */ 
void BITEK_Stop (void) 
{ 
    BITEK_SET_SDAT(LOW); 
    LO_PULSE; 
 
    BITEK_SET_SCLK(HIGH); 
    HI_PULSE; 
    HI_PULSE; 
    HI_PULSE; 
 
    BITEK_SET_SDAT(HIGH); 
    HI_PULSE; 
    HI_PULSE; 
    HI_PULSE; 
} /* BITEK_Stop */ 
 
 
 
#if (BITEK_TX_BURST) 
/* ------------------------------------------------------------------- 
    Name: BITEK_TxBurst - 
    Purpose: To transmit bulk data to BiTEKbus slave device. 
    Passed: 
        UB8  bSLA       = BiTEKbus slave address 
        UW16 wREG       = BiTEKbus register address 
        UB8  bCNT       = The number of data which will be transmitted 
            excluding slave and register address (bCNT: 1..255). 
        UB8  *pbDATA    = The pointer which points to the first data item. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_TxBurst ( 
UB8  bSLA,          /* BITEK slave address */ 
UW16 wREG,          /* BITEK register address */ 
UB8  bCNT,          /* The number of data which will be transmitted */ 
UB8  *pbDATA        /* Point to the first DATA item */ 
) 
{ 
    UB8 bIdx; 
 
 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData(wREG << 1); 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Write Data 
       -------------------------------- */ 
    for (bIdx = bCNT; bIdx; bIdx--) 
    { 
        bData = *pbDATA++; 
        BITEK_TxData(bData); 
        BITEK_GetACK(); 
    } /* for */ 
 
 
    BITEK_Stop(); 
} /* BITEK_TxBurst */ 
#endif 
 
 
#if (BITEK_TX_BYTE) 
/* ------------------------------------------------------------------- 
    Name: BITEK_TxByte - 
    Purpose: To transmit one byte data to BiTEKbus slave device. 
    Passed: 
        UB8  bSLA   = BiTEKbus slave address. 
        UW16 wREG   = BiTEKbus register address. 
        UB8  bDATA  = One byte transmitted data. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_TxByte ( 
UB8  bSLA,          /* BITEK slave address */ 
UW16 wREG,          /* BITEK register address */ 
UB8  bDATA          /* DATA item */ 
) 
{ 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData(wREG << 1); 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Write Data 
       -------------------------------- */ 
    BITEK_TxData(bDATA); 
    BITEK_GetACK(); 
 
    BITEK_Stop(); 
} /* BITEK_TxByte */ 
#endif 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_TxData - 
    Purpose: To do BITEK parallel/serial conversion for transmission. 
    Passed: 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_TxData (UB8 bData) 
{ 
    UB8     bMask; 
 
    /* MSB is sent first */ 
    for (bMask = 0x80; bMask; bMask >>= 1) 
    { 
        BITEK_SET_SDAT(bData & bMask) 
        LO_PULSE; 
 
        BITEK_SET_SCLK(HIGH); 
        HI_PULSE; 
 
        BITEK_SET_SCLK(LOW); 
        LO_PULSE; 
    } 
} /* BITEK_TxData */ 
 
 
#if (BITEK_TX_REPEAT) 
/* ------------------------------------------------------------------- 
    Name: BITEK_TxRepeat - 
    Purpose: To transmit the same data to BiTEKbus slave device repeatly. 
    Passed: 
        UB8  bSLA   = BiTEKbus slave address. 
        UW16 wREG   = BiTEKbus register address. 
        UB8  bCNT   = The number of data which will be transmitted 
            excluding slave and register address (bCNT: 1..255). 
        UB8  bDATA  = The repeated data. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_TxRepeat ( 
UB8  bSLA,          /* BITEK slave address */ 
UW16 wREG,          /* BITEK register address */ 
UB8  bCNT,          /* The number of data which will be transmitted */ 
UB8  bDATA          /* The repeated DATA */ 
) 
{ 
    UB8 bIdx; 
 
 
    if (bCNT == 0) 
        return; 
 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData(wREG << 1); 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Write Data 
       -------------------------------- */ 
    for (bIdx = bCNT; bIdx; bIdx--) 
    { 
        BITEK_TxData(bDATA);            
        BITEK_GetACK();                 
    } /* for */ 
 
 
    BITEK_Stop(); 
} /* BITEK_TxRepeat */ 
#endif // BITEK_TX_REPEAT 
 
 
#if (BITEK_TX_WORD) 
/* ------------------------------------------------------------------- 
    Name: BITEK_TxWord - 
    Purpose: To transmit one word data to BiTEKbus slave device. 
    Passed: 
        UB8  bSLA   = BiTEKbus slave address. 
        UW16 wREG   = BiTEKbus register address. 
        UB8  bDATA  = One word transmitted data. 
    Returns: None. 
    Notes: To send LSB first and then MSB. 
   ------------------------------------------------------------------- */ 
void BITEK_TxWord ( 
UB8  bSLA,          /* BITEK slave address */ 
UW16 wREG,          /* BITEK register address */ 
UW16 wDATA          /* DATA item */ 
) 
{ 
    BITEK_Start(); 
 
    BITEK_TxData(bSLA); 
 
    BITEK_GetACK(); 
 
 
    // If Extension = 1 ! 
    if (bSLA & 0x01) 
    { 
        // MSB Slave Address [14:7] 
        BITEK_TxData(wREG >> 7); 
        BITEK_GetACK(); 
    } 
 
    // LSB Slave Address [6:0] 
    BITEK_TxData(wREG << 1); 
    BITEK_GetACK(); 
 
 
    /* -------------------------------- 
        Write Data 
       -------------------------------- */ 
    // LSB Data 
    BITEK_TxData(wDATA);            /* Low Byte */ 
    BITEK_GetACK(); 
 
    // MSB Data 
    BITEK_TxData(wDATA >> 8);       /* High Byte */ 
    BITEK_GetACK(); 
 
    BITEK_Stop(); 
} /* BITEK_TxWord */ 
#endif 
 
 
#else 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_Init - 
    Purpose: To initialize the BITEK module via I2C. 
    Passed: None. 
    Returns: None. 
    Notes: 
  ------------------------------------------------------------------- */ 
void BITEK_Init (void) 
{ 
} /* BITEK_Init */ 
 
 
/* ------------------------------------------------------------------- 
    Name: BITEK_TxRepeat - 
    Purpose: To transmit the same data to BiTEKbus slave device repeatly via I2C. 
    Passed: 
        UB8  bSLA   = BiTEKbus slave address. 
        UW16 wREG   = BiTEKbus register address. 
        UB8  bCNT   = The number of data which will be transmitted 
            excluding slave and register address (bCNT: 1..255). 
        UB8  bDATA  = The repeated data. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
void BITEK_TxRepeat ( 
UB8  bSLA,          /* BITEK slave address */ 
UW16 wREG,          /* BITEK register address */ 
UB8  bCNT,          /* The number of data which will be transmitted */ 
UB8  bDATA          /* The repeated DATA */ 
) 
{ 
    UB8 bIdx; 
 
    if (bCNT == 0) 
        return; 
 
    I2C_Start(); 
 
    I2C_TxData(bSLA | (((wREG) >> 7) & 0x1E)); 
 
    I2C_GetACK(); 
 
 
    I2C_TxData(wREG); 
    I2C_GetACK(); 
 
 
    /* -------------------------------- 
        Write Data 
       -------------------------------- */ 
    for (bIdx = bCNT; bIdx; bIdx--) 
    { 
        I2C_TxData(bDATA);           
        I2C_GetACK();                
    } /* for */ 
 
 
    I2C_Stop(); 
} /* BITEK_TxRepeat */ 
 
#endif 
 
/* ------------------------------------------------------------------- 
    Name:  - 
    Purpose: . 
    Passed: None. 
    Returns: None. 
    Notes: 
   ------------------------------------------------------------------- */ 
 
 
/* ********************************************************************** 
 
    Description: 
 
 
   ********************************************************************** */ 
 
/* %% End Of File %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */