C51:24C02 write and read one byte

Publisher:勾剑寒Latest update time:2016-10-14 Source: eefocusKeywords:C51  24C02 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include // Header file containing 51 MCU register definitions

#include //Header file containing _nop_() function definition
#define OP_READ 0xa1 //Device address and read operation, 0xa1 is 1010 0001B
#define OP_WRITE 0xa0 //Device address and write operation, 0xa1 is 1010 0000B
sbit SDA=P3^4; //Define the serial data bus SDA bit as the P3.4 pin
sbit SCL=P3^3; //Define the serial clock bus SDA bit as the P3.3 pin


//Function: Delay 1ms (3j+2)*i=(3×33+2)×10=1010 (microseconds), which can be considered as 1 millisecond
void delay1ms()
{
   unsigned char i,j; 
  for(i=0;i<10;i++)
   for(j=0;j<33;j++)
    ;   
 }


//Function: delay for several milliseconds
 void delaynms(unsigned char n)
 {
   unsigned char i;
 for(i=0;i     delay1ms();
 }

//Function: start data transmission

void start()
{
 SDA = 1; //SDA is initialized to high level "1"
   SCL = 1; //When starting data transmission, SCL is required to be high level "1"
 _nop_(); //Wait for one machine cycle
 _nop_(
 ); //Wait for one machine cycle
 _nop_(); //Wait for one machine cycle
 SDA = 0; //The falling edge of SDA is considered to be the start signal
 _nop_(); //Wait for one machine cycle
 _nop_
 (); //Wait for one machine cycle _nop_
 (); //Wait for one machine cycle
 SCL = 0; //When SCL is low, the data on SDA is allowed to change (that is, subsequent data transmission is allowed)  
}
//Function: End data transmission 
void stop()
// Stop bit
{
 SDA = 0; //SDA is initialized to low level "0" _n
 SCL = 1; //When ending data transmission, SCL is required to be high level "1"
 _nop_(); //Wait for a machine cycle_nop_
 (); //Wait for a machine cycle_nop_
 (); //Wait for a machine cycle_nop_
 (); //Wait for a machine cycleSDA
 = 1; //The rising edge of SDA is considered the end signal_nop_
 (); //Wait for a machine cycle_nop_
 (); //Wait for a machine cycle_nop_
 (); //Wait for a machine cycle_nop_
 (); //Wait for a machine cycleSDA
 =0;
 SCL=0;
}

//Function: Read a byte from AT24Cxx
unsigned char ReadData()
// Move data from AT24Cxx to MCU
{
 unsigned char i;
 unsigned char x; //Store data read from AT24Cxx
 for(i = 0; i < 8; i++)
 {
  SCL = 1; //Set SCL to high level
  x<<=1; //Shift each binary bit in x one position to the left
  x|=(unsigned char)SDA; //Store the data on SDA into x through bitwise "OR" operation
  SCL = 0; //Read data on the falling edge of SCL
 }
 return(x); //Return the read data
}

//Answer check
bit ack()
{
 bit ack_bit;
 SDA = 1; // The sending device (host) should release the SDA line during the high level period of the clock pulse (SCL=1),
                 //so that the SDA line can be controlled by the receiving device (AT24Cxx)
 _nop_(); //Wait for a machine cycle 
 _nop_(); //Wait for a machine cycle 
 SCL = 1; //According to the above regulations, SCL should be high
 _nop_(); //Wait for a machine cycle _nop_(); //Wait for a 
 machine cycle _nop_ 
 (); //Wait for a machine cycle 
 _nop_(); //Wait for a machine cycle 
 ack_bit = SDA; //The receiving device (AT24Cxx) sends a low level to SDA, indicating that a byte has been received.
                //If a high level is sent, it means that it has not been received and the transmission is abnormal
 SCL = 0; //When SCL is low, the data on SDA is allowed to change (that is, subsequent data transmission is allowed)
 return ack_bit; 
}

//Write a byte
//Before calling this data write function, you need to call the start function start() first, so SCL=0
void WriteCurrent(unsigned char y)
{
 unsigned char i;
    for(i = 0; i < 8; i++) // Shift in 8 bits in a loop
 {
     SDA = (bit)(y&0x80); //Send the highest bit of data to S through the bitwise "AND" operation
                                    //Because the high bit is in front and the low bit is in the back during transmission
  _nop_(); //Wait for one machine cycle   
    SCL = 1; //Write data to AT24Cxx on the rising edge of SCL      
    _nop_(); //Wait for one machine cycle 
   _nop_(); //Wait for one machine cycle       
  
    SCL = 0; //Reset SCL to low level to form the 8 pulses required to transmit data on the SCL line
  y <<= 1; //Shift each binary bit in y one bit to the left
 }
    //Return AT24Cxx acknowledgement bit
}

 

/***************************************************
Function: Main function
************************************************/
main(void)
{
 //Data writing process:
 //Start-write chip addressing-response-write data first address-response-write data-response-termination signalstart
 (); //Start data transferWriteCurrent
 (0xa0); //Select the AT24Cxx chip to be operated, and inform it to write dataack
 ();
 WriteCurrent(0x36); //Write to the specified address 0x36
 ack();

 WriteCurrent(0x00); //Write data 0x00 to the current address (the address specified above)
 ack();
 stop(); //Stop data transfer
 delaynms(4); 


 //Read data flow:
 //Start-write chip addressing-response-write data first address-response-restart-write chip addressing-response-read datastart
 ();                      
 WriteCurrent(0xa0); //Select the AT24Cxx chip to be operated, and inform it to write dataack
 ();
 WriteCurrent(0x36); //Read data addressack
 ();

 start();              
 WriteCurrent(0xa1); //Select the AT24Cxx chip to be operated and tell it to read its data
 ack();
 P1=ReadData(); //Store the read data into P1
 stop(); //Stop data transmission

}

Keywords:C51  24C02 Reference address:C51:24C02 write and read one byte

Previous article:C51 timer to measure pulse width
Next article:c51: Inspection DS18B20

Recommended ReadingLatest update time:2024-11-16 14:28

Various patterns of running lights c51 program based on 51 single chip microcomputer
/*----------------------------------------------- Function: The running light moves and flashes symmetrically (double flashing) ----------------------------------------*/ #include REG52.H #define uint unsigned int void delay(uint); main() { uint comp1=0xfe; uint comp2=0x80;
[Microcontroller]
C51 Programming 25-Application (MCU and computer realize WiFi communication)
This article implements data communication between the microcontroller and the computer using the ESP-01S wifi module.  Set the baud rate of the WiFi module  Since the default baud rate of the ESP-01S wifi module is 115200, and the baud rate of the 51 microcontroller is usually set at 9600, it is necessary to set
[Microcontroller]
C51 Programming 25-Application (MCU and computer realize WiFi communication)
KEILC51 compilation problem ERRORL104: MULTIPLEPUBLICDEFINITIONS repeated definition
Today, my junior high school sister's program has a BUG. The reason is that the same variable is used in two C files, but the declaration is wrong. After finding the problem, I found that I must have used extern to declare global variables, but it still reported an error. I remember that it worked fine before. Dec
[Microcontroller]
Very simple 8*8LED dot matrix c51 source code
/* Experimental purpose: Learn the 8*8 dot matrix dynamic scanning method. */ /* Experimental phenomenon: The 8*8 dot matrix light column first displays dynamically from 0 to 9 at a certain interval. */ /*【Copyright】Copyright(C)铁牛 All Rights Reserved */ /*【Statement】This program is only for learning an
[Microcontroller]
Keil5 installation tutorial (including C51 and MDK coexistence) WIN10 tested and available
System environment: WIN10, tested and available. Friends who encounter problems with installation are welcome to harass my personal public account. As a low-level hardware worker, I often need to use stm32 and 51 microcontrollers, so I switch compilers back and forth between keil for C51 and mdk. It's really suffoca
[Microcontroller]
Keil5 installation tutorial (including C51 and MDK coexistence) WIN10 tested and available
Explanation of code, data, bdata, idata, xdata, and pdata in Keil C51
The 8051 structure provides the user with 3 different storage spaces, program memory ROM, data memory RAM (internal RAM and external RAM). Keil C51 defines different storage types through the following keywords to ensure that users can access the entire storage space of the 51 architecture. code: Access the progra
[Microcontroller]
Explanation of code, data, bdata, idata, xdata, and pdata in Keil C51
Serial communication between Labview and C51 microcontroller
There are two methods of Labview serial communication, one is to use visa (virtual instrument software framework), and the other is to call activeX control. The first method (VISA) program is shown in the figure First, set the serial port parameters, initialize the serial port, use VISA WITE in the loop to writ
[Microcontroller]
Single chip computer two machine communication c51 program
Project name: Dual-machine communication  Description: This program can realize mutual communication between two single-chip microcomputers; the serial port adopts mode 1         , 10-bit asynchronous reception and transmission, the baud rate is variable, and it is controlled by timer 1; and it contains checksum  Pro
[Microcontroller]
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号