From the course: C Programming for Embedded Applications

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

A bit field example with Keil

A bit field example with Keil - C Tutorial

From the course: C Programming for Embedded Applications

A bit field example with Keil

- [Instructor] Here's a real example, where we can see the benefit of packing data while improving readability. This is a typical configuration register seen in SOA microcontrollers by NXP. This is a structure for a timer status and control register. This eight bit register is used to configure the operation of a hardware timer module, which is useful for keeping track of time. Notice that here we have two fields that are longer than one bit each, PS and CLKS, and the rest are one bit long. Let's see the code to implement this data structure. This code is available in the exercise files folder, but I won't change much either, so it's okay to just watch. In line five, we have a union definition that contains three elements. The first element is in line six. This is the byte itself. As you can see, this is a uint8 from stdint. The next element is the struct with the bit fields for all the individual fields that we want to…

Contents