nech robim co robim toto je asi moje maximum :D [code]#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL //Defines clock speed
#define USART_BAUDRATE 9600 //Baudrate for serial comm.
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
#include <util/delay.h>
char rotDir = 'L';
char state;
void forward(void)
{
state = 'U';
PORTD=120;PORTA=1;
}
void backward(void)
{
state = 'D';
PORTD=116;PORTA=2;
}
void rightTurn(void)
{
PORTD=120;PORTA=0;
_delay_ms(350);
if (state == 'U') forward();
else if (state == 'D') backward();
}
void leftTurn(void)
{
PORTD=112;PORTA=1;
_delay_ms(350);
if (state == 'U') forward();
if (state == 'D') backward();
}
void leftPirouette(void)
{
state = 'a';
rotDir = 'L';
//PORTB = 0b00001010;
PORTD=116;PORTA=1;
}
void rightPirouette(void)
{
state = 'e';
rotDir = 'R';
//PORTB = 0b00010001;
PORTD=120;PORTA=2;
}
void stop(void)
{
if (state == 'U')
{
backward();
_delay_ms(40);
}
else if (state == 'D')
{
forward();
_delay_ms(40);
}
state = 'C';
PORTD=112;PORTA=0;
_delay_ms(1000);
}
//Serial com. Interrupt Service Routine (runs each time a byte is received)
ISR(USART_RX_vect)
{
char ReceivedByte;
ReceivedByte = UDR; // Retrieves byte from serial port (bluetooth module)
//UDR = state; // Echoes it back for fun
switch (ReceivedByte) //Which ASCII character was received?
{
case '!':
break;
case 'U': forward();
break; //Increase PWM duty cycle
case 'C': stop();
break; //Break motor by raising both direction inputs, PWM duty cycle 0%
case 'D': backward();
break; //Decrease PWM duty cycle
case 'L': leftTurn();
break;
case 'R': rightTurn();
break;
case 'a': leftPirouette();
break;
case 'e': rightPirouette();
break;
default: UDR = '?';
break; //Character unknown to my routine, discard character
}
}
int main(void)
{
DDRC=0;
DDRD=124;
PORTC=255;
OCR1A=255;
OCR1B=255;
TCCR1A = (1<<COM1A1) | (1<<COM1B1) |(1<<WGM10);
TCCR1B = (1<<WGM12) | (1<<CS10);
DDRA=3;
DDRB=0;
PORTB=255;
PORTA=3;
//UART
UCSRB = (1<<RXEN)|(1<<TXEN)|(1<<RXCIE); //Enable Tx and Rx
UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
UBRRL = BAUD_PRESCALE; //Sets
UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers
UCSRB |= (1 << RXCIE); //Enable USART-interrupt
/* This program is completely interrupt driven, so nothing goes on in while loop*/
while(1); //Never gets out from here!
return 0; //Never reaches this point!
}[/code]
ale nejde uz nwm co tam mam spravit snazil som sa spojit ten na microhobby s atmegou
http://microhobby.net/26-06-2011/projects/bluetooth-robot-bt-bot/