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

AVR-GCC ABC [2]

After the release of AVRStudio4, there is the ability to integrate the AVR-GCC compiler in it. As you know AVR studio has only assembler compiler- debugger. Integration of AVR-GCC is done by the plugin. The plugin detects AVR-GCC by itself, you don’t have to bother. What do we get from it? Of course a full set of good tools comparable to commercial. The 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 comply 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 which are not part of the 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

The standard avr-lib library is too small to fit many common interfacings like LCD, MMC, I2C, or Ethernet. To write all hardware routines is not always preferable. This why independently from WINAVR, there is an enthusiast who develops the open-source library AVRLIB, which can help save time and allow you to concentrate on the main problems. AVRLIB can be found HERE. You can download it from here. The library is really rich in its supported hardware or functions list. Everything is well documented and commented on. 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
ARP
ICMP
IP
UDP
DHCP
Network Stack infrastructure

Just remember that avr-gcc and arvlib are not the same, of course, it sounds similar. Procyon AVRLIB is a higher-level library used to program hardware and embedded systems at a higher level. From my personal experience, I can say that this can look tricky to use this library in the beginning. It may seem that you can #include like, and it compiles, in fact, not. Sometimes it is forgotten that these libraries are external files, which have to be added to makefile: SRC = $(TARGET).c c:\AVRLIB\lcd.c. To make life easier and not to think about a very directory 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. Therefore 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 LCD is connected to the 8-bit interface, I will use 4 bit. You will see how it is easy to configure the project. Let’s connect the 2×16 standard LCD module to Atmega8.
First, we create Programmers’s Notepad project, and there we create a new c file. In our case:

Then from the 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:

//************global.h*************
#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
#endif
//****************************

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
#define LCD_PORT_INTERFACE
#ifdef LCD_PORT_INTERFACE
#ifndef LCD_CTRL_PORT
// 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
#endif
#ifndef LCD_DATA_POUT
// 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
#endif
#endif

// if you chose the LCD_MEMORY_INTERFACE:
#ifdef LCD_MEMORY_INTERFACE
#ifndef LCD_CTRL_ADDR
// CPU memory address of the LCD control register
#define LCD_CTRL_ADDR    0x1000
#endif
#ifndef LCD_DATA_ADDR
// CPU memory address of the LCD data register
#define LCD_DATA_ADDR    0x1001
#endif
#endif
// 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
#endif
//****************************************

As you can see, you just need to change parameters according to your project. First 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 an LCD type:
#define LCD_LINES 2
#define LCD_LINE_LENGTH
You don’t have to change anything else. Let’s write the 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
lcdInit();
// direct printf output to LCD
rprintfInit(lcdDataWrite);
// print message on LCD
rprintf("Welcome to AVRlib!");
while(1)
{
}
return 0;
}

//********************************************

The 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
EXTRAINCDIRS = $(AVRLIB)
Save makefile in your project directory. Select Make All in your PN Tools menu. That’s all – you get the 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