Consider the following embedded C code snippet that writes a byte to an I2C device. What will be the return value of i2c_write_byte when called with device_addr = 0x50 and data = 0xA5?
int i2c_write_byte(uint8_t device_addr, uint8_t data) { // Simulated write: returns 0 on success, -1 on failure if (device_addr == 0x50) { // Device acknowledged return 0; } else { // No acknowledgment return -1; } } int main() { int result = i2c_write_byte(0x50, 0xA5); return result; }
Think about what the function returns when the device address matches 0x50.
The function returns 0 when the device address is 0x50, indicating a successful write. Other addresses return -1.
status after this I2C write sequence?Given the following code that writes multiple bytes to an I2C device, what is the final value of status?
int i2c_write_bytes(uint8_t device_addr, uint8_t* data, int length) { for (int i = 0; i < length; i++) { if (data[i] == 0xFF) { return -2; // Error: invalid data } } return 0; // Success } int main() { uint8_t buffer[3] = {0x10, 0x20, 0x30}; int status = i2c_write_bytes(0x60, buffer, 3); return status; }
Check if any byte in the buffer equals 0xFF.
None of the bytes in the buffer are 0xFF, so the function returns 0 indicating success.
Examine the following code intended to write a byte to an I2C device. Which option correctly identifies the error that will cause a compilation failure?
int i2c_write_byte(uint8_t device_addr, uint8_t data) { start_i2c(); send_address(device_addr, WRITE); send_data(data); stop_i2c(); return 0; }
Look carefully at the line endings.
The line send_data(data) is missing a semicolon, causing a syntax error.
Consider the following code snippet that writes data to an I2C device. Which option will cause a runtime error when executed?
int i2c_write_byte(uint8_t device_addr, uint8_t data) { if (device_addr == 0) { return -1; // Invalid address } // Simulate write return 0; } int main() { int result = i2c_write_byte(0, 0x55); return result; }
Check if any option describes a runtime error actually caused by the code.
The function returns -1 for device address 0 but does not cause a runtime error. Other options describe errors not present in the code.
Given the following function that writes multiple bytes to an I2C device, how many bytes will be written if length is 5 and data contains no 0xFF values?
int i2c_write_bytes(uint8_t device_addr, uint8_t* data, int length) { int bytes_written = 0; for (int i = 0; i < length; i++) { if (data[i] == 0xFF) { break; // Stop writing on invalid data } // Simulate writing one byte bytes_written++; } return bytes_written; } int main() { uint8_t buffer[5] = {0x01, 0x02, 0x03, 0x04, 0x05}; int count = i2c_write_bytes(0x40, buffer, 5); return count; }
Since no data byte is 0xFF, the loop writes all bytes.
The loop writes all 5 bytes because none are 0xFF, so bytes_written is 5.