AT89S52-based 24C02 usage

Publisher:WhisperingWishLatest update time:2016-10-24 Source: eefocusKeywords:AT89S52  24C02 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#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);  
 }
}

Keywords:AT89S52  24C02 Reference address:AT89S52-based 24C02 usage

Previous article:8051 MCU C language basics
Next article:ISD4004 voice chip based on AT89S52

Recommended ReadingLatest update time:2024-11-15 16:55

Circuit Design of Ultrasonic Distance Measurement System Based on AT89S52 Single Chip Microcomputer
  Ultrasonic waves are mechanical waves with a frequency of more than 20KHz, and their propagation speed in the air is about 340 m/s (at 20°C). Ultrasonic waves can be generated by ultrasonic sensors. There are two common types of ultrasonic sensors: one is to generate ultrasonic waves electrically, and the other is to
[Microcontroller]
Circuit Design of Ultrasonic Distance Measurement System Based on AT89S52 Single Chip Microcomputer
AT89S52+TLC5615 driver
I. Overview TLC5615 is a 10-bit D/A converter produced by Texas Instruments. It has a single voltage output, and the maximum output voltage can reach twice the reference voltage. It can carry a minimum load of 2K ohms. This chip has a power-on reset function, uses a three-wire serial bus interface, is compatible with
[Microcontroller]
AT89S52+TLC5615 driver
Interface hardware circuit between AT89S52 microcontroller and CC1100
To control the transmitting module and receiving module, the microcontroller must first initialize the interface of the microcontroller (SPI bus interface technology is a high-speed, high-efficiency serial interface technology, mainly used to expand peripherals and exchange data.), Then the radio frequency module need
[Microcontroller]
Interface hardware circuit between AT89S52 microcontroller and CC1100
AT89S52+12MHZ+TEA5767 digital tube display FM radio
/**************************************************************************************************************        AT89S52+12MHZ+TEA5767 digital tube display FM radio This program uses digital tube to display frequency. The crystal oscillator is 12M. It has passed the test. The frequency of the radio is adjusted
[Microcontroller]
Design of a smart home watering device using AT89S52 microcontroller
With the improvement of people's living standards, flowers are gradually favored by people, cultivating sentiment and purifying the air. Using a single-chip microcomputer, a home intelligent watering device is designed to realize automatic watering, saving manpower and facilitating people's business trips without affe
[Microcontroller]
Design of a smart home watering device using AT89S52 microcontroller
Typical applications of AT89S52 microcontroller in washing machines
  AT89S52 is a low-power, high-performance CMOS 8-bit microcontroller with 8K system programmable Flash memory. It is manufactured using Atmel's high-density non-volatile memory technology and is fully compatible with industrial 80C51 product instructions and pins. On-chip Flash allows program memory to be programmabl
[Microcontroller]
Typical applications of AT89S52 microcontroller in washing machines
Temperature display alarm system based on AT89S52 and DS18B20
  introduction   Temperature is one of the most basic environmental parameters. People's living environment is closely related to temperature, and temperature measurement is also of great concern to people. Therefore, it is of great significance to study the temperature measurement method and device, and temperature
[Microcontroller]
Temperature display alarm system based on AT89S52 and DS18B20
Design of temperature automatic control and detection system based on AT89S52
    Farmers in rural areas who are engaged in the large-scale sale of chili peppers must first dry the peppers. At present, they mainly use a closed oven to dry the peppers. It has strict temperature requirements. If the temperature is too high, the peppers will be burnt, and if the temperature is too low, blisters wil
[Microcontroller]
Design of temperature automatic control and detection system based on AT89S52
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号