Functions in C¶
The exercises in this material contribute directly to the course project as you can use them to read values from different SensorTag sensors. Therefore each task has a data register description from the sensor data sheet, which we use to calculate the measurement values. As you can see, all of the functions are not complete yet, but we will finish them later..
In this course, you are not expected to fully understand data sheets, therefore we explain what is needed to do.
Installing and using C Compiler¶
Take a look at the instuctions
here.
Returning answers in Lovelace¶
In your code module, you should NOT have the main-function, unless specifically asked.
However, you can use main-function to test your code followingly:
#include <stdio.h>
// #include <what the task requires>
// Function prototype
... function(arguments);
int main() {
result = function(arguments);
printf("...",result);
return 0;
}
// Function implementation
... function(arguments) {
...
}
In your answer, you need to only return you code file (without main-function) including:
- Preprocessor directives
#include <what the task requires>
- Function prototype
- Function implementation
For example, you can "comment out" the main function from the test code (using /* .. */ or //), so you don't have to create a new file.
Your c-code filename is something.c
If the compiler has warnings or errors, the checker won't even start. In that case, you will see the compiling errors in red. Please, be sure that you test your code in your development environment. Do not send code to Lovelace, that you have not compile and run successfully.
Important note for advanced users: malloc
, calloc
and free
operations are not permitted in the code.
Humidity (1p)¶
The image below shows the data register of the humidity sensor of the SensorTag device (HDC1000).The
datasheet shows that the register size is 16-bits. Humidity is calculated from the register value as
humidity = HUMIDITY[15:0] / 2**16 * 100
.
Task: Write a function that converts a 16-bit register value into humidity-% values based on the above information.
Use this function prototype float humidity(uint16_t reg);
You need to include the libraries
inttypes.h
, and
math.h
to use the function
pow. If you compile the code with
gcc
, you need to add parameter
-lm
.
Verification: With register value 0x8000
you should get 50
%.
Quick fix! If you get rounding errors in your answer, use double
variables.
Was this task useful for learning?
Temperature (1p)¶
The image below shows the data register of the ambient light sensor of the SensorTag device (TMP007). In the
datasheet we can find that the size of the register where temperature is stored is 16-bits and we are interested in bits
T0-T13
. From this value, we calculate the temperature as follows:
- Get the bits T0-T13 from the register value using bitwise operations
- Multiply this value by
0.03125
.
Task: Write a function that converts a 16-bit register value into temperature value based on the above information.
Use function prototype float temperature(uint16_t reg);
You need to include the library inttypes.h
.
Verification: With register value 0b0011001000000000
you should have 100.00
C
Quick fix! If you get rounding errors in your answer, use double
variables.
Was this task useful for learning?
Ambient light sensor (1p)¶
The image below shows the data register of the ambient light sensor of the SensorTag device (OPT3001). The page 20 of the
datasheet explains that the register size is 16-bits. We are interested in bits
E0-E3
and
R0-R11
. You need to separate these values from the register value with bitwise operations. After this, you can calculate the illuminance value
lux = 0.01 * 2**E[3:0] * R[11:0]
, where E[3:0] and R[11:0] correspond to the values as binary numbers.
Task: Write a function that converts a 16-bit register value into illuminance values based on the above information.
Use this function prototype float light(uint16_t reg);
You need to include the libraries
inttypes.h
, and
math.h
to use the function
pow. If you compile the code with
gcc
, you need to add parameter
-lm
.
Verification: With register value 0b0110110000110101
, you should get 2000
Lux.
Quick fix! If you get rounding errors in your answer, use double
variables.
Was this task useful for learning?
Air pressure (1p)¶
The image below shows the data registers of the air pressure sensor (BMP280) of the SensorTag device.
Accordding to the
datasheet (page 24). The value is calculated from three register values:
press_xlsb
,
press_lsb
and
press_msb
as 20-bit binary number:
press_msb[7:0],press_lsb[7:0],press_xlsb[7:4]
Task: Write a function that converts these register values into air pressure values based on the above information.
Use this function prototype uint32_t pressure(uint8_t xlsb, uint8_t lsb, uint8_t msb);
You need to include the libraries inttypes.h
.
Verification: With register value xlsb=-34, lsb=-68, msb=24
the result should be 101325
hPa.
Quick fix! If you get rounding errors in your answer, use double
variables.
Was this task useful for learning?
Give feedback on this content