#define F_CPU 16000000
#include <avr\io.h>
#include <avr\pgmspace.h>
#include <avr\interrupt.h>
#include <util\delay.h>
#include <math.h>
#define ButtonPort PORTA // Buttons port
#define ButtonDir DDRA // Buttons Direction
#define ButtonPin PINA // Buttons port
#define Down 0 // Down Button
#define Up 1 // Up Button
#define Right 2 // Right Button
#define Left 3 // Left Button
#define Run 4 // Run Button
#define Back 5 // Back Button
int main(void)
{
DDRB=0xff;
PORTB=0x00;
ButtonDir=0x00;
ButtonPort=0xff;
uint16_t baud = 2400;
uint16_t UBRR_Value = lrint (( F_CPU / (16 * baud) ) - 1);
UBRRL = (uint8_t) UBRR_Value;
UBRRH = (uint8_t) (UBRR_Value >> 8);
UCSRB = (1<<RXEN) | (1<<TXEN);
UCSRC |= (1 << UPM1); //Sets parity to EVEN
UCSRC |= (3 << UCSZ0); //8-bit data length
while (1)
{
if (bit_is_clear(ButtonPin,Up))
{
_delay_ms(500);
while( ! (UCSRA & (1<<UDRE) ) );
UDR = 1;
}
if (bit_is_clear(ButtonPin,Down))
{
_delay_ms(500);
while( ! (UCSRA & (1<<UDRE) ) );
UDR = 2;
}
if (bit_is_clear(ButtonPin,Right))
{
_delay_ms(500);
while( ! (UCSRA & (1<<UDRE) ) );
UDR = 4;
}
if (bit_is_clear(ButtonPin,Left))
{
_delay_ms(500);
while( ! (UCSRA & (1<<UDRE) ) );
UDR = 8;
}
if (bit_is_clear(ButtonPin,Run))
{
_delay_ms(500);
while( ! (UCSRA & (1<<UDRE) ) );
UDR = 16;
}
while (! (UCSRA & (1 << RXC)) );
PORTB= UDR;
_delay_ms(500);
}
}