NXP LPC11xx I2C 驱动程序单片机解密
- * 单片机解密 $Id:: i2c.c 3662 2010-06-03 19:47:02Z usb00423 $
- * 单片机解密 Project: NXP LPC11xx I2C example
- *
- * Description:
- * This file contains I2C code example which include I2C initialization,
- * I2C interrupt handler, and APIs for I2C access.
- *
- ****************************************************************************
- * Software that is described herein is for illustrative purposes only
- * which provides customers with programming information regarding the
- * products. This software is supplied "AS IS" without any warranties.
- * NXP Semiconductors assumes no responsibility or liability for the
- * use of the software, conveys no license or title under any patent,
- * copyright, or mask work right to the product. NXP Semiconductors
- * reserves the right to make changes in the software without
- * notification. NXP Semiconductors also make no representation or
- * warranty that such application will be suitable for the specified
- * use without further testing or modification.
- ****************************************************************************/
- #include "LPC11xx.h" /* LPC11xx Peripheral Registers */
- #include "type.h"
- #include "i2c.h"
- volatile uint32_t I2CMasterState = I2C_IDLE;
- volatile uint32_t I2CSlaveState = I2C_IDLE;
- volatile uint32_t timeout = 0;
- volatile uint32_t I2CMode;
- volatile uint8_t I2CMasterBuffer[BUFSIZE];
- volatile uint8_t I2CSlaveBuffer[BUFSIZE];
- volatile uint32_t I2CCount = 0;
- volatile uint32_t I2CReadLength;
- volatile uint32_t I2CWriteLength;
- volatile uint32_t RdIndex = 0;
- volatile uint32_t WrIndex = 0;
- /*
- From device to device, the I2C communication protocol may vary,
- in the example below, the protocol uses repeated start to read data from or
- write to the device:
- For master read: the sequence is: STA,Addr(W),offset,RE-STA,Addr(r),data...STO
- for master write: the sequence is: STA,Addr(W),offset,RE-STA,Addr(w),data...STO
- Thus, in state 8, the address is always WRITE. in state 10, the address could
- be READ or WRITE depending on the I2C command.
- */
- /*****************************************************************************
- ** Function name: I2C_IRQHandler
- **
- ** Descriptions: I2C interrupt handler, deal with master mode only.
- **
- ** parameters: None
- ** Returned value: None
- **
- *****************************************************************************/
- void I2C_IRQHandler(void)
- {
- uint8_t StatValue;
- timeout = 0;
- /* this handler deals with master read and master write only */
- StatValue = LPC_I2C->STAT;
- switch ( StatValue )
- {
- case 0x08: /* A Start condition is issued. */
- WrIndex = 0;
- LPC_I2C->DAT = I2CMasterBuffer[WrIndex++];
- LPC_I2C->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
- break;
- case 0x10: /* A repeated started is issued */
- RdIndex = 0;
- /* Send SLA with R bit set, */
- LPC_I2C->DAT = I2CMasterBuffer[WrIndex++];
- LPC_I2C->CONCLR = (I2CONCLR_SIC | I2CONCLR_STAC);
- break;
- case 0x18: /* Regardless, it's a ACK */
- if ( I2CWriteLength == 1 )
- {
- LPC_I2C->CONSET = I2CONSET_STO; /* Set Stop flag */
- I2CMasterState = I2C_NO_DATA;
- }
上一篇:新唐M051源码单片机解密

芯片解密