Forum: Mikrocontroller und Digitale Elektronik STK500 Serial Port String senden


von Flat (Gast)


Lesenswert?

Hi,

ich habe folgendes Problem.
Ich will über die Serielle Schnittstelle einen String senden.
Habe dazu diesen Code geschrieben. Es funktioniert auch alles, mein 
Terminal Programm empfängt auch die Daten, das Problem ist, der String 
ist nicht lesbar, also es kommen irendwas an.
Die Baudrate und Frequenz ist richtig eingestellt. Ich hoffe ihr könnt 
mit behilflich sein. Ich sitze schon seit paar Stunden dran, aber hatte 
bis jetzt kein Erfolg.

Hier mein QuellCode:
1
/*******************Includes*************************************/
2
3
#include <avr/io.h>
4
#include <stdio.h>
5
#include <stdbool.h>
6
7
 
8
9
/******************Macros and Defines*********************/
10
11
#define BAUD 19200
12
#define MYUBRR F_CPU/16/BAUD-1
13
14
 
15
16
/**************Function Prototypes*********************************/
17
18
void usart_init(uint16_t ubrr);
19
char usart_getchar( void );
20
void usart_putchar( char data );
21
void usart_pstr (char *s);
22
unsigned char usart_kbhit(void);
23
24
 
25
26
/*******************Main***********************/
27
28
int main( void ) {
29
30
    // configure PORTA as output
31
32
    DDRA = 0xFF;
33
34
    // setup PORTB data direction as an input
35
36
    DDRB = 0;
37
38
    // make sure it is high impedance and will not source
39
40
    PORTB = 0;
41
42
    // fire up the usart
43
44
    usart_init ( MYUBRR );
45
46
    // dump some strings to the screen at power on
47
48
    usart_pstr("Ready to rock and roll!\n\r");
49
    usart_pstr("Type in a character, and I will transpose it up by 1:\n\r");
50
51
    // main loop
52
53
    while(true) {
54
55
        // if a key has been pressed, then process it
56
57
        if(usart_kbhit()) {
58
            usart_putchar(usart_getchar() + 1);
59
        }
60
61
62
        // map the PINB inputs to the PORTA outputs
63
64
        PORTA = PINB;
65
66
 
67
68
        // a little humor is always good, PB0 gets the user yelled at
69
70
        if(bit_is_clear(PINB,PB0)) {
71
72
            usart_pstr("OUCH! Stop poking me!\n\r");
73
        }
74
    }
75
}
76
/*****************usart Related****************/
77
78
void usart_init( uint16_t ubrr) {
79
80
    // Set baud rate
81
82
    UBRRH = (uint8_t)(ubrr>>8);
83
    UBRRL = (uint8_t)ubrr;
84
85
    // Enable receiver and transmitter
86
87
    UCSRB = (1<<RXEN)|(1<<TXEN);
88
89
    // Set frame format: 8data, 1stop bit
90
91
    UCSRC = (1<<URSEL)|(3<<UCSZ0);
92
}
93
94
void usart_putchar(char data) {
95
96
    // Wait for empty transmit buffer
97
98
    while ( !(UCSRA & (_BV(UDRE))) );
99
100
    // Start transmission
101
102
    UDR = data;
103
}
104
char usart_getchar(void) {
105
106
    // Wait for incoming data
107
108
    while ( !(UCSRA & (_BV(RXC))) );
109
110
    // Return the data
111
112
    return UDR;
113
}
114
115
unsigned char usart_kbhit(void) {
116
117
    //return nonzero if char waiting polled version
118
119
    unsigned char b;
120
    b=0;
121
    if(UCSRA & (1<<RXC)) b=1;
122
    return b;
123
}
124
125
void usart_pstr(char *s) {
126
127
    // loop through entire string
128
129
    while (*s) {
130
        usart_putchar(*s);
131
        s++;
132
    }
133
}

von Jim M. (turboj)


Lesenswert?

Wie ist dein F_CPU definiert? Fuses richtig gesetzt (siehe 
AVR Fuses)?

von Flat (Gast)


Lesenswert?

Die Fuses waren Falsch gesetzt. Statt ein Internen Oszillator war der 
Externe voreingestellt gewesen. Danke für dein Tipp.

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.