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

📄 ma_i2c.c

📁 NXP LPC系列AMR7的开发程序源码(LCD
💻 C
📖 第 1 页 / 共 2 页
字号:

/*
*****************************************************************************
**
**      Project     : My project
**
**      Component   : LPC2106 (LPC2106)
**
**      Modulename  : I2C
**
**      Filename    : ma_i2c.c
**
**      Abstract    : This file implements a device driver for the I2C 
**                    module.
**
**      Compiler    : IAR C compiler
**
**      Date        : 2004-05-26 17:21:26
**
**      License no. : 9503-663-863-6224     Ivan
**
**      Warning     : This file has been automatically generated.
**                    Do not edit this file if you intend to regenerate it.
**
**      This device driver was created by IAR MakeApp version 
**      4.02A (NXP LPC210x: 4.00C) for the NXP LPC210x series of
**      microcontrollers.
**
**      (c)Copyright 2004 IAR Systems.
**      Your rights to this file are explained in the IAR MakeApp 
**      License Agreement. All other rights reserved.
**
*****************************************************************************
*/

/*
**===========================================================================
**  1       GENERAL
**  1.1     Revisions
**
**  Please read the IAR MakeApp for NXP LPC210x readme file 
**  
**
**===========================================================================
*/

/*
**===========================================================================
**  1.2     References
** 
**  No   Identification          Name or Description
**  ==   ===================     ================================
**
**  1    02/Oct/2003             NXP LPC210x Hardware Manual
** 
**===========================================================================
*/

/*
**===========================================================================
**  2.      INCLUDE FILES
**  2.1     Standard include files
**===========================================================================
*/

/*
**===========================================================================
**  2.2     Application include files
**===========================================================================
*/

#include "usercode.h"   /* Usercode macros (see <template.h>) */
#include "ma_tgt.h"     /* Target specific header file */
#include "ma_sfr.h"     /* Special function register bitfield macros */
#include "NXP/iolpc210x.h"  /* Defines Special function registers */

#include "ma_i2c.h"     /* Module driver header file */

/*
**===========================================================================
**  3.      DECLARATIONS
**  3.1     Internal constants
**===========================================================================
*/

#define MA_I2CONSET_I2C        0x00000040  /* I2C Control Set Register */
#define MA_I2CONSET_I2C_MASK   0x0000007C  /* Used bits */
#define MA_I2DAT_I2C           0x00000000  /* I2C Data Register */
#define MA_I2DAT_I2C_MASK      0x000000FF  /* Used bits */
#define MA_I2ADR_I2C           0x00000000  /* I2C Slave Address Register */
#define MA_I2ADR_I2C_MASK      0x000000FF  /* Used bits */
#define MA_I2SCLH_I2C          0x00000004  /* I2C SCL Duty Cycle High Register */
#define MA_I2SCLH_I2C_MASK     0x0000FFFF  /* Used bits */
#define MA_I2SCLL_I2C          0x00000004  /* I2C SCL Duty Cycle Low Register */
#define MA_I2SCLL_I2C_MASK     0x0000FFFF  /* Used bits */

// Ivan
char Info;
char StatusI2C;
// Ivan end
/*
**===========================================================================
**  3.2     Internal macros
**===========================================================================
*/

/*
**===========================================================================
**  3.3     Internal type definitions
**===========================================================================
*/

/*
**===========================================================================
**  3.4     Global variables (declared as 'extern' in some header file)
**===========================================================================
*/

/*
**===========================================================================
**  3.5     Internal function prototypes (defined in Section 5)
**===========================================================================
*/

/*
**===========================================================================
**  3.6     Internal variables
**===========================================================================
*/

/*
**===========================================================================
**  4.      GLOBAL FUNCTIONS (declared as 'extern' in some header file)
**===========================================================================
*/



void MA_Init_I2C( void ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**    Initialises the I2C module. Only sets those registers with values 
**    not equal to the power-on reset values. 
**
**  Parameters:
**      None
**
**  Returns:
**      None
**
**---------------------------------------------------------------------------
*/
{
    /*--- Handle user code on function entry ---*/
    ENTER_MA_INIT_I2C;

    /*--- Initialise registers ---*/
    I2CONSET = ( I2CONSET & ~MA_I2CONSET_I2C_MASK ) | MA_I2CONSET_I2C;

    /*--- Handle user code on function exit ---*/
    EXIT_MA_INIT_I2C;
    
} /* MA_Init_I2C */

    



void MA_Reset_I2C( void ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**    Resets the I2C module. Sets all registers.
**
**  Parameters:
**      None
**
**  Returns:
**      None
**
**---------------------------------------------------------------------------
*/
{
    /*--- Handle user code on function entry ---*/
    ENTER_MA_RESET_I2C;
    
    /*--- Clear flags ---*/
    I2CONCLR = 0x6c;    

    /*--- Reset registers ---*/
    I2SCLL   = ( I2SCLL & ~MA_I2SCLL_I2C_MASK ) | MA_I2SCLL_I2C;
    I2SCLH   = ( I2SCLH & ~MA_I2SCLH_I2C_MASK ) | MA_I2SCLH_I2C;
    I2ADR    = ( I2ADR & ~MA_I2ADR_I2C_MASK ) | MA_I2ADR_I2C;
    I2CONSET = ( I2CONSET & ~MA_I2CONSET_I2C_MASK ) | MA_I2CONSET_I2C;

    /*--- Handle user code on function exit ---*/
    EXIT_MA_RESET_I2C;
   
} /* MA_Reset_I2C */




S8 MA_Start_I2C( void ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Generates a start condition on I2C when bus is free. 
**      Master mode will also automatically be entered.
**
**      Note: After a stop condition, you may need a bus free time 
**            before you can generate a new start condition.
**
**  Parameters:
**      None
**
**  Returns:
**      MA_OK or I2C status code
**
**---------------------------------------------------------------------------
*/
{
    U8 Status, Flag;
    S8 ReturnCode;

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

    /*--- Issue a start condition ---*/
    I2CONSET_bit.STA = 1;      /* Set Start flag */ 

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

        if(( Status == 0x08 ) || ( Status == 0x10 ))
        {
            /*--- Start condition has been transmitted ---*/
            Flag = FALSE;
            ReturnCode = MA_OK;
        }
        else if( Status != 0xf8 )
        {
            /*--- ERROR ---*/
            Flag = FALSE;
            ReturnCode = (S8) Status;
        }
        else
        {
            /*--- Clear SI flag ---*/ 
            I2CONCLR = 0x08;    
        }
    }
    
    /*--- Clear Start flag ---*/ 
    I2CONCLR = 0x20;    

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

    return ReturnCode;
    
} /* MA_Start_I2C */




S8 MA_RepeatStart_I2C( void ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Generates a repeated start condition on the I2C bus. 
**      Master mode will also automatically be entered.
**
**  Parameters:
**      None
**
**  Returns:
**      MA_OK or I2C status code
**
**---------------------------------------------------------------------------
*/
{
    U8 Status, Flag;
    S8 ReturnCode;

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

    /*--- Issue a start condition ---*/
    I2CONSET_bit.STA = 1;      /* Set Start flag */ 
    I2CONCLR = 0x08;  /* Clear SI flag */     

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

        if(( Status == 0x08 ) || ( Status == 0x10 ))
        {
            /*--- Start condition has been transmitted ---*/
            Flag = FALSE;
            ReturnCode = MA_OK;
        }
        else if( Status != 0xf8 )
        {
            /*--- ERROR ---*/
            Flag = FALSE;
            ReturnCode = (S8) Status;
        }
        else
        {
            /*--- Clear SI flag ---*/ 
            I2CONCLR = 0x08;    
        }
    }
    
    /*--- Clear Start flag ---*/ 
    I2CONCLR = 0x20;    

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

    return ReturnCode;
    
} /* MA_RepeatStart_I2C */




S8 MA_Stop_I2C( void ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Generates a stop condition in master mode or recovers from an
**      error condition in slave mode.
**
**      Note: After this function is run, you may need a bus free time 
**            before you can generate a new start condition.
**
**  Parameters:
**      None
**
**  Returns:
**      MA_OK
**
**---------------------------------------------------------------------------
*/
{
    /*--- Handle user code on function entry ---*/
    ENTER_MA_STOP_I2C;

    I2CONSET_bit.STO = 1;      /* Set Stop flag */ 
    I2CONCLR = 0x08;  /* Clear SI flag */ 
            
    /*--- Wait for STOP detected ---*/
    while(            I2CONSET_bit.STO            == 1 )
    {
        /*--- do nothing ---*/
        ;
    }

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

    return MA_OK;
    
} /* MA_Stop_I2C */





S8 MA_MasterPutChar_I2C( U8 Data ) 
/*
**---------------------------------------------------------------------------
**
**  Abstract:
**      Sends a character on the I2C network.
**
**  Parameters:
**      Data        Data byte to send
**
**  Returns:
**      MA_BUSY     If previous byte not sent
**      MA_OK       Otherwise
**
**---------------------------------------------------------------------------
*/
{
    S8 ReturnCode;

⌨️ 快捷键说明

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