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…

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 which 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 it will be safe and reliable. AVR microcontroller niche is one of the most interesting when talking about programmers. If you take a look at the AVRDUDE configuration file, you will find that there is about 50 of them. Many of them are DIY while others are official. When I started with microcontrollers I also was looking for cheap and easy to build programming adapter. So I have built several if few years.

Continue reading

Using analog joystick in AVR projects

joystick

In many cases joystick manipulator is best choice for user input. Whether it is a game, robot or flying machine – joystick is most intuitive way of controlling them. You can actually find them in gaming controllers like PlayStation or XBOX. The one we are going to interface is Thumb Joystick I purchased some time ago from SparkFun. They are really cheap and as users report it is practically same as in XBOX 360 which can be replaces if one is broken. I didn’t bother making a PCB for it – 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. Push button is simply action button which can be activated by…

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. Just wanted to point out that I found some time to improve my current LCD library so it would 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 single AVR port. Sometimes this can’t be done due to 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 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. Couple examples should give some clue on how to…

Continue reading

FreeRTOS on AVR with external RAM

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

Continue reading

Running multiple FreeRTOS tasks on AVR

In the previous post, we just run a single task. Running RTOS with single task has no meaning at all. This can be quickly done with a conventional program. But what if we need to have more separate functions. To execute them at exact timing would require a separate timer or interrupt. But microcontroller cannot guarantee an interrupt for every task. This way it is hard to make code modular, and testing can be painful. Using RTOS solves this kind of problem. It allows programming each task as an endless loop. Kernel scheduler takes care of assuring each task gets its chunk of processing time. Additionally, it does bearing the priority systems – more critical tasks are executed before less important ones. Let us go further with our example code and add more tasks to our FreeRTOS engine. We already have LED flashing task that toggles LED every second. Additionally,…

Continue reading

Using FreeRTOS kernel in AVR projects

FreeRTOS is known as Real-Time Operating System. Probably it would be too dare call it real-time-os, preferably a real-time scheduler where applications can be split into independent tasks that share full processor resources by switching them rapidly it looks like all tasks are executed in parallel. This feature is called multitasking. There are lots of debates on using RTOS on AVR microcontrollers as they are arguably too small for the running scheduler. The main limitation is a small amount of ram and increased power usage. If you are going to use lots of tasks in the application, probably you will run out of RAM that is used for saving context when switching between tasks. Consider FreeRTOS only if you use larger scale AVRs like Atmega128 or Atmega256. Surely you can find smaller schedulers that are specially designed for smaller microcontrollers even tiny series. In another hand, if you master FreeRTOS,…

Continue reading