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

Power sources for AVR

Power sources for AVR microcontrollers are a crucial part. Every circuit has to be powered from some source like a battery or AC adapter 110V/220V. Using batteries is a more convenient way to power the microcontroller projects as the circuits are simpler and constructed devices become portable. There are many types of batteries in shapes and sizes or capacities. So when choosing a battery you should consider many factors: Capacity is a critical parameter measured in mA/h. This parameter defines how long your microcontroller project will be working before recharging or replacing batteries. The rule is simple – as bigger battery capacity, as long your circuit will be working. Still, on the other hand, your project may become more expensive or even heavier because of bigger batteries. The second parameter is the battery Voltage. If the battery’s voltage is too small for your circuit, you’ll have to connect several batteries in series. Another is the Expiry date. You don’t want your battery energy leakage or become obsolete because of this. Working temperature. If your project is working in more extreme temperatures – heat or cold, you should consider this. And the last parameter would be chape, size, and weight. If…

Continue reading

Atmega EEPROM memory writing

All mega family microcontrollers have internal EEPROM memory. They can have from 512bytes to 4kBytes. EEPROM memory has its own physical space and is linearly organized. ITo access EEPROM memory in mega, there are three registers used: Address register, Data register, and Control register. Address register EEAR (EEPROM Address Register) comprises two 8 bit registers, EEARH:EEARL. In this register, the EEPROM cell address has to be loaded. Data register EEDR (EEPROM Data Register). When writing to EEPROM, the data is loaded to this register. When getting data from EEPROM – you read data from this register. Control register EECR (EEPROM Control Register) is used to control access to EEPROM memory. EERIE – EEPROM Ready Interrupt Enable. This bit generates an interrupt after the write cycle is finished. If the bit is set to ‘1’ and “I” bit in SREG, then EEPROM Ready interrupt is enabled. If the bit is set to ‘0’ – then the interrupt is disabled. EEMWE – EEPROM Master Write Enable. The EEMWE bit determines whether setting EEWE to one causes the EEPROM to be written. When EEMWE is set, setting EEWE within four clock cycles will write data to the EEPROM at the selected address. If…

Continue reading