void delay()
{; ;}
void delay1ms ( uint z )
{
uint x, y;
for ( x = z; x >
0; x -- ) for ( y = 110; y > 0; y -- );
}
I2C startup: falling edge on SDA while SCL is high
void start_I2C ( ) /
{
sda = 1;
delay ();
scl = 1;
delay ();
sda = 0;
delay ();
}
I2C stop: A rising edge occurs on SDA during SCL high level
void stop_I2C () //Stop
{
sda = 0;
delay ();
scl = 1;
delay ();
sda = 1;
delay ();
}
Bus initialization: pull the bus high to release the bus
void init_I2C ()
{
sda = 1;
delay ();
scl = 1;
delay ();
}
Answer signal: During the high level of SCL, SDA is pulled low by the slave device to indicate the answer.
void response ()//Response
{
uchar i;
scl = 1;
delay ();
while ( ( sda == 1 ) && ( i < 250 ) ) i ++; // i < 255 here only means that there is no response, If i > 255, exit directly to avoid an infinite loop.
scl = 0;
delay ();
}
Write a byte: the high or low level on SDA can only change during the low period of SCL
void write_byte (uchar date)
{
uchar i, temp;
temp = date;
for ( i = 0; i < 8; i ++ )
{
temp = temp << 1; //Move the highest bit into CY
scl = 0;
delay ();
sda = CY;
delay ();
scl = 1;
delay ();
}
scl = 0;
delay ();
sda = 1;
delay ();
}
Read a byte:
uchar read_byte ()
{
uchar i, k;
scl = 0;
delay ();
sda = 1;
delay ();
for ( i = 0; i < 8; i ++ )
{
scl = 1;
delay ();
k = ( k << 1 ) | sda; // Also read from the highest bit
scl = 0;
delay ();
}
return k;
}
Previous article:Various working registers (timer/interrupt)
Next article:A little knowledge about MCU
- 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
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Brief Analysis of Automotive Ethernet Test Content and Test Methods
- Interpretation and test application of IEC harmonic standards
- VICOR engineers invite you to chat: How to improve the throughput and running time of automatic test equipment?
- About MSP430-Timer WDT
- Does anyone know the pin map of the YC1021 chip?
- Why Japan failed to kill South Korea's semiconductor industry
- How to select all device bit numbers or nominal values in the SCH file for AD1904 version
- Protocols and services required for the WiFi authentication process
- Testing methods and diagnostic analysis for DSP-containing circuit boards
- Fundamentals of Circuits and Analog Electronics Technology
- LAUNCHXL-F28379D comprehensive routine