main.c

/**********************************************************************
    main.c

    This file is part of the <MyPackage> package

    Copyright (c) 2003 dinux - dinux@mail.bg

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

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

#include <stdio.h>
#include <stdlib.h>
#include <avr/io.h>
#include <avr/pgmspace.h>

#include "main.h"
#include "motors.h"


//Stores boolean flags 
volatile struct _bitfield bits_store1;

//Send message to USART
void send_msg( PGM_P msg)
{
	while( *msg ) {
		while(!(UCSR0A & (1<<UDRE0)))  /* Wait until transmit is complete */
			;
		UDR0 = *msg++;
	}
}


int main (void)
{
	uint8_t c;

	
	//Initialize the UART.  - 8N1
	UCSR0B = ( (1<<RXEN0)|(1<<TXEN0) );
#if CPU_CLOCK==11059200
//        UBRR0L = 71; UBRR0H=0;		/* For 11.059 MHz CPU and 9600 RS232 link. */
        UBRR0L = 5; UBRR0H=0;		/* For 11.059 MHz CPU and 115200 RS232 link. */
#endif
#if CPU_CLOCK==16000000
//        UBRR0L = 103; UBRR0H=0;         /* For 16 MHz CPU and 9600 RS232 link. */
        UBRR0L = 25; UBRR0H=0;		/* For 16 MHz CPU and 38.4 RS232 link. */
#endif
#if CPU_CLOCK==18432000
//        UBRR0L = 119; UBRR0H=0;         /* For 18.4320 MHz CPU and 9600 RS232 links */
        UBRR0L = 9; UBRR0H=0;		/* For 18.4320 MHz CPU and 115200 RS232 link. */
//	UBRR0L = 4; UBRR0H=0;		/* For 18.4320 MHz CPU and 230400 RS232 link. */
#endif
	UCSR0C = (1<<UCSZ00)|(1<<UCSZ01);       //8 bit


	//Enter the main loop.
	for(;;) {
		while(!(UCSR0A & (1<<RXC0))) {	//Wait for a byte
		}
		c = UDR0;
		//Parse the input
		switch(c) {
			case ' ':
				send_msg(PSTR("Both motors stopped\r\n"));
				set_left_motor(0);
				set_right_motor(0);
				break;
			case 'q':
			case 'Q':
				send_msg(PSTR("Left motor forward\r\n"));
				set_left_motor(1);
				break;
			case 'a':
			case 'A':
				send_msg(PSTR("Left motor stopped\r\n"));
				set_left_motor(0);
				break;
			case 'z':
			case 'Z':
				send_msg(PSTR("Left motor backward\r\n"));
				set_left_motor(2);
				break;
			case 'w':
			case 'W':
				send_msg(PSTR("Right motor forward\r\n"));
				set_right_motor(1);
				break;
			case 's':
			case 'S':
				send_msg(PSTR("Right motor stopped\r\n"));
				set_right_motor(0);
				break;
			case 'x':
			case 'X':
				send_msg(PSTR("Right motor backward\r\n"));
				set_right_motor(2);
				break;
			default:
				send_msg(PSTR("? Unkown keystroke\r\n"));
		}	
	}

	return 0;
}