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

Tip on storing initial values in EEPROM of AVR microcontroller

AVR microcontrollers have EEPROM memory built-in, which adds a lot of abilities to store constant values, calibration data, and other data that may be read and changed at any time during program flow. I have used EEPROM for storing the last device configuration on a couple of projects, e.g., in TDA7313 volume, BASS, TREBLE input, and output channel information is updated each time when it has been changed. After the device is turned ON, it reads the last saved settings from EEPROM memory and restores the last device state. But what happens when you turn the device for the first time. It has to read initial values that have to be stored or read ‘FF,’ which may lead to a crash or something else. One way is to prepare a separate *.eep file with initial EEPROM data and then burn it separately to microcontrollers EEPROM. But this may not always be handy. So this is a simple trick to store initial values to EEPROM at the first microcontroller run and skip this part later without additional files needing to be flashed in. Probably others are doing a bit. Differently, this is how I do. After main() I add following code:…

Continue reading

Building and installing USBASP-USB programmer of AVR microcontrollers

USBASP is a well-known USB programmer for Atmel AVR microcontrollers initially supported by  Thomas Fischl. USBasp flash uploader is made of an Atmega8 or Atmega88 and few other components including resistors, capacitors, LEDs, and connectors. The adapter works on a firmware driver, which gives room for customization, and new chips updates. The USBasp programmer has been so famous for a long time. It has been supported by avrdude – an open-source programming software, which runs on multiple platforms like Windows, Linux, or Mac. Since Arduino’s popularity has skyrocketed, the need for such tools has diminished, and so the support has been stopped. I can assure you that USBasp still works and there is no problem to run it on Windows 10. USBasp circuit diagram Let us see what is this adapter is made of and how to set it up to run on Windows 10. You can find the original circuit on the author’s site: http://www.fischl.de/usbasp/. There you will also find several third-party PCB examples to choose from. The USBasp adapter is an Atmega8 (or Atmega88) microcontroller clocked with a 12MHz crystal. The assembled PCB is ready to be connected via a simple USB B-type cable (PC side needs A…

Continue reading

Interfacing rotary encoder to AVR microcontroller

Recently I was working on a project where I used a rotary encoder. I could share some thoughts on how the rotary encoder should be interfaced to AVR MCU and write a code to read its value. It is relatively easy to connect and program rotary encoders – only three wires are required to connect to the microcontroller (two for signal quadrature outputs) and one for reference GND). When the encoder knob is turned in either direction, it generates a Grey code on the outputs, allowing tracking turn speed and direction. Rotary encoder allows including a convenient user interface option with a single knob. Many rotary encoders also come with an integrated button on the knob itself – so menu navigation becomes even more comfortable. In our example project, we are going to use a 12-step mechanical rotary encoder from SparkFun. It is interfaced to ATMega32 board with graphical LCD.

Continue reading

Control AVR 8 bit Timer-Counter0 using AVR-GCC

Timers are an essential part of embedded systems. They make your life much more comfortable if used properly. Timers run independently to main program flow, so they can do job individually without disturbing precious calculations done by CPU. You just set up the timer and let it run while your main program performs other tasks. Timers are easy to set up. In this post, an example is made for Atmega8 timers. Other AVR microcontroller models and even different brand chips have similar timers. Atmega8 has two 8 bit timers/counters and one 16-bit timer counter with many abilities. Standard 8-bit Timer/Counter0 features are: Single channel counter; Frequency generator; External Event counter; 10 – bit clock prescaler. TCNT0 is an 8-bit timer counter register which keeps the current count number. Timer counter can be clocked by an external clock through pin T0 or from internal via pre-scaler. Timer counter is incrementing (MAX value is 0xFF) and then restarts from BOTTOM=0x00. After timer Overflows, the flag TOV0 is written to 1, and if TOIE0 bit in SREG is set, and then timer overflow interrupt is executed. TOV0 acts as a ninth bit of counter register that is only set but not cleared. When…

Continue reading

Example of enumeration c in microcontroller programming

Enumeration allows defining a user data types in C language. For this purpose a #define pre-processor is used which allows describing a set of constants. They allow reading and understanding program code much easier as you may define human readable types. For instance, using pre-processor we can define a simple numbers to be as follows: #define zero 0#define one 1#define two 3 Define is powerful tool for doing many different definitions in pre-processing stage of source code, but in C language there is an alternative way of defining a user data types –  enumerating using keyword enum: enum (zero=0,one, two); //zero=0, one=1; two=2 By default, enumeration assigns values from zero and up. You can use enumeration of new types as in following example: int n;enum (zero=0,one, two);n=one; //n=1 Also you can use enum  to assign special characters to meaningful words like this: enum escapes { BELL = ‘\a’, BACKSPACE = ‘\b’, HTAB = ‘\t’,RETURN = ‘\r’, NEWLINE = ‘\n’, VTAB = ‘\v’ };or enum boolean { FALSE = 0, TRUE }; An advantage of enum over #define is that it has scope, what means that the variable (just like any other) is only visible within the block it was declared.…

