⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ma_i2c.c

📁 NXP LPC系列AMR7的开发程序源码(LCD
💻 C
📖 第 1 页 / 共 2 页
字号:
    /*--- Handle user code on function entry ---*/
    ENTER_MA_MASTERPUTCHAR_I2C;
        
    /*--- Check if I2C Data register can be accessed ---*/
    if(         I2CONSET_bit.SI         )
    {
        /*--- Send data ---*/
        I2DAT = Data;
        I2CONCLR = 0x08;     /* Clear SI flag */ 
        ReturnCode = MA_OK;
    }
    else
    {
        /*--- Data register not ready ---*/
        ReturnCode = MA_BUSY;
    }

    /*--- Handle user code on function exit ---*/
    EXIT_MA_MASTERPUTCHAR_I2C;

    return ReturnCode;
    
} /* MA_MasterPutChar_I2C */





S8 MA_MasterGetChar_I2C( U8 Mode, U8 *pData ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Receives a character. I2C master mode used.
**      This function is also used to prepare if the master shall generate
**      acknowledge or not acknowledge.
**
**  Parameters:
**      Mode        = MA_I2C_ACK0   Set ACK=0. Slave sends next byte.
**                  = MA_I2C_ACK1   Set ACK=1. Slave sends last byte.
**                  = MA_I2C_READ   Read received data from data register.
**      pData       A pointer to where the received data shall be saved.
**
**  Returns:
**      MA_EMPTY    If no data available
**      MA_OK       Otherwise
**
**---------------------------------------------------------------------------
*/
{
    S8 ReturnCode;

    /*--- Handle user code on function entry ---*/
    ENTER_MA_MASTERGETCHAR_I2C;

    ReturnCode = MA_OK;

    if( Mode == MA_I2C_ACK0 )
    {
        /*--- The operation mode is changed from master transmit to master receive ---*/
        /*--- Set ACK=0 (informs slave to send next byte) ---*/
        I2CONSET_bit.AA = 1;       
        I2CONCLR = 0x08;     /* Clear SI flag */   
    }
    else if( Mode == MA_I2C_ACK1 )
    {
        /*--- Set ACK=1 (informs slave to send last byte) ---*/
        I2CONCLR = 0x04;     
        I2CONCLR = 0x08;     /* Clear SI flag */ 
    }
    else if( Mode == MA_I2C_READ )
    {            
        /*--- Check if I2C Data register can be accessed ---*/
        if(             I2CONSET_bit.SI             )
        {
            /*--- Read data ---*/
            *pData = (U8) I2DAT;
        }
        else
        {
            /*--- No data available ---*/
            ReturnCode = MA_EMPTY;
        }
    }

    /*--- Handle user code on function exit ---*/
    EXIT_MA_MASTERGETCHAR_I2C;

    return ReturnCode;
    
} /* MA_MasterGetChar_I2C */





S8 MA_MasterWrite_I2C( U8 *pTxData, U16 Length ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Sends the specified number of characters on the I2C network.
**
**      Note: After this function is run, you may need a bus free time 
**            before a new data transfer can be initiated.
**
**  Parameters:
**      pTxData     Pointer to the data to transmit
**      Length      The number of data bytes to transmit
**
**  Returns:
**      MA_ERROR    If an error occured
**      MA_OK       Otherwise
**
**---------------------------------------------------------------------------
*/
{
    S8 ReturnCode;
    U8 Status, Flag, ii;

    /*--- Handle user code on function entry ---*/
    ENTER_MA_MASTERWRITE_I2C;

	Info=0;
	StatusI2C=0;

    /*--- Generate Start condition ---*/
    ReturnCode = MA_Start_I2C();

    /*--- Transmit address ---*/
    if( ReturnCode == MA_OK )
    {
        /*--- Write SLA+W ---*/
        ReturnCode = MA_MasterPutChar_I2C( *pTxData );
        while( ReturnCode == MA_BUSY )
        {
            ReturnCode = MA_MasterPutChar_I2C( *pTxData );
        }
        pTxData++;
    }

    if( ReturnCode == MA_OK )
    {
        /*--- Wait until address transmitted and transmit data ---*/
        for( ii = 1; ii<Length; ii++ )
        {
            /*--- Wait until data transmitted ---*/
            Flag = TRUE;
            while( Flag )
            {
                /*--- Get new status ---*/
                Status = MA_CheckStatus_I2C();

                if( (Status == 0x18) || (Status == 0x28) )
                {
                    /*--- Data transmitted and ACK received ---*/
                    Flag = FALSE;

                    /*--- Write data ---*/
                    ReturnCode = MA_MasterPutChar_I2C( *pTxData );
                    while( ReturnCode == MA_BUSY )
                    {
                        ReturnCode = MA_MasterPutChar_I2C( *pTxData );
                    }
                    pTxData++;
                }
                else if( Status != 0xf8 )
                {
                    /*--- ERROR ---*/
                    Flag = FALSE;
                    ii = Length;
                    ReturnCode = MA_ERROR;
					Info|=1;
					StatusI2C=Status;
                }
            }
        }
    }

    /*--- Wait until data transmitted ---*/
    Flag = TRUE;
    while( Flag )
    {
        /*--- Get new status ---*/
        Status = MA_CheckStatus_I2C();

        if( (Status == 0x18) || (Status == 0x28) )
        {
            /*--- Data transmitted and ACK received ---*/
            Flag = FALSE;
        }
        else if( Status != 0xf8 )
        {
            /*--- ERROR ---*/
            Flag = FALSE;
            ii = Length;
			Info|=2;
            ReturnCode = MA_ERROR;
        }
    }

    /*--- Generate Stop condition ---*/
    MA_Stop_I2C();

    /*--- Handle user code on function exit ---*/
    EXIT_MA_MASTERWRITE_I2C;
  
    return ReturnCode;
    
} /* MA_MasterWrite_I2C */





S8 MA_MasterRead_I2C( U8 *pRxData, U16 Length ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Receives the specified number of characters on the I2C network.
**
**      Note1: Before using this function, the first byte in the receive
**      buffer must be loaded with slave address and direction bit (SLA+R).
**      The received data from the addressed slave will be saved in the 
**      buffer starting at the second byte.
**
**      Note2: After this function is run, you may need a bus free time 
**             before a new data transfer can be initiated.
**
**  Parameters:
**      pRxData     A pointer to the receive buffer
**      Length      The number of data bytes to receive
**
**  Returns:
**      MA_OK or I2C status code
**
**---------------------------------------------------------------------------
*/
{
    S8 ReturnCode;
    U8 Status, Flag, ii;

    /*--- Handle user code on function entry ---*/
    ENTER_MA_MASTERREAD_I2C;

    /*--- Generate Start condition ---*/
    ReturnCode = MA_Start_I2C();

    /*--- Transmit address ---*/
    if( ReturnCode == MA_OK )
    {
        /*--- Write SLA+R ---*/
        ReturnCode = MA_MasterPutChar_I2C( *pRxData );
        while( ReturnCode == MA_BUSY )
        {
            ReturnCode = MA_MasterPutChar_I2C( *pRxData );
        }
        pRxData++;
    }

    if( ReturnCode == MA_OK )
    {
        /*--- wait until address transmitted and receive data ---*/
        for( ii = 1; ii<= Length; ii++ )
        {
            /*--- wait until data transmitted ---*/
            Flag = TRUE;
            while( Flag )
            {
                /*--- Get new status ---*/
                Status = MA_CheckStatus_I2C();

                if(( Status == 0x40 ) || ( Status == 0x48 ) || ( Status == 0x50 ))
                {
                    /*--- Data received ---*/
                    Flag = FALSE;

                    if( ii == Length )
                    {
                        /*--- Set generate NACK ---*/
                        ReturnCode = MA_MasterGetChar_I2C( MA_I2C_ACK1, pRxData );
                    }
                    else
                    {
                        ReturnCode = MA_MasterGetChar_I2C( MA_I2C_ACK0, pRxData );
                    }

                    /*--- Read data ---*/
                    ReturnCode = MA_MasterGetChar_I2C( MA_I2C_READ, pRxData );
                    while( ReturnCode == MA_EMPTY )
                    {
               	        ReturnCode = MA_MasterGetChar_I2C( MA_I2C_READ, pRxData );
                    }
                    pRxData++;
                }
                else if( Status != 0xf8 )
                {
                    /*--- ERROR ---*/
                    Flag = FALSE;
                    ii = Length;
                    ReturnCode = MA_ERROR;
                }
            }
        }
    }

    /*--- Generate Stop condition ---*/
    MA_Stop_I2C();

    /*--- Handle user code on function exit ---*/
    EXIT_MA_MASTERREAD_I2C;

    return ReturnCode;
    
} /* MA_MasterRead_I2C */




U8 MA_CheckStatus_I2C( void ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Checks the I2C status.
**
**  Parameters:
**      None
**
**  Returns:
**      00h Bus error
**      08h START condition transmitted
**      10h Repeated START condition transmitted
**      18h SLA + W transmitted, ACK received
**      20h SLA + W transmitted, ACK not received
**      28h Data byte transmitted, ACK received
**      30h Data byte transmitted, ACK not received
**      38h Arbitration lost
**      40h SLA + R transmitted, ACK received
**      48h SLA + R transmitted, ACK not received
**      50h Data byte received in master mode, ACK transmitted
**      58h Data byte received in master mode, ACK not transmitted
**      60h SLA + W received, ACK transmitted
**      68h Arbitration lost, SLA + W received, ACK transmitted
**      70h General call address received, ACK transmitted
**      78h Arbitration lost, general call addr received, ACK transmitted
**      80h Data byte received with own SLA, ACK transmitted
**      88h Data byte received with own SLA, ACK not transmitted
**      90h Data byte received after general call, ACK transmitted
**      98h Data byte received after general call, ACK not transmitted
**      A0h STOP or repeated START condition received in slave mode
**      A8h SLA + R received, ACK transmitted
**      B0h Arbitration lost, SLA + R received, ACK transmitted
**      B8h Data byte transmitted in slave mode, ACK received
**      C0h Data byte transmitted in slave mode, ACK not received
**      C8h Last byte transmitted in slave mode, ACK received
**      F8h No relevant status information, SI=0
**      FFh Channel error
**
**---------------------------------------------------------------------------
*/
{
    U8 Status;

    /*--- Handle user code on function entry ---*/
    ENTER_MA_CHECKSTATUS_I2C;

    /*--- Wait for I2C Status changed ---*/
    while(            I2CONSET_bit.SI            == 0)
    {
        ;
    }
    
    /*--- Read I2C State ---*/
    Status = I2STAT;

    /*--- NOTE! SI flag is not cleared here ---*/

    /*--- Handle user code on function exit ---*/
    EXIT_MA_CHECKSTATUS_I2C;

    return Status;

} /* MA_CheckStatus_I2C */



/*
**===========================================================================
**  5.      INTERNAL FUNCTIONS (declared in Section 3.5)
**===========================================================================
*/

/*
**===========================================================================
** END OF FILE
**===========================================================================
*/ 



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -