单片机的常见通讯协议
常见单片机通讯协议
I2c通信
https://blog.csdn.net/yanlaifan/article/details/72843577?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170393696216800226590203%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170393696216800226590203&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-2-72843577-null-null.142^v99^pc_search_result_base3&utm_term=i2c%E9%80%9A%E4%BF%A1%E5%8D%8F%E8%AE%AE%E6%97%B6%E5%BA%8F%E5%9B%BE&spm=1018.2226.3001.4187
硬件层
两根线,SDA(数据)和SCL(时钟)
从最高位到最低位发送(MSB—>LSB)
协议层
协议
- 空闲态
- 起始信号
- 逻辑电平
- 时序图
- 停止位
在时钟线为高时,数据线为高,为1,否则为0
代码
以51单片机为例
I2C.h文件
1 2 3 4 5 6 7 8 9 10 11 12
| #ifndef __I2C_H__ #define __I2C_H__
void I2C_Start(void); void I2C_Stop(void); void I2C_SendByte(unsigned char Byte); unsigned char I2C_ReceiveByte(void); void I2C_SendAck(unsigned char AckBit); unsigned char I2C_ReceiveAck(void);
#endif
|
I2C.cpp文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <REGX52.H>
sbit I2C_SCL=P2^1; sbit I2C_SDA=P2^0;
void I2C_Start(void) { I2C_SDA=1; I2C_SCL=1; I2C_SDA=0; I2C_SCL=0; }
void I2C_Stop(void) { I2C_SDA=0; I2C_SCL=1; I2C_SDA=1; }
void I2C_SendByte(unsigned char Byte) { unsigned char i; for(i=0;i<8;i++) { I2C_SDA=Byte&(0x80>>i); I2C_SCL=1; I2C_SCL=0; } }
unsigned char I2C_ReceiveByte(void) { unsigned char i,Byte=0x00; I2C_SDA=1; for(i=0;i<8;i++) { I2C_SCL=1; if(I2C_SDA){Byte|=(0x80>>i);} I2C_SCL=0; } return Byte; }
void I2C_SendAck(unsigned char AckBit) { I2C_SDA=AckBit; I2C_SCL=1; I2C_SCL=0; }
unsigned char I2C_ReceiveAck(void) { unsigned char AckBit; I2C_SDA=1; I2C_SCL=1; AckBit=I2C_SDA; I2C_SCL=0; return AckBit; }
|
写入与读取1帧的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| void AT24C02_WriteByte(unsigned char WordAddress,Data) { I2C_Start(); I2C_SendByte(AT24C02_ADDRESS); I2C_ReceiveAck(); I2C_SendByte(WordAddress); I2C_ReceiveAck(); I2C_SendByte(Data); I2C_ReceiveAck(); I2C_Stop(); }
unsigned char AT24C02_ReadByte(unsigned char WordAddress) { unsigned char Data; I2C_Start(); I2C_SendByte(AT24C02_ADDRESS); I2C_ReceiveAck(); I2C_SendByte(WordAddress); I2C_ReceiveAck(); I2C_Start(); I2C_SendByte(AT24C02_ADDRESS|0x01); I2C_ReceiveAck(); Data=I2C_ReceiveByte(); I2C_SendAck(1); I2C_Stop(); return Data; }
|
实验验证
4路OLED的驱动
基于I2C通信的陀螺仪