Squeezing Every Bit From An ATMega

While the ATMega328 is “mega” for a microcontroller, it’s still a fairly limited platform. It has plenty of I/O and working memory for most tasks, but this Battleship game that [thorlancaster328] has put together really stretches the capabilities of this tiny chip. Normally a Battleship game wouldn’t be that complicated, but this one has audio, an LED display, and can also play a fine rendition of Nyan Cat to boot, which really puts the Atmel chip through its paces.

The audio is played through a 512-byte buffer and an interrupt triggers the microcontroller when to fill the buffer while it works on the other processes. The 12×12 LED display is also fed through a shift register triggered by the same interrupt as the audio, and since the build uses so many shift registers the microcontroller can actually output four separate displays (two players, each with a dispaly for shots and one for ships). It will also eventually support a player-vs-computer mode for the battleship game, and also has a mode where it plays Nyan cat just to demonstrate its own capabilities.

We’re pretty impressed with the amount of work this small microcontroller is doing, largely thanks to code optimization from its creator [thorlancaster328]. If there’s enough interest he also says he will provide the source code too. Until then, be sure to check out this other way of pushing a small microcontroller to its limits.

Thanks to [Thinkerer] for the tip!

16 thoughts on “Squeezing Every Bit From An ATMega

  1. I agree, I’m also surprised what a 328 can do (I use it it projects a lot as it is cheap and capable) – though the very first thing you need to is dump the ardunio libraries (and bootloader) in their entirety…

  2. I did battleship for a limited memory system back in the day and it’s more complex than it appears. I didn’t end up with room for a sophisticated opponent so the computer was reduced to taking random potshots, rather than doing anything smart like bracketing hits. Some of the difficulty was the limitations of the system (Casio calculator with a halfassed basic) as well as the lack of memory.

  3. The ATMega328 is not “mega” in any way, even in its heyday it was pretty mediocre. Back in 2006 or so I did a detailed comparison of various micros for a commercial project and the MSP430 bested the ATMega in every way AND was cheaper. The only reason anyone still talks about it is that it was picked up by Arduino which has given it a lease on life way beyond what it ever deserved. Nowadays there’s very little reason to put up with its limitations.

    Of course, if the point of a project is to do as much as possible within the limitations of a particular chip then that can be an interesting exercise in its own right. Not what you’d do for a commercial project of course but cool project!

    1. If you can box clever then the chances are you will do more with a basic atmel than most will do using an stm32 for example. I see to many people these days caught up in the cool to use processor selection and they only ever use 10% of that processors potential. I have numerous commercial products in use that have an atmel as an engine….it’s the code driving it that wins the race..

      1. Only using 10% of the processor’s potential is not necessarily a bad thing. What matters is unit price and development time. I like the stm32 because I’m familiar with it, and there’s a large family of devices to choose from. Even if I’m doing a really simple project, it would be a big hassle to find an atmel, and get into all the details of a different platform.

        1. What matters is unit price and development time. I like the Atmel because I’m familiar with it, and there’s a large family of devices to choose from. Even if I’m doing a really simple project, it would be a big hassle to find an STM32, and get into all the details of a different platform.

          In otherwords you are lazy and refuse to learn anything else, that will be a great selling point if you ever get a job that involves micro’s

        2. The thing about pushing the limits of a part is that you *could have* a much more amazing project if you starts off with a much better part. :P The ARM parts have ranges of an order of magnitude in clock speed or memory sizes that I can size the right parts for the job easily. i.e. I don’t under-utilize a part or paint myself to the corner. I have also jump between different series or even different vendors.

          I have a couple of projects on the STM32F that are simply not possible hardware-wise on the AVR. If you even need to use a high speed and ADC like 1Ms/s or even 4 Msps or higher resolution, you can get it on the ARM chips. I know I can do even more in the future by moving to the larger ARM parts.

      2. It depends. As Artenz mentioned, using 10% of the CPU may be a good thing.

        If low power is important, I’d rather have that oversized micro that sleeps 90% of the time than your ATMega that needs to be awake 80% of the time.
        If your “clever” design uses most of the ATMega’s resources, I would not be a happy customer if the next version needed a complete redesign because there were no resources left for new features.
        Especially since that ATMega nowadays costs more than many more capable parts.

        With the glut of powerful, low cost parts available nowadays, there are very few reasons to choose an ancient ATMega328 for anything, other than that you haven’t kept up with the times. You can either find a cheaper or a better micro (usually both) for pretty much any application.
        If I have the choice to hire a contractor who does well with power tools versus one that insists on only using hand tools, I will choose the one using power tools.

        1. The point of this project was to see how far I could stretch an Atmega. If I absolutely needed more resources, I could easily switch to an ARM / STM32 / ESP32 / etc. and most of the code and circuit would be the same aside from pinout and a couple of #DEFINEs for the ports/pins.

          As a young embedded programmer, learning how to be efficient is the important thing for me right now. A programmer who is able to stretch a small chip to its very limits will be much better at writing efficient code in general compared to a programmer who uses overpowered hardware and never learns to optimize.

          It even carries over to unrelated fields like web application development. Right now, I’m working on a web app that will be feature-identical to its competitor, have a fraction of the page size, and use orders of magnitude less data for keeping its live feed live. (Statcastr app on redhawksports.net, goal is to be like Statbroadcast). Efficiency matters a lot here because there are regions in my area with no WiFi or mobile data and it will sometimes have to run over LoRa. If this project ever goes commercial it’ll be much cheaper to host as well.

          If I always took the easy way out with powerful hardware, I would not be nearly as good as optimizing when it mattered.

          1. Hi Thor,

            You don’t need to justify your project to me, I completely understand the reasoning behind training yourself in writing efficient code, a very worthwhile effort indeed.

            My comment was mostly in reaction to the article’s claim that “the ATMega328 is “mega” for a microcontroller”, which it isn’t, and never has been. In fact, the whole point behind your effort seems to purposefully “stretch a small chip to its very limits” so you definitely understand this.

            I just wish the ATMega328 would die already. Atmel’s marketing department back in the day did a killer move when it decided to stick “mega” in the chip’s name, and unfortunately a whole generation of makers has been taken in and made fools of by that name.

            As I’m writing this, there’s yet another discussion going on in the Hackaday.io Hack Chat because someone wants to run the stupid chip at 3.3V and it can’t run at 16 MHz at that voltage so the bootloader doesn’t work right etc etc. I ran into this same problem years ago when a client insisted on using this chip.

            It’s all so unnecessary. But the same people that laugh when someone keeps using Windows XP will insist on using micros from the same era.

    2. There isn’t much of a reason to use the ATMega parts once you drop the requirement of running certain frameworks or existing code base. ARM has higher speeds, more memory etc at a price point lower than that of the old AVR parts.

      The hardware debugger and better bang for the buck are exactly why I would pick something else than the AVR.

  4. Enough interest? I never thought my original Reddit post would get featured on Hackaday. That’s more than enough interest for me. After I finish 1-player mode, I’ll get the code commented up and post it online. It should be quite helpful for intermediate-level programmers trying to make fast multitasking systems on resource-constrained processors.

  5. Love seeing ATmega338 pushed to it’s limits. It’s one of my favorite things to do, and something I constantly think about. Why have so much memory if you’re not going to use it all? *cough* single board computers *cough*

    I’m currently working on an ATmega328 game device myself. It’s a business card sized game console that loads games off of external ROM cartridges. Uses a little OLED and stuff. All through hole on the PCB atm, but will probably redo it mostly surface mount

    But yeah, neat project! Battle ship is fun. Hope you release the source stuff because I might want to make my own!

Leave a Reply to RW ver 0.0.1Cancel reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.