Turn-key PCB assembly services in prototype quantities or low-volume to mid-volume production runs


After release of AVRStudio4 there is ability to integrate AVR-GCC compiler in it. As you know AVR studio has only assembler compiler- debugger. Integration of AVR-GCC is done by plugin. Plugin detects AVR-GCC by it self, you don’t have to bother. What do we get from it? Of course – full set of good tools comparable to commercial. Convenient user interface, automatic makefile generation, visual debugging by watching processors register, or even you can flash the chip. We can say that abilities are:
1. Compilation, setting parameters, automatic AVR-GCC detection;
2. Graphical User Interface – convenient project setting;
3. Tree like project view;
4. Project can be complied with predefined configurations;
5. Convenient error handling;
6. Ability to use external makefiles;
7. Map and List file generation;
8. Plugin inspect connections among source files(c and h files who are not part of project);
9. User can work with c or ASM projects in one environment.
Other documentation can be found in AVRStudio help files. This feature is well documented.


AVRLIB library

Standard avr-lib library is too small to fit many common interfacing like LCD, MMC, I2C ir even Ethernet. To write all hardware routines is not always preferable. This why independently from WINAVR there are enthusiast who develop open source library AVRLIB, which can really help to save time and allow you concentrate to main problems. AVRLIB can be found HERE. You can download it from here. Library is really rich in its supported hardware or functions list. Everything is well documented and commented. There are also a lot of good examples to start using it. The main supported functions in AVRLIB are:

·	Byte Buffering (circular)
·	Bit Buffering (linear)
·	Printf and other formatted print functions
·	VT100 Terminal Output
·	Command Line Interface
·	FAT16/32 File System (support is read-only for now)
·	STX/ETX Packet Protocol
·	Fixed-Point Math Library (basic operations only)
AVR internals:
·	Timers (with PWM, interrupt management)
·	UART (interrupt driven)
·	A/D Converter
·	I2C Master/Slave (interrupt and non-intr)
·	SPI Interface
·	External Interrupts
External devices support:
·	Character LCD Modules (HD44780-based)
·	I2C EEPROM Memories
·	SPI EEPROM Memories
·	MMC/SD Card Interface (SPI mode)
·	LIS3L02 ST Accelerometer
·	IDE/ATA Interface (for hard disks and CF cards)
·	Quadrature Encoders
·	RC-Servos (up to 8 channels)
·	STA013 MP3 Decoder Chip
·	GPS Receivers (via serial port)
·	NMEA-0813 Protocol
·	Trimble TSIP Protocol
·	Graphic LCD Modules
·	KS0108/HD61202 Controller
·	T6963 Controller
·	LCD Fonts and Symbols
AVR simulated peripherals:
·	I2c Master (Bit-Bang)
·	UART (software-based, timer interrupt driven)
·	Pulse Output (timer-based, variable frequency)
·	Intel-type Memory Bus (Address & Data Buses + nRD,nWR)
Network support:
·	Device Drivers
·	RTL8019 Ethernet
·	AX88796 Ethernet
·	CS8900 Ethernet
·	Prism2 Wireless LAN
·	Network Protocols
·	IP
·	Network Stack infrastructure

Just remember that avr-gcc and arvlib is not the same, of cource it sounds similar. Proycon AVRLIB is higher level library used to program hardware and embedded systems in higher level. From my personal experience I can say, that in the beginning thiscan look tricky to use this library. It may seem that you can #include like and it compiles, in fact not. Sometimes it is forgotten that these libraries are external files, who has to be added to makefile: SRC = $(TARGET).c c:\AVRLIB\lcd.c. To make life easier and notto think about avery directorie where files are located, there are environmental variables created when AVRLIB is installed:


Now in makefile you only have to show SRC = $(TARGET).c $(AVRLIB)\file.c $(AVRLIB)\lcd.c. The same is with headers (.h files), who are not connected to any source files:EXTRAINCDIRS=$(AVRLIB). Don be afraid to experiment by yourself.

