Microcontroller Embedded C Programming Lecture 153| Structure exercise

  • Post author:
  • Post category:Blog

 

Structure exercise

 

In this article, let’s see one Structure exercise. The exercise is very simple.

Write a program to decode a given 32-bit packet information and print the values of different fields. Create a structure with member elements as packet fields as shown below.

Figure 1. Structure Exercise
Figure 1. Exercise

 

Here given the 32-bit packet and you have to decode the packet according to this packet format.

The packet has multiple fields here like the CRC field size is 2 bits, and status is 1 bit, Payload is 12 bits, battery status(BAT) is 3 bits, sensor field is 3 bits, like that. 

A packet information will be given to you and you have to extract these bit fields and you have to store them in the corresponding member elements in the structure.

For example, you have to extract the CRC field and you have to store it in the member element CRC of that structure. So, you have to create a structure with member elements as packet fields as shown in Figure 1.

 

How should the output look? as shown in Figure 2.

When you execute the program it should ask you to Enter the 32-bit packet value. 

Then, you enter 32-bit packet values (You have to enter the packet value in hex). And when you hit enter, it should print the values of different fields like the CRC field, Status field, payload field, etc. 

Figure 2. Output
Figure 2. Output

 

You have to use bitwise operators here. And bit extraction techniques to extract the bits, and then you have to store them in appropriate member elements of the structure, and then you have to print the member elements.

I will code this exercise in the following article.

 

Get Microcontroller Embedded C Programming Full Course on Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.