Embedded Programming

To get started this week, I downloaded and began to read the ATtiny44 datasheet. To be honest, it was way too much to take in as a straight through read, so I began to investigate the sections that I found particularly intersteing, namely the "pin Configurations", the "I/O Ports", and the "Clock System". I'm sure that over time the other sections will become more relevant to my goals, but to get started I wanted to understand these.

I now understand that ATtiny microprocessors are set to run their 8 MHz internal oscillator clocks by default with a scaling factor of 8. This means that the system clock is actually running at 1 MHz. Though this is suitable for certain aplications, a Crystal or Resonator is preferred as they will run significantly faster and are not voltage or temperature dependent.

I also understand that certain pins are meant to perform specific functions, such as Voltage, Ground, and Clock, and that other pins, such as the Reset pin, can be used as an extra I/O pin if you program the fuse accordingly.

When programming the pins on an ATtiny using the Arduino IDE, you have to be aware of the corresponding Arduino pins for your ATTiny. They are the numbers listed in the brown rectangles below. This is critical as you'd be reading from and writing to the wrong pins if you used the ATtiny pin numbers.

Speaking of Arduino... I was excited to make my Millenium Falcon circuit board light up, so I downloaded the Arduino IDE and dove into writing a bit of code.

My objective was simple - to make the LED light up when I pressed the button. I started by reviewing the sample code available within the Arduino IDE so that I could understand how to structure the code. I learned that there are thre main sections to the code:

1) the "Pre-setup" area - where you declare global variables, and load libraries

2) the "Setup" area - where you perform tasks that run only once, like setting pins on your ATtiny as INPUT or OUTPUT

3) the "Main loop" that runs infinitely and houses your workhorse code

At first, I had a hard time with the fact that my code would run infinitely. All of the code I have written in the past always had (at least in my mind) a finite approach - where it would run, but you could get the code to a point where it would stop and wait for user input, or in fact reach its end. You would actually have to go out of your way to write a loop if you wanted your code to iterate on things. To wrap my mind around things, I needed a way to debug the code so I could see what was happening. I accomplished this using the "Software Serial" library and the Serial Monitor.

I simply included the Software Serial library, established the pins I'd use to transmit and recieve data, started the communication at a specific data rate, and then sent the same "Hello World" message to the Serial Monitor.

With this knowledge, I could write and debug my code! I proceeded to write a simple program that turned on the LED when I pressed the button and turned it off when I released it. Then I used my FabISP to program my board. After I connected my ISP to my computer and to the Millenium Falcon board, I connected my FTDI cable to the Falcon.

Before I could push my code to my board, I had to ensure all of the settings were correct. I used the Arduindo IDE to set the "Board", "Processor", "Clock", and "Port" to the correct value. Then I ran the "Bootloader" to instruct the chip how I wanted to use it.

Finally, I uploaded my code to the board. Low-and-behold, it worked!

I feel good about this first step and am looking forward to learning about Pulse Width Modulation (PMW) to control the brightness of an LED as well as a Stepper Motor!

I also experimented with other programming interfaces, first trying a simple editor like Notepad++

...and then a more sophisticated interface like Visual Studio.

Although I like how clear the visual feedback is and I like how you can choose between Visual Basic, Visual C++, Python, and more, I still prefer the Arduino IDE. There's something rather comforting in its simplicity.

...but, to be thorough, I followed through on a simple "blink the LED" example using C. This actually turned out to be more challenging than I thought it should be.

First, I had to figure out how to get code onto a board without the Arduino IDE. I knew this should be a simple matter of writing C code and then using a Makefile to push it via a GIT Bash terminal, but I struggled with getting all of that set up on my Windows machine during the Electronics Production week, so I had a bit of work cut out for me.

I started my quest again to get things working on Windows, and found that I could install the Amtel Studio which is a development environment that contains all the pieces you need to program Atmel microcontrollers! Hooray!.. or so I thought...

I went through the installation process, watched a few videos, and still couldn't figure out how to push to my ATtiny44. I actually think that there's a limitation for the Tiny family of processors and that you can only push to the Mega family using the Studio. I could of course be wrong, but I needed to move on and seek an alternative route.

Further searching revealed a great tutorial that showed me how to get the AVR Toolchain on Windows 10. Thank goodness! I followed the steps, and installed the pieces that I was missing... the Amtel GNU Toolchain, GNU Make, and AVRdude.

Now that I had everything properly installed, I could focus on writing some code. I found a group of Getting started with AVR videos from Atmel and a great writeup about BIT operators that helped me understand how to efficiently speak to the microcontroller. Although I wrote a very simple bit of code, I feel accomplished in having learned the syntax for C and how to use an "OR" (|) orerator to toggle the state of a pin from high to low.

I used the Atmel Studio interface to write my code...

...then I saved the file in folder to which I subsequently added a Makefile. I started with the file that Neil created for his Hello.world board, and modified it to point to my C file. To be more specific, I:

1) Created a new text file in the same directory as my C code.

2) Copied Neil's source and pasted it into the text file.

3) Changed the "PROJECT=hello.ftdi.44.echo" line to read "PROJECT=main" - where "main" was the name of my C file.

4) Saved and closed the text file, then renamed the file to "Makefile" removing the ".txt" file extension.

Next, I started a GIT Bash terminal from the folder that contained my C file and my Makefile.

From that GIT Bash terminal I ran the "make program-usbtiny" command and it sucessfully pushed my code to my board!

...and my Milenium Falcon started flashing! Success!

Download my Arduino code

Download my C code and Makefile