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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? i2c.c

?? ATMEL的AVR單片機庫文件
?? C
?? 第 1 頁 / 共 2 頁
字號:
/*! \file i2c.c \brief I2C interface using AVR Two-Wire Interface (TWI) hardware. */
//*****************************************************************************
//
// File Name	: 'i2c.c'
// Title		: I2C interface using AVR Two-Wire Interface (TWI) hardware
// Author		: Pascal Stang - Copyright (C) 2002-2003
// Created		: 2002.06.25
// Revised		: 2003.03.02
// Version		: 0.9
// Target MCU	: Atmel AVR series
// Editor Tabs	: 4
//
// Description : I2C (pronounced "eye-squared-see") is a two-wire bidirectional
//		network designed for easy transfer of information between a wide variety
//		of intelligent devices.  Many of the Atmel AVR series processors have
//		hardware support for transmitting and receiving using an I2C-type bus.
//		In addition to the AVRs, there are thousands of other parts made by
//		manufacturers like Philips, Maxim, National, TI, etc that use I2C as
//		their primary means of communication and control.  Common device types
//		are A/D & D/A converters, temp sensors, intelligent battery monitors,
//		MP3 decoder chips, EEPROM chips, multiplexing switches, etc.
//
//		I2C uses only two wires (SDA and SCL) to communicate bidirectionally
//		between devices.  I2C is a multidrop network, meaning that you can have
//		several devices on a single bus.  Because I2C uses a 7-bit number to
//		identify which device it wants to talk to, you cannot have more than
//		127 devices on a single bus.
//
//		I2C ordinarily requires two 4.7K pull-up resistors to power (one each on
//		SDA and SCL), but for small numbers of devices (maybe 1-4), it is enough
//		to activate the internal pull-up resistors in the AVR processor.  To do
//		this, set the port pins, which correspond to the I2C pins SDA/SCL, high.
//		For example, on the mega163, sbi(PORTC, 0); sbi(PORTC, 1);.
//
//		For complete information about I2C, see the Philips Semiconductor
//		website.  They created I2C and have the largest family of devices that
//		work with I2C.
//
// Note: Many manufacturers market I2C bus devices under a different or generic
//		bus name like "Two-Wire Interface".  This is because Philips still holds
//		"I2C" as a trademark.  For example, SMBus and SMBus devices are hardware
//		compatible and closely related to I2C.  They can be directly connected
//		to an I2C bus along with other I2C devices are are generally accessed in
//		the same way as I2C devices.  SMBus is often found on modern motherboards
//		for temp sensing and other low-level control tasks.
//
// This code is distributed under the GNU Public License
//		which can be found at http://www.gnu.org/licenses/gpl.txt
//
//*****************************************************************************

#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>
#include <avr/twi.h>

#include "i2c.h"

#include "rprintf.h"	// include printf function library
#include "uart2.h"

// Standard I2C bit rates are:
// 100KHz for slow speed
// 400KHz for high speed

//#define I2C_DEBUG

// I2C state and address variables
static volatile eI2cStateType I2cState;
static u08 I2cDeviceAddrRW;
// send/transmit buffer (outgoing data)
static u08 I2cSendData[I2C_SEND_DATA_BUFFER_SIZE];
static u08 I2cSendDataIndex;
static u08 I2cSendDataLength;
// receive buffer (incoming data)
static u08 I2cReceiveData[I2C_RECEIVE_DATA_BUFFER_SIZE];
static u08 I2cReceiveDataIndex;
static u08 I2cReceiveDataLength;

// function pointer to i2c receive routine
//! I2cSlaveReceive is called when this processor
// is addressed as a slave for writing
static void (*i2cSlaveReceive)(u08 receiveDataLength, u08* recieveData);
//! I2cSlaveTransmit is called when this processor
// is addressed as a slave for reading
static u08 (*i2cSlaveTransmit)(u08 transmitDataLengthMax, u08* transmitData);

