Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The “code” that you write for Problem 1 is for a hypothetical microcontroller th

ID: 1715391 • Letter: T

Question

The “code” that you write for Problem 1 is for a hypothetical microcontroller that is similar to your Cerebot. The problem will describe the relevant details of the microcontroller; your task is to write the code that will perform particular tasks. You won’t be able to execute the code on your Cerebot to the same effect that is described in the problem, so you only need to submit the Word or PDF document containing your solutions.

Problem 1 (20 points)

Imagine a microcontroller like our Cerebot that uses the same GPIO structure and setup, but interfaces a different set of ports to its LEDs and pushbuttons. Pushbuttons BTN1, BTN2, and BTN3 interface with bits 4, 5, and 6 of PORTA, respectively. LEDs LD1, LD2, LD3, and LD4 interface with bits 8, 9, 10, and 11 of PORTB. Remember that the least significant bit of a register is bit 0.

Using as few lines of code as possible, perform each of the following tasks. For certain tasks, your goal should be to write one line of code. Since this microcontroller uses the same GPIO structure, you may use the same GPIO functions that you used in Lab 1

Configure all three pushbuttons as inputs.

Configure all four LEDs as outputs.

Turn on LEDs LD1 and LD2 if BTN3 is pressed, and turn them off otherwise.

Turn on LEDs LD3 and LD4 if BTN1 is pressed or if BTN2 is pressed but not both. LD3 and LD4 should be off if neither of BTN1 and BTN2 is pressed, or if both are.

Explanation / Answer

let's consider a microcontroller whose digital states can be read (input) or set (output) by the software. These go by the name of I/O pins, general-purpose I/O (GPIO), and occasionally general I/O (GIO). The basic use case is usually straightforward, at least when an LED is attached:

Initialize the pin to be an output (as an I/O pin, it could be input or output).

Set the pin high when you want the LED on. Set the pin low when you want the LED off. (Although the LED also can be connected to be on when the pin is low, but this example will focus on noninverted logic.

I’ll give you examples from three different user manuals so you get an idea of what to expect in your processor’s documentation. Atmel’s ATtiny AVR microcontroller manual describes an 8-bit microcontroller with plenty of peripherals. The TI MSP430x2xx User’s Guide describes a 16-bit RISC processor designed to be ultra low power. The NXP LPC13xx User Manual describes a 32-bit ARM Cortex microcontroller. You won’t need these documents to follow along, but I thought you might like to know the processors the examples are based upon.

As we work with registers, you will need to think about things at the bit level. Often you’ll turn specific bits on and off. If you’ve never used bitwise operations, now is the time to look them up. In C and C++, a bitwise-OR is | and bitwise-AND is &. The logical NOT operator (!) turns a 1 into a 0 (or a true into a false), and vice versa. The bitwise NOT (~) sets each bit to its opposite value:

Once you find the register in the manual, you can determine whether you need to set or clear a bit in the direction register. In most cases, you need to set the bit to make the pin an output. You could determine the address and hardcode the result:

However, please don’t do that.

The processor or compiler vendor will almost always provide a header that hides the memory map of the chip so you can treat registers as global variables. If they didn’t give you a header, make one for yourself so that your code looks more like one of these lines:

LPC13xx processor

MSP430 processor

ATtiny processor

Turn On the LED

The next step is to turn on the LED. Again, we’ll need to find the appropriate register in the user manual.

LPC13xx processor

MSP430 processor

ATtiny processor

The header file provided by the processor or compiler vendor shows how the raw addresses get masked by some programming niceties. In the LPC13xx.h, the I/O registers are accessed at an address through a structure (I made some reorganization and simplifications to the file):

Once we have the LED on, we’ll need to turn it off again. You just need to clear those same bits, as shown in the register section (Starting with Registers):

LPC13xx processor

MSP430 processor

ATtiny processor

Blinking the LED

To finish our program, all we need to do is put it all together. The pseudocode for this is:

NOTE ;-

Binary Hex Decimal Remember this number 0000 0 0 This one is easy. 0001 1 1 This is (1 << 0). 0010 2 3 This is (1<< 1). Shifting is the same as multiplying by 2shiftValue. 0011 3 3 Notice how in binary this is just the sum of one and two. 0100 4 4 (1 << 2) 0101 5 5 This is an interesting number because every other bit is set. 0110 6 6 See how this looks like you could shift the three over to the left by one? This could be put together as ((1<<2)|(1<<1)), or ((1<<2) + (1<<1)), or (3 << 1)). 0111 7 7 Look at the pattern of binary bits. They are very repetitive. Learn the pattern, and you’ll be able to generate this table if you need to. 1000 8 8 (1 << 3). See how the shift and the number of zeros are related? If not, look at the binary representation of 2 and 4. 1001 9 9 We are about to go beyond the normal decimal numbers. Because there are more digits in hexadecimal, we’ll borrow some from the alphabet. In the meantime, 9 is just 8 + 1. 1010 A 10 This is another special number with every other bit set. 1011 B 11 See how the last bit goes back and forth from 0 to 1? It signifies even and odd. 1100 C 12 Note how C is just 8 and 4 combined in binary? So of course it equals 12. 1101 D 13 The second bit from the right goes back and forth from 0 to 1 at half the speed of the first bit: 0, then 0, then 1, then 1, then repeat. 1110 E 14 The third bit also goes back and forth, but at half the rate of the second bit. 1111 F 15 All of the bits are set. This is an important one to remember.