This week’s assignement was to simply program the board we made in week 5 to do something with the LED and button. I added functionality to make the LED light up when the button is pressed.

It sounds simple, and even with my computer science background I struggled understanding the various parts of this project.

First, we had to define pins the button and LED were connected to. I had my button on PA7 and my LED on PA3.

#define button_pin (1 << PA7)
#define led_pin (1 << PA3)

Then, we had to initialize the pins, if necessary. I had to set the button_pin, which sets the value of the pin to 1, as opposed to floating. We have to do this because when the button is pressed, it connects the pin to ground, which pulls the value down to 0. We test to see if the button is pressed by seeing of the value of the pin is 0 or 1.

set(serial_port, button_pin);

Finally, we test for the button press, and turn the LED on if it is, and off if it is not. This code goes in the while(1) loop.

if(!pin_test(serial_pins, button_pin)) {
	set(serial_port, led_pin);
} else {
	clear(serial_port, led_pin);
}

I ran into a problem here where Neil’s code blocks on the get_char call, waiting for the computer to input characters. Because of this, the light would only change during keypresses from the computer. Turns out, Neil anticipated this problem and gave us the hello_world code driven by interupts instead of a main loop.