Oki. Also ich glaube wir verstehen auch was verschiedenes unter "Modul" ich meinte eins wie es im Buch beschrieben ist, und das muss man ja auch gar nicht compillieren... .
Hier ist der Code von serial.h:
Code:
#ifndef _SERIAL_H
#define _SERIAL_H
#define cReadBufferSize 32 // Size of ReadBuffer
#define cWriteBufferSize 128 // Size of WriteBuffer
#define cBlockung 1
#define cNonBlocking 0
#define cTXComplete 1
#define cTXNotComplete 0
#include "Serial\serial.c"
void RS232_init(long unsigned int baudrate);
int RS232_send(char *c, int length, int blocking);
int RS232_receive(char *c, int length, int blocking);
int RS232_WritingComplete(void);
int RS232_GetBytesInReadBuffer(void);
int RS232_GetBytesInWriteBuffer(void);
int RS232_GetWriteBufferSize(void);
int RS232_GetReadBufferSize(void);
int RS232_ReadBufferClear(void);
int RS232_WriteBufferClear(void);
#endif
Das ist der Code von "Serial\serial.c":
Code:
/////////////////////////////////////////////////////////////////////
// Arios: Serial Interface functions //
/////////////////////////////////////////////////////////////////////
// Author: Johannes Neumann //
// Date of completion: **.**.**** //
/////////////////////////////////////////////////////////////////////
// Defines //////////////////////////////////////////////////////////
#include <avr/interrupt.h>
#include <avr/io.h>
// Variables ////////////////////////////////////////////////////////
volatile unsigned int TXComplete;
volatile unsigned int WritePointer0 = 0, WritePointer1 = 0;
volatile unsigned int ReadPointer0 = 0, ReadPointer1 = 0;
volatile unsigned char ReadBuffer[cReadBufferSize];
volatile unsigned char WriteBuffer[cWriteBufferSize];
// Init /////////////////////////////////////////////////////////////
void RS232_init(long unsigned int baudrate) {
UCSRA = 0x00;
UCSRB = (1 << RXCIE) | (1 << RXEN);
UCSRC = (1 << UCSZ1) | (1 << UCSZ0) | (1 << URSEL);
UBRRH = (((F_CPU/baudrate) >> 4) - 1) >> 8;
UBRRL = (((F_CPU/baudrate) >> 4) - 1) & 0xFF;
TXComplete = cTXComplete;
}
// Send /////////////////////////////////////////////////////////////
int RS232_send(char *c, int length, int blocking) {
int i=0;
for (i=0; i < length; i++) {
if ((cWriteBufferSize + WritePointer1 - WritePointer0) % cWriteBufferSize >
(cWriteBufferSize - 2)) {
if (blocking) {
while((cWriteBufferSize + WritePointer1 - WritePointer0) % cWriteBufferSize >
(cWriteBufferSize - 2));
}
else return i;
}
TXComplete = cTXNotComplete;
asm("cli");
WriteBuffer[WritePointer1++] = c[i];
if (WritePointer1 > cWriteBufferSize - 1) WritePointer1 = 0;
asm("sei");
UCSRB = (1<<TXEN) | (1<<UDRIE);
}
return i;
}
// Receive //////////////////////////////////////////////////////////
int RS232_receive(char *c, int length, int blocking) {
int i;
TXComplete = cTXNotComplete;
for (i=0; i<length; i++) {
if (ReadPointer0 == ReadPointer1) {
if (blocking) while(ReadPointer0 == ReadPointer1);
else return i;
}
c[i] = ReadBuffer[ReadPointer1++];
if (ReadPointer1 > cReadBufferSize - 1) ReadPointer1 = 0;
}
return length;
}
// Complete? ////////////////////////////////////////////////////////
int RS232_WritingComplete(void) {
return TXComplete;
}
// Count of Bytes in ReadBuffer /////////////////////////////////////
int RS232_GetBytesInReadBuffer(void) {
return (cReadBufferSize + ReadPointer1-ReadPointer0) % cReadBufferSize;
}
// Count of Bytes in WriteBuffer ////////////////////////////////////
int RS232_GetBytesInWriteBuffer(void) {
return (cWriteBufferSize + WritePointer1-WritePointer0) % cWriteBufferSize;
}
// Return WriteBufferSize ///////////////////////////////////////////
int RS232_GetWriteBufferSize(void) {
return cWriteBufferSize;
}
// Return ReadBufferSize ////////////////////////////////////////////
int RS232_GetReadBufferSize(void) {
return cReadBufferSize;
}
// Return if ReadBuffer ist Empty ///////////////////////////////////
int RS232_ReadBufferClear(void) {
// kommt noch...
}
// Return if WriteBuffer ist Empty //////////////////////////////////
int RS232_WriteBufferClear(void) {
// kommt noch...
}
// Interrupt for Reading ////////////////////////////////////////////
SIGNAL (SIG_USART_RECV) {
ReadBuffer[ReadPointer0++] = UDR;
if (ReadPointer0 > cReadBufferSize) ReadPointer0 = 0;
}
// Interrupt for writing Data into UDR //////////////////////////////
SIGNAL (SIG_USART_DATA) {
UDR = WriteBuffer[WritePointer0++];
//printf(WriteBuffer[WritePointer0++]);
if (WritePointer0 < cWriteBufferSize - 1) WritePointer0 = 0;
if (WritePointer0 == WritePointer1) UCSRB = (1<<TXEN) | (1<<TXCIE);
}
// Interrupt for setting Complete-Flag //////////////////////////////
SIGNAL (SIG_USART_TRANS) {
UCSRB = (1<<RXCIE) | (1<<RXEN);
TXComplete = cTXComplete;
}
Und das ist mein Programm:
Code:
#include <Arios\serial.h>
#include <stdio.h>
int main (void) {
int i=0;
RS232_init(9600);
RS232_send("Hallo Welt!\n\r", 14, cNonBlocking);
return 0;
}
Obwohl ich nur einmal sende, wird unendlich mal ein "H" gesendet. (also der erste Buchstabe des Strings). Es muss also irgendwas mit den Interrupts zu tun haben.
Sorry, ich hatte oben vergessen, den Code beizufügen...
Vielen Dank,
Bääääär
Lesezeichen