94 scl=0; // Pull scl low to facilitate updating sda data in the next clock cycle
95 delay();
96 }
97 return k; // Read 1 byte of data from sda
98 }
99
100 // (6) Write a byte of data to a certain address (such as EEPROM)
101 void write_add(uchar address,uchar date)
102 {
103 start(); // The host sends a start signal
104 write_byte(0xa0); // Host writes device address
105 ack(); // Slave responds
106 write_byte(address); // The host writes the address of the data to be written
107 ack(); // Slave responds
108 write_byte(date); // Host writes data
109 ack(); // Slave responds
110 stop();
111 }
112 // (7) Read one byte of data from a certain address (such as EEPROM)
113 uchar read_add(uchar address)
114 {
115 flying dates;
116 start();
117 write_byte(0xa0); // write device address, write mode
118 ack();
119 write_byte(address); // write the address of the read data
120 ack();
121 start();
122 write_byte(0xa1); // write device address, read mode
123 ack();
124 date=read_byte(); // Data read from a specific address
125 stop();
126 return date;
127 }
6. Complete code (taking 51 as an example)
#include
#define uchar unsigned char
sbit sda=P2^0;
sbit scl=P2^1;
flying a;
void delay()
{ ;; }
void start() // start signal
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() // Stop
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() // Response
{
flying i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void init()
{
sda=1;
delay();
scl=1;
delay();
}
void write_byte(uchar date)
{
flying i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
flying i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void delay1(fly x)
{
fly a,b;
for(a=x;a>0;a--)
for(b=100;b>0;b--);
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
response();
write_byte(address);
response();
write_byte(date);
response();
stop();
}
uchar read_add(uchar address)
{
flying date;
start();
write_byte(0xa0);
response();
write_byte(address);
response();
start();
write_byte(0xa1);
response();
date=read_byte();
stop();
return date;
}
void main()
{
heat();
write_add(23,0xaa);
delay1(100); // Add delay to prevent timing errors
P1=read_add(23);
while(1);
}
Previous article:Usage of 8052 Timer 2
Next article:Counter based on STC51 microcontroller
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Conversion between CAD, word, and excel?
- STM32MP157A-DK1 Discovery Kit
- Please tell me the quiescent current of chips such as TM1638 and TM1629
- Magnetic field data collected by the LIS2DML array board
- Looking for senior engineers in electronics related industries, part-time, no need to work in the office
- ABI Research: By 2025, 514 million mobile phones will support UWB
- Why does the Q value of silver-plated wires have a higher value than that of enameled wires? --- How does the change of frequency affect the change of L and Q? Actual measurement comparison
- Global IoT chip market expected to reach $80 billion by 2025
- Things to note when selecting and using a POE switch
- MSP430 library hardware multiplier usage