0
0
Embedded Cprogramming~30 mins

I2C addressing (7-bit and 10-bit) in Embedded C - Mini Project: Build & Apply

Choose your learning style9 modes available
I2C Addressing (7-bit and 10-bit)
📖 Scenario: You are working on a small embedded system that communicates with sensors using the I2C protocol. Each sensor has an address that can be either 7-bit or 10-bit. You need to prepare the addresses correctly before sending them on the I2C bus.
🎯 Goal: Build a simple C program that stores I2C device addresses, distinguishes between 7-bit and 10-bit addresses, and prepares the addresses for communication by shifting them as required.
📋 What You'll Learn
Create an array of device addresses with both 7-bit and 10-bit values
Create a variable to hold the number of devices
Write a loop to process each address and prepare it for I2C communication
Print the original and prepared addresses for each device
💡 Why This Matters
🌍 Real World
Embedded systems often communicate with sensors and devices using I2C. Correctly preparing device addresses is essential for reliable communication.
💼 Career
Understanding I2C addressing and bit manipulation is important for embedded software engineers working with microcontrollers and hardware interfaces.
Progress0 / 4 steps
1
Create the I2C device addresses array
Create an array called device_addresses of type unsigned int with these exact values: 0x3C (7-bit), 0x1FF (10-bit), 0x50 (7-bit), and 0x2AA (10-bit).
Embedded C
Need a hint?

Use curly braces to list the values inside the array.

2
Create a variable for the number of devices
Create an int variable called num_devices and set it to the number of elements in the device_addresses array.
Embedded C
Need a hint?

Use sizeof to calculate the number of elements in the array.

3
Prepare addresses for I2C communication
Write a for loop using int i from 0 to num_devices - 1. Inside the loop, create an unsigned int variable called prepared_address. If the address is 7-bit (less than 128), shift it left by 1 bit. If it is 10-bit (128 or more), shift it left by 1 bit and set bit 0 to 1. Use device_addresses[i] to get the current address.
Embedded C
Need a hint?

Use bitwise shift << and bitwise OR | operators.

4
Print original and prepared addresses
Inside the for loop, add a printf statement to print the original address and the prepared address in hexadecimal format. Use 0x%X format specifier. Print exactly like this: Original: 0x3C, Prepared: 0x78 for each device.
Embedded C
Need a hint?

Use printf("Original: 0x%X, Prepared: 0x%X\n", device_addresses[i], prepared_address); inside the loop.