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

📄 twi.h

📁 AVR单片机平台Atmega32+CH375A芯片读写U盘代码
💻 H
字号:
#include <iom32v.h>

#define SR_DATA_LENGTH_MAX 517

#define TWPS0 0 
#define TWPS1 1 
#define TWEN  2 
#define TWIE  0 
#define TWEA  6 
#define TWINT 7 
#define TWSTA 5 
#define TWSTO 4 

// 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 

// 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 I2C_SEND_DATA_BUFFER_SIZE                0x20 
#define I2C_RECEIVE_DATA_BUFFER_SIZE                 0x20 

#define F_CPU 8000000 
#define TRUE  1 
#define FALSE 0    
// 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;

/*I2cComStatus的位定义*/
#define I2C_RECCOM  7
#define I2C_SENDCOM 6

#define SET_TRAN_COM   I2cComStatus|= (1<<I2C_SENDCOM)
#define SET_REC_COM    I2cComStatus|= (1<<I2C_RECCOM)

void i2cInit(void);                               //I2C初始化
void i2cSetBitrate(unsigned int  bitrateKHz);     //比特率初始化
void i2cSetLocalDeviceAddr(unsigned char deviceAddr, unsigned char genCallEn); //设置本地地址
void i2cSendStart(void);
void i2cSendStop(void);
void i2cWaitForComplete(void);
void i2cSendByte(unsigned char data);
void i2cReceiveByte(unsigned char ackFlag);
unsigned char i2cGetReceivedByte(void);
unsigned char i2cMasterSend(unsigned char deviceAddr, unsigned int length);
unsigned char i2cMasterReceive(unsigned char deviceAddr, unsigned int length);
unsigned char i2cMasterSendNI(unsigned char deviceAddr, unsigned char length, unsigned char* data);
unsigned char i2cMasterReceiveNI(unsigned char deviceAddr, unsigned char length, unsigned char *data);

⌨️ 快捷键说明

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