#define uchar unsigned char
#define uint unsigned int
sbit scl=P3^4;
sbit sda=P3^5;
void delay0()//about 4us delay
{
;;
}
void delay(uint z)//about 1ms delay
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void start()//start signal
{
sda=1;
delay0();
scl=1;
delay0();
sda=0;
delay0();
}
void stop()//stop signal
{
sda=0;
delay0();
scl=1;
delay0();
sda=1;
delay0();
}
void respons()//response signal
{
uchar i;
scl=1;
delay0();
while(sda==1&&i<250)i++;//Wait for response
scl=0;
delay0();
}
void write_byte(uchar date)//Write data, write one byte
{
uchar i;
scl=0;
delay0();
for(i=0;i<8;i++)
{
date=date<<1;
sda=CY;//Move the highest bit into the CY bit of PSW
delay0();
scl=1;
delay0();
scl=0;
delay0();
}
sda=1;//Release the sda line
delay0();
}
uchar read_byte()//read data, read one byte
{
uchar i,j;
scl=0;
delay0();
sda=1;
delay0();
for(i=0;i<8;i++)
{
scl=1;
delay0();
j=(j<<1)|sda;
scl=0;
delay0();
}
return j;
}
void write_add(uchar address,uchar date)//write one byte of data to a certain address
{
start();//start signalwrite_byte
(0xa0);//device address+0, i.e. 1010 000 0, the last '0' is the write direction bitrespons
();//responsewrite_byte
(address);//write the first address of the devicerespons
();
write_byte(date);//write datarespons
();
stop();//stop signal
}
uchar read_add(uchar address)//read data from a certain address
{
uchar l;
start(); //start signal
write_byte(0xa0); //device address + 0, i.e. 1010 000 0, the last '0' is the write direction bit
respons();
write_byte(address); //first address to be read
respons();
start(); //start signal again
write_byte(0xa1); //device address + 1, i.e. 1010 000 1, the last '1' is the read direction bit
respons();
l=read_byte(); //read data
stop();
return l; //return data
}
void init_24C02() //24C02 initialization
{
sda=1; //release data line
delay0();
scl=1; //release clock line
delay0();
}
void main()
{
init_24C02();
while(1)
{
write_add(1,0x00);
delay(100); //Delay is necessary because there needs to be a certain time interval between writing and reading the device, otherwise it will not react quickly
P2=read_add(1); //Read the data in 24C02, "1" is its storage address
while(1);
}
}
Previous article:8051 MCU C language basics
Next article:ISD4004 voice chip based on AT89S52
Recommended ReadingLatest update time:2024-11-15 16:55
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Plant growth monitoring system source code based on Raspberry Pi 5
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers 3rd Edition (Zhang Yigang)
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- ESP32-Cam Programmer
- NXP Rapid IoT Review] + ⑤ Full Interpretation of NXP Rapid IoT Code, Finding Traces of RTC
- Boosting the electric era, explore the three major product technologies of electric vehicles with Infineon
- Getting Started with Using TouchGFX to Develop STM32 Interface Applications (Part 2) - Button and Message Response Routines
- Request: Recommend a PHY chip for stm32f407!
- 【EasyARM-RT1052 Review】+ Unboxing Experience
- Practical RF training course sharing 2
- Looking for books that explain basic circuits?
- 60W Sensorless BLDC Motor Driver Reference Design
- DGUS T5UID1 implements modbus protocol