亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲蟲下載站

?? i2c.c

?? CortexM0上移植UCOS-II
?? C
字號:
/****************************************************************************
 *   $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[IIC_BUFSIZE];
volatile uint8_t I2CSlaveBuffer [IIC_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;
	}
	else
	{
	  LPC_I2C->DAT = I2CMasterBuffer[WrIndex++];
	}
	LPC_I2C->CONCLR = I2CONCLR_SIC;
	break;
	
	case 0x28:	/* Data byte has been transmitted, regardless ACK or NACK */
	if ( WrIndex < I2CWriteLength )
	{   
	  LPC_I2C->DAT = I2CMasterBuffer[WrIndex++]; /* this should be the last one */
	}
	else
	{
	  if ( I2CReadLength != 0 )
	  {
		LPC_I2C->CONSET = I2CONSET_STA;	/* Set Repeated-start flag */
	  }
	  else
	  {
		LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
		I2CMasterState = I2C_OK;
	  }
	}
	LPC_I2C->CONCLR = I2CONCLR_SIC;
	break;

	case 0x30:
	LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
	I2CMasterState = I2C_NACK_ON_DATA;
	LPC_I2C->CONCLR = I2CONCLR_SIC;
	break;
	
	case 0x40:	/* Master Receive, SLA_R has been sent */
	if ( (RdIndex + 1) < I2CReadLength )
	{
	  /* Will go to State 0x50 */
	  LPC_I2C->CONSET = I2CONSET_AA;	/* assert ACK after data is received */
	}
	else
	{
	  /* Will go to State 0x58 */
	  LPC_I2C->CONCLR = I2CONCLR_AAC;	/* assert NACK after data is received */
	}
	LPC_I2C->CONCLR = I2CONCLR_SIC;
	break;
	
	case 0x50:	/* Data byte has been received, regardless following ACK or NACK */
	I2CSlaveBuffer[RdIndex++] = LPC_I2C->DAT;
	if ( (RdIndex + 1) < I2CReadLength )
	{   
	  LPC_I2C->CONSET = I2CONSET_AA;	/* assert ACK after data is received */
	}
	else
	{
	  LPC_I2C->CONCLR = I2CONCLR_AAC;	/* assert NACK on last byte */
	}
	LPC_I2C->CONCLR = I2CONCLR_SIC;
	break;
	
	case 0x58:
	I2CSlaveBuffer[RdIndex++] = LPC_I2C->DAT;
	I2CMasterState = I2C_OK;
	LPC_I2C->CONSET = I2CONSET_STO;	/* Set Stop flag */ 
	LPC_I2C->CONCLR = I2CONCLR_SIC;	/* Clear SI flag */
	break;

	case 0x20:		/* regardless, it's a NACK */
	case 0x48:
	LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */
	I2CMasterState = I2C_NACK_ON_ADDRESS;
	LPC_I2C->CONCLR = I2CONCLR_SIC;
	break;
	
	case 0x38:		/* Arbitration lost, in this example, we don't
					deal with multiple master situation */
	default:
	I2CMasterState = I2C_ARBITRATION_LOST;
	LPC_I2C->CONCLR = I2CONCLR_SIC;	
	break;
  }
  return;
}

/*****************************************************************************
** Function name:		I2CStart
**
** Descriptions:		Create I2C start condition, a timeout
**				value is set if the I2C never gets started,
**				and timed out. It's a fatal error. 
**
** parameters:			None
** Returned value:		true or false, return false if timed out
** 
*****************************************************************************/
uint32_t I2CStart( void )
{
  uint32_t timeout = 0;
  uint32_t retVal = FALSE;
 
  /*--- Issue a start condition ---*/
  LPC_I2C->CONSET = I2CONSET_STA;	/* Set Start flag */
    
  /*--- Wait until START transmitted ---*/
  while( 1 )
  {
	if ( I2CMasterState == I2C_STARTED )
	{
	  retVal = TRUE;
	  break;	
	}
	if ( timeout >= MAX_TIMEOUT )
	{
	  retVal = FALSE;
	  break;
	}
	timeout++;
  }
  return( retVal );
}

