Consider this embedded C code snippet that reads a temperature sensor value through ADC and converts it to Celsius. What will be printed?
#include <stdio.h> #define ADC_MAX 1023 #define V_REF 5.0 int read_adc() { return 512; // Simulated ADC value } int main() { int adc_value = read_adc(); float voltage = (adc_value * V_REF) / ADC_MAX; float temperature_c = (voltage - 0.5) * 100; printf("Temperature: %.1f C\n", temperature_c); return 0; }
Calculate voltage first, then apply the formula for temperature.
The ADC value 512 corresponds to voltage = (512 * 5.0) / 1023 ≈ 2.502 V. Then temperature = (2.502 - 0.5) * 100 ≈ 200.2 C, which is printed as 'Temperature: 200.2 C'.
This code reads a light sensor value from ADC and converts it to a percentage of light intensity. What will be printed?
#include <stdio.h> #define ADC_MAX 1023 int read_adc() { return 256; // Simulated ADC value } int main() { int adc_value = read_adc(); float light_percent = (adc_value * 100.0) / ADC_MAX; printf("Light Intensity: %.1f%%\n", light_percent); return 0; }
Calculate the percentage by dividing ADC value by max ADC value.
256 / 1023 ≈ 0.25, so light intensity is about 25%.
Examine this code snippet that attempts to convert ADC reading to temperature. What error will it cause?
#include <stdio.h> #define ADC_MAX 1023 #define V_REF 5.0 int main() { int adc_value = 600; float voltage = adc_value / ADC_MAX * V_REF; float temperature_c = (voltage - 0.5) * 100; printf("Temperature: %.1f C\n", temperature_c); return 0; }
Check the order of operations and data types in the voltage calculation.
Integer division adc_value / ADC_MAX = 600 / 1023 = 0 (since both ints). Then voltage = 0 * 5.0 = 0. temperature_c = (0 - 0.5) * 100 = -50.0. Prints 'Temperature: -50.0 C'.
Identify which code snippet will cause a syntax error when compiling.
Look for missing semicolons or punctuation.
Option D misses a semicolon after read_adc() call, causing a syntax error.
A 10-bit ADC reads a temperature sensor. How many distinct temperature values can the system represent if the ADC output is directly converted to temperature using a linear scale?
Recall how many values a 10-bit number can represent.
A 10-bit ADC can represent 2^10 = 1024 distinct values, so 1024 temperature values are possible.