Older blog entries for shimniok (starting at number 28)

23 Aug 2013 (updated 23 Aug 2013 at 18:11 UTC) »

AVR Code Size Reduction

Atmel AVR035 (pdf)) has some tips on code size reduction. Here's what I learned in the real world, reducing my flash-constrained A2D code from 4k down to 1.4k, great because the onboard ATtiny44As only have 4K of flash.

Side note: My A2D boards convert Sharp Rangers or other analog signals to digital -- I2C, and soon SPI and Serial, with oversampling, decimation, and filtering to reduce noise and increase resolution.

The biggest saving by far? Well, sorry, you have to read all the way to the end to find out. The suspense must be killing you... ;) 
  1. Compile with full size optimization. In gcc, use -Os
  2. Use local variables whenever possible. This saved a few bytes here and there.
  3. Use the smallest applicable data type. Use unsigned if applicable. This definitely helped shave some space. With an 8-bit micro it takes more instructions to deal with > 8-bit data.
  4. Use for(;;) { } for eternal loops. No effect. Apparently gcc knows how to optimize while(1){}
  5. Use do { } while(expression) if applicable. Didn't seem to matter in gcc the one time I tried this.
  6. Use descending loop counters and pre-decrement if applicable. Didn't seem to matter in gcc the one time I tried this.
  7. Declare main as C_task if not called from anywhere in the program. This is an IAR-ism. The avr-gcc compiler automatically ensures that main doesn't return a value.
  8. Use macros instead of functions for tasks that generates less than 2-3 lines assembly code. They're right. I tried converting some macros to functions in the TWI slave library I was using. It added size.
  9. Code reuse is intra-modular. Collect several functions in one module (i.e., in one file) to increase code reuse factor. I don't know for sure if this helped but I program this way normally for maintainability.
I started out at 4.2k and doing these things reduced code size to around 3.1k.

I encourage you to read the document as some of the tips I didn't mention may also help you.

Biggest Savings?

The biggest savings by far was to avoid floating point operations at all costs. This saved me in a prior project as well. Floating point on a Tiny is just murder on flash memory.

Take a look at your map file (avr-gcc -Wl,-Map,MyProject.map in your makefile) and find any unnecessary or unexpected library calls. In my case I saw stuff like this:

                              A2Di2c.o (usiTwiSlaveInit)
                              ../A2D/libA2D.a(adc.o) (__floatunsisf)
                              ../A2D/libA2D.a(adc.o) (pow)
                              /usr/local/lib/gcc/avr/4.7.2/../../../../avr/lib/avr25/libm.a(pow.o) (exp)
                              /usr/local/lib/gcc/avr/4.7.2/../../../../avr/lib/avr25/libm.a(exp.o) (__fp_inf)
                              /usr/local/lib/gcc/avr/4.7.2/../../../../avr/lib/avr25/libm.a(pow.o) (__fp_nan)
                              /usr/local/lib/gcc/avr/4.7.2/../../../../avr/lib/avr25/libm.a(exp.o) (__fp_powser)

Lots of libm (math library) calls. I had carelessly made a call to pow() which converts to floating point and thus adds a ton of space. Instead I converted to a simple multiplication loop using integer math. Size dropped from 3.1k down to 1.4k. Wow.

Did this help you? If so, do me a favor and share via redit, twitter, Google+, etc. Thanks!

Syndicated 2013-08-23 17:00:00 (Updated 2013-08-23 17:25:12) from Michael Shimniok

Geek Destinations: Micro Center, DTC

Micro Center in the DTC is quite the maker oasis with gear from Arduino, Sparkfun, Adafruit, Parallax, Karlsson Robotics, Digispark, Velleman, Raspberry Pi, and more. 

This is just one wall.
In this age of internet shopping, you may not know that browsing through many shelves of geekware in person is very fun. You pass by rows of products, imagination spinning out of control, dreaming of all the miraculously cool things you could do...

And so, I walked out $56 poorer but with a new Raspberry Pi Model A, black case, SD card, and Pi Cobbler. 

Those of us in Denver have suffered ever since the demise of TechAmerica up on Colorado which stocked lots of component parts, test equipment, kits, and more. Well, Micro Center is heading in the right direction.

They stocked Sparkfun retail packages for quite awhile. 

Almost all the Arduino goodies from Uno through Esplora
They were also carrying Arduino products and some clones.  Now they've  massively expanded their DIY section from one wall to two walls plus two islands. I wouldn't be surprised if the trend continued.

I ended up buying this very RPi Model A, right here.
It reminds me of how cool it was to look at logic gates and speech synthesizers at Radio Shack.

The FIRST kids are no doubt going to love this!
They carry the PiBow and also my favorite case, which I got in black.
They also carry a few components parts like LEDs, at somewhat cheaper prices than Radio Shack.

Digispark is a cool concept built around an ATtiny85.

I love Adafruit. I got a Pi Cobbler today, too.

Lots of goodies from Parallax even an Elev-8 and PropScope.

Lots of robot kits, too.

Even an Egg-Bot!

Syndicated 2013-08-15 17:00:00 from Michael Shimniok

Robot Club Line Following

Congratulations to Ted who kicked all our respective butts in line following at this month's SHARC meeting. He completed the short course in only 5 seconds, less than half of most everyone.

Except me--I had an embarrassing 30 second time. Hey I'm just happy mine worked at all, given the failure rate of everything else I've touched the last few days.

SHARC meeting, my 3pi on the left, RPi telerover on the right
He and I were both running Pololu 3pi robots. His was nearly maxed on speed but perfectly tuned. I have my work cut out for me if I want to compete next month!

Syndicated 2013-08-12 16:00:00 from Michael Shimniok

3-State Jumper

Up to 27 A2D adapters can live on the same I2C bus. The address is configured with 3 jumpers, each with 3 states: soldered high, soldered low, and unsoldered. Pull-down and pull-up resistors are the key to detecting all three states.


I use a 220k pull-down resistor on each of the pins. That's because the MCU I'm using has a 50k pull-up resistor. If the jumper is left disconnected, it is pulled down with the 220k resistor and appears logic low until  the 50k pull-up resistor is enabled creating a voltage divider that applies logic high to the pin.

Internal 50k pull-up used to detect if jumper connected to VCC, GND, or nothing.

If the pin is high regardless of the pull-up resistor, it's because it's been soldered to VCC. Likewise if the pin is low regardless of the pull-up, it's soldered to GND.


Here's the code to get the address based on the jumper attached to the specified pin. ADDR_PORT and ADDR_DDR are defined in this case as PORTA and DDRA