/*****************************************************************************
** Function name:		I2CStop
**
** Descriptions:		Set the I2C stop condition, if the routine
**				never exit, it's a fatal bus error.
**
** parameters:			None
** Returned value:		true or never return
** 
*****************************************************************************/
uint32_t I2CStop( void )
{
  LPC_I2C->CONSET = I2CONSET_STO;      /* Set Stop flag */ 
  LPC_I2C->CONCLR = I2CONCLR_SIC;  /* Clear SI flag */ 
            
  /*--- Wait for STOP detected ---*/
  while( LPC_I2C->CONSET & I2CONSET_STO );
  return TRUE;
}

/*****************************************************************************
** Function name:		I2CInit
**
** Descriptions:		Initialize I2C controller
**
** parameters:			I2c mode is either MASTER or SLAVE
** Returned value:		true or false, return false if the I2C
**				interrupt handler was not installed correctly
** 
*****************************************************************************/
uint32_t I2CInit( uint32_t I2cMode ) 
{
  LPC_SYSCON->PRESETCTRL |= (0x1<<1);

  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<5);
  LPC_IOCON->PIO0_4 &= ~0x3F;	/*  I2C I/O config */
  LPC_IOCON->PIO0_4 |= 0x01;		/* I2C SCL */
  LPC_IOCON->PIO0_5 &= ~0x3F;	
  LPC_IOCON->PIO0_5 |= 0x01;		/* I2C SDA */
  /* IOCON may change in the next release, save change for future references. */
//  LPC_IOCON->PIO0_4 |= (0x1<<10);	/* open drain pins */
//  LPC_IOCON->PIO0_5 |= (0x1<<10);	/* open drain pins */

  /*--- Clear flags ---*/
  LPC_I2C->CONCLR = I2CONCLR_AAC | I2CONCLR_SIC | I2CONCLR_STAC | I2CONCLR_I2ENC;    

  /*--- Reset registers ---*/
#if FAST_MODE_PLUS
  LPC_IOCON->PIO0_4 |= (0x2<<8);
  LPC_IOCON->PIO0_5 |= (0x2<<8);
  LPC_I2C->SCLL   = I2SCLL_HS_SCLL;
  LPC_I2C->SCLH   = I2SCLH_HS_SCLH;
#else
  LPC_I2C->SCLL   = I2SCLL_SCLL;
  LPC_I2C->SCLH   = I2SCLH_SCLH;
#endif

  if ( I2cMode == I2CSLAVE )
  {
	LPC_I2C->ADR0 = CAT24C02_ADDR;
  }    

  /* Enable the I2C Interrupt */
  NVIC_EnableIRQ(I2C_IRQn);

  LPC_I2C->CONSET = I2CONSET_I2EN;
  return( TRUE );
}

/*****************************************************************************
** Function name:		I2CEngine
**
** Descriptions:		The routine to complete a I2C transaction
**				from start to stop. All the intermitten
**				steps are handled in the interrupt handler.
**				Before this routine is called, the read
**				length, write length, I2C master buffer,
**				and I2C command fields need to be filled.
**				see i2cmst.c for more details. 
**
** parameters:			None
** Returned value:		true or false, return false only if the
**				start condition can never be generated and
**				timed out. 
** 
*****************************************************************************/
uint32_t I2CEngine( void ) 
{
  RdIndex = 0;
  WrIndex = 0;

  /*--- Issue a start condition ---*/
  LPC_I2C->CONSET = I2CONSET_STA;	/* Set Start flag */

  I2CMasterState = I2C_BUSY;	

  while ( I2CMasterState == I2C_BUSY )
  {
	if ( timeout >= MAX_TIMEOUT )
	{
	  I2CMasterState = I2C_TIME_OUT;
	  break;
	}
	timeout++;
  }
  LPC_I2C->CONCLR = I2CONCLR_STAC;

  return ( I2CMasterState );
}
/* 進(jìn)入并啟動(dòng)I2CEngine之前,配置好所有的參數(shù)包括寫數(shù)據(jù)長度,讀數(shù)據(jù)長度
  I2C命令,初始化I2CMasterBuffer
  (1)如果只寫,I2CWriteLength為寫入數(shù)據(jù)字節(jié)數(shù),I2CReadLength讀長度為0,
	 所寫數(shù)據(jù)填入I2CMasterBuffer。
  (2)如果只讀,I2CReadLength為讀數(shù)據(jù)字節(jié)數(shù),I2CWriteLength為0。讀出
	 的數(shù)據(jù)填充至I2CSlaveBuffer。
  (3)如果既有讀又有寫,I2CWriteLength指定寫入數(shù)據(jù)長度,I2CReadLength
	 指定讀取數(shù)據(jù)長度。
	  */
