Sunday, January 1, 2017

Programming low frequency microcontrollers in WinAVR

Fixing clock incompatibility between microcontroller and ISP programmer

I was working on a low power project using ATtiny13 so I have decided to use the 128kHz internal oscillator as a CPU clock to save power. All went well but when I tried to modify the code and upload again an error message appeared in WinAVR saying

avrdude: initialization failed, rc=-1

After some research I found out what was going on.

Clock too slow for ISP programmer

The ISP programmer must have a speed lower than the microcontroller. 4 times lower is recommended.
The solution is to lower the ISP clock speed by using -B bitclock option. -B250 worked fine.

Example in avrdude. I use USBTinyISP programmer:

avrdude -p attiny13 -P usb -c usbtiny -B250

Append arguments in WinAVR

Open the make file and fin these lines (starting from line ~ 300)

AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)

Then append the line

AVRDUDE_FLAGS += -B250

Tuesday, November 22, 2016

How to make a double sided printed circuit board (PCB) at home

Homemade PCB - not as hard as you might think. Ordering the PCB from a manufacturer might be easier but the disadvantage is the long waiting time, and in case you made a mistake in board design you need to repeat the process again.

For prototyping or for a one board spin, it is more practical and economical to make the boards at home. Following this tutorial you will be able to make a nice board in just a few minutes. The tutorial looks long because of the explanations, but once you get experience it's an easy task.

STEP 1: Printing circuit diagram

You will need a laser printer for this. The toner in the laser printer has plastic particles that will melt and stick to the copper clad. The sole purpose of the toner is to protect the copper below it from the ferric chloride. I use Brother HL-1210WE laser printer.


Printed electrical circuit diagram



The circuit diagram is created using DipTrace. I find it much easier to use than Eagle. You can print directly from DipTrace but i prefer exporting top, bottom and silk layers to .png files with 600 dpi density, and then arrange them on a A4 project to be printed on a single A4 paper. For this you need an image editor like Gimp, Inkscape, etc. I use Inkscape and it's free. The project in the image editor must have the same DPI as the exported images from the PCB design software. Also the printer must be set to print with the same DPI and everything will have the correct scale.

For aligning the layers i print the board dimensions (see image above) and use the lines at the corners to align the two layers on the light projector.

Only the top layers must be mirrored. I do this in DipTrace when exporting the image but it can be done in the image editor to.

Wednesday, August 24, 2016

Library for interfacing alphanumeric LCD modules with AVR microcontrollers

This library provides an interface between the microcontroller and LCD module. Note that this code is not for I2C modules.

Library for interfacing alphanumeric LCD modules with AVR microcontrollers - ATmega328P

Main features:

- Supports 16x1, 16x2, 16x4, 20x4, 20x2, 32x2, 40x2 LCD display modules
- Option for automatically wrapping the text to a new line
- Numbers can be padded with zeros to maintain user interface layout
- Scrolling a string of characters
- Includes two types of big digits numerical fonts for making a clock
- Has support for user defined fonts and other special fonts included by default in the LCD memory
- Support for 8 and 4 bit mode interface
- LCD backlight dimming or on/off control using PWM 

Contents:

 

Hardware interfacing ATmega328 AVR microcontroller with a 16x2 LCD module with PWM brightness control

This LCD modules can be connected in 4 bit mode or 8 bit mode. Using 4 bit mode is recommended because it uses less pins but the code is a bit more complex. In the following example I use 4 bit mode.


Hardware interfacing AVR ATmega328 with an 16x2 LCD module with PWM brightness control