Microprocessors_

Using your calculator or otherwise, determine the 16 bit results of the following C language calculations. Express your answers in hexadecimal.

0xfe00 & (1 << 9)

1 2 3 4 5 6 7 8 9 a b c d e f 0xfe00 = 1111 1110 0000 0000 (1 << 9) = 0000 0001 0000 0000 1111 1110 0000 0000 + 0000 0001 0000 0000 = 0000 0000 0000 0000

0x1a23 | (0x9210)

0x1a23 = 0001 1010 0010 0011 0x9210 = 1001 0010 0001 0000

0001 1010 0010 0011 1001 0010 0001 0000 1001 1010 0011 0011

What value will X have after the following C code fragment

int16_t X;
X = -32767;
X = X 0 2;

1111 1111 1111 1111 2^15 = 32768 x = -32768 x = +32767 Signed integer overflow, wraps around to the highest opposite signed number.

A computer system is required to store unsigned numbers in the range 0 to 16 million. What is the minimum number of bits required to store one of these numbers?

24 bits

Comment on the roles played by each of the following in computer systems:

The address bus

Q2

Port A of the STM32F031 is associated with three registers, state the functions:

GPIOA->MODER

This register is used to configure the mode of the pins in port A. Each pin can be configured as input, output, analogue, or alternate functions. #### GPIOA->ODR This register is used to set the output state of the pins #### GPIOA->IDR This register is used to read the input state of the pins

An STM32F031 microcontroller program is required to set BIT2 of GPIOA->ODR without affecting the other bits. Show how you would do this in a single line of C-code.

GPIOA->ODR |= (1 << 2)

An STM32F031 microcontroller program is required to wait for bit 4 of GPIOA->IDR to become 0. The states of the other bits are not known in advance. Show how you would program this in C

while(GPIOA->IDR & (1 << 4))

Q3

The contents of some of the STM32F031 core registers are as shown below. Also shown are the contents of some memory locations. What number goes where when each of the following instructions is executed one after another in a program?

ram.png registers.png

Push R1

SP = SP - 4 = 0x20000ff8 0x20000ff8 = 0x0000007

Pop R0

R0 = 0x0000007 SP = SP + 4 = 0x0000ffc

Pop R2

R2 = 0x0000005 SP = SP + 4 0X00000ffc 0X00001000

Using your calculator or otherwise, determine the 16 bit result of the following C Language calculations. Express your answer in hexadecimal

1 2 3 4 5 6 7 8 9 a b c d e f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

0xfe00 & (1 << 9)

We will convert the hex into binary, understand what (1<<9) is, and then perform and AND comparison between the two values. Then convert back to hex

  1. 0xfe00
    1. Convert hex to binary
      1. f = 15 = 1111
      2. e = 14 = 1110
      3. 0 = 0 = 0000
      4. 0 = 0 = 0000
      5. Combined = 1111 1110 0000 0000
    2. (1<<9)
      1. This means; Shift 1, 9 bits to the left
        1. 0000 0001 0000 0000
    3. &
      1. Now we do a #bitwise AND comparison between the two 16bit values
        1. 1111 1110 0000 0000 & 0000 0001 0000 0000 = 0000 0000 0000 0000
    4. Convert back to hex
      1. 0000 = 0
      2. ANS = 0x0000

0x1a23 | (0x9210)

a 1. 0x1a23 1. Convert hex to binary 1. 1 = 1 = 0001 2. a = 10 = 1010 3. 2 = 2 = 0010 4. 3 = 3 = 0011 5. Combined = 0001 1010 0010 0011

1. (0x9210)
    1. Convert hex to binary
        1. 9 = 9 = 1001
        2. 2 = 2 = 0010
        3. 1 = 1 = 0001
        4. 0 = 0 = 0000
        5. Combined = 1001 0010 0001 0000

1. |
    1. Now we do a #bitwise OR comparison between the two 16bit values
        1.       0001 1010 0010 0011
           OR  1001 0010 0001 0000
           =    1001 1010 0011 0011

1. Convert back to hex
    1. 1001 = 9 = 9
    2. 1010 = 10 = a
    3. 0011 = 3 = 3
    4. 0011 = 3 = 3
    5. ANS = 0x9a33

What value will X have after the following C-Code fragment?

Q1

int16_t X; // X is a signed 16 bit num
X = -32767
X = X - 2

1111 1111 1111 1111 >1111 = 1+2+4+8 = 15 >1111 = 16+32+64+128 = 240 >1111 = 256+512+1024+2048 = 3840 >1111 = 4096+8192+16384+MSB= 28672 >15+240+3840+28672 = 32767

##### Explanation

What is MSB: MSB is the most significant bit, this defines whether a number is negative or positive. If its 1, then the number is negative, if its 0, its a positive.

so a 16bit signed number has a range of -32768 to +32767 Why can negative hold one more? There is only +0, there is no -0


Answer

So now, we know that -32768 is the maximum negative number that a 16bit signed int can hold, so if we add -2 to it, step by step; 1. -32767 - 1 = -32768 (MAX -) 2. -32768 - 1 = +32757 (MAX +)

What happened?

Because we tried to -1 from an already max number, we got a **Signed Integer Overflow

Question 2

Port A of the STM32F031 is associated with three registers,

GPIOA -> MODER

GPIOA -> ODR

GPIOA -> IDR

(i) State the functions of each of these registers:

GPIOA -> MODER This register is used to configure the mode of the pins in port A. each pin can be configured as Input, Output, Analog, or Alternate functions

GPIOA -> ODR This register is used to set or reset the output state of the pins.

GPIOA -> IDR This register is used to read the input state of the pins.

(ii) An STM32F031 microcontroller program is required to set BIT2 of GPIOA->ODR without affecting the other bits. Show how you would do this in a single line of C-code
GOIOA->ODR |= (1 << 2)
(iii) An STM32F031 microcontroller program is required to wait for bit 4 of GPIOA->IDR to become 0. The states of the other bits are not known in advance. Show how you would program this in C.
while(GPIOA->IDR & (1 << 4))
{
    // WAIT
}
(iv) Explain how “Pull-up” resistors are commonly used to convert mechanical switch or button movements into electrical signals suitable for a microcontroller’s digital inputs

“General Purpose Input/Output (GPIO) ports on the LPC1114 are like memory locations that are also connected to the real world” comment on this statement and outline the steps you would take to control an LED using a simple C program.

“Memory locations that are also connected to the real world” They are a bit like memory locations, but memory locations can hold values bigger than 1 bit while GPIO ports hold states, 1 bit, 1 or 0.

GPIOA->IDR