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

ATMEL AVR ISP programmer that works with AVRStudio4

Probably many AVR software developers that use the AVRStudio4 environment would like to program chips directly from AVRStudio4 using AVRISP. Usually, we use a simplified version of AVRISP that works with PonyProg or AVRDUDE. But here is the solution that can solve this problem. In sixca.com is a populated simplified version of the AVR ISP programmer that works with ATMEL original programming software. IC1 stands for any RS-232 driver like MAX232 or any similar(DS275). The wire from AVR-ISP to AVR board should not exceed 15cm.

Continue reading

All you need to start with Atmel AT89 microcontrollers

This is a simple development board for the Atmel AT89 series of microcontrollers that uses ISP programmability I found in sixca.com. This is a good start for learning with MCS-51 based projects. This board supports Atmel microcontroller series like AT89S51, AT89S52, AT89S53, AT89S8252 with DIP40 pin packages. The board uses the ISP programming method through the parallel port adapter. It has all I/O connectors, single-sided PCB. It has an onboard voltage regulator 7805.

Continue reading

Signal power and energy calculation

The most common signal characteristics are energy and power. In signal theory, these terms require additional comments because they are different from what we are using in AC or DC systems. What are power and energy? When we connect the R resistor to voltage U, the resistor will dissipate some power equal to P=U2/R. During time T, the energy loss on this resistor will be E= TU2/R. Now let us say that we add some signal s() instead of DC voltage. In this case, the power will depend on time as the signal is time-dependent. The term is called instantaneous power: p(s)=s(t)2/R

Continue reading