Assignment #2: Hello C

📚 Assignment 2 CMPE1250: Hello C 👋

📋 Overview

In this ICA, you’ll create a project in the IDE, Segger Embedded Studio. Upon complition of this assignment, you should have learned to:

  • Write some code in C
  • Familiarize yourself with the debugger in the IDE
  • Familiarize yourself with the debug terminal
  • Determine the size of some of the common types in C on this platform
  • Learn about C syntax and language constructs

1️⃣ Setup

Before starting:

  • Ensure your Nucleo board is ready

  • Install all required software

  • Create a standard C project in Segger as discussed in class

  • Structure your main function into:

    • One-time initializations
    • Infinite loop
  • To submit your answers you must:

    • Create a markdown file(.md) with your answers and puload to your repository (add link to Brightspace).

2️⃣ Part A – Type Sizes

  • Use sizeof and printf() to the debug terminalto determine the size of each type below.
  • Ensure that you have included the header noted here so standard C types are available:

    #include <stdint.h>

  • An example line of code that would show the size of type int to the debug console would look like this:

    printf("\r\nint : %d bytes", sizeof(int));

  • Determine the size of the following types, record your findings here. Keep the code that you used to find these values - you will be submitting it.
Type Verified Size (bytes)
int  
float  
double  
short  
long  
long long  
int64_t  
int8_t  
int *  
void *  

3️⃣ Part B – Bit Manipulation: Clear Bits

  • In the ‘one-time inits’ section of your program, copy the following snippet of code, and complete it according to the comments.
// create a variable that will be modified by the following code
// initialize this variable with the provided test values and record
// the resultant values for each provided input (in hexadecimal)

    uint16_t testval = 0x1234;

// write the code necessary to clear the middle eight bits in testval,
// leaving the other bits unchanged

////////////////////
// Clear the middle 8 bits (bits 4-12)
////////////////////
// use the debugger to inspect the result of your operation, and validate
// that it did what was expected

Part B - Findings

Test Value Observed Result (HEX)
0x1234 0x1004 (sample, expected)
0xF0F0  
0xFFFF  
0x0000  
12345  
8192  
0b1110001110001110  

4️⃣ Part C – Bit Manipulation: Set Bits

  • In the ‘one-time inits’ section of your program, copy the following snippet of code, and complete it according to the comments.
// create a variable that will be modified by the following code
// initialize this variable with the provided test values and record
// the resultant values for each provided input (in hexadecimal)

    uint16_t testval = 0x1234;

// write the code necessary to set the most significant nibble in testval,
// leaving the other bits unchanged

////////////////////
// Set the most significant nibble (bits 12-15)
////////////////////

// use the debugger to inspect the result of your operation, and validate
// that it did what was expected

Part C - Findings

Test Value Observed Result (HEX)
0x1234 0xF234 (sample, expected)
0xF0F0  
0xFFFF  
0x0000  
12345  
8192  
0b1110001110001110  

5️⃣ Part D – Print Binary

  • In the ‘one-time inits’ section of your program, copy the following snippet of code, and complete it according to the comments.
// complete the following code so that it displays the value of Num16
// in the debug terminal as binary

    uint16_t Num16 = 42;

// something is missing in the code below - it just loops forever!
// use the debugger to determine what is not working correctly.
// fix it!

printf ("\r\n%4.4X : 0b", Num16);
for (uint16_t mask = 0x8000; mask; )
{
printf ("%c" ,(Num16 & mask) ? '1' : '0');
}

5️⃣ Part E – Count Set Bits

  • In the ‘one-time inits’ section of your program, copy the following snippet of code, and complete it according to the comments.
// create a variable that will be used by the subsequent code

    uint16_t testval = 0x1234;

// write the code necessary to display (in the debug terminal)
// how many bits are set in the variable testval

////////////////////
// Count the number of set bits in testval
////////////////////

// use the debugger to help develop your code if necessary
// use the debug terminal to record the result