Ich sehe keinen Grund warum die beiden USART nicht unterschiedliche Baudraten haben könnten.
Druckbare Version
Ich sehe keinen Grund warum die beiden USART nicht unterschiedliche Baudraten haben könnten.
Dieser Quarz passt ja bestens zu allen Baudraten unter 200kBd, siehe hier (klick).Zitat:
... Quarz ... 7,3728MHz und ich habe auch schon 7,3728MHz ...
Moin,
Wozu brauche ich RX-Buffer ? laut meinem Code die Byte die von UDR0 empfangen wurden, werden nachher in der Variable "data" geschrieben. Später werden die Byte, die vom "data" empfangen wurden, nacheinander in usart_command[usart_command_size] geschrieben. Meiner Meinung nach brauche ich kein RX-Buffer.Zitat:
Ich habe mir das Programm mal kurz angeschaut, da ist kein RX-Buffer vorhanden.
Aber es kann sein, dass mein Gedanke falsch ist. Wenn es der Fall ist, kannst du mir durch ein Beispiel illustrieren, wie ich RX-Buffer in meinem Code einsetzen soll.
Ich habe folgende Änderung in meinem Code gemacht:
- Neues Baudrate : 9600bps
- Die Taktangabe, die auf meinem Quarz steht, ist 7,372MHz. Ich habe genau diese Taktangabe im Quelltext hineingeschrieben.
- Änderung von Stoptbit in PC und Mikrocontroller-Programm: Jetzt 1 Stopbit anstelle 2 Stopbits
- Der PC, die beiden Motorsteuerungen und der Mikrocontroller haben das gleiche Baudrate und zwar 9600bps.
- Ich habe sogar jeweils der Buffer usart_buffer_max_size, sowie usart_command_max_size auf 512u und 20 erweitern
Leider ist das Problem gleich geblieben. Ich vermute, der PC bekommt nicht rechtzeitig eine Rückmeldung vom Mikrocontroller, bevor er die nächsten Positionskoordinaten sendet. Laut meinem Gedanke soll der PC für jede gesendete Positionskoordinaten ein Signal vom Mikrocontroller bekommen, bevor er die nächste Positionskoordinaten sendet. Aber das funktioniert für höchstens zwei oder drei Positionskoordinaten. Aber die restlichen Positionskoordinaten werden vom Pc nicht nicht übertragen.
Ich habe delay-Funktion benutzt, um die Kommunikation zu verbessert. Aber es klappt immer nicht!!
Ich wäre sehr dankbar für weitere Hilfe.
Danke im Voraus..
Mikrocontroller-Code:
Code:#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
# define F_CPU 3686400UL
#include <util/delay.h>
#define FOSC 7372000 // Clock Speed
#define BAUD 9600UL
#define UBRR_VAL ((FOSC+BAUD*8)/(BAUD*16)-1) // clever runden
///////////////////////////////////////////////////////////////////
#define usart_buffer_max_size 512u
#define usart_command_max_size 20
char usart_command[usart_command_max_size + 1] = {0};
char usart0_tx_buffer[usart_buffer_max_size];
char usart1_tx_buffer[usart_buffer_max_size];
volatile uint8_t usart_command_size = 0;
volatile uint8_t usart0_tx_buffer_size = 0;
volatile uint8_t usart0_tx_buffer_start = 0;
volatile uint8_t usart1_tx_buffer_size = 0;
volatile uint8_t usart1_tx_buffer_start = 0;
/////////////////////////////////////////////////////////////////////
volatile uint8_t command_ready = 0;
/////////////////////////////////////////////////////////////////////
// Configuration USART0, USART1 and setting Baudrate
void USART_Init(unsigned int ubrr)
{
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char) ubrr;
UBRR1H = (unsigned char)(ubrr>>8);
UBRR1L = (unsigned char) ubrr;
UCSR0B = (1<<RXEN0) | (1<<TXEN0) | (1<<RXCIE0) | (0<<TXCIE0) | (0<<UDRIE0);
UCSR0C = (0<<USBS0) | (1<<UCSZ01) | (1<<UCSZ00); // 1 Stopbit, data = 8 Bits
UCSR1B = (1<<RXEN1) | (1<<TXEN1) | (0<<RXCIE1) | (0<<TXCIE1) | (0<<UDRIE1);
UCSR1C = (0<<USBS1) | (1<<UCSZ11) | (1<<UCSZ10); // 1 Stopbit, data = 8 Bits
}
////////////////////////////////////////////////////////////////////////////////
// function receive USART0
unsigned char USART0_Receive (void)
{
while(!(UCSR0A & (1<<RXC0)) ); // Wait for data to be received
return UDR0; // Get and return received data from buffer
}
// function transmit USART0
void USART0_Transmit (unsigned char data0)
{
while ( !(UCSR0A & (1<<UDRE0)) ); // Wait for empty transmit buffer
UDR0 = data0; // Put data into buffer, sends the data
}
// function receive USART1
unsigned char USART1_Receive (void)
{
while(!(UCSR1A & (1<<RXC1)) ); // Wait for data to be received
return UDR1; // Get and return received data from buffer
}
// function transmit USART1
void USART1_Transmit (unsigned char data1)
{
while ( !(UCSR1A & (1<<UDRE1)) ); // Wait for empty transmit buffer
UDR1 = data1; // Put data into buffer, sends the data
}
/////////////////////////////////////////////////////////////////////
// Eingang vom Ringpuffer
void USART0_QueueIn(char c)
{
int i;
if (usart0_tx_buffer_size < usart_buffer_max_size)
{
i = (usart0_tx_buffer_size + usart0_tx_buffer_start) % usart_buffer_max_size;
usart0_tx_buffer[i] = c;
++usart0_tx_buffer_size;
}
else
{
usart0_tx_buffer_size = 0;
}
}
// Ausgang vom Ringpuffer
char USART0_QueueOut(void)
{
char c;
if (usart0_tx_buffer_size == 0)
return 0;
c = usart0_tx_buffer[usart0_tx_buffer_start];
--usart0_tx_buffer_size;
usart0_tx_buffer_start = (usart0_tx_buffer_start + 1) % usart_buffer_max_size;
return c;
}
// Eingang vom Ringpuffer
void USART1_QueueIn(char c)
{
int i;
if (usart1_tx_buffer_size < usart_buffer_max_size)
{
i = (usart1_tx_buffer_size + usart1_tx_buffer_start) % usart_buffer_max_size;
usart1_tx_buffer[i] = c;
++usart1_tx_buffer_size;
}
else
{
usart1_tx_buffer_size = 0;
}
}
// Ausgang vom Ringpuffer
char USART1_QueueOut(void)
{
char c;
if (usart1_tx_buffer_size == 0)
return 0;
c = usart1_tx_buffer[usart1_tx_buffer_start];
--usart1_tx_buffer_size;
usart1_tx_buffer_start = (usart1_tx_buffer_start + 1) % usart_buffer_max_size;
return c;
}
// Sendung einer Antwort über der Ringpuffer zum USART0
static void USART0_Send(const char *s)
{
int i;
for (i = 0; s[i] != 0; ++i)
USART0_QueueIn(s[i]);
if (usart0_tx_buffer_size > 0)
UCSR0B |= 1 << UDRIE0;
}
// Sendung eines Befehls über der Ringpuffer zum USART1
static void USART1_Send(const char *s)
{
int i;
for (i = 0; s[i] != 0; ++i)
USART1_QueueIn(s[i]);
if (usart1_tx_buffer_size > 0)
UCSR1B |= 1 << UDRIE1;
}
// Verarbeitung eines Befehls
static void ProcessCommand(void)
{
int i;
int x;
int y;
char x_moteur[12];
char y_moteur[12];
for (i = 0; i < usart_command_size; ++i)
if (usart_command[i] == ',')
break;
if (i <= 0 || i >= usart_command_size - 1)
{
// Man hat kein Kommazeichen in Mitte des Strings gefunden -> Fehler
USART0_Send("\x15"); // NAK, Sendung hat nicht geklappt.
usart_command_size = 0;
return;
}
// Ich wandle x und y in Integer um, um Berechnung durchzuführen, wenn es nötig ist
// Extraktion von X und Y
usart_command[i] = 0;
usart_command[usart_command_size] = 0;
x = atoi(usart_command);
y = atoi(usart_command + i + 1);
usart_command_size = 0;
itoa(x, x_moteur, 10);
itoa(y, y_moteur, 10);
// Sendung position x_moteur
USART1_Send("#1s");
USART1_Send(x_moteur);
USART1_Send("\r");
USART1_Send("#1A\r"); // Losfahren der Platine auf die x-Achse
// _delay_ms(30000); // Warten 15000ms --> 15s
// Sendung position y_moteur
USART1_Send("#2s");
USART1_Send(y_moteur);
USART1_Send("\r");
USART1_Send("#2A\r"); // Losfahren der Platine auf die y-Achse
_delay_ms(30000); // Warten 30000ms --> 30s
USART0_Send("\x06"); // ACK --> Freigage: PC kann nächste Data senden.
}
// Interruptsfunktion für das Empfangen von Byte
// Diese Funktion ist High, wenn RXCIE0 = 1
ISR(USART0_RX_vect)
{
char data;
data = UDR0;
// Verarbeitung des Befehls -->("D X,Y F")
// X = x_moteur und Y = y_moteur
if (data == 'F')
{
command_ready = 1;
}
else if (data == 'D')
{
// Erzeugung eines neuen Befehls
usart_command_size = 0;
}
else
{
// Als man weder F, noch D empfängt, speichert man
//die Koordinatenposition(X,Y) in einem Befehl
if (usart_command_size < usart_command_max_size)
{
usart_command[usart_command_size] = data;
++usart_command_size;
}
else
{
usart_command_size = 0;
}
}
}
// Interruptsfunktion für Byte-Übertragung
// Diese Funktion ist an, wenn UDRIE1 = 1
ISR(USART0_UDRE_vect)
{
UDR0 = USART0_QueueOut();
// Sendung stoppen, wenn es keine Daten mehr gibt zu senden
if (usart0_tx_buffer_size == 0)
UCSR0B &= ~(1 << UDRIE0);
}
// Interruptsfunktion für Byte-Übertragung
// Diese Funktion ist an, wenn UDRIE1 = 1
ISR(USART1_UDRE_vect)
{
UDR1 = USART1_QueueOut();
// Sendung stoppen, wenn es keine Daten mehr gibt zu senden
if (usart1_tx_buffer_size == 0)
UCSR1B &= ~(1 << UDRIE1);
}
void VorEinstellungMotor(void)
{
//USART1_Send("#1|0\r"); // Deaktivierung von Antwort der Motorsteuerung Nr 1
//USART1_Send("#2|0\r"); // Deaktivierung von Antwort der Motorsteuerung Nr 2
//USART1_Send("#1:baud=7"); // Baudrate = 9600
//USART1_Send("#2:baud=7"); // Baudrate = 9600
USART1_Send("#1D0\r"); // Festlegen der Position der Platine als Ursprung auf die x-Achse
USART1_Send("#2D0\r"); // Festlegen der Position der Platine als Ursprung auf die y-Achse
USART1_Send("#1p2\r"); // Modus: Absolut Positionierung --> p=2
USART1_Send("#2p2\r"); // Modus: Absolut Positionierung --> p=2
}
int main (void)
{
USART_Init(UBRR_VAL); // Initialisierung von USART0 and USART1
VorEinstellungMotor(); // Initialisierung Motorsteuerung
sei(); // Aktierung Interruptsfunktion
while (1) // Endlosschleife
{
if(command_ready)
{
ProcessCommand();
command_ready = 0;
}
}
}
Weißt zufällig jemand wie ich der Hardware Handshake RTS/CTS in meinemm Mikrocontroller-Code einsetzen, um die Datenfluss zwischen dem Mikrocontroller und dem PC zu kontrollieren .
Danke im Voraus !!