#include
#include //My custom LCD1602 header file
/*---------------------------------------------------------------
24C32 can store 4K (1024*4) bytes (8 bits), so the maximum addressing address is 0x0FFF, and 24C32 is a slave.
----------------------------------------------------------------*/
#defineWriteDeviceAddress0x0A2 //Define the write address of the device in the I2C bus (Note: change according to the custom slave address interface)
#defineReadDviceAddress0x0A3 //Define the read address of the device in the I2C bus (Note: change according to the custom slave address interface)
sbitSCL=P0^4; //My 24C32 interface settings
sbitSDA=P3^7;
//2us delay subroutine
voiddelay_2us(uchari)
{
while(--i);
}
// Start the bus
voidStart()
{
SDA=1;
SCL=1;
delay_2us(1);
SDA=0;
delay_2us(1);
SCL=0;
}
//End the bus
voidStop()
{
SDA=0;
SCL=1;
delay_2us(1);
SDA=1;
delay_2us(1);
SDA=0;
SCL=0;
}
//Send the response signal
//voidMACK()
//{
//SDA=0;
//SCL=1;
//delay_2us(1);
//SCL=0;
//SDA=1;
//}
//Send a no-acknowledge bit signal
voidMNACK()
{
SDA=1;
SCL=1;
delay_2us(1);
SCL=0;
SDA=0;
}
//Detect slave response signal
bitCACK()
{
bitflag;
SDA=1;
SCL=1;
delay_2us(1);
flag=SDA;
SCL=0;
return(flag);
}
/*--------------------------------------------------------------------------
//Write 8 bits to 24C32
---------------------------------------------------------------------------*/
Write8Bit(ucharinput)
{
uchartemp;
for(temp=0;temp《8;temp++)
{
SDA=(bit)(input&0x80);
SCL=1;
delay_2us(1);
SCL=0;
input=input《《1;
}
}
/*--------------------------------------------------------------------------
//Write a byte into 24C32
---------------------------------------------------------------------------*/
voidWrite24C32(ucharch,uintaddress)
{
Start();
Write8Bit(WriteDeviceAddress);
while(CACK());
Write8Bit(address/256); (Note: the address is sent twice, with the high bit first!)
while(CACK());
Write8Bit(address%256);
while(CACK());
Write8Bit(ch);
while(CACK());
Stop();
}
/*--------------------------------------------------------------------------
//Read 8 bits from 24C32
---------------------------------------------------------------------------*/
ucharRead8Bit()
{
unsignedchartemp,rbyte=0;
for(temp=0;temp《8;temp++)
{
SDA=1;
SCL=1;
rbyte=rbyte|((unsignedchar)(SDA));
if (temp < 7) // Note: only shift left 7 times
{
rbyte=rbyte《《1;
}
SCL=0;
}
return(rbyte);
}
/*--------------------------------------------------------------------------
Read 1 byte from 24c32
---------------------------------------------------------------------------*/
ucharRead24C32(uintaddress)
{
ucharch;
Start();
Write8Bit(WriteDeviceAddress);
while(CACK());
Write8Bit(address/256);
while(CACK());
Write8Bit(address%256);
while(CACK());
Start();
Write8Bit(ReadDviceAddress);
while(CACK());
ch=Read8Bit();
MNACK();
Stop();
return(ch);
}
/*--------------------------------------------------------------------------
Main function
---------------------------------------------------------------------------*/
voidmain(void) //main program
{
high rise;
LCD1602_init(); //1602 initialization
Write24C32 ('8', 0x01FF); // write '5' into 0x1FF
LCD1602_puts(0,0,“24C32W_TestOK!”);
newchar = Read24C32 (0x01FF); // Receive data from 0x1FF
LCD1602_puts(0,1,“ReadValue=”);
LCD1602_puts(10,1,newchar);
while(1);
}
Previous article:nRF24L01 wireless module schematic and connection with 5V microcontroller
Next article:Sharing of DC motor PWM speed control program based on AT89C51 single chip microcomputer
- Popular Resources
- Popular amplifiers
- 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
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- It’s too difficult for beginners.
- Help converting brd file to AD pcbdoc file
- Circuit Principle
- MSP430 interrupt priority and interrupt nesting
- A novice asks about the selection of optocouplers for PWM isolation control of MOS tubes
- Signal Generator and DA Conversion FPGA Case Tutorial
- IAR Mini Classroom | How to place a group of functions or variables in a specific segment
- Are there any FPGA weekend training courses in Xi'an?
- When an oscilloscope measures and amplifies weak signals, the waveform will fluctuate when the human body passes through the oscilloscope, amplifying circuit or signal source.
- LM25119 has no output