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

📄 iic_slave._h

📁 单片机的应用程序
💻 _H
字号:

#ifndef IIC_SLAVE_H
#define IIC_SLAVE_H

#include <iom48v.h>
#include <macros.h>

/* TWSR values (not bits) */
/* Master */
#define I2C_START   0x08
#define I2C_RESTART   0x10

/* Master Transmitter */
#define I2C_MT_SLA_ACK  0x18
#define I2C_MT_SLA_NACK  0x20
#define I2C_MT_DATA_ACK  0x28
#define I2C_MT_DATA_NACK 0x30
#define I2C_MT_ARB_LOST  0x38

/* Master Receiver */
#define I2C_MR_ARB_LOST  0x38
#define I2C_MR_SLA_ACK  0x40
#define I2C_MR_SLA_NACK  0x48
#define I2C_MR_DATA_ACK  0x50
#define I2C_MR_DATA_NACK 0x58

/* Slave Transmitter */
#define I2C_ST_SLA_ACK   0xA8
#define I2C_ST_ARB_LOST_SLA_ACK 0xB0
#define I2C_ST_DATA_ACK   0xB8
#define I2C_ST_DATA_NACK  0xC0
#define I2C_ST_LAST_DATA  0xC8

/* Slave Receiver */
#define I2C_SR_SLA_ACK    0x60
#define I2C_SR_ARB_LOST_SLA_ACK  0x68
#define I2C_SR_GCALL_ACK   0x70
#define I2C_SR_ARB_LOST_GCALL_ACK 0x78
#define I2C_SR_DATA_ACK    0x80
#define I2C_SR_DATA_NACK   0x88
#define I2C_SR_GCALL_DATA_ACK  0x90
#define I2C_SR_GCALL_DATA_NACK  0x98
#define I2C_SR_STOP     0xA0

/* Misc */
#define I2C_NO_INFO   0xF8
#define I2C_BUS_ERROR  0x00

/*
 * The lower 3 bits of TWSR are reserved on the ATmega163.
 * The 2 LSB carry the prescaler bits on the newer ATmegas.
 */
#define I2C_STATUS_MASK (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3))
#define I2C_STATUS  (TWSR & I2C_STATUS_MASK)

/*
 * R/~W bit in SLA+R/W address field.
 */
#define I2C_READ  1
#define I2C_WRITE  0

typedef unsigned char	bool;
typedef unsigned char	uint8;
typedef unsigned int	uint16;

extern bool i2c_slave_init(void);     
extern bool i2c_slave_sendbyte(unsigned char databyte);
extern bool i2c_slave_recvsubaddress(unsigned char* addrbyte);
extern bool i2c_slave_recvcommand(unsigned char *databyte);

#define I2CSlaveInit()     (TWCR=(1<<TWEA)|(1<<TWEN))

#define I2CChkAck()  	   (TWSR&0xf8)
#define I2CWaitAck() 	   {while(!(TWCR&(1<<TWINT)));}

#define I2CSendAck() 	   (TWCR|=(1<<TWEA))
#define I2CSendByte(x) 	   {TWDR=(x);TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);} 

#define I2CRcvAckByte()    (TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA))

#endif
 

⌨️ 快捷键说明

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