From MARS Wiki
/*
Addition: Intial Port Set Up and Movement Funcitons
Condition: Working
Date: Unknown
Addition: LCD Functions
Condition: Working
Date: April 13,2007
*/
#include <mega162.h>
#include <delay.h>
#include <stdio.h>
int Read_Infrared();
void Go_Forward();
void Go_Reverse();
void Go_Left();
void Go_Right();
void Stop();
#define SIDE_SONIC PINA.0 //SIDE Ultrasonic
#define FRONT_SONIC PINA.1 //FRONT Ultrasonic
/************** LCD ***************/
#define LCD_DATA_WR PORTC
#define LCD_DATA_RD PINC
#define LCD_RS PORTD.4
#define LCD_RW PORTD.3
#define LCD_E PORTD.2
#define LINE1 (putcmd_lcd(0x80))
#define LINE2 (putcmd_lcd(0xC0))
#define DISPLAY_ON (putcmd_lcd(0x0C))
#define DISPLAY_OFF (putcmd_lcd(0x08))
#define BLINK_ON (putcmd_lcd(0x0D))
#define BLINK_OFF (putcmd_lcd(0x0C))
#define CURSOR_ON (putcmd_lcd(0x0E))
#define CURSOR_OFF (putcmd_lcd(0x0C))
#define LCD_HOME (putcmd_lcd(0x02))
#define LCD_CLEAR_HOME (putcmd_lcd(0x01))
void putchar_lcd(unsigned char);
void putcmd_lcd(unsigned char);
void putstringf_lcd(unsigned char flash *);
void putstring_lcd(unsigned char *);
void init_lcd(void);
/***************************************/
int main(void)
{
// Declare your local variables here
unsigned char Infered_value=0;
unsigned char i =0;
unsigned char *message = "GOOOOOOO!!!!!!!!";
unsigned char *leftmsg = "GO LEFT!!!";
unsigned char *attack = "ATTACK!!!";
unsigned char *back = "BACK IT UP!!!";
// Crystal Oscillator division factor: 1
/*#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif
*/
// Input/Output Ports initialization
PORTA=0x00;
DDRA=0xF8;
//PINA.0 input side ultrasonics
PORTB=0x00;
DDRB=0x00; // All Inputs
PORTC=0x00;
DDRC=0xFF; // All Outputs
PORTD=0x0;
DDRD=0x1C; // All Inputs
PORTE=0x00;
DDRE=0x07; // All output 0 - 2
init_lcd(); //YOU MUST INTIALIZE THE LCD BEFORE YOU CAN USE IT
LCD_CLEAR_HOME; //Clear the LCD
while(i<=4)
{
LINE1;
delay_ms(124);
putchar_lcd('5');
delay_ms(124);
LCD_CLEAR_HOME;
i++;
}
i=0;
while(i<=4)
{
LINE1;
delay_ms(124);
putchar_lcd('4');
delay_ms(124);
LCD_CLEAR_HOME;
i++;
}
i=0;
while(i<=4)
{
LINE1;
delay_ms(124);
putchar_lcd('3');
delay_ms(124);
LCD_CLEAR_HOME;
i++;
}
i=0;
while(i<=4)
{
LINE1;
delay_ms(124);
putchar_lcd('2');
delay_ms(124);
LCD_CLEAR_HOME;
i++;
}
i=0;
while(i<=4)
{
LINE1;
delay_ms(124);
putchar_lcd('1');
delay_ms(124);
LCD_CLEAR_HOME;
i++;
}
i=0;
LCD_CLEAR_HOME;
LINE1;
putstring_lcd(message);
if(SIDE_SONIC==1)
{ //May change to other side later
Go_Right();
}
else
{ //May be changed to other side later
putstring_lcd(leftmsg);
Go_Left();
}
while (1)
{
if(FRONT_SONIC==1)
{
Go_Forward();
putstring_lcd(attack);
}
else
{
if(SIDE_SONIC==1)
{ //May change to other side later
Go_Right();
}
else
{ //May be changed to other side later
putstring_lcd(leftmsg);
Go_Left();
}
}
};
return 0;
}
//
/******* IR SENSOR FUNCTION*********/
int Read_Infrared()
{
/*
front right=PB0 Front Left= PB1 Back Left=PB2 Back Right=PB3
Returns an integer between 0 and 15 refering to the combinations of configurations of irfered sensors seeing black
*/
unsigned char Infrared_Command;
Infrared_Command= PINB & 0x0F;
return Infrared_Command;
}
/****************END*****************/
/************ MOTOR CONTROL FUNCTIONS*********/
void Go_Forward()
{
Stop();
delay_ms(1);
PORTA.5=1;
PORTA.3=1;
PORTA.2=0;
PORTA.4=0;
PORTA.7=1;
PORTE.1=1;
PORTE.0=0;
PORTA.6=0;
}
void Go_Reverse()
{
Stop();
delay_ms(1);
PORTA.5=0;
PORTA.3=0;
PORTA.2=1;
PORTA.4=1;
PORTA.7=0;
PORTE.1=0;
PORTE.0=1;
PORTA.6=1;
}
void Go_Right()
{
Stop();
delay_ms(1);
PORTA.5=1;
PORTA.3=1;
PORTA.2=0;
PORTA.4=0;
PORTA.7=0;
PORTE.1=0;
PORTE.0=1;
PORTA.6=1;
}
void Go_Left()
{
Stop();
delay_ms(1);
PORTA.5=0;
PORTA.3=0;
PORTA.2=1;
PORTA.4=1;
PORTA.7=1;
PORTE.1=1;
PORTE.0=0;
PORTA.6=0;
}
void Stop()
{
PORTA.5=0;
PORTA.3=0;
PORTA.2=0;
PORTA.4=0;
PORTA.7=0;
PORTE.1=0;
PORTE.0=0;
PORTA.6=0;
}
/********************END************************/
/*****************LCD FUNCTIONS***********************/
void putchar_lcd(unsigned char ch)
{
LCD_RW = 0; // select the write option
LCD_RS = 1; // select the data register
delay_ms(1); // need a slight delay here
LCD_E = 1; // strobe the enable line
LCD_DATA_WR = ch; // place the character on the "data" bus
delay_ms(1); // need a slight delay here for data setup
LCD_E = 0;
delay_ms(1); // can only write so fast!!!!
}
void putcmd_lcd(unsigned char ch)
{
LCD_RW = 0; // select the write option
LCD_RS = 0; // select the command register
delay_ms(1); // need a slight delay here
LCD_E = 1; // strobe the enable line
LCD_DATA_WR = ch; // place the character on the "data" bus
delay_ms(1); // need a slight delay here for data setup
LCD_E = 0;
delay_ms(5); // just to be safe, delay after commands
}
void putstringf_lcd(unsigned char flash *str)
{
while(*str != 0) // do until the NULL character...
{
putchar_lcd(*str++); // send the character and then increment to the next
}
}
void putstring_lcd(unsigned char *str)
{
while(*str != 0) // do until the NULL character...
{
putchar_lcd(*str++); // send the character and then increment to the next
}
}
void init_lcd(void)
{
delay_ms(15); // must wait for 15mSec...
putcmd_lcd(0x30); // write Function Set.
delay_ms(5); // wait 5mSec...
putcmd_lcd(0x30); // write Function Set...
delay_ms(1); // delay....
putcmd_lcd(0x30); // write Function Set
delay_ms(1); // delay....
putcmd_lcd(0x38); // write function set (last time)
delay_ms(1);
putcmd_lcd(0x08); // display off.
delay_ms(1);
putcmd_lcd(0x01); // display on.
delay_ms(1);
putcmd_lcd(0x02); // LCD home
delay_ms(1);
putcmd_lcd(0x0E); // turn the cursor on.
delay_ms(5); // delay again...
}
/********************END*****************************/
/*****************END OF CODE*********************?