This program is the bottom-level C subroutine of the I2C operating platform (master mode software platform), such as sending data
and receiving data, sending response bits, and providing several operating functions directly facing the device. It is very convenient
to connect and expand with user programs...
After my own experience, it is absolutely easy to use!
/
bit ack; /*Response flag*/
/*
Start bus function
Function prototype: void Start_I2c();
Function: Start I2C bus, that is, send I2C start condition.
/
void Start_I2c()
{
SDA=1; /*Send data signal of start condition*/
_Nop();
SCL=1;
_Nop(); /*Start condition establishment time is greater than 4.7us, delay*/
_Nop();
_Nop();
_Nop();
_Nop();
SDA=0; /*Send start signal*/
_Nop(); /*Start condition locking time is greater than 4μs*/
_Nop();
_Nop();
_Nop();
_Nop();
SCL=0; /*Clamp I2C bus, prepare to send or receive data*/
_Nop();
_Nop();
}
/*
End bus function
Function prototype: void Stop_I2c();
Function: End I2C bus, that is, send I2C end condition.
/
void Stop_I2c()
{
SDA=0; /*Send data signal of end condition*/
_Nop(); /*Send clock signal of end condition*/
SCL=1; /*End condition establishment time is greater than 4μs*/
_Nop();
_Nop();
_Nop();
_Nop();
_Nop();
SDA=1; /*Send I2C bus end signal*/
_Nop();
_Nop();
_Nop();
_Nop();
}
/*
Byte data transmission function
prototype: void SendByte(uchar c);
Function: Send data c, which can be an address or data. After sending, wait for a response and operate
on this status bit. (No response or non-response makes ack=0 false)
Sending data is normal, ack=1; ack=0 means that the controlled device has no response or is damaged.
/
void SendByte(uchar c)
{
uchar BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) /*The length of the data to be transmitted is 8 bits*/
{
if((c<
_Nop();
SCL=1; /*Set the clock line high to notify the controlled device to start receiving data bits*/
_Nop();
_Nop(); /*Ensure that the clock high level period is greater than 4μs*/
_Nop();
_Nop()
; _Nop();
SCL=0;
}
_Nop();
_Nop();
SDA=1; /*Release the data line after 8 bits are sent and prepare to receive the acknowledgement bit*/
_Nop();
_Nop();
SCL=1;
_Nop();
_Nop();
_Nop();
if(SDA==1)ack=0;
else ack=1; /*Judge whether the response signal is received*/
SCL=0;
_Nop();
_Nop();
}
/*
Byte data transfer
function prototype: uchar RcvByte();
Function: Used to receive data from the device and judge bus error (no response signal is sent).
Please use the response function after sending.
/
uchar RcvByte()
{
uchar retc;
uchar BitCnt;
retc=0;
SDA=1; /*Set the data line to input mode*/
for(BitCnt=0;BitCnt<8;BitCnt++)
{
_Nop();
SCL=0; /*Set the clock line low to prepare for receiving data bits*/
_Nop();
_Nop(); /*Clock low level period is greater than 4.7μs*/
_Nop();
_Nop();
_Nop();
SCL=1; /*Set the clock line high to make the data on the data line valid*/
_Nop();
_Nop();
retc=retc<<1;
if(SDA==1)retc=retc+1; /*Read the data bit, and put the received data bit into retc*/
_Nop();
_Nop();
}
SCL=0;
_Nop();
_Nop();
return(retc);
}
/
Response subfunction
Prototype: void Ack_I2c(bit a);
Function: The master controller sends a response signal (can be a response or non-response signal)
/
void Ack_I2c(bit a)
{
if(a==0)SDA=0; /*Send a response or non-response signal here*/
else SDA=1;
_Nop(); _Nop()
;
_Nop();
SCL=1;
_Nop();
_Nop(); /*Clock low level period is greater than 4μs*/
_Nop();
_Nop();
_Nop();
SCL=0; /*Clear the clock line and clamp the I2C bus to continue receiving*/
_Nop();
_Nop();
}
/*
Send byte data to the device without sub-address Function
prototype: bit ISendByte(uchar sla,ucahr c);
Function: The whole process from starting the bus to sending address, data, and ending the bus, from the device address sla.
If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit ISendByte(uchar sla,uchar c)
{
Start_I2c(); /*Start the bus*/
SendByte(sla); /*Send device address*/
if(ack==0)return(0);
SendByte(c); /*Send data*/
if(ack==0)return(0);
Stop_I2c(); /*End the bus*/
return(1);
}
/*
Send multi-byte data to a sub-address device Function
prototype: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);
Function: The whole process from starting the bus to sending address, sub-address, data, and ending the bus, from the device
address sla, sub-address suba, the content to be sent is the content pointed to by s, and no bytes are sent.
If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit ISendStr(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
Start_I2c(); /*Start bus*/
SendByte(sla); /*Send device address*/
if(ack==0)return(0);
SendByte(suba); /*Send device subaddress*/
if(ack==0)return(0);
for(i=0;i
SendByte(*s); /*Send data*/
if(ack==0)return(0);
s++;
}
Stop_I2c(); /*End the bus*/
return(1);
}
/*
Read byte data from a device without subaddress Function
prototype: bit IRcvByte(uchar sla,ucahr *c);
Function: The whole process from starting the bus to sending the address, reading data, and ending the bus, from the device
address sla, the return value is in c.
If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit IRcvByte(uchar sla,uchar *c)
{
Start_I2c(); /*Start the bus*/
SendByte(sla+1); /*Send the device address*/
if(ack==0)return(0);
*c=RcvByte(); /*Read data*/
Ack_I2c(1); /*Send the non-answer bit*/
Stop_I2c(); /*End the bus*/
return(1);
}
/*
Function to read multi-byte data from a sub-address device Function
prototype: bit ISendStr(uchar sla,uchar suba,ucahr *s,uchar no);
Function: From starting the bus to sending the address, sub-address, reading data, and ending the bus, from the device
address sla, sub-address suba, the read content is put into the storage area pointed to by s, and no bytes are read.
If 1 is returned, it means the operation is successful, otherwise the operation is wrong.
Note: The bus must be ended before use.
/
bit IRcvStr(uchar sla,uchar suba,uchar *s,uchar no)
{
uchar i;
Start_I2c(); /*Start bus*/
SendByte(sla); /*Send device address*/
if(ack==0)return(0);
SendByte(suba); /*Send device subaddress*/
if(ack==0)return(0);
Start_I2c();
SendByte(sla+1);
if(ack==0)return(0);
for(i=0;i
*s=RcvByte(); /*Send data*/
Ack_I2c(0); /*Send acknowledge bit*/
s++;
}
*s=RcvByte();
Ack_I2c(1); /*Send no acknowledge bit*/
Stop_I2c(); /*End the bus*/
return(1);
}
Previous article:51 MCU combined with NRF24L01 to wirelessly control the servo
Next article:51 MCU interrupt 1 interrupt overall introduction
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Matrix keyboard program problem
- [GD32L233C-START Review] (4) LPTIMER wake-up trigger ADC in Sleep low power mode
- [Qinheng RISC-V core CH582] Development environment installation
- High salary recruitment: European sales manager (German required)
- Application Notes on Flash Serial Programming for HuaDa MCU F003/F005/L110 Series
- MSP430 register detailed classification
- EEWORLD University Hall----Open Source PWM Robotic Arm (Arduion Version)
- 1MHz square wave double frequency
- CC3200 Modify the sprinkler routine of Out of Box
- Upgrading the embedded RISC-V compiler