9. Embedded programming¶
Moving the smoke around the electronics…
This week’s objective was a simple one: Upload some code to the Hello World board and have it do something. Funny how “simple” doesn’t always work out that way.
Fuses and Padlocks¶
The Hello World board I made back in Exercise 6 was readied for use. I very carefully mapped the pins from the FabISP programmer onto the pins of the HW board, then connected everything as appropriate. I confirmed using the Arduino IDE that the FabISP was properly connected to the Mac, and the LED that started flashing on the HW board confirmed that it, too, was still happily in the state I left it in at the end of Exercise 6.
It was now time to reprogram it to do something more exciting. First step was to burn the fuses to set up the clock and data transfer. I used www.engbedded.com/fusecalc to work out the correct settings for the attiny44. The settings we selected for the chip are as below:
I then modifed the makefile with the settings indicated (68-DF-FF) and ran the make command with the program-usbtiny option. The board confirmed that the fuses had been successfully set.
The trouble began as we tried to upload the c code. The HW board would no longer respond to anything we tried to send it. Somehow I bricked the chip. Some hunting on the net brought out the Dragon board, but after looking at what would be required there (a high-voltage reset!), I decided it would be easier to replace the attiny chip.
Unfortunately the already fragile board did not survive the operation, so everything went into the trash, and it was time to see how much I’d learned in Exercise 6.
A new board¶
Back (literally) to the drawing board. I figured since I had to make a new board, might as well do something interesting this time. So taking inspiration from my FabAcademy classmate and his creative “Millenium Falcon” board, I thought I’d have a go at making a swan in honour of our local castle.
Die Schwanenburg by Maarten Takens
Design¶
On to Eagle. I started from the same schematic, but added two new LEDs to pins PA6 and PA7. The schematic is a combination of direct connections and labeled nets. I placed the elements on the new board layout in a geometry reminiscent of a bird (I tried for swan, but it’s ended up looking more like a crow). I had to try several arrangements to finally connect all of the nets, and I had to use two zero ohm resistors. Once the design was completed, I ran the ULP “cmd-change-brd-width” to redo the width of all of the traces. That required a bit more rearranging (and the second zero ohm resistor). The final design looks like this:
Manufacture¶
The milling of the board proceeded in the same way as before, except that I did most of it independently this time. Remembering that the Mac version of Eagle is buggy, I took the schematic and board files to a PC and exported the board as a monochrome PNG image with 1500DPI resolution.
The board image was imported into GIMP, where I split the inside from the outside (by lassoing the insides and deleting everything). I had to tidy up the edges a bit to ensure the cutting would be complete. Finally I ran the two files through fabmodules.org. The settings for the traces were as follows:
The Roland mill was straightforward to set up. First stick a fresh PC board to a stiff bit of MDF with double sided tape. Then clamp the board to the mill bed. Next move the head to the bottom left hand corner of the desired cutting area. Set the XY zero. Then move diagonally to the middle of the cutout and set the Z zero using the calibration device. That part was new for me, but simple - place the device below the end mill, then use the second menu entry to “set Z with calibration device”. The machine found its Z. Then I started the spindle (10000 RPM) and dropped the mill to the Z zero. To dig into the board, I drove it 0.08mm into the copper. Finally I started the cut.
Partway through the cut, I hit “VIEW” to see how it was getting on. The machine interrupted its work and spat the bed out to the edge of the table. A quick vacuuming revealed the photo below. After inspection, a longer press of the “VIEW” button returns the machine to work.
Once the traces had been cut, the outline could be milled. For this I switched to the 2mm end mill, then ran the outline image through mods with a 1mm/s speed and drill size 0.5mm. (In retrospect, I should have gone back to the image and made it a bit larger, as the edges of the board ended up too close to the traces).
After soldering all of the components in place (we ran out of attiny44’s, so I used an attiny84 instead. The only difference apparently is the size of the onboard memory), the final assembled board looks like this:
Flashing the fuses¶
After successfully completing the smoke test, we used the hello.ftdi.44.make file to set the fuses. This time we used the defaults provided in Neil’s example. I made copies of the .make and .c files, renaming them to reflect the change to the attiny84. I also had to update the code in the .make file (see green boxes).
I compiled the new code and uploaded the hello.world.echo program to the board. The first pass through the make file, with program-usbtiny-fuses set the fuses properly.
Then, with teeth clenched and fingers crossed, I ran the make program again (make -f hello.ftdi.84.echo.c.make program-usbtiny) to upload the code to the board. To our combined relief, it “just worked.”
Coding in C¶
As suggested in lecture, the starting point for my foray into the coding world was somebody else’s example code. I chose the hello.ftdi.44.echo.c code provided by Neil in the class notes. This compiled no problem and uploaded directly to the board, with the success indicated below.
Having confirmed that the chip would receive the compiled code and respond to communications from the computer, I then looked to take the example program apart and write something that could operate the elements I’d built into the hardware, namely 3 LEDs and a pushbutton switch. The code I used is shown below.
The first include statement enables the AVR’s communications capabilities and the second provides some simple utilities for making delay loops. Because this is C, the next line is the function header which is required when the function definitions follow the main program block. The program begins at int main(void).
There are three registers on an AVR tiny 84. Each register is 8 bits in size. The DDRx registers control the flow of information, (0) for input or (1) for output, of each of the microcontroller’s pins. The pins are gathered into two ports, labelled “P” for port and “A” or “B” for the two ports, as in the diagram below.
In my pcb layout, I used pins 6 (PA7), 7 (PA6) and 8 (PA5) for the LEDs and pin 10 (PA3) for the switch. These are the bitwise assignments in the next line. DDA7 corresponds to PA7, etc.
To write data to the pins, one uses the PORTx register. The register is again set bitwise for clarity, so the next block of code flashes the LED connected to PA7 once, then immediately flashes the LED connected to PA6. The function delay_ms, defined at the end of the code, provides a millisecond counter.
The while loop is used to read the state of the pushbutton. While(1) is always true, so the program is running in an infinite loop. The if statement triggers when the button is pressed. The delay and the second if statement are used to deal with the “switch bounce” which occurs as the contacts make initial intermittent contact when the pushbutton is pressed. The rest of the while loop flashes the third LED.
The code compiled and uploaded fine.
The following video demonstrates the code running.
Coding in Arduino¶
The Arduino IDE makes the programming easier by removing the compiler commands from around the main code. There are two default subroutines provided in the IDE. The first one, called “setup”, runs only once, as the device is first powered up. The second one, called “loop”, runs forever until the device is powered off.
The ports have different names in Arduino than they had in C. The LEDs now are connected to Arduino “pins” 5,6,7 and the pushbutton to pin 3. The data directionality is set, as in the C program, with statements at the start of the code (in the setup subroutine). Here the syntax is pinMode(x,INPUT|OUTPUT).
The LEDs are set to flash using digitalWrite(x,HIGH|LOW) and the delay() function. Then the reading of the pushbutton is done in the infinite loop() subroutine, again with a switch bounce defense.
The Arduino code is shown below, along with the port labelling figure again for reference.
The Arduino IDE needs to be set up to work with the FabISP. The settings are in the Tools menu, shown below. The board is programmed using the “Upload using Programmer” option in the Sketch menu, as shown below.
The code loads easily, and the video below shows the arduino version of the code running on the chip (note the longer delay on the green LED).
Downloads¶
hello.world.c newbird-hello-world.ino