Friday 18 January 2013

SAMPLE CODE FOR THE LINE FOLLOWER (ROUND 1)


#define F_CPU 1000000UL            //Define Clock speed. This is currently set to 1Mhz(Which is default for a factory shipped AVR Micro). UL represents Hertz.
#include <avr/io.h>                //Header file for basic I/O Operations
#include<util/delay.h>          //Header file for the Delay Routine


void wait(float sec, int freq)             //Delay Routine (Leave it as it is)
{

int i;

if(freq==1)
for(i=0;i<(int)(1302*sec);i++)
_delay_loop_1(0);

if(freq==2)
for(i=0;i<(int)(2604*sec);i++)
_delay_loop_1(0);

if(freq==4)
for(i=0;i<(int)(15*sec);i++)
_delay_loop_2(0);

if(freq==8)
for(i=0;i<(int)(30*sec);i++)
_delay_loop_2(0);

if(freq==12)
for(i=0;i<(int)(46*sec);i++)
_delay_loop_2(0);

if(freq==16)
for(i=0;i<(int)(61*sec);i++)
_delay_loop_2(0);

}



int main()                    //Start of MAIN
{

DDRC=0x00;    //Set PORT C as input for the sensors.
/*Left sensor connected to bit 7 of PORT C (Change it a/c to your setup)
   Middle sensor connected to bit 6 of PORT C (Change it a/c to your setup)
   Right sensor connected to bit 5 of PORT C (Change it a/c to your setup)
*/

DDRD=0xFF;    //Set PORT D as output for connection with motor driver

/*A1-->PD0--> + --> +ve of the left motor
  A2-->PD1--> - --> -ve of the left motor
  B1-->PD2--> + --> +ve of the right motor
  B2-->PD3--> - --> -ve of the right motor
*/

int middle_sensor=0,left_sensor=0,right_sensor=0;

PORTD=0b00010101;       //Motor driver pulses along with enable active at PD4-->ENABLE

while(1)    //Infinite Loop
{
 left_sensor=PINC&0b10000000;  //Left sensor connected to bit 7 of PORT C (Change it a/c to your setup)
 middle_sensor=PINC&0b01000000;   //Middle sensor connected to bit 6 of PORT C (Change it a/c to your setup)
 right_sensor=PINC&0b00100000;    //Right sensor connected to bit 5 of PORT C (Change it a/c to your setup)

if(left_sensor==1 && middle_sensor==0 && right_sensor==0)     //For sensor state : 1 0 0 -> sharp left
{
PORTD=0b00010110;    //Motor driver bit combination for sharp left turn
wait(.2,8);      
wait(1,8);
PORTD=0b00010101;            //Move Forward
}

if(left_sensor==0 && middle_sensor==0 && right_sensor==1)     //For sensor state : 0 0 1 -> Sharp Right
{
PORTD=0b00011001;            //Motor driver bit combination for sharp right turn
wait(.2,8);
wait(1,8);
PORTD=0b00010101;            //Move Forward
}

if(left_sensor==1 && middle_sensor==1 && right_sensor==1)     //For sensor state : 1 1 1 -> All Sensor on horizontal strip(Move Forward and flash Led)
{

PORTD=0b00010101;            //Move Forward if you are on a horizontal strip.
PORTD=0b10000000;     //Glow an LED if on a horizontal strip.(LED connected to bit 7 of PORT D)
}

/*(Here in this code i have not written all the possible logic condtions (for three sensors!) which might arise on the track.

You will require to put all other logic condtions which you find is necessary.)*/

} //END of WHILE
 return 0;
} //END OF MAIN

The IR Line sensor

This is a 8 sensor array of iR sensors

The Sensor Array
The sensor board consists of 8 IR sensors. The sensors are placed 1.2 cm apart from each other.Two on oboard LM324 onboard convert the anaglog signals from the IR photodiode and convert them to digital signals. There is a onboard preset to calibrate the sensors. The sensor board PCB is shown below. I used EaglePCB for the designing because it is quite easy to learn and comes free. The sensor board is single sided so it should be possible to etch at home. Just print the board to size and etch it.



The above picture is of the silk side of the sensor array. Here you can clearly see how the components have been placed in the vero board. The symbols marked with "L#" represent respective IR diodes and the symbols marked with "PD#" represent the respective IR sensors.


The solder side of the above sensor module

You need to solder your vero board according to the above given layout. Just try to relate the layout with the silside of the sensor module. Although this module has 8 sensors but in most of the competitions 3 sensors will suffice! One on the line. The other two on both sides of the line; the Left Sensor and the Right Sensor.


