How to Send Data Using I2C in Embedded C: Simple Guide
To send data using
I2C in embedded C, you first initialize the I2C peripheral, then start communication with the target device address, send the data bytes, and finally stop the communication. Use functions like I2C_Start(), I2C_Write(), and I2C_Stop() to control the data flow on the bus.Syntax
The basic steps to send data over I2C in embedded C are:
- Initialize the I2C peripheral to set clock speed and pins.
- Start the I2C communication with the device address.
- Write the data bytes to the bus.
- Stop the communication to release the bus.
Each step corresponds to a function call controlling the I2C hardware.
c
void I2C_Init(void); void I2C_Start(void); void I2C_Write(uint8_t data); void I2C_Stop(void); // Usage example: I2C_Init(); I2C_Start(); I2C_Write(device_address << 1); // Send device address with write bit I2C_Write(data_byte); // Send data byte I2C_Stop();
Example
This example shows how to send a single byte of data (0x55) to a device with address 0x3C using I2C in embedded C. It assumes the I2C peripheral functions are implemented for your hardware.
c
#include <stdint.h> #include <stdio.h> // Mock functions simulating I2C hardware control void I2C_Init(void) { // Initialize I2C clock and pins printf("I2C Initialized\n"); } void I2C_Start(void) { // Send start condition printf("I2C Start Condition Sent\n"); } void I2C_Write(uint8_t data) { // Write byte to I2C bus printf("I2C Write: 0x%02X\n", data); } void I2C_Stop(void) { // Send stop condition printf("I2C Stop Condition Sent\n"); } int main(void) { uint8_t device_address = 0x3C; // 7-bit address uint8_t data_byte = 0x55; // Data to send I2C_Init(); I2C_Start(); I2C_Write(device_address << 1); // Address + write bit (0) I2C_Write(data_byte); // Send data I2C_Stop(); return 0; }
Output
I2C Initialized
I2C Start Condition Sent
I2C Write: 0x78
I2C Write: 0x55
I2C Stop Condition Sent
Common Pitfalls
Common mistakes when sending data via I2C include:
- Not shifting the device address left by 1 bit before sending, which causes wrong addressing.
- Forgetting to send the stop condition, which can lock the bus.
- Not checking for acknowledgment (ACK) from the slave device after sending address or data.
- Incorrect initialization of the I2C peripheral clock or pins.
Always ensure the device address is shifted and the stop condition is sent to avoid bus errors.
c
/* Wrong way: Sending device address without shifting */ I2C_Start(); I2C_Write(0x3C); // Incorrect: should be 0x3C << 1 I2C_Write(data_byte); I2C_Stop(); /* Correct way: Shift address left by 1 to add write bit (0) */ I2C_Start(); I2C_Write(0x3C << 1); // Correct I2C_Write(data_byte); I2C_Stop();
Quick Reference
Remember these quick tips when sending data over I2C:
- Initialize your I2C peripheral before use.
- Shift the 7-bit device address left by 1 bit before sending.
- Send a start condition before writing.
- Write all data bytes sequentially.
- Send a stop condition to end communication.
- Check for ACK from the slave device if possible.
Key Takeaways
Always initialize the I2C peripheral before sending data.
Shift the 7-bit device address left by 1 bit to include the write bit.
Send start and stop conditions to control the I2C bus properly.
Write data bytes sequentially after the device address.
Check for acknowledgment from the slave device to ensure data reception.