#include <mega8.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>
// Standard Input/Output functions
#include <stdio.h>
#define RXC 7
int s_m[4]={0xC,0x6,0x3,0x9};
unsigned char USART_Recive(void)
{
if((UCSRA &(1<<RXC)))
return UDR;
}
void rigth(void);
void left(void);
// Declare your global variables here
char input_ch='s';
bit cb=1;
int cunt=0
;
char out[16];
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State6=P State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x40;
DDRC=0x3F;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud rate: 4800
UCSRA=0x00;
UCSRB=0x10;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x0C;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
lcd_clear();
lcd_putsf(" HELLO ");
delay_ms(1000);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("OPEN HYPER... &");
lcd_gotoxy(0,1);
lcd_putsf("PRESS R/L 4 RUN");
while (1)
{
// Place your code here
lcd_gotoxy(0,0);
input_ch=USART_Recive();
switch(input_ch)
{
case 'l' :
case 'L' :
{
lcd_gotoxy(0,0);
lcd_putsf("DIRECT IS RIGTH");
lcd_gotoxy(0,1);
lcd_putsf("PRESS S TO STOP");
cunt=1;
break;
}
case 'r' :
case 'R' :
{
lcd_gotoxy(0,0);
lcd_putsf("DIRECT IS LEFT ");
lcd_gotoxy(0,1);
lcd_putsf("PRESS S TO STOP ");
cunt=2;
break;
}
case 's' :
case 'S' :
{
lcd_gotoxy(0,1);
lcd_putsf("PRESS R/L TO RUN");
cunt=3;
break;
}
}//end of switch 1
switch(cunt)
{
case 1 :
{
rigth();
break;
}
case 2 :
{
left();
break;
}
case 3 :
{
if(cb==1)
{
cb=!cb;
lcd_gotoxy(0,0);
lcd_putsf(" STOPED MOTOR ");
delay_ms(300);
}
else
{
cb=!cb;
lcd_gotoxy(0,0);
lcd_putsf(" ");
delay_ms(300);
}
}
}//end of switch 2
};
}//end of main\
// start function
void rigth(void) //rigth drive motor
{
int in_port;
int i;
for(i=0;i<4;i++)
{
in_port=PORTC & 0xf0;
PORTC=in_port | s_m[i];
delay_ms(10);
}
}
void left(void) //left drive motor
{
int in_port;
int i;
for(i=3;i>=0;i--)
{
in_port=PORTC & 0xf0;
PORTC=in_port | s_m[i];
delay_ms(10);
}
}