Hallo zusammen,
ich nutze ein Alvidi - Evalboard mit einem AT90CAN128.
Leider bereitet mir der UART einige Probleme.
Zum Test habe ich mal die Software vom OLIMEX - Board aufgespielt.
1 | /*
|
2 | * UART.c
|
3 |
|
4 | /* Sample program for Olimex AVR-CAN with AT90CAN128 processor
|
5 | * Echoes back the received characters on the USART0.
|
6 | * Compile with AVRStudio+WinAVR (gcc version 3.4.6)
|
7 | */
|
8 |
|
9 | #define __AVR_AT90CAN128__ 1
|
10 | #define OSCSPEED 16000000 /* in Hz */
|
11 |
|
12 | #include "avr/io.h"
|
13 |
|
14 | void PORT_Init()
|
15 | {
|
16 | PORTA = 0b00000000;
|
17 | DDRA = 0b00000000;
|
18 |
|
19 | PORTB = 0b00000000;
|
20 | DDRB = 0b00000000;
|
21 |
|
22 | PORTC = 0b00000000;
|
23 | DDRC = 0b00000000;
|
24 |
|
25 | PORTD = 0b00000000;
|
26 | DDRD = 0b00000000;
|
27 |
|
28 | PORTE = 0b00010000; //Turn Led off (Bit4 = 1)
|
29 | DDRE = 0b00010010; //Led set as output (Bit4 = 1), TX set as output (Bit1 = 1)
|
30 |
|
31 | PORTF = 0b00000000;
|
32 | DDRF = 0b00000000;
|
33 | }
|
34 |
|
35 | void WDT_Off(void)
|
36 | {
|
37 | asm("push r30"); // using GCC without optimizations results in
|
38 | asm("ldi r30, 0x18"); // a code not meeting the requirements
|
39 | asm("sts 0x60, r30"); // of the datasheet.
|
40 | asm("ldi r30, 0x7");
|
41 | asm("sts 0x60, r30");
|
42 | asm("pop r30");
|
43 | WDTCR = (1<<WDCE) | (1<<WDE);
|
44 | WDTCR = 0x07; //increase Watchdog time-out period (Bit0,1,2 = 1)
|
45 | }
|
46 |
|
47 | void UART_Init(uint32_t Baud)
|
48 | {
|
49 | unsigned int BaudRate = OSCSPEED / (16 * Baud) - 1; //calculate BaudRate
|
50 | //set BaudRate into registers
|
51 | UBRR0H = (unsigned char) (BaudRate>>8);
|
52 | UBRR0L = (unsigned char) BaudRate;
|
53 |
|
54 | UCSR0B = 0b00011000; //enable TX(Bit4 = 1), RX(Bit3 = 1)
|
55 |
|
56 | UCSR0C = 0b00000110; //set frame format (8 bits, 1 stop bit)
|
57 | }
|
58 |
|
59 | unsigned char UART_Receive()
|
60 | {
|
61 | if (UCSR0A & 0b10000000)
|
62 | return UDR0;
|
63 | else
|
64 | return 0;
|
65 | }
|
66 |
|
67 | void UART_Transmit(unsigned char Data)
|
68 | {
|
69 | while (!(UCSR0A & 0b00100000));
|
70 | UDR0 = Data;
|
71 | }
|
72 |
|
73 | int main()
|
74 | {
|
75 | unsigned char Ch;
|
76 | WDT_Off();
|
77 | PORT_Init();
|
78 | UART_Init(9600);
|
79 | while (1)
|
80 | {
|
81 | asm("WDR"); //avoiding reset of the program
|
82 | Ch = UART_Receive(); //receiving char
|
83 |
|
84 | if (Ch)
|
85 | {
|
86 | UART_Transmit(Ch); //returning char
|
87 | }
|
88 | }
|
89 |
|
90 | }
|
Verdrahtet hab ich soweit alles richtig, da mir das Programm wie
angegeben ein Echo vom gesendeten Befehl zurückliefert.
Jedoch stimmen die Rücksendezeichen nicht mit den gesendeten überein.
Siehe Bild im Anhang
Ich wäre sehr dankbar wenn mir jemand helfen könnte!
Vielen Dank!
Gruss