PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : RS232 funktioniert nicht



Johannes G.
20.08.2006, 08:42
Hallo,

ich versuche seit gestern RS232 zum laufen zu bringen, aber es klappt irgendwie nicht..
Hier erstmal der Code (fast so wie aus dem AVR Datenblatt):
uart.c

#define BAUD 38400


void usart_init()
{
/* Calculate baud rate */
uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(16*BAUD) - 1);

/* Set baud rate */
UBRRH = (uint8_t) (ubrr>>8);
UBRRL = (uint8_t) ubrr;

/* Enable Receiver and Transmitter */
UCSRB = (1<<RXEN) | (1<<TXEN);

/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL) | (1<<USBS) | (1<<UCSZ0);
}

void usart_transmit(uint8_t data)
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;

/* Put data into buffer, sends the data */
UDR = data;
}

void usart_print(char *s)
{
do
{
usart_transmit(*s);
}
while (*s++);
}

main.c

#define F_CPU 16000000
#include <avr/io.h>
#include <inttypes.h>

#include "usart.c"

int main(void)
{
usart_init();

while(1)
{
usart_print("Test");
}

return 0;
}

Achso, ich benutze einen AVR Mega8


Nur irgendwie empfange ich am Computer gar nix und weiß nicht woran es liegt..
Könnt ihr mir da helfen?


Viele Grüße,
Johannes

m.a.r.v.i.n
20.08.2006, 12:25
Hallo Johannes,

der Code sieht erst mal OK aus.
Ich tippe mal auf falsche Fusebits (CLKSEL = Externer Quarz) oder die RS232 Anschaltung. Dann poste mal den Schaltplan.

Gruß m.a.r.v.i.n

Johannes G.
20.08.2006, 12:33
Hallo,

wie stell ich unter Linux die Fusebits ein? Oder wie unter Windows?

Im Moment kann ich es leider nicht weiter testen, weil mein Programmer (mySmartUSB) irgendwie kaputt ist.

Und ich habe keine RS232 schnittstelle, nur USB und benutze um Moment einen USB->RS232 Adapter, vielleicht liegts auch an dem.


Viele Grüße,
Johannes

m.a.r.v.i.n
20.08.2006, 13:43
Hallo Johannes,

ich habe auch den mySmartUSB Flasher. Kommt ab und zu vor, das man die Firmware neu flashen muß (Nach Anstecken leuchtet keine LED mehr). Aktuelle Firmware gibt es auf der myAvr Homepage. Genau den Anweisungen folgen, dann kann nichts schiefgehen.

Der mySmartUSB bringt ja auch eine USB UART Bridge mit. Mit dem myAVR QuickProg Tool (ebenfalls von der myAVR Homepage) kann man per Software in den UART Mode wechseln und die UART testen (Control Center starten)

