9. Embedded programming¶
Our individual assignment for this week was to read a microcontroller datasheet and to program our boards in as many languages with as many different environments as possible. Our group assignment was to compare the performance and workflows of different computer architectures.
Our group assignment was to compare the performance/workflows of different computer architectures. Since most of us had no experience with anything but arduino, we chose to just try and figure out how to use a raspberry pi.
The first thing we did was hook up a mouse and keyboard to the Pi through the USB ports, then we plugged in an HDMI port so that we could see the interface on the projector.
My classmate, Kai, was a huge help with this because he has had experience with the raspberry pi before. The first thing we did was get a “hello” to print. To do this, we assigned one command to print out “Hello”.
Then, in another part of the interface, we ran this new command and the Pi correctly printed back its little “hello” world example.
Then, just for fun, we added a bit more to it.
The next thing we did with the pi was make an LED blink. We got a basic blink program in python code put onto the pi, (PY-thon…PI… heh), then we had to find out what pins could do what actions, so we used this pinout for the Raspberry 3B as a reference.
Once we knew which ones to plug the jumper cables into, we grabbed a breadboard, an led, and a resistor. I then got things set up and wired correctly so the circuit would work with those pins.
We ran the code, but things weren’t working at first. After a few minutes of searching we realised we just had the wrong pins so we quickly fixed that and tried again. This time, it worked!
I started out with reading the datasheet because I knew it would help me with my programing later on in the week. I read the Attiny44 datasheet that Neil linked for us in the weeks notes. It was 286 pages long.
I’ll include some of the more important things I found below.
At a certain point, I realized I was quickly going to have more screencaptures than I knew what to do with because of just how much information is in the data sheet. It all seems useful, but I know I eventually wont have space if I keep recording everything that just might be helpful in the future. By the end of the document, I had a whole new appreciation for everything that goes on inside of those teeny tiny chips. There’s so much stuff happening in such an impossibly small space that it’s hard to believe honestly, but I guess that’s just the magic of computers.
After going through the sheet, I wanted to start programming my board. I had already successfully gotten the board to communicate through serial communications two weeks ago by using Neil’s example code, so I figured it would be redundant to do that all over again. Instead, I opted for trying to get a simple blink program going.
Given that our assignment was to try this in as many different ways as possible, I sort of went back to my roots for extra things to work with. I got onto tinker circuits, which I haven’t done for months, and I used the block based coding to get basic C code for a blink program in the arduino IDE. The image below shows the block code, and the one below that shows the actual C code that is going on behind the scenes.
I took the code from that and opened it up in the arduino IDE.
My LED is attached to pin 11 on my chip, so I changed that around in the code for my board.
Then, I checked that all of the necessary settings in arduino were correct. It seems that they have stayed the same since the last time I programmed a board using the tiny44, which was nice.
Then, I plugged in my boards, burned the bootloader, and programmed it using the “Upload using programmer” option under the sketch tab.
When I did this, though, nothing blinked at all. This leads me to believe that I have the wrong pin number in my code, but it’s possible that it’s something else entirely too.
My first guess was correct, I just had the wrong pin name. I found this pinout diagram for the Attiny44 chip and I realized that even though it is listed as pin 11 on the datasheet, you have to reference pin 2 in the code because the 11th pin on the chip is actually “PA2”.
When I referenced the correct pin, it worked.
It was pretty fun to see my board working, and even after all of the microcontroller programming we’ve done so far, part of it still just feels like magic to me.
I wanted to back and just use the provided blink example code straight from the Arduino IDE to do it again, but on further inspection I realized there’s literally no difference in the code from what I just did with the block code, so I went on the the LED fade example in arduino instead.
Scratch that. I was going to try to make the LED fade, but after checking that handy pinout diagram from earlier I realized that I dont have my LED attached to a PWM capable pin. When I can get back to the lab, I think I’ll remake a board that lets me do this because I would like to see the fade effect happen on one of my boards at some point.
I moved on to trying a button program instead. I got the example code from Arduino pulled up first.
Then, I switched out the correct pins and tried uploading the code. For some reason, though, It didn’t do anything at all regardless of if I was pushing the button or not. I double checked that I had listed the correct pins, which I had, and I checked all of my settings to be sure I hadn’t missed something simple like setting the programmer to a USBtiny or something like that, but once again nothing appeared to be wrong.
I looked around some more after than and eventually landed on this page that appears to have come from a past fabrication course at MIT.
I didn’t understand too much of what was going on in there, but I did find this code that I figured I might be able to use for my own board.
I opened it up and put it into arduino IDE, then I substituted the correct terms and pins (I think, honestly not too sure), so that it should work for my board.
I verified and uploaded the code, but unfortunately it didn’t work as expected. The LED was turning on and off from pushing the button, but there was no apparent correlation between these two things. Sometimes pushing the button would turn it on indefinitely and then I would push it again and it would turn off, other times I would push it and it was only on when the button was pushed, and even more times, I would push the button and literally nothing would change. Meanwhile during all of this confusion, the LED was extremely dim. I’ve included a video of this odd behavior below, but it may be hard to see the LED going on and off.
I wanted to give it a shot again the next day with a different program from the same page as before. It’s still a button program, but this time the formatting is a bit different, so I won’t have to change quite as many pin numbers and things like that.
Upon reading through the new code that I wanted to try, I had a realization. There are two “ports” on the chip, PORTA and PORTB, I kept on seeing these and I didn’t really know what to make of them but after reading the text in the image below it sort of clicked for me. There’s a difference between “ports” and “pins”. The ports all have pins designated beneath them, the ports are the overarching categories, e.g. “PA5” or “PB4”, because there are two ports, A and B, and then each of those ports can be listed specifically by the pin numbers associated with it, e.g. “PA1-PA5”, or “PB1-PB5”. I’m adding to this a little bit after the fact, in the code, you define the “input_pins” as PINA because that is the port that contains the individual pins themselves, then you have to define the “input_pin” (notice the singular “pin”) so that it knows which of the pins to look for out of PORTA. Just another interesting thing I thought I should add in.
I brought the code over to arduino and changed out what I thought were the correct pins.
The only things I changed were in the #define section, and I’ve circled the exact changes in blue. I’m crossing my fingers hoping this will work better than the strange behavior I got from my board last time.
Here’s the main code from the program. I loaded it onto my board and that all went smoothly, now it’s time to see if it works.
It works! The button is very stable now, not nearly as crazy as it was the day before with the dim led and the weird behavior.
Coming off of the excitement that this brought me, I really want to do something a bit more with the button and the LED. Maybe I could make it blink multiple times for one button press? We’ll see…
Basic C Code¶
The next thing I programmed my board with was the most basic C code for a button that I could get. The code itself was written by Mr. Rudolph at the lab, and everything should be set for the correct ports and pins on my chip.
At this point, just uploading the code isn’t really a challenge anymore, especially when all of the pins are already set for me. I got the code put onto my board and it worked well once again.
Next, after looking around at some past sites, I found this one where he had made the LED blink on and off from while holding the button down. It’s not quite the same as just pressing the button once an having it blink allot, but it seems interesting to try nonetheless. I looked at the images of the code on his site and tried to replicate it for myself. It really wasn’t hard to change at all.
I then compiled and uploaded the code, I’ve included the entire process below.
The last thing I want to do is program my board in an environment other than the arduino IDE. Like Mary Fabian did last year with the command prompt, I want to get my board programmed through windows powershell (an updated version of command prompt with slightly more helpful features). To do this, I used the structure of her main.c file with my own pin values subbed in. It took me longer than I thought it would to get the right pins, but when I finally realized what I was missing it seemed way to simple. I thought I would need much more specific callouts to get it going but it was actually just the difference between an A and a B.
I made this into my own main.c file in atmel, then I dropped it into a new folder that I created just for this task. I then got the makefile off of Sarah Coston’s website and I then put that into the same folder.
I’m realizing now that I havent been writing down what’s been happening. Basically I’ve been pushing through for the past hour and trying to get things figure out for how to make this all work through powershell, but I keep running into issues. The current one for me is that I have both my makefile and the correct main.c file in the folder for all of this, but whenever I try to actually “make” all I see is this.
I think I’ll take a break for a bit…
I’m not really sure what to do to fix this problem, but I did realize along the way that I could just get the original main.c and makefile from the weeks lecture notes. I’m going to try it all again using those rather than ones from other sites, we’ll see how it goes.
Still not working, all I’m getting is the exact same error message over and over again no matter what I try, it’s getting pretty tiring.
I was looking around for anything that could help me understand what the issue was, and I eventually found this page that has a lot of information about makefiles on it. I spent a while looking through it, but nothing was really what I needed to fix this problem. In all honesty, I’m not sure if it’s that the page does not have enough info on it, or if I’m just too tired to make any sense out of it.
I have no idea what to do. I think for the time being I’m going to stop trying to make it work in powershell and find some other IDE to do it in. Hopefully when I can get back to the lab someone will know what to do to fix the issue, because as of right now I surely don’t.
Well, I’m trying to get this working in Atmel Studio now, but it seems that the problems just never end. Before I forget, I’m using this tutorial to get my USBtiny set up in atmel. Before I can even get going in atmel, I have to be able to use my USBtinyISP programmer with it. To set up a USBtiny in atmel, you need to have the path to AVRdude, as well as AVRdude.exe, and for the life of me I absolutely could not find AVRdude.exe anywhere on my computer. I searched all through my files, and I even ran a scan on my entire local drive, It still came up with nothing for AVRdude.exe. So, I assumed that maybe I just didn’t have AVRdude installed. After further investigation, I do have it installed, also, just in case maybe I had somehow managed to get AVRdude without the .exe, I checked on their site to see if I could just try re-downloading AVRdude. When I checked it out, though, there were about 60+ options for AVRdude that I could install, and I certainly didn’t know what the right one was going to be.
Over the course of all of this, I’ve been getting fairly fed up with programming. It feels like I’ve been trying for forever, it’s been about five hours now, three of which I’ve spent trying to get Powershell or Atmel to work, I think I’m going to just call it a day and look into this more tomorrow when I have a clearer head.
Finally, Some Progress…¶
I got back to the lab and thankfully the powershell problems were an easy fix. Basically, I had thought that I was using a main.c file to do the programing, but it was actually a main.c.ino file that was still meant to be used in the arduino software, I renamed it and changed the file type, then things started working as expected. The correct C file is circled below.
Once I fixed this, things started working again. I typed in “make” and it generated the hex code.
Then I typed “make program-usbtiny” and it all worked as expected, the code was uploaded to my board and the onboard LED started blinking right away!
Here are all of my files from this week.