funguje to!!! trosku som sa s tym musel pobit, ale dovolim si poslat na ukazku program ktory prijima data z usartu a generuje signal pre serva (mam vyskusane len pre jedno servo, ale urcite bude fungovat pre vsetkych 6).
zatial velke Ď
// program na ovladanie 6 servomotorov
#include<avr/io.h>
#include<avr/interrupt.h>
// Define baud rate
#define F_CPU 16000000UL
#define USART_BAUD 38400
#define USART_UBBR_VALUE F_CPU/16/USART_BAUD-1
unsigned int counter_value = 0;
unsigned int servo_value[6];
void timerInit(void)
{
// kazdych 312 taktov pri 16 mhz generuj prerusenie .. cca 19,5 us
ICR1 = 311;
// Timer/Counter Mode 14 (Fast PWM, TOP=ICR1): WGM13:0 = 1110
// clk/1: CS13:0 = 001
TCCR1A |= 1<<WGM11;
TCCR1B |= 1<<WGM13 | 1<<WGM12 | 1<<CS10;
// Timer/Counter1, Overflow Interrupt Enable
TIMSK |= 1<<TOIE1;
sei();
}
void init_uart(unsigned int ubrr)
{
/* Set baud rate */
UBRRH = (unsigned char)(ubrr>>8 ) ;
UBRRL = (unsigned char)(ubrr);
/* Enable receiver and transmitter */
UCSRB |= (1<<RXEN);
UCSRB |= (1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0);
}
unsigned char USART_vReceiveByte()
{
// Wait until a byte has been received
while((UCSRA&(1<<RXC)) == 0);
// Return received data
return UDR;
}
// ak nastane prerusenie
ISR(TIMER1_OVF_vect)
{
// ak dosiahne counter_value hodnotu serva, vynuluje sa prislusny pin
if (counter_value == servo_value[0]) PORTB &= ~_BV(PB0);
if (counter_value == servo_value[1]) PORTB &= ~_BV(PB1);
if (counter_value == servo_value[2]) PORTB &= ~_BV(PB2);
if (counter_value == servo_value[3]) PORTB &= ~_BV(PB3);
if (counter_value == servo_value[4]) PORTB &= ~_BV(PB4);
if (counter_value == servo_value[5]) PORTB &= ~_BV(PB5);
if (counter_value == 1024)
{
//reset counter_value
counter_value = 0;
//set all servo pins to 1
PORTB |= _BV(PB0) | _BV(PB1) | _BV(PB2) | _BV(PB3) | _BV(PB4) | _BV(PB5);
}
counter_value++;
}
int main(void)
{
unsigned char u8Data;
unsigned int i;
// Set PB0 - PB5 as outputs
DDRB = 0x3F;
// Initialise USART
init_uart(USART_UBBR_VALUE);
// Initialize TIMER
timerInit();
while(1)
{
//kontrola, ci prisiel spravny paket
label:
u8Data = USART_vReceiveByte();
if (u8Data == 0xFF)
{
u8Data = USART_vReceiveByte();
if (u8Data == 0xFF)
{
u8Data = USART_vReceiveByte();
if (u8Data == 0xFF)
{
u8Data = USART_vReceiveByte();
if (u8Data == 0x00)
{
// citaj data z usartu
for(i=0;i<6;i++)
{
servo_value* = USART_vReceiveByte();
}
}
else goto label;
}
else goto label;
}
else goto label;
}
else goto label;
}
}
a program na pc, ktory posiela data (servo beha hore dole)
[code]
#include <stdio.h> /* Standard input/output definitions /
#include <string.h> / String function definitions /
#include <unistd.h> / UNIX standard function definitions /
#include <fcntl.h> / File control definitions /
#include <errno.h> / Error number definitions /
#include <termios.h> / POSIX terminal control definitions */
// ‘open_port()’ - Open serial port 1. Returns the file descriptor on success or -1 on error.
int open_port(void)
{
int fd; /* File descriptor for the port /
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/
Could not open the port.
*/
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
return (fd);
}
//konfiguracia portu 38400 baud, no parity, 1 stop bit
void configure_port(void)
{
int fd; /* File descriptor for the port */
struct termios options;
/*
- Get the current options for the port…
*/
tcgetattr(fd, &options);
/*
- Set the baud rates to 38400…
*/
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
/*
- Enable the receiver and set local mode…
*/
options.c_cflag |= (CLOCAL | CREAD);
//character size
options.c_cflag &= ~CSIZE; /* Mask the character size bits /
options.c_cflag |= CS8; / Select 8 data bits */
//no parity
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Set the new options for the port…
tcsetattr(fd, TCSANOW, &options);
}
void main(void)
{
int fd = open_port();
unsigned char i = 35;
unsigned int smer = 0;
configure_port();
unsigned short int n;
while(1)
{
if (i == 35) smer = 0;
if (i == 120) smer = 1;
if (smer == 0) i++;
else i–;
unsigned char send_bytes] = { 0x00, i, 0x0F, 0xDD, 0x55, 0xEE, 0xEE, 0xFF, 0xFF, 0xFF};
n = write(fd, send_bytes, 10); //Send data
if (n < 0)
fputs(“write() of 10 bytes failed!\n”, stderr);
usleep(20000);
}
close(fd);
}
[/code]*