Die Fuses kann man mit diesem Tool zwar auch auf externen Quarz stellen, aber das würde ich nicht tun (ist nämlich für das myAVR Board gedacht mit 3,68MHz Quarz). Die Fuses setzt man am besten mit Avrdude. Gehört zur WinAVR Toolsuite dazu, gibt es ebenso für Linux.
Mit dem AVR Fuses Calculator (http://palmavr.sf.net/fc/) kann man sich online die korrekten Fuses berechnen lassen.

Diese Einstellung (http://palmavr.sourceforge.net/cgi-bin/fc.cgi?P_PREV=ATmega8&P=ATmega8&V_LOW=FF&V_HIGH=99&M_LOW_0x3F=0x3F&M_LOW_0x40=&M_LOW_0x80=0x80&M_HIGH_0x01=&M_HIGH_0x06=0x00&M_HIGH_0x08=&M_HIGH_0x10=&M_HIGH_0x20=0x00&M_HIGH_0x40=&M_HIGH_0x80=&B_WTDON=P&B_SPIEN=P&B_BOOTSZ1=P&B_BOOTSZ0=P) sollte funktionieren. Ergibt für Low: 0xFF, High: 0xD9

Diese Werte gibt man dann in der Befehlszeile für Avrdude ein:

"avrdude" -p m8 -c avr910 -P com3 -U lfuse:w:FF -U hfuse:w:D9 -F

Oder man startet die avrdude-gui Bediengrafik und klickt sich das ganze zusammen, wie in dem Screenshot zu sehen.

Gruß m.a.r.v.i.n

Johannes G.
20.08.2006, 14:53
Hallo,

ich hab die Firmware neu geflasht, aber es geht trotzdem noch nicht...

Wenn ich ein Programm übertragen will leuchtet nur die rote LED und nur eine grüne..
Wenn ich ganz viel Glück habe geht es dann doch, aber meistens kommt das:

avrdude: verifying ...
avrdude: verification error, first mismatch at byte 0x0000
0x12 != 0x00
avrdude: verification error; content mismatch

avrdude: safemode: Fuses OK

avrdude done. Thank you.


Ich ruf bei denen morgen mal an



Viele Grüße,
Johannes

Johannes G.
21.08.2006, 17:09
Hallo,

so, der mySmartUSB geht wieder, nun habe ich aber folgendes Problem:
Wenn ich "avrdude -p m8 -c avr910 -P /dev/ttyUSB0 -U lfuse:w:FF -U hfuse:w:D9 -F" eingebe kommt das:



Found programmer: Id = "AVR ISP"; type = S
Software Version = 1.5; Hardware Version = 2.0
Programmer supports auto addr increment.

Programmer supports the following devices:
Device code: 0x55 = ATtiny12
Device code: 0x01 = ATtiny13
Device code: 0x56 = ATtiny15
Device code: 0x13 = AT90S1200
Device code: 0x28 = AT90S4414
Device code: 0x20 = AT90S2313
Device code: 0x4c = AT90S2343
Device code: 0x30 = AT90S4433
Device code: 0x6c = AT90S4434
Device code: 0x38 = AT90S8515
Device code: 0x68 = AT90S8535
Device code: 0x41 = ATMEGA103
Device code: 0x02 = ATMEGA64
Device code: 0x43 = ATMEGA128
Device code: 0x03 = AT90CAN128
Device code: 0x74 = ATMEGA324
Device code: 0x0e = ATMEGA644
Device code: 0x04 = ATMEGA162
Device code: 0x64 = ATMEGA163
Device code: 0x75 = ATMEGA169
Device code: 0x14 = ATMEGA329
Device code: 0x15 = ATMEGA3290
Device code: 0x16 = ATMEGA649
Device code: 0x17 = ATMEGA6490
Device code: 0x72 = ATMEGA32
Device code: 0x60 = ATMEGA161
Device code: 0x76 = ATMEGA8
Device code: 0x3a = ATMEGA8515
Device code: 0x05 = ATMEGA8535
Device code: 0x5e = ATTINY26
Device code: 0x06 = ATMEGA48
Device code: 0x07 = ATMEGA88
Device code: 0x08 = ATMEGA168
Device code: 0x21 = ATtiny2313
Device code: 0x09 = AT90PWM2
Device code: 0x0a = AT90PWM3
Device code: 0x22 = ATtiny25
Device code: 0x23 = ATtiny45
Device code: 0x24 = ATtiny85
Device code: 0x0b = ATMEGA640
Device code: 0x0c = ATMEGA1280
Device code: 0x0d = ATMEGA1281
Device code: 0x25 = ATtiny24
Device code: 0x26 = ATtiny44
Device code: 0x27 = ATtiny84

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.06s

avrdude: Device signature = 0x07931e
avrdude: reading input file "FF"
avrdude: error opening FF: No such file or directory
avrdude: input file FF auto detected as invalid format
avrdude: can't open input file FF: No such file or directory
avrdude: write to file 'FF' failed

avrdude: safemode: Fuses OK

avrdude done. Thank you.


Irgendwas passt da nicht..

Viele Grüße,
Johannes

m.a.r.v.i.n
21.08.2006, 18:48
Hallo Johannes,

sorry, ich hatte nur die Zeile abgetippt die avrdude-gui mir anzeigte.
So sollte es klappen:

"avrdude" -p m8 -c avr910 -P com3 -U lfuse:w:0xff -U hfuse:w:0xd9 -F

Gruß m.a.r.v.i.n

Johannes G.
21.08.2006, 19:08
Hallo,

nun kommt das:


avrdude: Device signature = 0x07931e
avrdude: reading input file "0xff"
avrdude: error opening 0xff: No such file or directory
avrdude: input file 0xff auto detected as invalid format
avrdude: can't open input file 0xff: No such file or directory
avrdude: write to file '0xff' failed


Viele Grüße,
Johannes

m.a.r.v.i.n
21.08.2006, 19:52
OK, letzter Versuch:

"avrdude" -p m8 -c avr910 -P com3 -U lfuse:w:0xff:m -U hfuse:w:0xd9:m -F

Gruß m.a.r.v.i.n

Johannes G.
22.08.2006, 10:08
Hallo,

ok, nun geht es fast ;)

hier nochmal der Code:
main.c

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
#include <inttypes.h>

#include "usart.c"

void wait_ms(int i)
{ int x = 0;
while(x <= i)
{
_delay_ms(1);
x++;
}
}

int main(void)
{
usart_init();

while(1)
{
usart_print("Test\n");
wait_ms(1000);
}

return 0;
}

usart.c

#define USART_BAUD_RATE 9600

#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16l)-1)


void usart_init()
{
/* Set baud rate */
UBRRH = (uint8_t) (USART_BAUD_SELECT>>8);
UBRRL = (uint8_t) USART_BAUD_SELECT;

/* Enable Receiver and Transmitter */
UCSRB = (1<<RXEN) | (1<<TXEN);

/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL) | (1<<USBS) | (1<<UCSZ0);
}

void usart_transmit(uint8_t data)
{
/* Wait for empty transmit buffer */
while ( !( UCSRA & (1<<UDRE)) )
;

/* Put data into buffer, sends the data */
UDR = data;
}

void usart_print(char *s)
{
do
{
usart_transmit(*s);
}
while (*s++);
}

Nun kommt im Terminal immer Zeichensalat (wenigstens etwas :D )


Hast du noch eine Idee an was es liegen könnte?



Viele Grüße,
Johannes[/code]

m.a.r.v.i.n
22.08.2006, 10:43
Hallo Johannes,

wird doch langsam.
Probiers mal mit deinem ersten Code Beispiel.
Ich weiß nicht ob der PreProzessor das hier

#define USART_BAUD_SELECT (F_CPU/(USART_BAUD_RATE*16l)-1)
korrekt ausrechnet.
Oder probier mal:

#define USART_BAUD_SELECT ((F_CPU/(USART_BAUD_RATE*16l))-1)
Der Preprozessor kennt nämlich keine Rechenregeln wie Punkt vor Strich o.ä.

Gruß m.a.r.v.i.n

SprinterSB
22.08.2006, 10:51
Das sieht nach 9600/6N2 aus. Standard-Format ist 8N1 (8 Datenbits, No parity, 1 Stop).

Versuchs mal mit
// Data mode 8N1, asynchron
UCSRC = (1 << URSEL) | (1 << UCSZ1) | (1 << UCSZ0);

Hast du die Bauderate/Format auch an der Gegenstelle (PC) angepasst?

Johannes G.
22.08.2006, 11:08
Hallo,

danke, nun geht alles!


Viele Grüße,
Johannes