Continue reading

Choosing right programmer for AVR microcontrollers

Once you start building something with microcontrollers, one thing you need to take into account is a programming adapter. This is a device that allows uploading compiled code into the chip. I don’t know if this is still fun to build your DIY programming adapter, which is not guaranteed to support all chips, nor will it be safe and reliable. AVR microcontroller niche is one of the most interesting when talking about programmers. If you look at the AVRDUDE configuration file, you will find that there are about 50 of them. Many of them are DIY, while others are official. When I started with microcontrollers, I also was looking for a cheap and easy to build programming adapter. So I have built several if a few years. The first of them was RS232 based programmer that worked with then-popular PonyProg software. Later followed LPT port programmer:

Continue reading

Using analog joystick in AVR projects

joystick

In many cases, the joystick manipulator is the best choice for user input. Whether it is a game, robot, or flying machine – a joystick is the most intuitive way of controlling them. You can actually find them in gaming controllers like PlayStation or XBOX. The one we are going to the interface is Thumb Joystick I purchased some time ago from SparkFun. They are really cheap and as users report it is practically the same as in XBOX 360 which can be replaced if one is broken. I didn’t bother making a PCB for it – I just used a breakout board for it, which also can be found on SparkFun. Simply speaking, this joystick is nothing more than two potentiometers and one pushbutton. It is designed so that potentiometers are oriented perpendicular, and thus moving stick; you can have X and Y-axis control. The push-button is simply an action button that can be activated by pressing the joystick down. So controlling joystick is a matter of analog read of both potentiometers with microcontroller ADC inputs.

Continue reading

Interrupt driven AVR USART communication

avr usart

Simple USART routines are not always practical because waiting transmitting buffer to be ready in a simple loop occupies processor resources. If we do not know when data will be received, we are just wasting resources inside waiting loop. Another issue arises when multiple data bytes has to be sent/received. In this case, we have to write complex code to deal with data flow. Since microcontroller speciality are interrupts, it is better to use them to handle communications and this way improve overall performance and energy saving. Instead, of continuously checking if there new data received in the UDR register or checking if send buffer is free, lets us write more effective USAR communication code with a guardian which would wake the MCU if it have received a byte via USART. In other hand, Interrupt mode allows to perform other tasks at full capacity while waiting for USART interrupt. Let us implement interrupt driven AVR USART communication example. For this we will need to include the interrupt library: Then we will have to enable USART Receive Complete Interrupt (USART_RXC_vect) and enable global interrupts. Then write interrupt service routine ISR(USART_RXC_vect). Let us write simple bounce program – USART sends back incremented…

Continue reading

AVR GCC LCD library allows connecting pins in any order

Probably some of you are struggling in finding a proper LCD driver that would work on any circuit. I just wanted to point out that I found some time to improve my current LCD library to support a mixed pin connection case. Earlier, you had to connect LCD in a pretty strict way where pins had to be aligned and attached to a single AVR port. Sometimes this can’t be done for various reasons – you want to use those pins for other alternative functions, or you want to trace your PCB better, etc. In this updated version of the library, there are two more modes added: LCD_4BIT_M and LCD_8BIT_M that allow controlling LCDs either in 4 or 8-bit mode but with any pin connection layout. So data pins and control pins can be connected to any PIN and any port. A couple of examples should give some clue on how to start using it. If you used this library for some project, you only need to modify the header file while the project source code can remain the same. Configuring LCD for 4-bit mixed pin mode Let’s look at what you need to start using these features. We can…

Continue reading

FreeRTOS on AVR with external RAM

AVR microcontrollers aren’t the best choice to run FreeRTOS scheduler due to low on-chip RAM. Atmega128 has only 4K of RAM, so this limits the FreeRTOS functionality to very basic. Anyway, this problem can be solved by adding extra RAM which may be connected to an external memory interface. We have already built an external memory block of 8K previously so now we can test it with FreeRTOS applications. Lets continue with our previous code which runs several simple tasks (button state reading, LCD output and LED flash), and  we can add more to it. We are going to set up an external RAM for storing heaps. This will allow to store large data buffers without worrying too much about heap and stack overlaps.

Continue reading