If you are into the Atmel microcontroller camp, chances are if you are adding some degree of automation to your electronic project you have heard of the popular ATtiny85 microcontroller, launched back all the way in 2005 (who would have thought!).
Found in projects ranging from a somewhat complex USBtinyISP programmer (a low cost ISP programmer), to simple NeoPixel LED strip drivers, to some more impressive ones such as Pulse Oximeters. If your design does not require many IOs, there’s a high chance you’ll use one. Because let’s face it, there are tons of resources online and the device is pretty inexpensive; and like with any other electronic project, it’s highly likely you will run into a problem when building your gadget, be it an unexpected short circuit (don’t let magic smoke happen!), failure during programming, etc. Trust me, all those resources come in handy so you find and fix any error.
The project I wanted to build (which you can check out the story here) was ‘somewhat’ straightforward, I had a RN4020 Bluetooth LE module that needed some initialization via it’s UART interface, and was going to hand over the communication to a USB to UART IC once the configuration was done. Having used the ATtiny85 in the past I know first hand that I am in for a wild ride if I want to have full duplex UART communication as the chip does not have an built in peripheral, only that not so friendly to configure USI (Universal Serial Interface); and using any ATmega was out of the question since I had a target size for the PCB.
I started to wonder what chip would fit my bill since most projects I've done in the past usually involve a 64 pin or higher packages, so I decided to visit Microchip’s website and search for parts they have available.
Luckily their parametric search engine was comprehensive. Since I was aiming to 1 UART module in the tiniest available package, I navigated through the columns to select the features I needed and was left with few results.
Two parts stood out, the ATtiny402, and ATtiny412; not that different in price and feature set I went ahead with the ATtiny412. Then I paid a visit to my favorite parts supplier Mouser to check on it’s availability, because there’s no use if that chip is right for us but we can’t buy it, right?
Before making the commitment, I went ahead and checked the chip’s datasheet.
We always need to check the fine print of each device we use, otherwise we might be in for a nasty surprise when your gadget is not doing what you expected
My focus was on knowing how setup the UART interface and the digital IO as I wanted to read one of the RN4020’s outputs to know when ATtiny was ready to let go of the serial lines.
Out of sheer curiosity I decided to check on example projects and documentation from Microchip’s website to see if there was a big difference in between one family and the other. So I went ahead and checked the related documents in the chip product page I was greeted with a healthy app note catalog.
After scrolling through some pages inside the Getting started with USART I was greeted with the first code snippet:
USART0.BAUD = (uint16_t) USART0_BAUD_RATE(BAUD_RATE);
Being acquainted with data structures from my days in college and few projects of my own, I did not pay much attention. But then it hit me…
Chips like ATtiny85 or ATmega328 DO NOT USE struct for their register definitions, they use good ol’ macro definitions!
#define UCSR0B _SFR_MEM8(0xC1) //Like this one
UCSR0B = (1 << TXEN0) | (1 << RXEN0); //And we use them like this
So I decided to look inside the definition file and sure enough found a data structure, sweet!
Nothing to worry about, it just means we must to use one of the C operators to access data structure.
We need to use the dot (.) operator to access the structure members i.e. the variables inside the structure
All that remained was to read the chip’s datasheet to know what bits to set, just like any other Atmel microcontroller.
Moving onto the digital IOs, I checked the Getting Started with GPIO file, and just like the USART configuration after a few pages I was greeted with a similar code snippet.
PORTn.DIR = PORTn.DIR | PINx_bm;
I felt acquainted with the bit manipulation and logic operators used by Microchip in their example. But given that we now were dealing with data structures, I checked again the definition file to be sure what members the structure had.
And there were a couple of variables that intermediately gained my attention. Each of the marked members is a shortcut to each of the operations you can do with the ports, just like bitwise operations!
We can use those shortcuts to avoid the common mistakes of logic operations such as setting a pin as input or output, or pull it high or low incorrectly.
PORTA.OUTSET = PIN7_bm; //Setting the PIN using the 'new' way
PORTA.OUT |= PIN7_bm; //Or the old way using bitwise operations
PORTA.DIRSET = PIN7_bm; //Setting the pin as output
With the new ‘coding approach’ now in mind I started working on my program, a simulation here and there. Once I was satisfied with the overall flow; it was time to load the program into the chip. And oh boy I was in for a surprise, it turns out that the new AVR 1 series does not use the well known ISP programming interface! They use the Unified Program and Debug Interface (UPDI), which means that you only need a couple of pins to load your program, and best of all is that if you want to use hardware debugging to debug your code, there’s no need to set fuses anymore that have the potential to brick your device if not done properly!
But if you do not feel like spending that much on an Atmel ICE or are on a budget, and have some Arduino compatible boards laying around, you can build a low cost UPDI programmer(click here to go to the post), the only thing you will be missing is the hardware debugging.
Now that you have the basics, you can easily dive deeper into other peripherals and take advantage of how easy to use the new chips are and tinker to your heart desires.
Using this new chip series enables you to speed up your projects by a long shot instead of wasting tons of time trying to hack around your way into getting to work something as simple an UART interface, your clients or bosses might thank you for it.
I hope you’ve enjoyed this brief introduction to the new AVR series!
If you liked this post, you can also have a look at my own website nicatronix.com that I am currently building to share more related stuff on Electronics!