/**************************************
Main chip: STC12C5A60S2 (1T)
Working frequency: 12.000MHz
**************************************** /
#include"REG51.H"
#include"INTRINS.H"
typedefunsignedcharBYTE;
typedefunsignedshortWORD;
sbitSCL=P3^4;//clock of AT24C04
sbitSDA=P3^5;//data of AT24C04
BYTEBUF[16];//data buffer
BYTEcodeTESTDATA[]=
{
0x00,0x11,0x22,0x33,0x44 ,0x55,0x66,0x77,
0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF
};
voidDelay5us();
voidDelay5ms();
voidAT24C04_Start();
voidAT24C04_Stop();
voidAT24C04_SenDACK(bitack);
bitAT24C04_Rec vACK();
voidAT24C04_SendByte (BYTEdat);
BYTEAT24C04_RecvByte();
voidAT24C04_ReadPage();
voidAT24C04_WritePage();
voidmain()
{
AT24C04_WritePage();
Delay5ms();
AT24C04_ReadPage();
while(1);
}
/*********** ***************************
Write 1 page (16 bytes) of data to AT24C04
and write the 16 test data starting from TESTDATA as the device 00~0F address
******************************************/
voidAT24C04_WritePage()
{
BYTEi;
AT24C04_Start();//Start signal
AT24C04_SendByte(0xa0);//Send device address + write signal
AT24C04_SendByte(0x00);//Send storage unit address
for (i=0; i《16; i++)
{
BUF[i] = AT24C04_RecvByte();
if (i == 15)
{
AT24C04_SendACK(1); //The last data needs to be NAK
}
else
{
AT24C04_SendACK(0); //Response ACK
}
}
AT24C04_Stop(); //Stop signal
}
/**************************************
Delay 5 microseconds (STC12C5A60S2@12M)
Different working environments require adjusting this function
This delay function is calculated using 1T instruction cycle, which is different from the traditional 12T MCU.
**************************************/
void Delay5us()
{
BYTE n = 4;
while (n--)
{
_nop_();
_nop_();
}
}
/**************************************
Delay 5 milliseconds (STC12C5A60S2@12M)
Different working environments require adjusting this function
This delay function is calculated using 1T instruction cycle, which is different from the traditional 12T MCU.
**************************************/
void Delay5ms()
{
BE n = 2500;
while (n--)
{
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
}
/**************************************
start signal
**************************************/
void AT24C04_Start()
{
SDA = 1; //Pull the data line high
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SDA = 0; //Generate falling edge
Delay5us(); //Delay
SCL = 0; //pull the clock line low
}
/**************************************
stop signal
**************************************/
void AT24C04_Stop()
{
SDA = 0; //Pull the data line low
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SDA = 1; //Generate rising edge
Delay5us(); //Delay
}
/**************************************
Send acknowledgment signal
Entry parameters: ack (0:ACK 1:NAK)
**************************************/
void AT24C04_SendACK(bit ack)
{
SDA = ack; //Write response signal
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SCL = 0; //pull the clock line low
Delay5us(); //Delay
}
/**************************************
receive response signal
**************************************/
bit AT24C04_RecvACK()
{
SCL = 1; //pull the clock line high
Delay5us(); //Delay
CY = SDA; //Read response signal
SCL = 0; //pull the clock line low
Delay5us(); //Delay
return CY;
}
/**************************************
Send one byte of data to the IIC bus
**************************************/
void AT24C04_SendByte(BYTE dat)
{
SWITCH in;
for (i=0; i<8; i++) //8-bit counter
{
dat <<= 1; //Move out the highest bit of data
SDA = CY; //Send data port
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SCL = 0; //pull the clock line low
Delay5us(); //Delay
}
AT24C04_RecvACK();
}
/**************************************
Receive one byte of data from the IIC bus
**************************************/
BYTE AT24C04_RecvByte()
{
SWITCH in;
BYTE dat = 0;
SDA = 1; //Enable internal pull-up and prepare to read data
for (i=0; i<8; i++) //8-bit counter
{
that <<= 1;
SCL = 1; //pull the clock line high
Delay5us(); //Delay
dat |= SDA; //Read data
SCL = 0; //pull the clock line low
Delay5us(); //Delay
}
return that;
}
Previous article:51 microcontroller programming skills combining assembly and C language
Next article:CAN bus becomes the bus standard for vehicle electrical systems
Recommended ReadingLatest update time:2024-11-15 08:19
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Inertial instrument testing and data analysis (Yan Gongmin)
- Basics of Machine Learning: From Getting Started to Job Hunting (Hu Huanwu)
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- RTL8382M/RTL8380M Industrial-grade managed embedded full Gigabit switch core module
- 05 Serial port experience and receiving variable-length data
- IC card swiping technology under the screen
- AC detector
- Selection and application of freewheeling diode
- Challenges 5G Brings to Base Station RF PA Design
- Division circuit
- Android smart wearable device development from entry to mastery pdf download
- Why can't we develop industry software like Matlab?
- Android control LED-2-JNI basis of SinlinxA33 development board