/*****************************************************************
	                      IIC寫1個(gè)字節(jié)
功  能: 向IIC設(shè)備指定地址寫一個(gè)字節(jié)

參  數(shù): Dev_WR_Addr:設(shè)備地址 
         DataAddr   :數(shù)據(jù)起始地址	
         Pdata      :具體數(shù)據(jù)的指針
         Len        : 數(shù)據(jù)長度(字節(jié)為單位)

返回值: 無
*****************************************************************/
void I2C_WriteByte( uint8_t Dev_WR_Addr,uint8_t DataAddr,uint8_t Data ) 
{
	uint32_t j;
    I2CWriteLength = 3;	//1字節(jié)設(shè)備地址、1字節(jié)數(shù)據(jù)地址、1字節(jié)具體數(shù)值		  
    I2CReadLength  = 0;
    I2CMasterBuffer[0] = Dev_WR_Addr;  
    I2CMasterBuffer[1] = DataAddr;
	I2CMasterBuffer[2] = Data;	
    I2CEngine(); 
	for (j = 0; j < 0x20000; j++ ); 	   //寫延遲
} 

/*****************************************************************
	                    24C08寫一頁16個(gè)字節(jié)
功  能: 向IIC設(shè)備指定地址寫n個(gè)字節(jié)

參  數(shù): Pdata      :具體數(shù)據(jù)的指針
         Len        :數(shù)據(jù)長度(字節(jié)為單位)
		 DataAddr   :數(shù)據(jù)起始地址
         Dev_WR_Addr:設(shè)備地址  
		         	              
返回值: 無
*****************************************************************/
void I2C_Write( volatile uint8_t *Pdata,uint8_t Len,uint8_t DataAddr,uint8_t Dev_WR_Addr) 
{
    uint8_t i;
	uint32_t j;
    I2CWriteLength = Len+2;			  
    I2CReadLength  = 0;
    I2CMasterBuffer[0] = Dev_WR_Addr;  
    I2CMasterBuffer[1] = DataAddr;
	for(i=0;i<Len;i++)
	{		 		
        I2CMasterBuffer[i+2] = Pdata[i];  
	}	
    I2CEngine(); 
	for (j = 0; j < 0x20000; j++ ); 	   //寫延遲
} 

/*****************************************************************
	                      IIC讀1個(gè)字節(jié)
功  能: 在IIC設(shè)備內(nèi)部指定地址讀出一個(gè)字節(jié)

參  數(shù): Dev_WR_Addr:設(shè)備地址 
         DataAddr   :內(nèi)部數(shù)據(jù)地址

返回值: 無
*****************************************************************/
uint8_t I2C_ReadByte( uint8_t Dev_WR_Addr,uint8_t DataAddr ) 
{
    I2CWriteLength = 2;				  //1字節(jié)設(shè)備地址、1字節(jié)數(shù)據(jù)地址
    I2CReadLength  = 1;				  //讀1字節(jié)具體數(shù)值
    I2CMasterBuffer[0] = Dev_WR_Addr;
    I2CMasterBuffer[1] = DataAddr;				
    I2CMasterBuffer[2] = Dev_WR_Addr | RD_BIT;
    I2CEngine();
	return I2CSlaveBuffer[0];
}

