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

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

?? i2c.c

?? 基于QT60xx0的I2C接口開發(fā)
?? C
字號(hào):
/*============================================================================
    Project     QT60xx0 Example Code
    File        i2c.c
    Function    Driver code for QT60xx0 via I2C interface
    Requires    Silicon Labs C8051F310 target
    Originated	D Spokes

	Description	This driver bit-bangs the I2C master function.
				Port 0, bit 7 provides the SCL signal
				Port 0, bit 6 provides the SDA signal.
				Interface to the driver comprises three functions:
				  InitI2c() configures the processor I/O and initialises the driver variables
				  I2cWrite() executes a multi-byte write transfer to a slave
				  I2cRead() executes a multi-byte read transfer from a slave    
				All three functions return a single byte indicating driver status

    (c) Quantum Research Group
============================================================================*/
#include "C8051F310.H"
#include "common.h"
#include "i2c.h"

/* Ports */
sbit SCL		= P0 ^ 7;
sbit SDA		= P0 ^ 6;
sbit CHECK		= P1 ^ 0;

/* ------------ DEFINES --------------- */
/* I2c read/write flags */
#define I2C_WRITE_FLAG	0x00	/* bit 0 of slave-addres byte */
#define I2C_READ_FLAG	0x01

/* bus logic levels */
#define LO 	0
#define HI	1

/* Delay constants */
#define _5US	8

/* Retry times on NACK */
#define NACK_RETRY_MAX	5

/* ------------ LOCAL DATA ------------ */
xdata UINT8 I2cStatus;
xdata UINT8 RxBit;
xdata UINT8 RxByte;

/* ---------- MODULE FUNCTIONS  ------- */
void Delay ( UINT8 Time );
UINT8 SetScl( UINT8 State );
UINT8 SetSda( UINT8 State );
void FloatSda( void );
UINT8 I2cStart();
UINT8 I2cStop();
UINT8 I2cTxBit( UINT8 BitVal );
UINT8 I2cRxBit( void );
UINT8 I2cTxByte( UINT8 TxData);
UINT8 I2cRxByte( UINT8 AckState );


/*---------------- Function Header -------------------------------------------

	FUNCTION	InitI2c
	PURPOSE		Initialises I2c driver
	INPUT		None
	OUTPUT		I2c status byte indicating the idle-state of the bus
	
----------------------------------------------------------------------------*/
UINT8 InitI2c ( void )
{

	/* configure C8051F310 ports for I2C operation:
		SDA is connected to Port0, bit6
		SCL is connected to Port0, bit7
		both pins function in open-drain mode
	*/
	P0SKIP = 0xc0;
	P0MDOUT &= ~0x30;	/* set SDA and SCL = open-drain */
	
	I2cStatus = 0;		/* initialise driver status */
	
	/* set initial idle state for bus (SDA and SCL = 1) */
	return ( SetSda( HI ) || SetScl( HI ) );

}