// functions
void i2cInit(void)
{
	// set pull-up resistors on I2C bus pins
	// TODO: should #ifdef these
	sbi(PORTC, 0);	// i2c SCL on ATmega163,323,16,32,etc
	sbi(PORTC, 1);	// i2c SDA on ATmega163,323,16,32,etc
	sbi(PORTD, 0);	// i2c SCL on ATmega128,64
	sbi(PORTD, 1);	// i2c SDA on ATmega128,64

	// clear SlaveReceive and SlaveTransmit handler to null
	i2cSlaveReceive = 0;
	i2cSlaveTransmit = 0;
	// set i2c bit rate to 100KHz
	i2cSetBitrate(100);
	// enable TWI (two-wire interface)
	sbi(TWCR, TWEN);
	// set state
	I2cState = I2C_IDLE;
	// enable TWI interrupt and slave address ACK
	sbi(TWCR, TWIE);
	sbi(TWCR, TWEA);
	//outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
	// enable interrupts
	sei();
}

void i2cSetBitrate(u16 bitrateKHz)
{
	u08 bitrate_div;
	// set i2c bitrate
	// SCL freq = F_CPU/(16+2*TWBR))
	#ifdef TWPS0
		// for processors with additional bitrate division (mega128)
		// SCL freq = F_CPU/(16+2*TWBR*4^TWPS)
		// set TWPS to zero
		cbi(TWSR, TWPS0);
		cbi(TWSR, TWPS1);
	#endif
	// calculate bitrate division	
	bitrate_div = ((F_CPU/1000l)/bitrateKHz);
	if(bitrate_div >= 16)
		bitrate_div = (bitrate_div-16)/2;
	outb(TWBR, bitrate_div);
}

void i2cSetLocalDeviceAddr(u08 deviceAddr, u08 genCallEn)
{
	// set local device address (used in slave mode only)
	outb(TWAR, ((deviceAddr&0xFE) | (genCallEn?1:0)) );
}

void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(u08 receiveDataLength, u08* recieveData))
{
	i2cSlaveReceive = i2cSlaveRx_func;
}

void i2cSetSlaveTransmitHandler(u08 (*i2cSlaveTx_func)(u08 transmitDataLengthMax, u08* transmitData))
{
	i2cSlaveTransmit = i2cSlaveTx_func;
}

inline void i2cSendStart(void)
{
	// send start condition
	outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWSTA));
}

inline void i2cSendStop(void)
{
	// transmit stop condition
	// leave with TWEA on for slave receiving
	outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA)|BV(TWSTO));
}

inline void i2cWaitForComplete(void)
{
	// wait for i2c interface to complete operation
	while( !(inb(TWCR) & BV(TWINT)) );
}

inline void i2cSendByte(u08 data)
{
	// save data to the TWDR
	outb(TWDR, data);
	// begin send
	outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
}

inline void i2cReceiveByte(u08 ackFlag)
{
	// begin receive over i2c
	if( ackFlag )
	{
		// ackFlag = TRUE: ACK the recevied data
		outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT)|BV(TWEA));
	}
	else
	{
		// ackFlag = FALSE: NACK the recevied data
		outb(TWCR, (inb(TWCR)&TWCR_CMD_MASK)|BV(TWINT));
	}
}

inline u08 i2cGetReceivedByte(void)
{
	// retieve received data byte from i2c TWDR
	return( inb(TWDR) );
}

inline u08 i2cGetStatus(void)
{
	// retieve current i2c status from i2c TWSR
	return( inb(TWSR) );
}

void i2cMasterSend(u08 deviceAddr, u08 length, u08* data)
{
	u08 i;
	// wait for interface to be ready
	while(I2cState);
	// set state
	I2cState = I2C_MASTER_TX;
	// save data
	I2cDeviceAddrRW = (deviceAddr & 0xFE);	// RW cleared: write operation
	for(i=0; i<length; i++)
		I2cSendData[i] = *data++;
	I2cSendDataIndex = 0;
	I2cSendDataLength = length;
	// send start condition
	i2cSendStart();
}

void i2cMasterReceive(u08 deviceAddr, u08 length, u08* data)
{
	u08 i;
	// wait for interface to be ready
	while(I2cState);
	// set state
	I2cState = I2C_MASTER_RX;
	// save data
	I2cDeviceAddrRW = (deviceAddr|0x01);	// RW set: read operation
	I2cReceiveDataIndex = 0;
	I2cReceiveDataLength = length;
	// send start condition
	i2cSendStart();
	// wait for data
	while(I2cState);
	// return data
	for(i=0; i<length; i++)
		*data++ = I2cReceiveData[i];
}

