hello...
I'm still developing a code here...
can somebody test it out somehow?
I don't know what is the problem
the reciever or the transmitter
here is the code for the reciever:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
/* PB4 M1 - pwm B
* PB2 M1
* PB3 M2 - pwm A
* PB1 M2
* PD6 M1 -enable
* PB0 M2 -enable
* forward: PB1,PB2 low, pwm
* reverse: PB1,PB2 high, inverted pwm
* stop: PB1,PB2 low, pwm 0(low);
* protocol has to send first the direction, 2. left speed (M2), 3. right speed
*/
void forward(void);
void reverse(void);
void left(void);
void right(void);
void stop(void);
void USART_init(uint16_t ubrr_value);
uint8_t direction; //0-stop, 1-forward, 2-reverse, 3 - left, 4 - right
char getspeed = 'n';
uint8_t EEMEM speedL = 150;
uint8_t EEMEM speedR = 150;
int main()
{
DDRB = 0xFF; //motors output
DDRD |= (1 << PD6); //motor output
PORTD |= (1 << PD6); //enable 1
PORTB |= (1 << PB0); //enable 2
TCCR1B |= (1 << CS10);
USART_init(25); //9600 baud
direction = 0;
sei();
while (1)
{
switch (direction)
{
case 0:
stop();
break;
case 1:
forward();
break;
case 2:
reverse();
break;
case 3:
left();
break;
case 4:
right();
break;
}
}
};
void reverse()
{
PORTB = 0b00000101;
TCCR1A = 0b10110001;
};
void forward()
{
PORTB = 0b00000011;
TCCR1A = 0b11010001;
};
void right()
{
PORTB = 0b00000111;
TCCR1A = 0b11110001;
};
void left()
{
PORTB = 0b00000001;
TCCR1A = 0b10100001;
};
void stop()
{
OCR1A = 0;
OCR1B = 0;
};
//UART PART
void USART_init(uint16_t ubrr_value)
{
UBRRL = ubrr_value;
UBRRH = (ubrr_value >> 8);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0);
UCSRB = (1 << RXEN)|(1<<RXCIE);
};
ISR(USART_RX_vect)
{
char info;
while (!(UCSRA & (1 << RXC)));
info = UDR;
if (info == 'y')
{
while (!(UCSRA & (1 << RXC)));
direction = UDR;
if (!(direction == 0))
{
while (!(UCSRA & (1 << RXC)));
OCR1A = UDR;
while (!(UCSRA & (1 << RXC)));
OCR1B = UDR;
}
else
{
OCR1A = 0;
OCR1B = 0;
}
}
else
{
while (!(UCSRA & (1 << RXC)));
direction = UDR;
OCR1A = eeprom_read_byte(&speedL);
OCR1B = eeprom_read_byte(&speedR);
}
};
if there is a problem here please let me know...
Thank you