#define uchar unsigned char //Define for easy use
#define uint unsigned int
#define ulong unsigned long
#define WriteDeviceAddress 0xa0 //Define the address of the device in the IIC bus
#define ReadDviceAddress 0xa1
sbit SCL=P2^7;
sbit SDA=P2^6;
sbit P20=P2^0;
//Timing function
void DelayMs(unsigned int number)
{
unsigned char temp;
for(;number!=0;number--)
{
for(temp=112;temp!=0;temp--) ;
}
}
//Start bus
void Start()
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
//End bus
void Stop()
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
//Send ACK0
void NoAck()
{
SDA=1;
SCL=1;
SCL=0;
}
//Test ACK
bit TestAck()
{
bit ErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit);
}
//Write 8 bits to 24c02
Write8Bit(unsigned char input)
{
unsigned char temp;
for(temp=8;temp!=0;temp--)
{
SDA=(bit)(input&0x80);
SCL=1;
SCL=0;
input=input<<1;
}
}
//Write a byte to 24c02
void Write24c64(uchar ch,uchar address)
{
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(address);
TestAck();
Write8Bit(ch);
TestAck();
Stop();
DelayMs(10);
}
//Read 8 bits from 24c02
uchar Read8Bit()
{
unsigned char temp,rbyte=0;
for(temp=8;temp!=0;temp--)
{
SCL=1;
rbyte=rbyte<<1;
rbyte=rbyte|((unsigned char)(SDA));
SCL=0;
}
return(rbyte);
}
//Read 1 byte from 24c02
uchar Read24c64(uchar address)
{
uchar ch;
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(address);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
ch=Read8Bit();
NoAck();
Stop();
return(ch);
}
//This lesson tests writing a byte to 24c02 and reading it out for verification
void main(void) // Main program
{
uchar c1;
uchar address =
for ( address =
{
Write24c64(0x00,address); // Write 0x88 to the second address space of 24c02
c1=Read24c64(address);
}
P20=0;
while(1); // Program hangs
}
Previous article:Output 1KHz and 500Hz audio signals to drive speakers
Next article:Course Design of 51 Single Chip Microcomputer Controlled Traffic Lights
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [RVB2601 Creative Application Development] A Universal (Sensor) Alarm
- How to Design Nearly 100% Duty Cycle for Ultra-Low Power Applications Using WEBENCH
- Features of Storage Products for Embedded and Industrial Applications
- 【CH579M-R1】Drive OLED to prepare for the project
- [Repost] Only these 8 tips are needed to restore the circuit board to the circuit diagram
- EEWORLD University Hall----Principles of Operating Systems by Chen Xiangqun, Peking University
- After more than two months of hard work, it is finally finished. Haha
- TI CC2531 USB Evaluation Module Kit
- Lantern with capacitive touch support
- TI's active equalization solution