This is also the iic bus structure!
Here is a summary of the program structure of iic
Read and write operations in the same function (taking reading eeprom as an example)
(1) Open the bus
(2) Write address word and wait for response (write command)
(3) Write the address to be read and wait for a response
(4) Turn on the bus again
(5) Write address word and wait for response (read command)
(6) Read the address you want to read
(7) Send a response
(8) End the bus
Put the code:
unsigned char read_eeprom(unsigned char add)
{
unsigned char dat;
IIC_Start(); //(1)Start the bus
IIC_SendByte(SlaveAddrW); //(2)Write address word, wait for response (write command)
IIC_WaitAck();
IIC_SendByte(add); //(3)Write the address to be read and wait for response
IIC_WaitAck();
IIC_Start(); //(4)Start the bus again
IIC_SendByte(SlaveAddrR); //(5)Write address word, wait for response (read command)
IIC_WaitAck();
dat = IIC_RecByte(); //(6)Read the address to be read
IIC_Ack(0); //(7)Send a response
IIC_Stop(); //(8)End the bus
return dat;
}
The process of writing eeprom is the same as PCF8591
void write_eeprom(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(SlaveAddrW); //Address word
IIC_WaitAck();
IIC_SendByte(add); //Transmit the address to be written
IIC_WaitAck();
IIC_SendByte(dat); //Number to be written
IIC_WaitAck();
IIC_Stop(); //End
somedelay(10); // appropriate delay
}
Now let's summarize the pitfalls in the I2C official driver:
(1) The delay needs to be changed to 33 nops
(2) After reading the data, the host sends a response of ack(0)
Previous article:[Bluebridge Cup MCU Group] PCF8591 Complaints about Bluebridge Cup's I2C
Next article:【Blue Bridge Cup MCU Group】(3)Keyboard
Recommended ReadingLatest update time:2024-11-16 22:34
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Principles and Applications of Single Chip Microcomputers and C51 Programming (3rd Edition) (Xie Weicheng, Yang Jiaguo)
- Inertial instrument testing and data analysis (Yan Gongmin)
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
- ADS1278 data acquisition chip data distortion problem
- Could you please advise what is wrong with this circuit?
- Fundamentals of MOSFET and IGBT Gate Driver Circuits
- TI_DSP_SRIO - DirectIO Operations - Basics
- Is it possible to use the same set of programs for zstack router and terminal?
- How to use PNP and NPN transistors
- These are two different power supplies. Can I collect the voltage drop of R0? Get the AD value of the voltage, divide it by the resistance value, and then...
- Renesas CPK-RA6M4 Development Board Review ----ADC
- EEWORLD University - Cadence Allegro 17.4 Quadcopter Full Zero-Based Introductory Course
- How to find MSP430 program examples on TI's official website