Forum: Mikrocontroller und Digitale Elektronik ATmega128 RS232 Komunikation


von spassvogel (Gast)


Lesenswert?

Hallo zusammen,

ich habe folgendes Board:
http://www.chip45.com/products/savvy128-1.3_avr_atmega_system_platine_atmega128_usb_rs232_rs485_sd-karte.php

Ich möchte über die RS232-Schnittstelle Daten austauschen. Ich dazu habe 
ich den µC(D-Sub9 male) mit dem COM1 -Anschluss des PCs(D-Sub9 male) 
verbunden. Hierzu nutze ich fertiges Kabel.

Zu erst möchte ich lediglich sicher stellen das ich Zeichenaustauschen 
kann und die Komunikation funktioniert.

PCseitig nutze ich Realterm für die Komunikation. Mit folgendem Code 
hätte ich erwartet, das mit zu erst '...' dann 'Running' dann 
'BaudRateRegister set to 103' abschließend 'wait' auf dem Terminal 
anzeigt. Wenn ich dann ein Zeichen sende sollte mir der µC mit 'Got 
intruction' antworten.

Nach dem µC starten '...RunningBaudRateRegister set to 103 Wait' zweimal

Beim Senden bekomme ich die Fehlermeldung "ie_NOpen - device not open.". 
hat jemand eine Idee woran dies liegen kann? Falsches Kabel?

Lg



1
#include <stdio.h> 
2
#include <stdlib.h>
3
#include <avr\io.h> 
4
#include <avr/interrupt.h> //for the interupts for receiving via UART
5
#include <string.h> //for building strings of characters
6
#include <avr/pgmspace.h>  //for PSTR for handling the location of characters
7
8
//Set the baud rate of the communication
9
10
#define BAUD 9600UL      // Baudrate
11
 
12
// Berechnungen
13
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1)   // clever runden
14
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1)))     // Reale Baudrate
15
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD) // Fehler in Promille, 1000 = kein Fehler.
16
 
17
#if ((BAUD_ERROR<990) || (BAUD_ERROR>1010))
18
  #error Systematischer Fehler der Baudrate grösser 1% und damit zu hoch! 
19
#endif
20
21
22
//Four variables for buffering of received messages
23
  volatile char c; //The temp place to dump the character we just received
24
  volatile char buffer[30]; // The array holding the characters that have been sent
25
  volatile int bufferlocation; //The current location in the buffer
26
  volatile int bufferflag; //A flag to tell whether there is complete data in the buffer to be processed
27
28
//Declare the location of the stream for the output
29
  void uart_printchar(char c, FILE *stream){
30
    // Wait until UDR ready
31
      while(!(UCSR0A & (1 << UDRE0)));
32
        UDR0 = c;    // send character
33
  }
34
35
//USART RX interrupt this code is executed when we recieve a character
36
  ISR(USART0_RX_vect){        
37
    c = UDR0; //Read the value out of the UART buffer
38
    buffer[bufferlocation] = c; //Dump that value out into the buffer array
39
    bufferlocation++;  //Increment the bufferlocation so its ready to write next time
40
41
    if (c == 13) //Number 13 is carrage return
42
      bufferflag = 1; //If its a carriage return then mark the string as ready to process
43
  }
44
45
int BaudRateRegister;
46
47
int main(void){ 
48
//---------------Start of Initalisation---------------
49
50
//Configure the UART registers
51
  UCSR0B = ((1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0));
52
    //RXCIE0 Enable interupt after recieved
53
    //RXEN0 Reciever enabled
54
    //TXEN0 Transmit enabled
55
56
  UCSR0C = ((1<<UCSZ00)|(1<<UCSZ01));
57
    //UCSZ00 and UCSZ01 use 8 bit character size
58
59
//Set the baud rate 
60
  BaudRateRegister=F_CPU/16/BAUD-1;
61
  UBRR0H=((BaudRateRegister>>8)&0x00FF);
62
  UBRR0L=(BaudRateRegister&0x00FF); 
63
64
//Define and initialise stream used for printing to uart
65
  FILE uart_str = FDEV_SETUP_STREAM(uart_printchar, NULL, _FDEV_SETUP_RW);
66
67
//Redirect stdout, so that printf() knows where to go
68
  stdout = &uart_str;
69
70
//Enable interrupts  
71
  sei();  
72
//---------------End of Initalisation---------------
73
  printf_P(PSTR("..."));
74
  printf_P(PSTR("Running"));
75
  printf_P(PSTR("BaudRateRegister set to %u\n"),BaudRateRegister);
76
  printf_P(PSTR("Wait"));
77
//Start of infinate while loop
78
   while(1){
79
    if (bufferflag == 1){ 
80
      printf_P(PSTR("Got instruction"));
81
      bufferflag = 0; //Reset the buffer flag to say its been processed
82
      bufferlocation =0; //Restart the buffer location
83
    }
84
   }
85
//End of infinate while loop
86
   
87
}
88
//End of main routine

: Bearbeitet durch User
von Peter II (Gast)


Lesenswert?

spassvogel schrieb:
> Beim Senden bekomme ich die Fehlermeldung "ie_NOpen - device not open.".

das muss auch ohne Kabel gehen. Hast du die richtige Schnittstelle 
gewählt?

von spassvogel (Gast)


Lesenswert?

ja den gleichen comPort wie zum Empfangen

von Peter II (Gast)


Lesenswert?

spassvogel schrieb:
> ja den gleichen comPort wie zum Empfangen

hä? kannst du etwas zwei einstellen? Normalerweise nutzt man ein Port 
zum senden und empfangen.

Nimmer einfach mal ein andere Programm z.b. putty

von spassvogel (Gast)


Lesenswert?

Peter II schrieb:
> Nimmer einfach mal ein andere Programm z.b. putty

mit putty kommt es auch zweimal an. aber Empfangen geht.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.