Turn-key PCB assembly services in prototype quantities or low-volume to mid-volume production runs

ADC on Atmega328. Part 1

Microcontrollers are meant to deal with digital information. They only understand ‘0’ and ‘1’ values. So what if we need to get some non-digital data into the microcontroller. The only way is to digitize or simply speaking to convert analog in to digital. This is why almost all microcontrollers are featured with the ADC module. Atmega328 microcontroller also has 8 (or 6 in PDIP package) ADC input channels. All these can be used to read an analog value that is within the reference voltage range. Let us see how this is easy. Continue reading

Using Standard IO streams in AVR GCC

In the previous tutorial, we learned how to transmit data over USART. By using simple functions, we can send or read bytes/arrays. We learned how to do this very efficiently with interrupts and buffers. But when things start to be more involved and our data messages has to be somehow formatted, our send and receive functions would begin to grow tremendously. Don’t waste your time figuring all out. AvrLibc has an excellent standard library stdio.h which is specially designed to deal with formatted data streams. This library offers superior functionality but also takes a significant amount of code space and program speed. So before use, it make sure your program isn’t time critical or won’t take more code space then AVR can hold. Continue reading

Programming AVR USART with AVR-GCC. Part 2

In the previous part of the USART tutorial, we have discussed the most straightforward way of implementing USART transmitting and receiving routines. These are OK to use, but in more intense and power critical applications they are not practical and efficient. Firs of all using loops to poll for transmitting buffer to be ready or wait for received byte consumes lots of processing power what also leads to more power consumption. In reception mode we cant predict when actual data will be received, so the program has to check for received data indicating flag constantly and don’t miss it as next upcoming byte may clear it. So there is a better way of using USART – so-called Interrupt Driven USART. USART Interrupt sources If you look into datasheet you will… Continue reading

Programming AVR USART with AVR-GCC. Part 1

AVR USART tutorial is going to be multi-part tutorial as this peripheral is a sophisticated device and needs special attention. USART Overview USART is an acronym of Universal Synchronous and Asynchronous serial Receiver and Transmitter. Instead of using this long expression lets stick to USART. So, at least one USART is found in most of AVR microcontrollers (except few most of Tiny ones). Atmega328 microcontroller has one USART module that is highly configurable and flexible. Datasheet provides a list of supported features including Full Duplex, Asynchronous and Synchronous operation, Master or Slave operation mode, variable frame size, even or odd parity bits, one or two stop bits, several interrupt sources and even more. We won’t be able to cover all of them in tutorial – we will take common cases… Continue reading

Using watchdog timer in your projects

All AVR microcontrollers have internal watchdog timer that can be successfully used in your projects. Atmega328 and other modern AVR microcontrollers have so-called Enhanced Watchdog Timer (WDT). It has few very useful features including: separate 128kHz clock source, ability to reset microcontroller and generate interrupt. From hardware perspective watchdog timer is nothing more than simple counter that gives a pulse when it counts up. This pulse can be used either to generate interrupt or simply reset MCU (or do both). Watchdog timer can be reset to zero at any time with simple WDR command, and this is where fun begins. If you enabled watchdog timer, you have to take care and reset it before it fills up and resets MCU. Otherwise if your program hangs or sticks in some infinite… Continue reading

Accessing AVR EEPROM memory in AVRGCC

AVR microcontrollers have some amount of EEPROM memory on-chip. For instance Atmega328 has 1K of byte addressable EEPROM. EEPROM memory can be used to store and read variables during program execution and is nonvolatile. It means that it retains values when power supply is off. EEPROM memory comes handy when we need to store calibration values, remember program state before powering off (or power failure) or simply store constants in EEPROM memory when you are short of program memory space especially when using smaller AVRs. Think of simple security system – EEPROM is ideal place to store lock combinations and code sequences and passwords. AVR Datasheets claim that EEPROM can withhold at least 100000 write/erase cycles. Continue reading