I found out that I had specified the RX and TX pin the wrong way around....RX was 12 on IC, so 1 in code. TX is 13 on IC, so 0 in code. But this didn't work, I got no feedback in the serial communication. When I switched them: RX =0 and TX = 1, it worked....See sketch below
I started playing in the Arduino IDE because I'm a bit familiar. I noticed pretty quickly that a 4kB sketch is very small and you easily reach the limits. I tried using a float in one of the setups for example. That didn't work...I immediately got errors that it became too big:
Finally I wrote a sketch that makes the LED blink 3 times and say to the serial port : "you pressed it". Here's the sketch, and a video to demonstrate.
Then I took a shot at C. I started out by using Neil's sketch and trying to run it on the board. I followed this tutorial from fablab Providence to do it. First thing that went funny was when I tried getting the first echo from the board and I got this string of odd characters....
the baud rate of the Arduino IDE serial monitor was set to 155200 as mentioned, but I didn't burn the bootloader to 20MHz using the sudo steps in the tutorial (I had - what turned out later: wrongly - burned the bootloader to 8 MHz when using the Arduino IDE). But since this wasn't working I burned it to 20MHz now. And tried to run the make command again. But it kept giving me this "avrdude: Error: Could not find USBtiny device (0x1781/0xc9f)". Aaarrrrgggg. Fiddling with the connection didn't help.
And also Arduino couldn't see the board anymore, not as a 20mhz, and not as a 8 mhz board. Uh oh....I think I broke it...
Arduino: 1.0.6 (Mac OS X), Board: "ATtiny44 (internal 8 MHz clock)"
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.
After this I've tried a bunch of things: checking in terminal whether it could see the board, checking in Arduino. Hooking up a normal Arduino Board (UNO), tried turning it into an ISP with the sketch in the examples, but now this one could also not be found. I reinstalled the FTDI drivers, and the AVR pack, I tried different cables, reinstalled arduino, tried a higher version... I got so confused that I tried loading a simple blink sketch to the UNO board, on a Mac, Linux AND a Windows machine. When all that failed I gave up. I know have no way to even figure out where the problem lies... When I try to use arduino IDE to upload something to the echoboard via the ISP it tells me this when uploading the sketch.
Good news in the end. Replacing the microcontroller with a new one fixed everything. Until I burned the bootloader to 20MHz, and it turned unfindable again...So I replaced the controller one more time. Worked again fine at 8MHz, but then Emma burned it to 20MHZ external clock with her ISP. And this worked this time! She uses the same software, same settings everything but a different type of ISP.
Emma's going to check whether it has something to do with the resonator on the Zaerc board. Now I know at least the internal clock means the clock inside the microcontroller and external clock refers to an additional resonator inside the circuit. To be continued.
HA! Ok back to C. I went through the tutorial. I SKIPPED THE BOOTLOADING STEP, and tried uploading Neils code again. And win! I had to unplug and replug once and then it worked with serial connection in 115200 Baud rate
When that worked I had a go at rewriting my Arduino code as C code. I found this Instructable as a starting point (basically the Arduino Blink sketch written in C. I asked Dennis, one of my students to explain a little of the C code so I could annotate and change it. He wasn't very familiar with programming hardware in C but could recognize some of the syntax which helped me on my was.
With the instructable I was able to recreate the LED as output but got confused about all the DDRB, PORTB and PINBs in the code. I had to read this article to understand how registers work and how to set pins as inputs and outputs. To summarize: if you know on what pin your LED or SWITCH is sitting by reading the schematic and then reading the diagram showing the pinnames as recognized by the attiny (same names you use in C code), then you also know what register you are working in.
Eg. in my case: in the schematic, my LED is on pin 6, which equals pin PA7 on the microcontroller (the number on the left next to it). My button was pin 10 = PA3 on the controller (and referred to as PA3 in the C code). So both my input and my output are in the A register. SO: anytime I see DDRx, PORTx or PINx (x being A, B, C or D), I should change it to DDRA, PORTA, PINA The article above also helped with deciphering existing code and info found in the datasheet. I read the datasheet before going into the C code and a lot of things started adding up. Then the main loop looks alright and I wanted to test it.
Thisis a useful tutorial on makefiles, when programming in C, using makefiles etc. Written for the fab examples. this tutorials list from Providence is great overall.
First try: it compiled but no blinking.....Then to debug I commented out the if loop with the switch and tried to turn only the LED on: SUCCESS!! My colleague Tamara who's an experienced coder helped me with debugging, she made it simpler really quickly by suggesting to leave out the whole button thing and just start with the LED, if that works, take next step. Gooooood tip, thanks
Then I had to google a little about reading input values from sensors in C. Because I'm working in the A register (getting analog reads), I had to enable the internal pullup resistors that turns a floating value into either a HIGH or LOW value. This article explains really well how to do that in C. It also explains quite well how to configure a pin as input and how to get a reading from an input pin. I had to test some stuff a few times and change some things to find out, but finally: success! Then I cleaned my code because I did some unneccessary extra things in it.
In conclusion: C is definitely a lot harder but you can figure stuff out with good examples by others to work from. The HUGE benefit is that you can upload so much more code to your board than when using the arduino IDE. It's much much more space efficient because it compiles it into computer language before uploading (so efficient, I like) Also the way it's written, it actually makes more sense when you look at the hardware, it refers to stuff as it is no beating around the bush with non-sensical representations. Which is all nice. And a bit harder. By the way I must confess that I totally tried to avoid any hexadecimal numbers because my brain can't take that yet.
Contains overview of the pins and their functionality, and also describes the toolbox you'll use it with. Eg. it's based on the AVR architecture, and that it is reprogrammable in-system with AVR and C. It uses Harvard architecture and it explains that it briefly overlaps: while executing something, it's starting up the next command.
What I already noticed when working in Arduino: there's room for 4Kb in the Flash Program Memory, separate from that is the data memory. The attiny44 32 bytes in the register file, 64 in Input/Output memory and 256 bytes RAM.
It explains a bit about why you need a resonator/clock and which kinds do what, somehow. This is a bit vague to me but I understand that the kind of resonator you choose influences the accuracy of any timing you program the microcontroller to do. I read this article on stackoverflow which clarified a bit. Also it stated that the default oscillator is 8 MHz. There was some confusion as to whether we should set our attiny to 8 or 20....You can do both, 8 is just default and for 20 you need a crystal resonator, I think (not 100% sure) but we all have one of those on our echo boards so should be fine.
Apparently you can also save power with this one because it can have a standby mode. Eg if you only want it to do something once every hour. Not sure how to make use of that just by reading this though.
I read that it has built-in protection against current drops or socalled brown-outs. And it explains the ways you can reset the controller in §8.2. The C code examples were also good to have a look at but it's very far from human language and would need some diving into for me.
then there was a whole bunch of stuff I didn't really get until I got to noise canceling techniques: could be useful, although I suppose this will be important if your trying to do very advanced stuff with high accuracy. Chapter 18.7 on preventing flash corruption during energy drops could be useful I guess. Annnnnddd chapter 19.2 on fuses kindly reminded me that fuse status is not affected during resets/chip erase. If only it were that easy (see above). !9.5 on serial programming says something about needing en internal clock for serial programming (which we have: the resonator).Maximum operating voltage = useful ofcourse. And this correlation between frequency and active supply current gives an understanding how to choose power source. Followed by a load of other graphs that explain relations between configurations.
and finally a bunch of packaging info, useful if you need to design your own footprints for eagle board layouts and such.
Wow they're really on top of things at the datasheet offices: even an overview of all revisions made to the information at the very end. Tight ship they run. And an index, always handy especially for these big ones.