Week 6: Embedded Programming
Programming our device
The goal of this week’s assignment is to program our board. Since we have a single input (a button) and a single output (a LED light), the programs that I could come up with weren’t too complicated.
- Software Used: C
I used my friend Anish Athalye’s Embedded Programming guide from two years ago as a reference to my work first
It is important to refer to our diagram for appropriate pin connections (this is from my work two weeks ago). We see that my LED is connected to PA3, and the switch is connected to PA2.
The workflow for this assignment is the follows:
- Program my file, say it is named
led.c
- Modify my
makefile
so it’ll run example.c as the input - Run
sudo make program-avrisp2-fuses
- Run
sudo make program-avrisp2
Here is my code snippet. The behavior is as follows: the button acts as a toggle, turning the LED on or off, but in the process of toggling we switch the LED light 3 times instead of once (so on -> off would see us do ON-OFF-ON-OFF).
// controllable LED
//
// set lfuse to 0x5E for 20 MHz xtal
//
// Andrew Xia, 10.23.18
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
// set clock divider to /1
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
// PINx is input
// PORTx is output
// DDRx is data direction register
// http://maxembedded.com/2011/06/port-operations-in-avr/
// our LED is connected to PA3
DDRA |= (1 << PA3); // set PA3 to output port
// our switch is connected to PA2
DDRA &= ~ (1 << PA2); // set PA2 to input port
while (1)
{
if (PINA & (1 << PA2)) // if there is an input and it is from PA2
{
// toggle LED (a 3x process)
PORTA ^= (1 << PA3); // reverse
while (PINA & (1 << PA2)){
// wait a little bit longer as a debouncing measure
_delay_ms(10);
} ; // wait
_delay_ms(500);
PORTA ^= (1 << PA3); // reverse
while (PINA & (1 << PA2)){
// wait a little bit longer as a debouncing measure
_delay_ms(10);
} ; // wait
_delay_ms(500);
PORTA ^= (1 << PA3); // reverse
while (PINA & (1 << PA2)){
// wait a little bit longer as a debouncing measure
_delay_ms(10);
} ; // wait
}
}
}
As I took 6.115 two years ago, I felt decently comfortable with this assignment and the mechanics of the Tiny44. As for understanding the details of how the microncontroller actually works, I’ll need to read the documentation more in depth too.
Here is a demo of my code!
Assignments
- Program my board