📄 i2c.c
字号:
#include <at89x52.h>#include "i2c.h"/* Generate the start condition */void I2C_Start(){ I2C_NOP(); I2C_SCL_CLR; I2C_NOP(); I2C_SDA_SET; I2C_SCL_SET; I2C_NOP(); I2C_SDA_CLR; I2C_NOP(); I2C_SCL_CLR; I2C_NOP();} /* Generate the stop condition */void I2C_Stop(){ I2C_NOP(); I2C_SCL_CLR; I2C_NOP(); I2C_SDA_CLR; I2C_NOP(); I2C_SCL_SET; I2C_NOP(); I2C_SDA_SET; I2C_NOP(); I2C_SCL_CLR; I2C_NOP();} /* Master send a byte to the slave device */void I2C_Send_Byte(unsigned char byte){ unsigned char mask; /* Send the byte from MSB to LSB */ for (mask = 0x80 ; mask > 0 ; mask = mask >> 1 ) { I2C_SCL_CLR; if (byte & mask) I2C_SDA_SET; else I2C_SDA_CLR; I2C_NOP(); I2C_SCL_SET; I2C_NOP(); I2C_SCL_CLR; I2C_NOP(); } /* Check the Acknowledge from slave device */ I2C_SDA_SET; I2C_NOP(); I2C_SCL_SET; I2C_NOP(); while(I2C_SDA_GET == I2C_NO_ACK); I2C_NOP(); I2C_SCL_CLR; I2C_NOP(); I2C_SDA_SET; // Release the Bus I2C_NOP();} /* Master send the slave address */void I2C_Send_Addr(unsigned char addr, unsigned char dir){ I2C_Send_Byte(addr + dir);}/* Master get a byte from the slave device */unsigned char I2C_Get_Byte(unsigned char last){ unsigned char mask; unsigned char retVal; retVal = 0; /* initial the return value */ for (mask = 0x80 ; mask != 0 ; mask = mask >> 1) { I2C_NOP(); I2C_SCL_SET; I2C_NOP(); if(I2C_SDA_GET) retVal = retVal | mask; I2C_NOP(); I2C_SCL_CLR; I2C_NOP(); } if ( last == 1) I2C_SDA_SET; /* No Acknowledge */ else I2C_SDA_CLR; /* Acknowledge */ I2C_NOP(); I2C_SCL_SET; I2C_NOP(); I2C_SCL_CLR; I2C_NOP(); I2C_SDA_SET; // Release the bus I2C_NOP(); return retVal;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -