⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iic_avr.h

📁 串口转发IIC程序
💻 H
字号:
/* 这份源代码文件已被未注册的SourceFormatX格式化过 */

/* 如果您想不再添加此类信息,请您注册这个共享软件  */

/* 更多相关信息请访问网站: http://cn.textrush.com  */

///////////////////////////////////////////
//		IIC_AVR.H
//	Function:	IIC driver for atmel mega16
//	Create : Guobg
//	Date :	20060113
//	Revision:	0.0
///////////////////////////////////////////
//	Modify History
//
///////////////////////////////////////////
#ifndef __IIC_AVR_H__
	#define __IIC_AVR_H__
	#include "globle.h"

//	#include "TPXFXS.H"
	#ifndef CPU_CLOCK_FREQUENCE
		#define CPU_CLOCK_FREQUENCE 8000000L
	#endif
	#ifdef GCC_AVR
		#include "avr\io.h"
		#include "TWI.H"
		#include <avr \signal.h>
	#endif
	#ifdef ICC_AVR
		#include "iom16v.h"
	#endif
	#ifndef GCC_AVR

/* TWSR values (not bits) */

/* Master */
		#define TW_START		0x08
		#define TW_REP_START	0x10

/* Master Transmitter */
		#define TW_MT_SLA_ACK	0x18
		#define TW_MT_SLA_NACK	0x20
		#define TW_MT_DATA_ACK	0x28
		#define TW_MT_DATA_NACK 0x30
		#define TW_MT_ARB_LOST	0x38

/* Master Receiver */
		#define TW_MR_ARB_LOST	0x38
		#define TW_MR_SLA_ACK	0x40
		#define TW_MR_SLA_NACK	0x48
		#define TW_MR_DATA_ACK	0x50
		#define TW_MR_DATA_NACK 0x58

/* Slave Transmitter */
		#define TW_ST_SLA_ACK			0xA8
		#define TW_ST_ARB_LOST_SLA_ACK	0xB0
		#define TW_ST_DATA_ACK			0xB8
		#define TW_ST_DATA_NACK			0xC0
		#define TW_ST_LAST_DATA			0xC8

/* Slave Receiver */
		#define TW_SR_SLA_ACK				0x60
		#define TW_SR_ARB_LOST_SLA_ACK		0x68
		#define TW_SR_GCALL_ACK				0x70
		#define TW_SR_ARB_LOST_GCALL_ACK	0x78
		#define TW_SR_DATA_ACK				0x80
		#define TW_SR_DATA_NACK				0x88
		#define TW_SR_GCALL_DATA_ACK		0x90
		#define TW_SR_GCALL_DATA_NACK		0x98
		#define TW_SR_STOP					0xA0

/* Misc */
		#define TW_NO_INFO		0xF8
		#define TW_BUS_ERROR	0x00
	#endif
	#define IIC_TIME_OUT_MAX_COUNTER	0xFF					// Max Time out 255 * tick
	#define IIC_TIME_OUT_STOP			0x00					// 0-Timer count stop

//IIC Mode
	#define IIC_MODE_SLAVE_TRANSMIT		0x00
	#define IIC_MODE_SLAVE_RECEIVE		0x01
	#define IIC_MODE_MASTER_TRANSMIT	0x02
	#define IIC_MODE_MASTER_RECEIVE		0x03
	#define IIC_MODE_SHUT_DOWN			0xFF

// Following statment define BP_CODE for baudrate
// SCL Frequency = Clock CPU/(16+ 2 * TWBR * POW(4,TWPS))
	#define IIC_BAUDRATE_TWPS_NODIV 0
	#define IIC_BAUDRATE_TWPS_DIV4	1
	#define IIC_BAUDRATE_TWPS_DIV16 2
	#define IIC_BAUDRATE_TWPS_DIV64 3
	#define IIC_BAUDRATE_TWBR_200K	12							// TWPS no div @ 8M
	#define IIC_BAUDRATE_TWBR_100K	32							// TWPS no div @ 8M
	#define IIC_BAUDRATE_200K		0
	#define IIC_BAUDRATE_100K		1