/*****************************************************************
	                      IIC讀N個(gè)字節(jié)(N<=64)
功  能: 在IIC設(shè)備內(nèi)部指定地址讀出N個(gè)字節(jié)

參  數(shù): Pdata      :具體數(shù)據(jù)的指針
         Len        :數(shù)據(jù)長度(字節(jié)為單位)
		 DataAddr   :數(shù)據(jù)起始地址
         Dev_WR_Addr:設(shè)備地址

返回值: 無
*****************************************************************/
void I2C_Read( volatile uint8_t *Pdata,uint8_t Len,uint8_t DataAddr,uint8_t Dev_WR_Addr ) 
{
    uint32_t j;
    I2CWriteLength = 2;				 //1字節(jié)設(shè)備地址、1字節(jié)數(shù)據(jù)地址
    I2CReadLength  = Len;			 //讀Len字節(jié)具體數(shù)值
    I2CMasterBuffer[0] = Dev_WR_Addr;
    I2CMasterBuffer[1] = DataAddr;				
    I2CMasterBuffer[2] = Dev_WR_Addr | RD_BIT;
    I2CEngine();
	for (j = 0; j < 0x20000; j++ );  //	等待讀完成
}

/**
  * @}
  */ 

/**
  * @}
  */ 

/************* (C) COPYRIGHT 2010 Wuhan R&D Center, Embest *****文件結(jié)束*******/

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
日韩欧美三级在线| 成人av在线影院| 欧美日韩一本到| 亚洲国产一二三| 91搞黄在线观看| 日日夜夜一区二区| 日韩午夜激情av| 国产成人综合亚洲91猫咪| 国产日韩一级二级三级| 成人avav影音| 亚洲自拍欧美精品| 51精品秘密在线观看| 久久aⅴ国产欧美74aaa| 久久久国际精品| 99久久精品国产网站| 亚洲一二三级电影| 久久先锋资源网| 91网上在线视频| 日韩高清欧美激情| 久久精品亚洲乱码伦伦中文 | 中文字幕av一区二区三区高| 国产成+人+日韩+欧美+亚洲| 亚洲人吸女人奶水| 日韩一卡二卡三卡国产欧美| 国产99久久久精品| 亚洲地区一二三色| 久久久久久久久久电影| 91成人在线免费观看| 免费欧美在线视频| **性色生活片久久毛片| 7777精品伊人久久久大香线蕉的| 激情都市一区二区| 亚洲激情图片小说视频| 精品区一区二区| 91丨porny丨首页| 喷白浆一区二区| 亚洲免费在线视频一区 二区| 欧美精品免费视频| 99免费精品在线| 久久电影网站中文字幕| 夜夜嗨av一区二区三区四季av| 日韩美女天天操| 欧美私模裸体表演在线观看| 国产盗摄精品一区二区三区在线 | 7777女厕盗摄久久久| 不卡在线视频中文字幕| 免费观看久久久4p| 亚洲影视在线播放| 中国色在线观看另类| 日韩欧美黄色影院| 欧美日韩一区小说| 99精品久久只有精品| 国产麻豆日韩欧美久久| 视频一区二区中文字幕| 六月丁香婷婷色狠狠久久| 一二三区精品视频| 最新中文字幕一区二区三区| 欧美精品一区二区三区四区| 欧美精品自拍偷拍| 在线观看日韩电影| 99久久精品99国产精品| 国产美女精品人人做人人爽| 免费高清成人在线| 日本欧美大码aⅴ在线播放| 亚洲精品国产a久久久久久| 欧美国产1区2区| 久久久久久日产精品| 91精品婷婷国产综合久久竹菊| 色噜噜狠狠色综合中国| 99久久亚洲一区二区三区青草| 国产精品乡下勾搭老头1| 奇米影视一区二区三区| 污片在线观看一区二区| 亚洲一区二区免费视频| 樱花草国产18久久久久| 亚洲自拍都市欧美小说| 亚洲色大成网站www久久九九| 国产精品国产三级国产有无不卡| 国产亚洲视频系列| 国产日韩欧美精品电影三级在线 | 国产精品综合一区二区三区| 久久99久久精品欧美| 蜜桃视频在线一区| 九九精品视频在线看| 国产在线精品一区二区不卡了 | 国产精品色婷婷久久58| 国产亚洲女人久久久久毛片| 久久久久久久国产精品影院| 久久久久久久久岛国免费| 久久精品亚洲精品国产欧美kt∨ | 日韩精品一区二区三区老鸭窝 | 久久精品亚洲乱码伦伦中文| 国产日韩欧美电影| 亚洲三级免费电影| 一区二区免费看| 日韩电影在线观看一区| 久久99精品久久久| 成人av综合一区| 一本到高清视频免费精品| 欧美四级电影在线观看| 日韩精品在线一区| 国产精品的网站| 亚洲国产精品综合小说图片区| 肉色丝袜一区二区| 国产成人av一区二区三区在线 | 日本精品一区二区三区高清 | 99这里只有精品| 欧美日韩一级黄| 欧美精品一区二区三区蜜桃视频| 国产精品国产三级国产aⅴ原创 | 91麻豆精品国产91久久久久久久久 | 日韩欧美一区二区久久婷婷| 精品久久久久久综合日本欧美| 国产欧美中文在线| 亚洲午夜久久久久久久久电影网| 免费在线观看一区| caoporn国产一区二区| 欧美在线观看视频在线| 久久综合久久99| 亚洲欧美另类小说| 韩国女主播成人在线观看| 91在线视频播放地址| 日韩一区二区在线看片| 国产精品国产三级国产aⅴ入口 | 在线观看亚洲专区| 亚洲精品在线免费播放| 亚洲欧美色综合| 国产精品一区专区| 欧美另类videos死尸| 中文文精品字幕一区二区| 亚洲二区在线视频| 成人午夜精品在线| 欧美一级黄色大片| 亚洲蜜臀av乱码久久精品蜜桃| 久久99精品久久久久久动态图| 在线观看亚洲精品视频| 国产精品色婷婷久久58| 精品一区二区三区在线视频| 在线精品视频免费观看| 国产精品久久夜| 精品一区二区日韩| 在线电影欧美成精品| 亚洲视频你懂的| 成人深夜在线观看| 欧美成人乱码一区二区三区| 亚洲一区二区三区不卡国产欧美 | 亚洲欧洲成人av每日更新| 精品一区二区日韩| 日韩一级黄色片| 一区二区在线观看av| 成人小视频免费在线观看| 精品国产免费视频| 秋霞成人午夜伦在线观看| www精品美女久久久tv| 免费人成网站在线观看欧美高清| 欧美性高清videossexo| 亚洲美女免费视频| 成人高清视频免费观看| 亚洲国产高清不卡| 国产一区二区三区四| 欧美刺激午夜性久久久久久久| 视频一区在线视频| 欧美日韩国产高清一区二区| 亚洲女爱视频在线| 91在线视频播放| 日韩理论片中文av| 99视频超级精品| 亚洲色欲色欲www| av一二三不卡影片| 亚洲欧洲成人精品av97| 97久久精品人人做人人爽50路| 国产精品理论在线观看| 成人免费的视频| 中文字幕一区视频| 99v久久综合狠狠综合久久| 国产精品国产三级国产aⅴ无密码 国产精品国产三级国产aⅴ原创 | 欧美三级在线看| 亚洲高清中文字幕| 欧美精品一二三| 免费成人在线播放| 久久亚洲综合av| 国产成人免费视频一区| 国产精品久久久久影院色老大| 色综合久久综合| 亚洲国产一区二区三区| 日韩视频在线一区二区| 国产精华液一区二区三区| 国产精品美日韩| 色国产综合视频| 天天操天天综合网| 精品国产一区二区精华| 国产精品一区一区三区| 成人免费在线观看入口| 精品视频999| 国产在线精品免费| 国产精品久久毛片av大全日韩| 91官网在线免费观看| 久久国产精品色婷婷| 中文字幕一区二区不卡| 欧美日韩视频专区在线播放|