Take the three-axis acceleration sensor MMA7660 as an example:
#define MMA7660_SDA GPIO_Pin_10 // PC10 //IIC data line interface
#define MMA7660_SCL GPIO_Pin_15 // PA15 //IIC clock line interface
#define MMA7660_INT GPIO_Pin_14 // PA14
1 /*
2 *================================================ =========
3 * Function: IIC bus initialization signal
4 * Parameters: None
5 * Function return value: None
6 * SDA ---
7 * |________
8 * SCL ----------
9 * |____
10 *================================================ =========
11 */
12 void I2C_Start(void)
13 {
14 MMA7660_SCL_H;
15 MMA7660_SDA_H;
16 Delayus(1);
17 MMA7660_SDA_L;
18 Delayus(1);
19 }
20
twenty one /*
22 *================================================ =========
23 * Function: IIC bus stop signal
24 * Parameter: None
25 * Function return value: None
26 * SDA ---------
27 * ______|
28 * SCL ---------
29 * |___
30 *================================================ =========
31 */
32 void I2C_Stop(void)
33 {
34 MMA7660_SCL_H;
35 MMA7660_SDA_L;
36 Delayus(1);
37 MMA7660_SDA_H;
38 Delayus(1);
39 }
40 /*
41 *================================================ =========
42 * Function: Receive the ACK signal initiated by the slave to the host.
43 * Parameter: ACKBIT
44 *
45 * Function return value: MMA_ERROR/MMA_OK
46 *================================================ =========
47 */
48 unsigned char I2C_SlaveAck(void)
49 {
50 unsigned char ts=0;
51 MMA7660_SCL_L;
52 MMA7660_SDA_H;
53
54 MMA_SDA_IOIN(); //SDA is set as input
55 Delayus(1);
56 MMA7660_SCL_H;
57
58 while(MMA7660_SDA_D!=0)
59 {
60ts++;
61 if(ts>200)
62 {
63 MMA_SDA_IOOUT(); //SDA is set as output
64 MMA7660_SCL_L;
65 return MMA_ERROR; //Return error
66 }
67 }
68 MMA7660_SCL_L;
69 MMA_SDA_IOOUT(); //SDA is set to output
70 //MMA7660_SDA_H;
71 Delayus(1);
72 return MMA_OK; //Return error
73 }
74
75 /*
76 *================================================ =========
77 * Function: IIC writes one byte of data
78 * Parameter: a: One byte of data to be written
79 * Function return value: None
80 *================================================ =========
81 */
82 void I2C_WriteByte(unsigned char a)
83 {
84 unsigned char i;
85 for(i=0; i<8; i++)
86 {
87 MMA7660_SCL_L;
88 if((a&0x80)!=0) MMA7660_SDA_H;
89 else MMA7660_SDA_L;
90 a <<= 1;
91 Delayus(1);
92 MMA7660_SCL_H;
93 Delayus(1);
94 }
95 MMA7660_SCL_L;
96 if(I2C_SlaveAck()==MMA_ERROR) //Wait for the ACK signal from the slave.
97 {
98 return ;
99 }
100 }
101
102 /*
103 *================================================ =========
104 * Function: IIC reads one byte of data
105 * Parameter: None
106 * Function return value: returns a byte of data read out
107 *================================================ =========
108 */
109 unsigned char I2C_ReadByte(void)
110 {
111 unsigned char a =0;
112 unsigned char i;
113 MMA_SDA_IOIN(); //SDA is set as input
114 for(i=0; i<8; i++)
115 {
116 a <<= 1;
117 MMA7660_SCL_H;
118 Delayus(1);
119 if(MMA7660_SDA_D==1) a |= 0x01;
120 Delayus(1);
121 MMA7660_SCL_L;
122 Delayus(2);
123 }
124 return a;
125 }
126
127 /*
128 ************************************************* *********
129 *
130 * MMA7660 related functions
131 *
132 ************************************************* *********
133 */
134 /*
135 *================================================ =========
136 * Function: Write MAA7660 register
137 * Parameters:
138 * Regs_Addr - Register address
139 * Regs_Data - Register value
140 * Function return value:
141 *================================================ =========
142 */
143 void MMA7660_WriteReg(unsigned char Regs_Addr, unsigned char Regs_Data)
144 {
145 I2C_Start();
146 I2C_WriteByte(MMA7660_DEV_WRITE); //Write the Slave address first and configure it to write mode
147 I2C_WriteByte(Regs_Addr); //Write register address
148 I2C_WriteByte(Regs_Data); //Write register contents
149 I2C_Stop(); //End this IIC process
150 }
151
152 /*
153 *================================================ =========
154 * Function: Read MAA7660 single byte
155 * Parameters
156 * Regs_Addr - Register address
157 * Function return value: register value
158 *================================================ =========
159 */
160 unsigned char MMA7660_ReadReg(unsigned char Regs_Addr)
161 {
162 unsigned char ret;
163
164 I2C_Start();
165
166 I2C_WriteByte(MMA7660_DEV_WRITE); //Write the Slave address first and configure it to write mode
167 I2C_WriteByte(Regs_Addr); //Write register address
168
169 I2C_Start();
170 I2C_WriteByte(MMA7660_DEV_READ); //Write the Slave address and configure it to read mode
171 ret=I2C_ReadByte(); //Read data from the sensor
172 I2C_SlaveAck();
173 I2C_Stop(); //End this IIC process
174
175 return ret;
176
177 }
Previous article:Microcontroller ultrasonic ranging module learning notes_transmitter learning
Next article:I2C bus learning (IV)--reading and writing process
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
- Sensor Basics and Common Terms
- Introduction to the basic knowledge of lithium batteries (PPT)
- EEWORLD University - Understanding PID Control (English subtitles)
- Live broadcast registration with prizes [Microchip Embedded Security Solutions | Automotive Network Security]
- sensor
- Canaan-K510 kit version network usage and current difficulties
- IAR Compilation Optimization Level Introduction
- Problem with peripheral clock enable statement
- Three-phase brushless DC motor control board
- Choosing the right ARM-based MCU