/*---------------- Function Header -------------------------------------------

	FUNCTION	Delay
	PURPOSE		Programme delay in the microsecond range -
				Called to meet I2C bus timing requirements

----------------------------------------------------------------------------*/
void Delay ( UINT8 Time )
{
	UINT8 i;

	EA = 0;						/* interrupts disabled during delay */
	for (i = 0; i < Time; i++);
	EA = 1;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	SetScl
	PURPOSE		Sets I2C SCL line to the required level. Because the bus is a
				wire-OR configuration, it may take some time to establish a
				logic '1' due to slow risetime, clock stretching etc.
				An arbitrary timeout of 250us is allowed.
	INPUT		Required bus state
	OUTPUT		Flag indicating result: 1 = OK, 0 = bus line stuck.
	MODIFIES	I2cStatus
				 
----------------------------------------------------------------------------*/
UINT8 SetScl( UINT8 State )
{
//	UINT16 Timeout = 450;	/* approx 250us */
	UINT16 Timeout = 10000;	/* approx 250us */

	SCL = State;
	while ( (SCL != State) && --Timeout);

	if ( Timeout )
		return true;
	else
	{
		I2cStatus |= I2C_ERROR_SCL_STUCK;
		return false;
	}
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	SetSda
	PURPOSE		Sets I2C SDA line to the required level. Because the bus is a
				wire-OR configuration, it may take some time to establish a
				logic '1' due to slow risetime, clock stretching etc.
				An arbitrary timeout of 250us is allowed.
	INPUT		Required bus state
	OUTPUT		Flag indicating result: 1 = OK, 0 = bus line stuck.
	MODIFIES	I2cStatus
				 
----------------------------------------------------------------------------*/
UINT8 SetSda( UINT8 State )
{
//	UINT16 Timeout = 450;	/* approx 250us */
	UINT16 Timeout = 10000;	/* approx 250us */

	SDA = State;
	while ( (SDA != State) && --Timeout);

	if ( Timeout )
		return true;
	else
	{
		I2cStatus |= I2C_ERROR_SDA_STUCK;
		return false;
	}
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	FloatSda
	PURPOSE		Sets I2C SDA line to 'input mode'.
				Note: this function does not check the line is high because
				a slave may be legitimately driving SDA low.
				 
----------------------------------------------------------------------------*/
void FloatSda( void )
{
	SDA = HI;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cStart
	PURPOSE		Applies an appropriately timed START condition to the I2c bus.
	OUTPUT		Flag indicating result: 1 = OK, 0 = bus line stuck.
	MODIFIES	I2cStatus
				 
----------------------------------------------------------------------------*/
UINT8 I2cStart()
{
	if ( !SDA )		/* ensure SDA is high */
	{
		if ( !SetSda(HI) )
			return false;
	}

	if ( !SCL )		/* ensure SCL is high */
	{
		if ( !SetScl(HI) )
			return false;
	}

	Delay( _5US );			 /* Philips tSU:STA > 4.7us */

	SetSda(LO);
	Delay( _5US );			 /* Philips tHD:SDA > 4us */
	
	return ( SetScl(LO) );
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cStop
	PURPOSE		Applies an appropriately timed STOP condition to the I2c bus.
	OUTPUT		Flag indicating result: 1 = OK, 0 = bus line stuck.
	MODIFIES	I2cStatus
				 
----------------------------------------------------------------------------*/
UINT8 I2cStop()
{
	SetSda(LO); /* ensure SDA is low */

	if ( !SetScl(HI) )
		return false;
	Delay( _5US );			 /* Philips tSU:STO > 4us */
	
	return ( SetSda(HI) );
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cTxBit
	PURPOSE		Drives the specified data bit to the I2c bus.
	OUTPUT		Flag indicating result: 1 = OK, 0 = bus line stuck.
	MODIFIES	I2cStatus
				 
----------------------------------------------------------------------------*/
UINT8 I2cTxBit( UINT8 BitVal )
{
	if ( !SetSda(BitVal) )
		return false;
	Delay( _5US );			 /* Philips tLOW > 4.7us */

	if ( !SetScl(HI) )
		return false;
	Delay( _5US );			 /* Philips tHIGH > 4us */
	
	return ( SetScl(LO) );
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cRxBit
	PURPOSE		Receives a data bit from the I2c bus.
	OUTPUT		Flag indicating result: 1 = OK, 0 = bus line stuck.
				Data bit is saved to RxBit
	MODIFIES	I2cStatus
				 
----------------------------------------------------------------------------*/
UINT8 I2cRxBit( void )
{
	FloatSda();	/* float SDA - can't check state as slave maybe driving SDA */
	Delay( _5US );			 /* Philips tLOW > 4.7us */

	if ( !SetScl(HI) )
		return false;
	Delay( _5US );			 /* Philips tHIGH > 4us */
	RxBit = SDA;
	return ( SetScl(LO) );
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cTxByte
	PURPOSE		Sequences transmission of a data byte to the I2c bus.
	OUTPUT		Flag indicating result: 1 = slave acknowledged, 0 = no ack/fail.
				Data bit is saved to RxBit
	MODIFIES	I2cStatus: State of ACK bit and error flags
				 
----------------------------------------------------------------------------*/
UINT8 I2cTxByte( UINT8 TxData)
{
	UINT8 i;
	UINT8 t = TxData;

	for (i= 0;i < 8;i++)
	{
		if ( !I2cTxBit( (t & 0x80) ? 1 : 0 ) )
			return false;
		t <<= 1;
	}

	if ( I2cRxBit() )
	{
		if (RxBit)
			I2cStatus |= I2C_ERROR_NO_ACK;
		return ( !RxBit ); /* returns '1' if ACK recieved */ 
	}
	else
		return false;

}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cTxByte
	PURPOSE		Sequences reception of a data byte from the I2c bus.
	INPUT		Flag indicating state of ACK bit to be sent to the slave
	OUTPUT		Flag indicating result: 1 = OK, 0 = fail.
				Data byte is saved to RxByte
	MODIFIES	I2cStatus: State of error flags
				 
----------------------------------------------------------------------------*/
UINT8 I2cRxByte( UINT8 AckState )
{
	UINT8 i;
	UINT8 r = 0;

	for (i= 0;i < 8;i++)
	{
		if ( !I2cRxBit() )
			return false;
		r <<= 1;
		r |= RxBit;
	}

	RxByte = r;

	return ( I2cTxBit ( AckState ) );

}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cWrite
	PURPOSE		Sequences a write-cycle to a slave device on the I2c bus.
				send START
				send slave-adddress + write flag (check ACK)
				send write pointer (check ACK)
				read data bytes (with NACK at last byte) (check all ACKs)
	INPUT		I2cAddress:    specifies slave device 7-bit address
				DeviceAddress: specifies address pointer within the device
				Data:          Pointer to write-data (byte) array
				ByteCount:     specifies number of bytes to write 
	OUTPUT		I2cStatus byte indicating result of I2c cycle.
	MODIFIES	I2cStatus:     all flags
				 
----------------------------------------------------------------------------*/
UINT8 I2cWrite( UINT8 I2cAddress, UINT8 DeviceAddress, UINT8 *Data, UINT8 ByteCount)
{
	UINT8 i;
	UINT8 *WriteDataPtr;
	
CHECK = 1;
	i = 0;
	
	do {	/* attempt to address device up to NACK_RETRY_MAX times */
	
		I2cStatus = 0;

		if ( I2cStart() )
		{
			if ( !I2cTxByte( (I2cAddress * 2) + I2C_WRITE_FLAG ) ) 
				I2cStop();
		}
	} while ( (I2cStatus != 0) && (++i < NACK_RETRY_MAX) );
CHECK = 0;

	if ( i >= NACK_RETRY_MAX )
		return I2cStatus;
	

	if ( !I2cTxByte( DeviceAddress ) ) 
	{
		I2cStop();
		return I2cStatus;
	}

	WriteDataPtr = Data;
	for ( i = 0; i < ByteCount; i++)
	{
		if ( !I2cTxByte( *WriteDataPtr++ ) ) 
		{
			I2cStop();
			return I2cStatus;
		}
	}

	I2cStop();
	return I2cStatus;
}

/*---------------- Function Header -------------------------------------------

	FUNCTION	I2cRead
	PURPOSE		Sequences a read-cycle to a slave device on the I2c bus:
				send START
				send slave-adddress + write flag (check ACK)
				send read pointer (check ACK)
				send re-START
				send slave-adddress + read flag (check ACK)
				read data bytes (with NACK at last byte)
	INPUT		I2cAddress:    specifies slave device 7-bit address
				DeviceAddress: specifies address pointer within the device
				Data:          Pointer to return-data (byte) array
				ByteCount:     specifies number of bytes to read 
	OUTPUT		I2cStatus byte indicating result of I2c cycle.
	MODIFIES	I2cStatus:     all flags
				 
----------------------------------------------------------------------------*/
UINT8 I2cRead( UINT8 I2cAddress, UINT8 DeviceAddress, UINT8 *Data, UINT8 ByteCount)
{
	UINT8 i;
	UINT8 *ReadDataPtr;
	
	i = 0;
	do {	/* attempt to address device up to NACK_RETRY_MAX times */
	
		I2cStatus = 0;

		if ( I2cStart() )
		{
			if ( !I2cTxByte( (I2cAddress * 2) + I2C_WRITE_FLAG ) ) 
				I2cStop();
		}
	} while ( (I2cStatus != 0) && (++i < NACK_RETRY_MAX) );

	if ( i >= NACK_RETRY_MAX )
		return I2cStatus;


	if ( !I2cTxByte( DeviceAddress ) )
	{
		I2cStop();
		return I2cStatus;
	}

	i = 0;
	do {	/* attempt to address device up to NACK_RETRY_MAX times */
	
		I2cStatus = 0;

		if ( I2cStart() )
		{
			if ( !I2cTxByte( (I2cAddress * 2) + I2C_READ_FLAG ) ) 
				I2cStop();
		}
	} while ( (I2cStatus != 0) && (++i < NACK_RETRY_MAX) );

	if ( i >= NACK_RETRY_MAX )
		return I2cStatus;


	ReadDataPtr = Data;
	for ( i = 0; i < ByteCount; i++)
	{
		if ( I2cRxByte( (i == (ByteCount - 1) ? 1 : 0 ) ) )
			 *ReadDataPtr++ = RxByte;
		else
		{
			I2cStop();
			return I2cStatus;
		}
	}

	I2cStop();
	return I2cStatus;
}

?? 快捷鍵說明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲国产经典视频| 国产精品一二三| 亚洲高清久久久| 亚洲一区中文日韩| 伊人性伊人情综合网| 亚洲色欲色欲www| 亚洲色图清纯唯美| 亚洲精品写真福利| 亚洲图片有声小说| 午夜电影久久久| 日韩精品一二三| 99免费精品在线| 97se亚洲国产综合自在线| 99久久久精品免费观看国产蜜| 不卡大黄网站免费看| 成人性生交大片免费看视频在线 | 国产**成人网毛片九色| 国产自产高清不卡| 国产成人av在线影院| 成人av片在线观看| 91麻豆免费在线观看| 色八戒一区二区三区| 欧美亚洲国产bt| 欧美一区午夜视频在线观看| 日韩免费成人网| 亚洲精品一区二区三区四区高清| 久久影音资源网| 国产精品久久毛片| 一区二区三区影院| 香蕉乱码成人久久天堂爱免费| 日本色综合中文字幕| 极品少妇xxxx偷拍精品少妇| 国产91富婆露脸刺激对白| 99热精品国产| 在线不卡免费av| 久久久久久久综合| 亚洲精品国产无天堂网2021 | 在线免费观看日本一区| 成人免费视频视频在线观看免费| 国产主播一区二区三区| 日本美女视频一区二区| 免费人成黄页网站在线一区二区| 热久久国产精品| 国产成人亚洲综合色影视 | 久久女同精品一区二区| 久久色视频免费观看| 久久久久久久久久久久久夜| 久久婷婷久久一区二区三区| 日韩三级.com| 久久精品日韩一区二区三区| 中文字幕日本不卡| 一区二区三区中文字幕| 一区二区三区欧美| 国产精品一级在线| 99精品视频一区| 国产视频一区在线观看| 中文字幕巨乱亚洲| 久久青草欧美一区二区三区| 国产色综合一区| 亚洲男人天堂一区| proumb性欧美在线观看| 国产精品成人一区二区三区夜夜夜 | 波多野结衣在线aⅴ中文字幕不卡| 欧美色国产精品| 国产精品人妖ts系列视频| 五月天丁香久久| 91麻豆精品在线观看| 欧美精品一区二区三区蜜臀| 亚洲午夜影视影院在线观看| 成人在线视频首页| 精品日韩成人av| 性欧美大战久久久久久久久| 狠狠色丁香久久婷婷综| 欧美丝袜丝交足nylons| 精品毛片乱码1区2区3区| 亚洲欧美日韩国产手机在线| 视频在线观看一区二区三区| 色香蕉成人二区免费| 26uuu国产日韩综合| 艳妇臀荡乳欲伦亚洲一区| 麻豆精品一区二区综合av| 色综合网站在线| 久久综合成人精品亚洲另类欧美 | 精品久久人人做人人爽| 亚洲欧美一区二区三区极速播放| 麻豆精品一二三| 一本色道a无线码一区v| 国产欧美日本一区二区三区| 天天av天天翘天天综合网色鬼国产 | 91年精品国产| 久久久久久久电影| 亚洲国产精品自拍| 国产精品1区2区| 91精品国产一区二区| 一区二区三区在线视频观看| 国产成人精品影院| 欧美一二三四区在线| 亚洲综合免费观看高清完整版在线| 黑人巨大精品欧美一区| 在线播放91灌醉迷j高跟美女 | 亚洲精品免费视频| 久久精品国产免费看久久精品| 99久久婷婷国产| 国产午夜精品美女毛片视频| 蜜桃视频第一区免费观看| 欧美性欧美巨大黑白大战| 国产精品久久久久久久久快鸭 | 精品一区二区三区免费毛片爱| 欧美色综合天天久久综合精品| 国产精品国产三级国产普通话99 | 欧美日韩精品系列| 亚洲精品成人精品456| 北条麻妃一区二区三区| 久久久电影一区二区三区| 激情图区综合网| 国产欧美视频一区二区三区| 国产高清不卡一区二区| 久久免费看少妇高潮| 国产制服丝袜一区| 亚洲精品一区二区三区蜜桃下载 | 亚洲国产日韩在线一区模特| 国产aⅴ综合色| 欧美国产精品中文字幕| 国产乱人伦精品一区二区在线观看| 欧美一区二区二区| 久久99国产精品免费网站| 精品三级在线观看| 精品一区二区三区蜜桃| www久久精品| 国产一区二区三区精品视频| 欧美国产日韩亚洲一区| 成人av电影在线| 成人免费在线播放视频| 日本道免费精品一区二区三区| 亚洲精品综合在线| 欧美日韩中文另类| 肉肉av福利一精品导航| 国产亚洲综合性久久久影院| 国产成人在线观看免费网站| 国产精品传媒在线| 欧美在线你懂得| 天堂一区二区在线| 欧美一二区视频| av在线一区二区| 亚洲在线视频网站| 日韩午夜激情视频| 高清国产午夜精品久久久久久| 成人免费在线视频| 精品视频在线视频| 国产成人99久久亚洲综合精品| 中文字幕在线播放不卡一区| 91色|porny| 婷婷综合五月天| 欧美精品一区二区在线观看| 成人手机在线视频| 亚洲黄色av一区| 久久欧美中文字幕| 91福利资源站| 免费观看在线色综合| 久久久久久久久久久电影| 91一区在线观看| 麻豆精品久久精品色综合| 欧美激情一区不卡| 欧美亚洲丝袜传媒另类| 久久精品国产99国产精品| 国产精品色一区二区三区| 欧美中文字幕亚洲一区二区va在线 | 亚洲免费色视频| 91精品婷婷国产综合久久| 99精品国产视频| 青青草伊人久久| 亚洲天堂成人在线观看| 欧美一级淫片007| www.av亚洲| 久久国产麻豆精品| 亚洲色欲色欲www| 欧美精品一区二| 欧美视频精品在线观看| 老司机精品视频在线| 亚洲第一成人在线| 亚洲国产经典视频| 欧美一区二区成人6969| 97精品久久久久中文字幕 | 欧美性大战xxxxx久久久| 国产精品中文字幕欧美| 午夜欧美视频在线观看| 久久精品一区蜜桃臀影院| 欧美日韩精品欧美日韩精品一 | 欧美唯美清纯偷拍| 国产传媒一区在线| 日韩国产高清影视| 亚洲天堂成人网| 久久久久国产一区二区三区四区| 欧美图片一区二区三区| 国产精品99久久久| 国产一区二区三区在线观看精品 | 精品国产第一区二区三区观看体验| 在线日韩国产精品| av成人老司机| 久久精品72免费观看|