// Following statment define IIC Recieve,Transmit Buffer
	#define IIC_MAX_BUFFER_LEN			16
	#define IIC_ERROR_NOERROR			0x00					//No Error
	#define IIC_ERROR_NOT_ACK_SLA		0x81					//No ACK during address
	#define IIC_ERROR_NOT_ACK_DATA		0x82					//No ACK during data transfer
	#define IIC_ERROR_ARBITRATION_LOST	0x83					//Arbitration lost bus control
	#define IIC_ERROR_TIME_OVERLOW		0x84					//Time overlow
	#define IIC_ERROR_BUFFER_OVERLOW	0x85					//Buffer overlow
struct tagIIC_MSG_PACKAGE
{
	U8	ucMsgID;
	U8	ucSourceAddress;
	U8	ucDataByte[8];
	struct
	{
		int bIsIntegrityMsg : 1;
		int ucMsgNo : 3;										//Message sequence no
		int ucValidBytes : 4;									//Valid data nums in ucDataByte
	} ucMsgControllByte;
	U8	CheckSum;												// XOR all data byte above
};
struct IIC_BUFFER
{
	union
	{
		U8							ucData[IIC_MAX_BUFFER_LEN]; // Data buffer
		struct tagIIC_MSG_PACKAGE	IIC_MessagePack;
	} IIC_BUFFER_UNION;
	U8	ucWriteP, ucReadP;										// Data buffer point
	U8	ucDestinationAddress;									// Destination Address

	// 	N/A for IN buffer
	struct
	{
		int bEnAccess : 1;										// Allow user access data buffer
	} ucIICBufferStatus;
};
void	IIC_Init(U8 ucSlaveAddr, U8 ucBaudrate);
	#ifdef ICC_AVR
void	IIC_handler(void);
	#endif

//IIC cell-operation define here
	#define IIC_START			TWCR |= (_BV(TWINT) | _BV(TWSTA) | _BV(TWEN) | _BV(TWIE)); \
	ucIICTimeoutCount = IIC_TIME_OUT_MAX_COUNTER;
	#define IIC_REAPET_START	TWCR |= \
		(								\
			_BV(TWINT) |				\
			_BV(TWSTA) |				\
			_BV(TWEN) |					\
			_BV(TWIE)					\
		);								\
	ucIICTimeoutCount = IIC_TIME_OUT_MAX_COUNTER;
	#define IIC_STOP			TWCR |= (_BV(TWINT) | _BV(TWSTO) | _BV(TWEN) | _BV(TWIE)); \
	ucIICTimeoutCount = IIC_TIME_OUT_STOP;
	#define IIC_SLA				TWDR = IIC_Buffer_Out.ucDestinationAddress << 1; \
	ucIICTimeoutCount = IIC_TIME_OUT_MAX_COUNTER;
	#define IIC_BUFFER_TO_BUS	TWDR = IIC_Buffer_Out.IIC_BUFFER_UNION.ucData[ \
		IIC_Buffer_Out.ucReadP++];											   \
	ucIICTimeoutCount = IIC_TIME_OUT_MAX_COUNTER;
	#define IIC_BUS_TO_BUFFER	IIC_Buffer_In.IIC_BUFFER_UNION.ucData[IIC_Buffer_In. \
		ucWriteP++] = TWDR;															 \
	ucIICTimeoutCount = IIC_TIME_OUT_MAX_COUNTER;
U8		I2C_Send(U8 I2C_Addr, U8 *I2C_MsgData, U8 MsgDataLen, U8 s_Timeout);
U8		I2C_Send(U8 I2C_Addr, U8 *I2C_MsgData, U8 MsgDataLen, U8 s_Timeout);
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -