Week 5

Let's go home

Embedded Programming

The assignment for this week was to program our board to do something.

I decided that for my programming, I would take advantage of the fact that I added both a button and an LED to my board. My simple goal: have the LED flash when I press and hold the button.

I started by checking out NG's code, hello.arduino.328P.blink.c, for testing a blinking LED.

I first modified it by defining the led_port, led_direction, and led_pin for the specific board that I designed from week 5. This meant changing the definition section of the file to:

#define led_port PORTA
#define led_direction DDRA
#define led_pin (1 << PA7)

I also deleted pin_test and bit_test because they were never used in the program.

In order to create a makefile for this program, I just took the makefile for the echo-hello world program from week 5 and changed the project name to zuck.ftdi.44.blink, which is the name of my C program file.

The final two files for testing blinking on my board are here:

I compiled the C program into a .hex file by running: make -f zuck.ftdi.44.blink.c.make. Then, I ran make -f zuck.ftdi.44.blink.c.make program-usbtiny. And finally, I ran make -f zuck.ftdi.44.blink.c.make program-usbtiny-fuses

The result.... a success! The LED is blinking!

Now, to get that button to control the LED...

In order to figure out how to use the button, I opened up the book "Make: AVR Programming," which we have in the Harvard Fab Lab. I then learned about how inside the AVR, there is a pull-up resistor which aims to create a fixed current across the pin that my button is going to interact with. This way, when the button isn't pressed, the electricity on that pin isn't too variable between VCC and ground, but rather stays closer to VCC.

I then applied what the Make book suggested. First, I defined the port, direction, and pin for my button in a similar way as the code above. This meant adding these definitions to the file:

#define btn_port PORTB
#define btn_direction DDRB
#define btn_pin (1 << PB2)

I initialized the pull-up resistor on the input pin for the button with this line: btn_port |= (1 << PB2);.

The last thing I did was add the logic for testing if the button is being pressed or not. To condition on the button being pressed, I used if (bit_is_clear(PINB, PB2)). When this was true, I added the lines that executed blinking from the first version of the file. When it wasn't, I just cleared the LED port/pin.

I used the same makefile and once again just changed the project name

The final two files for using the button to control blinking on my board are here:

I also compiled and loaded the program using the same commands as above, ust with different filenames.

The result.... a success! The LED is blinking only when I push the button!