AVR microcontroller interrupts using WINAVR

I can’t imagine a microcontroller without interruptions. Without interrupts, you would have to make loops to check if one or another event occurred. Polling has many disadvantages like programs have to make loops, which takes valuable microcontroller resources. These resources may be used for other data processing tasks. This is why the microcontroller comes with built-in interrupt support. Instead of checking events, the microcontroller can interrupt normal program flow and jump to interrupt the service subroutine. During an interrupt, the microcontroller stops the task at the current point, saves necessary information to stack, and gives resources to interrupt the subroutine. When interrupt subroutine finishes, and then normal program flow continues. In assembler your program would start with interrupt table: After the table goes the main program (RESET) and other interrupt subroutines where the program jumps after the interrupt occurs.

Continue reading

ADC on AVR using on chip comparator

Not all AVR microcontrollers come with built-in ADC. But there is a way of building one using an on-chip comparator and timer counter. The comparator compares voltages on +v (Ain0) and -v (Ain1) inputs. If the Ain0 pin’s voltage is greater than in Ain1, then the comparator ACO is set to ‘1’ otherwise, it is ‘0’. Bellow you see AVR comparator circuit. The working of this ADC is as follows. First, PB0 is set to ‘0’ to discharge the capacitor. Then PB0 is programmed as input with no pull-ups, and Timer is started to count.

Continue reading

Understanding Timing diagrams of digital systems

Timing diagrams are the main key in understanding digital systems. Timing diagrams explain digital circuitry functioning during time flow. Timing diagrams help to understand how digital circuits or sub-circuits should work or fit into a larger circuit system. So learning how to read Timing diagrams may increase your work with digital systems and integrate them. Bellow is a list o most commonly used timing diagram fragments: Low level to supply voltage:

Continue reading

Control motor PWM schematic

In general, there are two ways to control DC motor speed: by varying supply voltage and pulse width modulation (PWM). The first control method is not convenient, especially in digital systems. It requires analog circuitry, and so on. The second motor speed control method is very convenient for digital systems because all control is made using only digital signals. As you already know, PWM (Pulse Width Modulation) is all about switching speed and pulse width (duty cycle). Duty cycle is the ratio of signal time ON/T. T is the period of the signal. In the above diagram, you see two signals. The first duty cycle is about t1/T=1/3, and another’s duty cycle would be about t2/T=2/3. And notice the period of signals are the same.

Continue reading

Programming AVR fuse bits – oscillator settings

I guess many of you were confused when programming AVR fuse bits. I get many newbie questions like “I programmed Atmega8, but it doesn’t work”. Then my standard answer is: “Did you touch configuration bits?” and if yes, then I am almost 90% sure that he did it wrong. Most of them understand wrongly that programmed fuse bit in configuration should be left unchecked and opposite. Let’s take a look at widely used programming software – PonyProg. First thing you do before programming the chip is set configuration bits (Atmega8 in the picture): The first attention should be paid to clock sources. Four bits are controlling Atmega8 clock sources: CKSEL0, CKSEL1, CKSEL2, CKSEL3.

Continue reading

Connect LCD to Atmega using 3 wires

After I implemented interfacing LCD to ATmega8 using 2 wires https://scienceprog.com/interfacing-lcd-to-atmega-using-two-wires/ I decided to make another example but with 3 wires. Using 3 wires makes control much simpler as it is easier to control data flow to shift register. Here I used the same super cheap 74HC164 serial in parallel out register. LCD to shift register is connected in 8-bit mode. LCD strobe pin E is controlled with an Atmega PC2 pin. The register is clocked with PC1, and data is transferred with a PC0 pin. Notice that I used the same data line (PC0) to control the LCD RS pin. RS pin is controlled in this way: after 8 bits are sent to shift register, enable data line by setting ‘1’ (PC0) without clocking and after this LCD E pin is strobed. For more details, take a look at my source code at the end of the article. If there is any questions, don hesitate to ask. Source code:3 Wire LCD interface to ATMEGA

Continue reading

RTC example on ARM7 LPC2148 using WinARM

Finally got my LPC2148 RTC working on my development board. arm7 base development board for lpc2148 I am quite new to ARM microcontrollers, so I managed to make a few tests using them by driving some peripherals and writing some test routines. The first code I tried was a simple LED blink program first lpc2148 arm7 microcontroller test led blink This one is running microcontrollers’ real-time clock (RTC) and generating interrupts every second. When Interrupt occurs, the microcontroller sends a particular message to UART that I could see via the Terminal program. The main program: And zipped project files:RTC clock example on ERM7 LPC2148 using WinARM

Continue reading

Increase microcontroller code efficiency

C compilers are getting more advanced, but there is always a trade-off between speed and code size. Compiled code can be faster or smaller, but not both. So you have to choose which part is more important speed or code size. The Increase in microcontroller code efficiency can be done in many ways. Don’t trust compiler optimization features, as they might not be as effective as you expect. It is better to grab some profiler and inspect what parts of your code take the most time and size. It is better to follow some techniques that may reduce code execution time and increase microcontroller code efficiency:

Continue reading

Interfacing LCD to Atmega using two wires

This is not a new idea of interfacing LCD using two wires, but it can help in many situations when there are not enough microcontroller pins. This example is based on the Hitachi 44780 Alphanumerical LCD. This circuit I provide represents an idea, but I think it should also work if soldered. We know that to make LCD working, you need at least 6 (in 4-bit mode) wires to control. But what if you need as many pins as possible from your avr and still want to see results on LCD. Then you need to use a serial LCD or make one. In this example, you need to convert serial data coming to LCD using a shift register. I suggest using the 74HC164. You need only two wires to push data to shift register and then give them to LCD using the “E” strobe signal. How serial LCD operates? Atmega’s PC1 pin clocks shift register, and PC0 is the data line. Before you write to the shift register, clean it by sending “0” in eight clock cycles. You can erase the register with an additional wire controlling register reset, but there would be 3 lines used. After the register is…

Continue reading