Function calls and stacking of parameters in embedded systems

Usually, when you write C programs for microcontrollers, you use functions that can be called any time in the main program or another function. The compiler compiles these functions as subroutines. These functions can be called from the main function or other functions – this is called nesting (nested subroutines). If you see the compiled program’s listings, you will see that subroutines are called by using the call or rcall keyword. The argument of this instruction is a subroutine address that will be executed on the next processor cycle. Call instruction also writes return address from function to the stack to continue the program after returning from the function.

Continue reading

Build Piconomic Atmega128 development board by yourself

First of all, I have ordered to make PCB by using a milling-drilling plotter. Of course, I could make the board by myself, but I wanted to save some time and nerves in case of errors. After one week my board was shipped to me: Then I bought all parts needed. One thing that didn’t go well was that I couldn’t get capacitors and resistors in SMD packages 0603, so I used 0805 to lie on the side.

Continue reading

Generate sine wave modulated PWM with AVR microcontroller

This example will show how ease can sinewave modulated PWM modulated using an AVR microcontroller and a few code lines. For this example, I used Atmega8 MCU. All project is set up in VMLAB simulator. To achieve this, I saved a sinewave lookup table in a Program memory (don’t forget to include interrupt.h header file): PWM is generated by using Phase and frequency correct PWM using a 16-bit timer in Atmega8. Modulation is done by updating the OCR1A value with one from the sinewave table when comparing matches. Reading from flash program memory is done simply: The line above is placed inside the output compare interrupt for OCR1A service routine. Resulting signal in scope simulator: Download project Sinewave modulated PWM source code if you want to try it by yourself. This way, you can modulate PWM with any signal shape that is stored in the lookup table. Updated AVRStudio4 project files!

Continue reading

Variables in embedded C programming language

What are variables in the C language? Variables are simple keywords that are defined by the values. Values can be changed. Variables are like a box with some size where values like apples can be put in. So variables can be various forms and sizes, so-called variable types. Variable type is defined by a reserved word which indicates the type and size of variable identifier: unsigned char my_char; long int all_my_numbers; int number; Why do we need variables? The basic answer is that memory is limited, and the compiler needs to know much space to reserve for each variable. The coder needs to specify the variable type and its size by using one of the reserved words from the table:

Continue reading

Two AVR-GCC examples of using AVR 8 bit Timers

Timers are an essential part of microcontrollers. They can be used in many areas starting with simple timed routines to RTOS. The good thing is that timers run independently of the main program flow. Set up the timer counter and let it do its job while your main program runs and does its job. Let’s take the AVR Atmega8 microcontroller. It has two independent 8-bit timers. One is a little simpler which is featured with essential functions like: Single-channel counter; Frequency generator; External Event counter; 10 – bit clock pre-scaler. Another is more advanced where is OCR (output compare register) included, which allows implementing PWM routines. It has the following features: Single Channel Counter Clear Timer on Compare Match (Auto Reload) Glitch-free, phase Correct Pulse Width Modulator (PWM) Frequency Generator 10-bit Clock Pre-scaler Overflow and Compare Match Interrupt Sources (TOV2 and OCF2) Allows Clocking from External 32 kHz Watch Crystal Independent of the I/O Clock Datasheet documents these timers pretty well. Let’s view a couple of examples with both 8-bit timers. The first example is to do a simple task – every time timer overflows, it has to increase the variable value. And this variable is sent to PORTB in…

Continue reading

Simple TTL to RS232 adapter

During my spare time, I made a quick TTL design for an RS232 adapter that works with my AVR development board. This adapter helps to connect TTL (Transistor-Transistor Logic) level signals to the RS232 interface. TTL side is a 9-pin female connector, and RS232 sire is a male connector. The unit is powered from the target board using power pins. The RS232 TTL adapter circuit: Put all the in a plastic case and see what I have got:

Continue reading

Simplest dor-bell based on AVR microcontroller

This circuit is so simple, and generated sound may look ugly, but on the other hand, this is a good starting point for newbies with AVR projects. This circuit is made of one IC AT90S2313 (may be substituted by Attiny2313). The circuit is low power and reliable as it consists of very few parts (if SMD chip used, then it can be placed inside a postcard): A microcontroller can be powered by using from 2.7 to 6 V power supply. If the AT90S2313 microcontroller is used, then you need to connect a crystal resonator. If ATtiny2313 is used, you can use an internal RC clock source and reduce the number of parts needed. Transistors can be any general-purpose transistors. If the sounder requires less than 20mA of current, you may connect it directly to the port pin. After reset, the circuit starts the program and plays a melody one time. After that, it goes to power-down mode. Crystal frequencies may be chosen from 32768 to 10MHz. Firmware is written for 10MHz. If you use a different clock source, then reduce SoundTab coefficients in the source code and rebuild it. Download Source code

Continue reading

Logical elements and their classification

Logical elements are circuits that perform logical operations. Logical elements are used in digital circuits and are used to express Boolean algebra. The system of logical operations was defined by George Boolean English mathematician in 1848. Firs logical element was used by Ernest Nagel in 1910. It was a calculator made of telephone relays… Let’s go to nowadays and clarify what is logical algebra. There are only two values in logical algebra: True or False , they are identified as numbers 1 for True and 0 – for False. Because there are two digits used, so this is why digital electronics is called as it is – Digital Electronics – the electronics which operates with two digits. Logical operations may be as follows: Logical negation – ¬A (in different sources negation has various markings like Ä); Logical OR – C=A + B (in other literature you may find C=AuB); Logical AND – C=A*B (in other literature you may find C=AˆB). There is whole theory of logical algebra. You may find a lot information in Wikipedia sources.

Continue reading