亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
视频一区二区三区中文字幕| 欧美一激情一区二区三区| 91日韩精品一区| 精品电影一区二区| 国产精品久久久一本精品 | 欧美另类高清zo欧美| 精品少妇一区二区三区在线视频| 亚洲一区二区五区| 97se狠狠狠综合亚洲狠狠| 在线播放亚洲一区| 欧美理论电影在线| 国产欧美日韩三区| 久久99精品久久只有精品| 亚洲韩国精品一区| 日韩国产精品91| 久久综合色一综合色88| 自拍偷拍亚洲激情| 午夜伊人狠狠久久| 成人av网址在线| 欧美一区二区美女| 亚洲六月丁香色婷婷综合久久| 玉足女爽爽91| 99国产一区二区三精品乱码| 欧美不卡123| 亚洲国产日日夜夜| www..com久久爱| 精品欧美久久久| 艳妇臀荡乳欲伦亚洲一区| 91精品国产综合久久蜜臀| 伊人开心综合网| 91看片淫黄大片一级在线观看| 亚洲欧洲日产国码二区| 制服丝袜亚洲精品中文字幕| 精品一区二区三区影院在线午夜 | 91九色02白丝porn| 国产欧美久久久精品影院| 免费在线观看日韩欧美| 91一区二区在线| 天天综合日日夜夜精品| 中文字幕一区二区三区乱码在线| 这里只有精品免费| 欧美一区二区三级| 99视频超级精品| 亚洲三级久久久| 久久综合九色综合97婷婷女人| 狠狠色丁香久久婷婷综合丁香| 99久久伊人网影院| 亚洲欧洲国产日韩| 欧美精选在线播放| 亚洲综合色视频| 色狠狠色噜噜噜综合网| 亚洲欧洲综合另类在线| 99久久国产综合精品女不卡| 国产精品久久久久久亚洲伦| 成人亚洲精品久久久久软件| 久久久精品影视| 欧美日韩国产高清一区二区三区| 国产91富婆露脸刺激对白| 国产综合色视频| 日韩av在线播放中文字幕| 日韩av不卡在线观看| 蜜臀精品一区二区三区在线观看| 午夜久久久久久电影| 久久久99免费| 精品国产一区二区在线观看| 粉嫩嫩av羞羞动漫久久久| 亚洲综合小说图片| 亚洲成人黄色影院| 精品免费视频.| jlzzjlzz亚洲日本少妇| 亚洲福利一区二区| 久久久久久一级片| 在线免费观看日本欧美| 久久av中文字幕片| 亚洲精品高清视频在线观看| 精品99一区二区三区| 色婷婷久久久亚洲一区二区三区| 蜜桃av噜噜一区| 亚洲精品高清视频在线观看| 久久亚洲精精品中文字幕早川悠里 | 国产目拍亚洲精品99久久精品| 色综合久久综合| 国产在线视频一区二区三区| 一区二区国产视频| 国产精品欧美一级免费| 欧美精品一区二区久久久| 欧美亚洲自拍偷拍| 国产91在线看| 精品一区二区三区在线播放 | 国产剧情在线观看一区二区| 亚洲精品第1页| 欧美国产精品一区二区三区| 欧美日本一区二区| 在线视频欧美区| 成人黄色在线网站| 国产一区二区福利| 麻豆精品久久精品色综合| 亚洲一二三四区| 亚洲日本在线视频观看| 久久九九99视频| 26uuu欧美| 日韩欧美精品三级| 制服视频三区第一页精品| 欧美日韩视频一区二区| 黄页网站大全一区二区| 日本不卡在线视频| 婷婷国产v国产偷v亚洲高清| 一区二区三区欧美日| 亚洲人成7777| 亚洲激情一二三区| 亚洲午夜一区二区| 亚洲国产精品麻豆| 天堂成人免费av电影一区| 在线观看亚洲一区| 免费在线看一区| 精品sm在线观看| 欧美性大战久久久久久久蜜臀 | 色老汉av一区二区三区| 丝袜美腿成人在线| 久久久99免费| 久久综合网色—综合色88| 欧美性大战久久久久久久| 91亚洲资源网| av一本久道久久综合久久鬼色| 丁香一区二区三区| 国产乱码精品一区二区三| 五月天激情综合| 亚洲黄色免费电影| 久久不见久久见免费视频1| 亚洲一区二区三区四区在线免费观看 | 午夜精品一区在线观看| 青青草原综合久久大伊人精品优势| 丝袜国产日韩另类美女| 国产成人综合亚洲网站| 黑人巨大精品欧美一区| 粉嫩一区二区三区在线看| 成人欧美一区二区三区在线播放| 欧美在线视频全部完| 久久日一线二线三线suv| 国产精选一区二区三区| 久久影音资源网| 成人黄色电影在线 | 欧美极品少妇xxxxⅹ高跟鞋| 1024成人网| 日本欧美在线观看| 国产成人aaaa| 欧美日韩在线播放三区四区| 欧美xxxx老人做受| 综合欧美一区二区三区| 首页国产欧美久久| 激情小说欧美图片| av网站免费线看精品| 日韩久久久精品| 亚洲精品老司机| 国产在线视频不卡二| 91黄色免费看| 久久久综合网站| 亚洲成人激情av| 91在线视频网址| 亚洲精品一区二区三区在线观看| 欧美大尺度电影在线| 亚洲一区二区偷拍精品| 国产美女精品人人做人人爽| 成人免费精品视频| 日韩欧美亚洲国产精品字幕久久久| 国产精品超碰97尤物18| 精品一区二区三区欧美| 欧美主播一区二区三区| 中文天堂在线一区| 美女免费视频一区| 91麻豆精品国产91久久久更新时间| 日韩一区二区三区视频在线| 一区二区三区中文在线观看| 懂色av中文字幕一区二区三区| 日韩女同互慰一区二区| 丝袜美腿亚洲综合| 91激情在线视频| 亚洲最大色网站| 欧美军同video69gay| 亚洲午夜一区二区| 91精品国产综合久久精品麻豆| 免费久久99精品国产| 久久综合色之久久综合| 欧美日韩一区 二区 三区 久久精品| 色综合中文字幕| 国产成人精品影视| 国产自产视频一区二区三区| 久久精品国产免费| 亚洲h动漫在线| 亚洲国产三级在线| 国产精品美女久久久久久久久久久 | 精品国产一区二区三区忘忧草 | 欧美精品九九99久久| 亚洲美女精品一区| 91视频免费观看| 亚洲精品视频在线观看免费| 91亚洲资源网| 《视频一区视频二区| 99精品国产99久久久久久白柏| 国产精品久线在线观看|