Wednesday 9 January 2013

Loch Master Files for the Serial AVR Programmer


Components List

D1    1N4148  Switching Diode
D2    Zener-diode 500mW, 5.1V
D3    Zener-diode 500mW, 5.1V
T1    BC 846
R1    4.7K
R2    10k
R3    4.7K
R4    33K
R5    10K
J1     DB9-connector
-       Pointed Vero board small (ID : KS 100/4)
-       Pin strip, 6 pins (break from male berg strip)
-       Single core wires
-       Serial (Male-Female) connector

This is the Component List for building the Serial AVR Programmer. Assemble them on a Vero board (general purpose board) according to the LochMaster layouts. The layout has been optimized (for easier routing) from the Schematic for the Serial AVR Development Board which was published in my previous post.You can download them from the following link. 

Click here to download the LochMaster files for the Serial AVR Programmer.


Monday 7 January 2013

A Low Cost Serial Programmer for AVRs


The Serial Programmer for AVR

A hardware device is required to load your program (in the form of Hex file!) into the Flash memory of the Micro-controller, after which it performs according to the program instructions inside it. The device is called a Programmer. The above circuit is of a serial programmer for AVR micro-controllers which is easy to build at a low cost in home. Most of the PC's and Laptops have a serial port, so it can be used in both with ease.

[N.B. This programmer will not work with USB-SERIAL converters. It only works with native Serial Port.]

In-System Programming 

In-System-Programming is a feature which allows programming and reprogramming of any AVR micro-controller at the end system. Simply put, if you have a micro-controller which lists that it is “In-System-Programmable” then you will be able to program it on site. Which is unlike older MCU’s which would have to be removed from the main target system and then put into a separate hardware programmer to burn their flash (loading the firmware into the flash memory). For example lets say, you need to upgrade the software version in your Android phone (i.e. if you want to install the latest Android Os.), then you don’t need to take your phone to a service centre! You can just download it and upgrade your software version right from your phone. This would be possible because the phone’s processor is “In-System-Programmable”.
"In-System Programming eliminates the physical removal of the chips from the system. This saves time, and money, both during development in the lab, and when updating the software and parameters in the field", 

The Serial Programmer
Here is the PCB etch diagram which is pretty simple and can be etched easily by toner transfer or any other method of PCB etching. Or you can even try to solder the components on a pointed vero board according to the PCB layout.
PCB layout for Etching.


You can connect your serial programmer to an Atmega 8 as shown in the following figure. [Image courtesy : electronics-diy.com]

You can also build a simple socket board for programming your Atmel AVR 8 (you can also have an ISP connector for your development board according to the programmer ISP layout and then you can directly plug in the programmer to your board) as shown in the following picture. [Image courtesy : electronics-diy.com]

Socket PCB for an AVR Atmega 8

Extra bit (For Advanced Readers)

Most AVR devices have a facility known as SPI which stands for Serial Programming Interface. This is the functionality that allows us to program (load the firmware in the flash memory of the MCU) the micro-controller.
The SPI is a pretty simple programming interface (the hardware philosophy behind loading the flash memory with the firmware) which incorporates only three wires. The serial programming interface (SPI) consists of three wires: The MISO (Master Input-Slave Output) line, MOSI (Master Output-Slave Input) line and the SCK (System Clock) line.
Here in this serial programmer we have used these three SPI lines along with the Ground, VCC and Reset pin.
While programming the AVR, the in system programmer always operates as the Master mode, and the target system always operates as a Slave mode.
The In-System Programmer (Master) provides the clock for the communication on the SCK Line. Each pulse on the SCK Line transfers one bit from the Programmer (Master) to the Target (Slave) on the Master Out – Slave In (MOSI) line. Simultaneously, each pulse on the SCK Line transfers one bit from the target (Slave) to the Programmer (Master) on the Master In – Slave Out (MISO) line.
The programmer is quite simple and it is based on the SI-Prog from the author of PonyProg software. The Zener diodes D2, D3 with the resistors R1, R3 reduce the voltage from the ouput pins DTR, RTS on the serial port to around 5V which is suitable for microcontroller (MOSI, SCK). MISO signal is connected directly to the input CTS pin. The Zener diode D1 with the resistor R2 drive the NPN transistor T1, which controls the RESET signal. The AVR microcontrollers are in reset when the signal has low level. The resistor R5 works as a pull-up for reset signal. 

Software 

PonyProg software is required to program using this serial device. Its an open source software hosted by http://www.lancos.com/prog.html

Click here to download the Eagle files for the Schematic and the PCB layout. [This link will soon be available- check this space!]