Leilani | How to Make

Creative

Embedded Programming

Preparation

This week we got to program our board to do something with the light and button that we added in the electronic design week. It's been quite a while since I've programmed in C!

The important thing to start with is the system diagram for the board.

wiring diagram

It is tough to see, but the led is connected to PB2, and the button (which is represented by the 6mm switch) is connected to PA2. Then, I wanted to see how I would actually program this board to do something. Since I had some trouble (and had to add a jumper cable) to the last board, I wanted to make sure everything connected ok.

schemeatic view

I got the green light and was able to run the same hello world program from a few weeks ago, using the avrisp with the following commands:

  • make -f hello.ftdi.44.echo.c.make
  • make -f hello.ftdi.44.echo.c.make program-avrisp2-fuses
  • make -f hello.ftdi.44.echo.c.make program-avrisp2
  • sudo python term.py /dev/ttyUSB0 115200

Programming

For this assignment, the minimal component should set the clock, set the direction, and turn on the led. Since is easier to augment a working piece of code, I started with the hello world program For setting the clock, that means to keep the code that says: CLKPR = (1 << CLKPCE); CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0); So I didn't do anything with that (it's already in the hello world program)

To be able to turn on the led, we have to set a pin as input. At first I found this to be quite frustrating, so I will talk a little about the components of the ATtiny44 in particular.

  • The ATtiny44 has two ports, PA and PB. As stated earlier, I identified the led to be PB2 and the button to be PA2.
  • qI was told you might also need to tell the microcontroller that a port is an output port, to do this, you set the corresponding bit of its DDR register (DDRA or DDRB) to 1. If you want to set it as an input, then you set it 0.
  • To read in from a pin, you need to set an input's big in PORTA or PORTB to 1.

To, to be able to turn off and on my led with the button I had to do two things:

  • Do a pin gets to check if the bit corresponding to PA2 (the button) on PINA was pressed by checking if that bit is 1
  • If it is, set the led port to be on by setting it's bit<;li>
  • Otherwise, clear that bit.

The modified source code is here.

Testing

After playing with multiple variations of the above program, it was time to see if it actually worked. At first, I just wanted to see if I could turn on the light. In order to to this, I commented out all the code in the main loop and added the line and added the line: set(PORTB, (1 << PB2)); When the light when on, it was a really nice moment. I squealed.

let there be light

After a bit of struggling (and a lot of questions, I tried using the button to turn on and off the light and it worked!

schemeatic view

One thing to keep in mind is that the button was very sensitive. I may want to add in a delay for future projects.