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

📄 i2c.h

📁 atmel avr hardwire i2c source
💻 H
字号:
/***********************************************************************/
/*  FILE        :I2C.c                                                 */
/*  DATE        :2004 04 07                                            */
/*  DESCRIPTION :Program of snake                                      */
/*               Test TWI ATmega 128                                   */
/*  Original File is Pascal Stang                                      */
/*  This file is generated by Sysonchip .							   */
/***********************************************************************/
/*HISTORY 															   */
/*---------------------------------------------------------------------*/
/*YYYY-MM-DD version   name         description                        */
/*2003-06-25  0.0.9	   Pascal Stang Creates the file                   */
/*2004-04-07  0.0.9    snake        modyfy for ATmega 128              */
/***********************************************************************/

#ifndef I2C_H
#define I2C_H

#include <util/twi.h>
#include "types.h"
#include "delay.h"
#include "Constants.h"

// defines and constants
#define TWCR_CMD_MASK       0x0F
#define TWSR_STATUS_MASK    0xF8

// return values
#define I2C_OK              0x00
#define I2C_ERROR_NODEV     0x01

//#define F_CPU        7372800
#define I2C_SEND_DATA_BUFFER_SIZE       0x20
#define I2C_RECEIVE_DATA_BUFFER_SIZE    0x20

#define TRUE				0x01
#define FALSE				0x00
// types
typedef enum
{
	I2C_IDLE = 0,
	I2C_BUSY = 1,
	I2C_MASTER_TX = 2,
	I2C_MASTER_RX = 3,
	I2C_SLAVE_TX = 4,
	I2C_SLAVE_RX = 5
} eI2cStateType;
// functions

//! Initialize I2C (TWI) interface
void i2cInit(void);

//! Set the I2C transaction bitrate (in KHz)
void i2cSetBitrate(unsigned int bitrateKHz);

// I2C setup and configurations commands
//! Set the local (AVR processor's) I2C device address
void i2cSetLocalDeviceAddr(unsigned char deviceAddr, unsigned char genCallEn);

//! Set the user function which handles receiving (incoming) data as a slave
void i2cSetSlaveReceiveHandler(void (*i2cSlaveRx_func)(unsigned char receiveDataLength, unsigned char* recieveData));
//! Set the user function which handles transmitting (outgoing) data as a slave
void i2cSetSlaveTransmitHandler(unsigned char (*i2cSlaveTx_func)(unsigned char transmitDataLengthMax, unsigned char* transmitData));

// Low-level I2C transaction commands 
//! Send an I2C start condition in Master mode
void i2cSendStart(void);
//! Send an I2C stop condition in Master mode
void i2cSendStop(void);
//! Wait for current I2C operation to complete
void i2cWaitForComplete(void);
//! Send an (address|R/W) combination or a data byte over I2C
void i2cSendByte(unsigned char data);
//! Receive a data byte over I2C  
// ackFlag = TRUE if recevied data should be ACK'ed
// ackFlag = FALSE if recevied data should be NACK'ed
void i2cReceiveByte(unsigned char ackFlag);
//! Pick up the data that was received with i2cReceiveByte()
unsigned char i2cGetReceivedByte(void);
//! Get current I2c bus status from TWSR
unsigned char i2cGetStatus(void);

// high-level I2C transaction commands

//! send I2C data to a device on the bus
void i2cMasterSend(unsigned char deviceAddr, unsigned char length, unsigned char *data);
//! receive I2C data from a device on the bus
void i2cMasterReceive(unsigned char deviceAddr, unsigned char length, unsigned char* data);

//! send I2C data to a device on the bus (non-interrupt based)
void i2cMasterSendNI(unsigned char deviceAddr, unsigned char length, unsigned char* data);
//! receive I2C data from a device on the bus (non-interrupt based)
void i2cMasterReceiveNI(unsigned char deviceAddr, unsigned char length, unsigned char *data);

//! Get the current high-level state of the I2C interface
eI2cStateType i2cGetState(void);

// another mothod
#define MAX_TRIES 100

int neweprom_read_byte(uint16_t eeaddr, char *buf);
int neweprom_write_byte(uint16_t eeaddr, char buf);
void neweprom_init(void);

#endif


⌨️ 快捷键说明

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