📄 twi.h
字号:
#define pinSCL 5 //PC5 SCL
#define pinSDA 4 //PC4 SDA
#define fSCL 100000 //TWI时钟为100KHz
#define fosc 7372800
#define baud 115200
//预分频系数=1(TWPS=0)
#if fosc < fSCL*36
#define TWBR_SET 10; //TWBR必须大于等于10
#else
#define TWBR_SET (fosc/fSCL-16)/2; //计算TWBR值
//(F_CPU/fSCL-16)/2; //计算TWBR值
#endif
#define TW_ACT (1<<TWINT)|(1<<TWEN)|(1<<TWIE)
//TWCR只能IN/OUT,直接赋值比逻辑运算(|= &=)更节省空间
#define SLA_24CXX 0xA0 //24Cxx系列的厂商器件地址(高四位)
#define ADDR_24C64 0x00
// AT24C64的地址线A2/1/0全部接地,SLAW=0xA0+0x00<<1+0x00,SLAR=0xA0+0x00<<1+0x01
//TWI_操作状态
#define TW_BUSY 0
#define TW_OK 1
#define TW_FAIL 2
//TWI_读写命令状态
#define OP_BUSY 0
#define OP_RUN 1
//TWI读写操作公共步骤
#define ST_FAIL 0 //出错状态
#define ST_START 1 //START状态检查
#define ST_SLAW 2 //SLAW状态检查
#define ST_ADDR 3
#define ST_WADDR 4 //ADDR状态检查
//TWI读操作步骤
#define ST_RESTART 5 //RESTART状态检查
#define ST_SLAR 6 //SLAR状态检查
#define ST_RDATA 7 //读取数据状态检查,循环n字节
//TWI写操作步骤
#define ST_WDATA 8 //写数据状态检查,循环n字节
#define FAIL_MAX 20 //重试次数最大值
#define KEY 0X04
#define FREQ 4
#define TWI_ADDRESS 0X32
#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
/* R/~W bit in SLA+R/W address field. */
#define TW_READ 1
#define TW_WRITE 0
//#define LCD_EN_PORT PORTD
//#define LCD_RW_PORT PORTD
//#define LCD_RS_PORT PORTD
//#define CONTROL_PORT DDRD
//#define LCD_DATA_PORT PORTB
//#define LCD_DATA_DDR DDRB
//#define LCD_DATA_PIN PINB
//定义全局变量
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -