Wednesday, June 18, 2008

Final step towards line following robot

If you had successfully coded for input to microcontroller,then u have almost finished the coding part.The final step of coding is to modify it in such a way that the microcontroller could control four output pins using three inputs(sensors).

Hardware part require certain components in addition to atmega8 and L293D.Atmega8 and L293D can be bought from any electronics shop.The others are:
> IR based Line Detecting Module-3
> dc motor with gearbox-2
> plastic wheel(woth rubber grip)-2
> 12v(AAAsize)NiMh battery Pack
The above components can be got from triindia roboshop through online shopping from triindia.co.in.

The circuit can be easily set up.Body design of robot is the next major factor.If it is not properly designed,then the robot won't follow a proper line.Design part is left to you.

The above mentioned sensors are used to sense white line over black or green background.The sensors need to be tuned accordingly.The sensor provides digital output.So,itz really easy to code and no need to use the ADC part of atmega8.Our robot can take curves inclined at 90 degrees or more.

This was our first step towards embedded world and we succeeded...

Friday, May 23, 2008

Varying brightness of LED using PWM

We did it by using timer/counter2 register in atmega8.

Timer/Counter2 is a general purpose, single channel, 8-bit Timer/Counter module. The main
features are:
• Single Channel Counter
• Clear Timer on Compare Match (Auto Reload)
• Glitch-free, phase Correct Pulse Width Modulator (PWM)
• Frequency Generator
• 10-bit Clock Prescaler
• Overflow and Compare Match Interrupt Sources (TOV2 and OCF2)
• Allows Clocking from External 32 kHz Watch Crystal Independent of the I/O Clock

our program for varying brightness of LED which is connected to the pin PB3(oc2) is given below

#include< avr/io.h >
int main(void)
{
TIMSK=0XC0;
TCCR2=0XE1;
DDRB=0X08;
PORTB=0XFF;
OCR2=0X7F;
return(0);
}

the above program create a 50% duty cycle of pwm. And lights the LED with half the brightness.
By varying the value of OCR2 register we can change the brightness.
For eg by giving it as 0x00 LED stops lighting and by giving 0xff LED lights with full brightness.

for understanding the above program you must know something about the use of registers and bits in them which is explained in complete datasheet of atmega8.

Thursday, May 22, 2008

Running the motor


Next step is to run the motor or motors using atmega8.For interfacing the motor to atmega8,a h bridge(L293D, a i6 pin ic) is used.A 12V battery is required to run the motor
This schematic shows the use of L293D to drive a pair of DC motors/Geared Motors. Connections M1-A and M1-B correspond to Motor 1 while connections M2-A and M2-B correspond to Motor 2.Connect these control inputs the atmega 8 microcontroller.

Tuesday, May 20, 2008

Input to the microcontroller

Now it seems really simple to blink a LED using a microcontroller.The earlier mentioned program is the simplest to achieve the task.It just uses the port registers.In a expert way , we can achieve the same by using the timer(TCCR),counter(TCNT) and interrupt(TIMSK) registers.In this case,the timer is set and counter starts counting up or down depending on the values set in the TCNT register.When the timer overflows,TOV0 bit of flag register is set(set to zero).The interrupt can be enabled by setting the bits of TIMSK register.The code consists of just 5 to 6 lines which can be easily coded by reading Atmega8 datasheet.

Next step is to give input to microcontroller and get the output according to it.This task can be easily accomplished by writing a small program that uses the 3 port registers(PORTX,PINX,DDRX).A modified version of this code is used as the program for the line following robot.

If you can code for fullfilling the above 2 tasks,then we assure you that you can build your own maiden robot...
We studied this from experience and thus we gurantees it...

Any doubts and suggestions are always welcome and we have no hestitation to help...We look forward for genuine replys...

First step into EMBEDDED world....Blinking a LED using ATMEGA8



We are on the game...
The above circuit diagram represents the burning circuit for Atmega8.As you see there is a LED at pin 15 in the programmer schematic.This circuit is only used for testing the programmer(here the programmer software is uisp.But we can use any programmer software that supports DAPA-'Direct AVR Parallel Access) with the following simple program-avrledtest.c

As soon as you've connected your programmer to the parallel port and switched on power, you can test the hardware with:

# uisp -dprog=dapa
Atmel AVR ATmega8 is found.


avrledtest.c

#include

void delay_ms(unsigned short ms)
/* delay for a minimum of */
/* with a 4Mhz crystal, the resolution is 1 ms */
{
unsigned short outer1, outer2;
outer1 = 200;

while (outer1) {
outer2 = 1000;
while (outer2) {
while ( ms ) ms--;
outer2--;
}
outer1--;
}
}

int main(void)
{
/* enable PB0 as output */
PORTB=0xff;
sbi(DDRB,PB0);
while (1) {
/* led on, pin=0 */
cbi(PORTB,PB0);
delay_ms(500);
/* set output to 5V, LED off */
sbi(PORTB,PB0);
delay_ms(500);
}
return 0;
}

The above program switch the LED between on and off state with the help of a delay function.Some registers and built in functions are used in the program that are strangers to a beginner.A brief idea is given below:



The DDRx Register

The DDRD register sets the direction of Port D. Each bit of the DDRD register sets the corresponding Port D pin to be either an input or an output. A 1 makes the corresponding pin an output, and a 0 makes the corresponding pin an input. To set the first pin of Port D to be an output pin, you could use the sbi(reg,bit) function, which sets a bit (makes it high or binary 1) in a register:

sbi(DDRD, 0); //these two statements are equivalent
sbi(DDRD, PD0); //both set the first pin of port D to be an input
//by setting the 0 bit of the DDRD register.

To set the second pin to be an input, you could use the cbi(reg,bit) function which clears a bit (makes it low or binary 0) in a register:

cbi(DDRD, 1); //these two statements are equivalent
cbi(DDRD, PD1); //both set the second pin of port D to be an output
//by setting the 1 bit of the DDRD register.

You can also set the value of all the bits in the DDRx register (or any register) using the outb(reg,byte) command. It writes a byte (8 bits) to a register. For example, if you wanted to set pins 1-4 of port B to output and pins 5-8 to input, you could use:

outb(DDRB, 0x0F); //Set the low 4 pins of Port B to output
//and the high 4 pins to input

An alternate way to write a value to a register is using the same syntax as a C assignment:

DDRB = 0x0F; //Set the low 4 pins of Port B to output
//and the high 4 pins to input


The PORTx Register


The PORTx register functions differently depending on whether a pin is set to input or output. The simpler case is if a pin is set to output. Then, the PORTC register, for example, controls the value at the physical IO pins on Port C. For example, we can set all the port C pins to output and then make 4 of them high (binary 1) and 4 of them low (binary 0):

DDRC = 0xFF; //Set all Port C pins to output
PORTC = 0xF0; //Set first 4 pins of Port C low and next 4 pins high

When a pin is set to be an input, PORTx register DOES NOT contain the logic values applied to the pins. We use the PINx register for that. If a pin is an ouput, a 1 in the PORTx register sets a pull-up resistor. This is helpful for a variety of circuits.

DDRC = 0x00; //Set all Port C pins to input
PORTC = 0xFF; //Set pull-up resistors on Port C



The PINx Register

When a pin is set to input, the PINx register contains the value applied to the pin. The pins have an electrical threshold of around 2.5 volts. If a voltage above this level is applied to an input pin, the corresponding bit of the PINx register will be a 1. Below this voltage, the bit will be a zero.To read the value of an input port, you can use the inb(reg) function or a direct assignment. It returns an 8-bit number that is the value of the 8 bits in the specified register.

u08 foo; // declare an 8-bit variable

DDRD = 0x00; // set port D pins to input
PORTD = 0xFF; // set pull-ups on port D

foo = PIND; // read the value of the port D pins
// and store in the variable foo



Compilation can be done using avr-gcc by creating a makefile as follows:

MCU=atmega8
CC=avr-gcc
OBJCOPY=avr-objcopy
# optimize for size:
CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues

all: avrledtest.hex
avrledtest.hex : a.out
$(OBJCOPY) -R .eeprom -O ihex a.out avrledtest.hex
a.out : avrledtest.o
$(CC) $(CFLAGS) -o a.out -Wl,-Map,avrledtest.map avrledtest.o
avrledtest.o : avrledtest.c
$(CC) $(CFLAGS) -Os -c avrledtest.c

# erase the AVR before it is programmed
load: avrledtest.hex
uisp -dlpt=/dev/parport0 --erase -dprog=dapa
uisp -dlpt=/dev/parport0 --upload if=avrledtest.hex -dprog=dapa -v=3 --hash=32

clean:
rm -f *.o *.map *.out

Store this makefile in your current working directory using a vim or emac editor.Issue "make" to compile the program and "make load" to program the microcontroller with uisp.When you give the "make" command,"make" will search for file named makefile in the current directory and execute the commands in the file."make" command is highly specific about indentation.So,be sure that you give the separators and indentations as given above(do copy-paste). Any change in any part will lead to error messages while compiling.

As soon as the programming cycle is finished,LED starts to flash...
HURRAY!!!We have successfully entered to our dream world of embedded system....

Hacking microcontroller

Our first step(Hello World program) is to set up a burning circuit for Atmega8 microcontroller.GNU/LINUX provide all the tools required to make this journey fun and exciting.The first question that comes to our mind is

'What is a microcontroller?'In a single sentence it can be defined as a single chip computer.Atmega8 is a 28 pinned IC,where each pin has a dual function.We preferred it because it is the cheapest microcontroller available.A microcontroller is packed with RAM, ROM, Timers, Counters,Internal Circuitry,Analog comparators,ADC etc.In addition,it is also equipped with a good amount of 'flash' memory which can be erased with electric signals.The only extra component required for its functioning is the power supply.


Next question that arises in our mind is 'What is meant by a burning circuit?'
The basic idea is to write programs (mostly in assembly language or C.We used C as it is easy to code) on the PC and convert them to machine code belonging to the target microcontroller's instruction set. Once this is done, the machine code can be transferred to the non-volatile memory of the microcontroller via simple circuits connected to the serial or parallel port. This process is called `programming' or `burning' the micro.

Burning circuit helps u to familiarise with atmega8 and the softwares required to burn a program from pc to microcontroller.

Four software packages that we used to compile and burn program to a microcontroller:
# binutils-2.15.tar.bz2. This is the source code for tools like the assembler, linker etc.
# gcc-core-3.4.2.tar.bz2. This package contains the source of the GNU C compiler.
# avr-libc-1.0.4.tar.bz2. Source code for the AVR C library containing many utility functions.
#uisp-20050207.tar.gz.This is the program used to transfer machine code to the microcontroller.

The first three packages binutils,gcc-core and avr-libc are interconnected and version specific.Even higher versions can't take the place of lower versions.This is just one such set of interconnected packages.You can use any one of such sets.This set of packages support coding in C.But uisp is just a program which can be selected without any version constraints.There is another program that replaces uisp which is known by sp12.However,uisp is more convenient to use.


Before keeping the first footstep into the vast world of microcontrollers,some points need to be kept in mind:
*before installation see whether the following packages are installed or not
-lex packages otherwise linker will not be properly installed.
-g++ packages for proper installation of gcc-core.
-BSD for avr-libc(avr library).
*PATH must be specified each time when a new terminal is opened to compile programs.

Now we can start our exciting journey to the world of microcontroller....

Packages can be acquired either from a bootable CD(GNU/LINUX) or by downloading from internet.As you can see, booting from CD(GNU/LINUX) is the easiest method where no software installation is required.But as enthusiastic by nature we selected the round about way,that is,download and then install.Its,of course, a bit difficult at the same time exciting too.

Platform we work is the LINUX OS...

Following are the steps that we took to accomplish the above task:

We installed all the programs to /usr/local/avr. This is to keep the program separate from your normal Linux C compiler. Create this directory with the command:

mkdir /usr/local/avr

You can add it already now to your PATH:
mkdir /usr/local/avr/bin
export PATH=/usr/local/avr/bin:${PATH}


Software installation: GNU binutils-

The binutils package provides all the low-level utilities needed for building object files. It includes an AVR assembler (avr-as), linker (avr-ld), library handling tools (avr-ranlib, avr-ar), programs to generate object files loadable to the microcontroller's EEPROM (avr-objcopy), disassembler (avr-objdump) and utilities such as avr-strip and avr-size.

Run the following commands to build and install the binutils :

tar jxvf binutils-2.15.tar.bz2
cd binutils-2.15/
mkdir obj-avr
cd obj-avr
../configure --target=avr --prefix=/usr/local/avr --disable-nls
make

# as root:
make install



Add the line /usr/local/avr/lib to the file /etc/ld.so.conf and run the command /sbin/ldconfig to rebuild the linker cache.


Software installation: AVR gcc-

avr-gcc will be our C compiler.

Run the following command to build and install it:

tar jxvf gcc-core-3.4.2.tar.bz2
cd gcc-3.4.2

mkdir obj-avr
cd obj-avr
../configure --target=avr --prefix=/usr/local/avr --disable-nls --enable-language=c

make

# as root:
make install



Software installation: The AVR C-library-

Run the following command to build and install it:

tar jxvf avr-libc-1.0.4.tar.bz2
cd avr-libc-1.0.4
PREFIX=/usr/local/avr
export PREFIX
sh -x ./doconf
./domake

cd build
#as root:
make install


Software installation: The Programmer-

The programmer software loads the specially prepared object code into the EEPROM of our microcontroller.

The uisp programmer for Linux is a very good programmer. It can be used directly from within a Makefile. You just add a "make load" rule and you can compile and load the software in one go.

uisp is installed as follows:

tar jxvf uisp-20050207.tar.bz2.tar
cd uisp-20050207
./configure --prefix=/usr/local/avr
make

# as root:
make install

Now we have completed with all software installations.To see whether it's properly installed,all we need to do is run the simple program that follows.

"Hello, world" program for Embedded system-

PC programmers have it easy; once they install a compiler/interpreter for their favourite programming language, they can write a `hello,world' program and have it running instantly. It's not so simple for an embedded system programmer. Compiling the program is just half the job - the other half is downloading the code to the target microcontroller and getting it running. We will do things one step at a time - we will first write a simple C program and convert it to machine code, just to make sure that our newly installed development environment is working fine.

Here is our test program:

/* a.c, simple test program */
#include
main()
{
uint8_t c = 1;
}


We will compile it as shown below(make sure that /usr/local/avr/bin is in the path):

avr-gcc -mmcu=atmega8 -Os a.c


Try running the resulting `a.out'. You will get an error message:

./a.out: cannot execute binary file


The reason is that `a.out' contains machine code which will work only on the ATMega8 microcontroller and not on the host PC. This machine code has to be downloaded to the microcontroller using a special program (we will use a very capable tool called `uisp')

If you have reached here successfully ,it means the first three packages are successfully installed.Installation of uisp can be confirmed by:
# export PATH=$PATH:/usr/local/avr/bin
# uisp
uisp: No commands specified. Try 'uisp --help' for list of commands.


We use the parallel port and uisp to program the AVR. Uisp uses the ppdev interface of the kernel. Therefore you need to have the following kernel modules loaded:

# /sbin/lsmod
parport_pc
ppdev
parport

Check with the command /sbin/lsmod that they are loaded otherwise load them (as root) with:

modprobe parport
modprobe parport_pc
modprobe ppdev


It is a good idea to execute these commands automatically during startup. You can add them to a rc script (e.g for Redhat /etc/rc.d/rc.local).
To use the ppdev interface as normal user root needs to give you write access by once running the command

chmod 666 /dev/parport0

Make as well sure that no printer daemon is running on the parallel port. If you have one running then stop it before you connect the programmer cable. Now everything is ready to compile and program our microcontroller.


Error message like above confirms the proper installation of uisp.

Awaiting for suggestions...
We apologize for errors(if any) has occured as we ourselves are fresh to this field.We will be thankfull to those who points out our errors(if any)....

our maiden ROBOT

This is our first venture as Btech computer science students of a MIT-like institution of Kerala.We ultimately decided to create our own LINE FOLLOWING ROBOT USING ATMEGA8 MICROCONTROLLER.We had been successful in achieving our target and decided to share with you the experience and steps we took to make this journey a success. We first thought of getting acquainted with atmega8,a cheapest microcontroller.It provided us with a good platform as beginners..........................

We being great supporters of Free Software,all our posts are concerned with LINUX OS unless specified.We request all of you to follow the path of Free Software..............

Wednesday, May 14, 2008

we rock!!!!!!!

HI
this is me .suni.
ssssp stands for sajitha,suji,suni,syama and parvathy..,,,And atlast we too started blogging..