AVR-GCC example of using WINAVR and AVRLIB

You can find a lot of examples in AVR-GCC and AVRLIB documentation and example directories. There fore I am not trying to show you something new. But I’ll explain a little bit about makefile configuration.


Because there is an example in AVRLIB where is LCD connected to with 8 bit interface, I will use 4 bit. You will se how it is easy to configure project. Lets connect 2×16 standard LCD module to Atmega8.
First we create Programmers Notepad project and there we create new c file. In our case:


Then from AVRLIB\Conf folder copy and files to your project directory. Add these files to your project to project by pressing add in PN.
Firstly edit < global.h > file:

#ifndef GLOBAL_H
#define GLOBAL_H
#include "avrlibdefs.h"
#include "avrlibtypes.h"
#define F_CPU        8000000                       // 7.37MHz processor
#define CYCLES_PER_US ((F_CPU+500000)/1000000)     // cpu cycles per microsecond

There we set processors frequency with #define F_CPU 8000000 also we can add additional macro commands and settings.
Then we have to configure :

#ifndef LCDCONF_H
#define LCDCONF_H
// port and pins you will use for control lines
#define LCD_CTRL_PORT    PORTD  //Kontrol ports pins
#define LCD_CTRL_DDR    DDRD
#define LCD_CTRL_RS       0
#define LCD_CTRL_RW       1
#define LCD_CTRL_E        2
// port you will use for data lines
#define LCD_DATA_POUT    PORTD //We use 4,5,6,7 pins
#define LCD_DATA_PIN    PIND
#define LCD_DATA_DDR    DDRD
// access mode you will use (default is 8bit unless 4bit is selected)
#define LCD_DATA_4BIT   //Enables 4 bit mode

// if you chose the LCD_MEMORY_INTERFACE:
// CPU memory address of the LCD control register
#define LCD_CTRL_ADDR    0x1000
// CPU memory address of the LCD data register
#define LCD_DATA_ADDR    0x1001
// LCD display geometry
// change these definitions to adapt settings
#define LCD_LINES                2    // Row number
#define LCD_LINE_LENGTH            16    // Line lenght
// cursor position to DDRAM mapping
#define LCD_LINE0_DDRAMADDR        0x00
#define LCD_LINE1_DDRAMADDR        0x40
#define LCD_LINE2_DDRAMADDR        0x14
#define LCD_LINE3_DDRAMADDR        0x54

As you can see you just need to change parameters according to your project. Firs of all control port pins are selected and their numbers, then data port are selected. Then enable 4 bit mode. If 8 bit mode is used, then #define LCD_DATA_4BIT should be commented. Further we select LCd type:
#define LCD_LINES 2
You don’t have to change anything else. Lets write main program:

//----- Include Files ---------------------------------------------------------
#include         // include I/O definitions (port names, pin names, etc)
#include     // include "signal" names (interrupt names)
#include     // include interrupt support
#include  //atmega8 biblioteka
#include "global.h"        // include our global settings
#include "rprintf.h"    // include printf function library
#include "lcd.h"

//----- Begin Code ------------------------------------------------------------
int main(void)
// initialize LCD
// direct printf output to LCD
// print message on LCD
rprintf("Welcome to AVRlib!");
return 0;


Program is easy and really short. You can find more comments on it in example directories. And the last part is to configure makefile. Change the following parts of your makefile with your Mfile program:
TARGET = lcds
MCU = atmega8
FORMAT = ihex
SRC = $(TARGET).c $(AVRLIB)/lcd.c $(AVRLIB)/rprintf.c $(AVRLIB)/timer.c
Save makefile in your project directory. Select Make All in you PN Tools menu. That’s all – you get file, which you can burn to your Atmega8 and test it.

One Comment:

  1. A great article, very very usefull,I like it lot!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.