u08 i2cMasterSendNI(u08 deviceAddr, u08 length, u08* data)
{
	u08 retval = I2C_OK;

	// disable TWI interrupt
	cbi(TWCR, TWIE);

	// send start condition
	i2cSendStart();
	i2cWaitForComplete();

	// send device address with write
	i2cSendByte( deviceAddr & 0xFE );
	i2cWaitForComplete();

	// check if device is present and live
	if( inb(TWSR) == TW_MT_SLA_ACK)
	{
		// send data
		while(length)
		{
			i2cSendByte( *data++ );
			i2cWaitForComplete();
			length--;
		}
	}
	else
	{
		// device did not ACK it's address,
		// data will not be transferred
		// return error
		retval = I2C_ERROR_NODEV;
	}

	// transmit stop condition
	// leave with TWEA on for slave receiving
	i2cSendStop();
	while( !(inb(TWCR) & BV(TWSTO)) );

	// enable TWI interrupt
	sbi(TWCR, TWIE);

	return retval;
}

u08 i2cMasterReceiveNI(u08 deviceAddr, u08 length, u08 *data)
{
	u08 retval = I2C_OK;

	// disable TWI interrupt
	cbi(TWCR, TWIE);

	// send start condition
	i2cSendStart();
	i2cWaitForComplete();

	// send device address with read
	i2cSendByte( deviceAddr | 0x01 );
	i2cWaitForComplete();

	// check if device is present and live
	if( inb(TWSR) == TW_MR_SLA_ACK)
	{
		// accept receive data and ack it
		while(length > 1)
		{
			i2cReceiveByte(TRUE);
			i2cWaitForComplete();
			*data++ = i2cGetReceivedByte();
			// decrement length
			length--;
		}

		// accept receive data and nack it (last-byte signal)
		i2cReceiveByte(FALSE);
		i2cWaitForComplete();
		*data++ = i2cGetReceivedByte();
	}
	else

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲日本va在线观看| 国产精品福利av| 97se亚洲国产综合自在线观| 香蕉乱码成人久久天堂爱免费| 日韩欧美久久久| 在线观看免费亚洲| 国产精品1区2区| 午夜精品一区二区三区电影天堂 | 欧美日韩久久久| 国产精品亚洲专一区二区三区 | 日韩精品一区二区在线| 不卡av在线网| 国内外精品视频| 午夜精品一区二区三区免费视频 | 玖玖九九国产精品| 亚洲一区二区三区四区不卡| 国产精品美女久久久久久久久| 日韩免费性生活视频播放| 欧美色区777第一页| 99麻豆久久久国产精品免费| 国产一区二区三区在线观看精品| 日韩激情视频在线观看| 亚洲一区在线播放| 亚洲丝袜自拍清纯另类| 国产精品色婷婷久久58| 26uuu另类欧美亚洲曰本| 欧美一区二区三区免费在线看| 日本韩国欧美在线| 91亚洲国产成人精品一区二区三| 国产99精品国产| 国产美女一区二区| 狠狠色丁香九九婷婷综合五月| 日韩黄色免费电影| 天堂一区二区在线| 天堂久久久久va久久久久| 亚洲电影第三页| 亚洲一区二区视频| 亚洲激情av在线| 亚洲激情图片qvod| 亚洲一区二区三区影院| 亚洲第四色夜色| 午夜国产不卡在线观看视频| 亚洲国产精品久久一线不卡| 午夜欧美大尺度福利影院在线看 | 美女爽到高潮91| 精品一区二区在线观看| 精品一区精品二区高清| 国产精品亚洲第一区在线暖暖韩国 | 亚洲国产一区视频| 亚洲一区二区三区国产| 亚洲高清不卡在线| 蜜桃一区二区三区四区| 久久99精品国产麻豆婷婷 | 久久不见久久见中文字幕免费| 免费欧美在线视频| 精品一区二区三区在线观看国产 | 国产精品自拍一区| 成人综合在线观看| 99久久国产综合精品麻豆| 91久久精品一区二区三区| 欧美曰成人黄网| 欧美一级黄色录像| 久久久久久久精| 国产精品成人免费在线| 亚洲午夜免费视频| 麻豆精品一区二区av白丝在线| 国产一区二区三区电影在线观看| 丁香桃色午夜亚洲一区二区三区| 色综合天天综合在线视频| 欧美日韩精品一区二区三区蜜桃| 日韩三级电影网址| 日本一区二区三级电影在线观看| 亚洲欧美自拍偷拍| 日韩精品一卡二卡三卡四卡无卡| 国产精品一卡二卡在线观看| 色综合久久综合| 日韩视频中午一区| 中文字幕一区二区三| 天涯成人国产亚洲精品一区av| 国内精品视频一区二区三区八戒 | 久久众筹精品私拍模特| 国产欧美久久久精品影院| 亚洲免费av观看| 蜜臀久久久久久久| 99久久久无码国产精品| 欧美一区二区三区四区五区| 亚洲国产精品成人久久综合一区| 亚洲成人一区二区在线观看| 国产寡妇亲子伦一区二区| 欧美亚洲禁片免费| 久久久久国产精品厨房| 午夜电影久久久| av在线不卡网| 精品国产成人系列| 一区二区在线观看av| 国产一区二区0| 欧美一区二区三区系列电影| 中文字幕成人av| 男女男精品视频| 91国模大尺度私拍在线视频| 欧美精品一区二区久久婷婷| 亚洲福利视频三区| 99精品久久只有精品| www久久精品| 丝袜亚洲另类欧美| 91成人国产精品| 国产精品免费丝袜| 狠狠色丁香久久婷婷综合丁香| 欧美猛男男办公室激情| 亚洲免费在线播放| 成人国产亚洲欧美成人综合网| 欧美本精品男人aⅴ天堂| 亚洲第一二三四区| 色94色欧美sute亚洲线路一久| 欧美国产欧美亚州国产日韩mv天天看完整 | 视频一区视频二区在线观看| 97se亚洲国产综合自在线观| 欧美极品aⅴ影院| 激情久久五月天| 日韩一本二本av| 调教+趴+乳夹+国产+精品| 欧美性受xxxx黑人xyx| 久久久久亚洲蜜桃| 久久亚洲综合色| 一区二区三区精品| eeuss鲁片一区二区三区| 久久伊99综合婷婷久久伊| 免费看欧美女人艹b| 欧美丰满嫩嫩电影| 五月天视频一区| 欧美日韩二区三区| 午夜精品久久久久影视| 91国偷自产一区二区三区成为亚洲经典| 国产精品福利一区二区三区| 成+人+亚洲+综合天堂| 国产免费成人在线视频| 国产成人免费视| 欧美国产丝袜视频| 成人国产视频在线观看| 日韩美女精品在线| 色偷偷一区二区三区| 亚洲主播在线播放| 欧美日韩国产免费| 美女视频第一区二区三区免费观看网站| 欧美精品一级二级三级| 久久福利视频一区二区| 精品国产亚洲一区二区三区在线观看| 精品一区二区三区免费毛片爱 | 日本一区二区动态图| 成人激情小说网站| ㊣最新国产の精品bt伙计久久| 国产98色在线|日韩| 1区2区3区国产精品| 91成人网在线| 久色婷婷小香蕉久久| 久久精品亚洲精品国产欧美kt∨ | 久久久精品一品道一区| 国产成人aaaa| 亚洲猫色日本管| 欧美精品免费视频| 激情欧美日韩一区二区| 国产精品麻豆欧美日韩ww| 91色porny在线视频| 婷婷六月综合网| 2欧美一区二区三区在线观看视频 337p粉嫩大胆噜噜噜噜噜91av | 日韩国产在线一| 久久久久一区二区三区四区| 成人av网站免费观看| 亚洲一区二三区| 日韩亚洲欧美在线| 成人福利在线看| 亚洲chinese男男1069| 精品久久久久香蕉网| 成人涩涩免费视频| 亚洲va欧美va天堂v国产综合| 2024国产精品视频| 色综合天天综合狠狠| 日本不卡中文字幕| 国产精品免费看片| 5566中文字幕一区二区电影| 国产成人免费视| 天堂一区二区在线| 中文字幕亚洲一区二区va在线| 欧美美女视频在线观看| 豆国产96在线|亚洲| 亚洲gay无套男同| 国产精品入口麻豆原神| 欧美日本一区二区在线观看| 丰满白嫩尤物一区二区| 亚洲18色成人| 国产精品国产精品国产专区不片| 欧美巨大另类极品videosbest | 一本久久精品一区二区| 老司机精品视频在线| 樱桃国产成人精品视频| 久久综合狠狠综合久久激情| 欧美日本一区二区三区四区| 99精品黄色片免费大全| 国产美女av一区二区三区| 天天亚洲美女在线视频|