UART Problem

Gast #3384852
Lesenswert?

Servus,
ich führe folgenden Code auf einem ATMEGA8 (16Mhz) aus.
1
#ifndef F_CPU
2
#define F_CPU 16000000UL
3
#endif
4
#define UART_BAUD_RATE      9600 
5

6
#include <avr/interrupt.h>
7
#include <util/delay.h>
8
#include "uart.h"
9

10
int main(void)
11
{
12
  DDRB |= (1 << DDB2);
13
  //uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU));
14
  
15
  sei();
16
  
17
  while(1)
18
  {  
19
    PORTB ^= (1<<PB2);
20
    _delay_ms(1000);
21
  }
22
}

Die LED (an PB2) blinkt mit einer Frequenz von 1Hz, soweit alles gut.
Nun mein Problem, wenn ich die Funktion "uart_init" einkommentiere 
blinkt die LED nicht mehr, woran kann das liegen?

Grüße
Gast #3384873
Lesenswert?

Uwe S. schrieb:
> ne die LED blinkt mit 0,5Hz.

Stimmt, danke.

Noch etwas zusatzinfo:

Makefile:
1
TARGET = main.elf
2
HEX = main.hex
3
CC = avr-gcc
4
OBJ = main.o uart.o
5
CFLAGS = -mmcu=atmega8 -c -Wall -O
6
 
7
all: $(TARGET)
8
 
9
$(TARGET): $(OBJ)
10
  $(CC) -o $(TARGET) $(OBJ)
11
  avr-objcopy -j .text -j .data -O ihex $(TARGET) $(HEX)
12

13
%.o: %.c
14
  $(CC) $(CFLAGS) $<
15

16
prog: 
17
  avrdude -p m8 -e -c avr911 -U flash:w:main.hex:i -P /dev/ttyUSB0
18

19
clean:
20
  rm -f $(TARGET)
21
  rm -f $(HEX)
22
  rm -f $(OBJ)

Der avr-gcc läuft meines erachtens ohne Fehler durch:
  % make                                                           !109
avr-gcc -mmcu=atmega8 -c -Wall -O main.c
avr-gcc -o main.elf main.o uart.o
avr-objcopy -j .text -j .data -O ihex main.elf main.hex

Die ISR befindet sich in der uart.c
1
ISR (UART0_RECEIVE_INTERRUPT)  
2
/*************************************************************************
3
Function: UART Receive Complete interrupt
4
Purpose:  called when the UART has received a character
5
**************************************************************************/
6
{
7
    unsigned char tmphead;
8
    unsigned char data;
9
    unsigned char usr;
10
    unsigned char lastRxError;
11
 
12
 
13
    /* read UART status register and UART data register */ 
14
    usr  = UART0_STATUS;
15
    data = UART0_DATA;
16
    
17
    /* */
18
#if defined( AT90_UART )
19
    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
20
#elif defined( ATMEGA_USART )
21
    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
22
#elif defined( ATMEGA_USART0 )
23
    lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) );
24
#elif defined ( ATMEGA_UART )
25
    lastRxError = (usr & (_BV(FE)|_BV(DOR)) );
26
#endif
27
        
28
    /* calculate buffer index */ 
29
    tmphead = ( UART_RxHead + 1) & UART_RX_BUFFER_MASK;
30
    
31
    if ( tmphead == UART_RxTail ) {
32
        /* error: receive buffer overflow */
33
        lastRxError = UART_BUFFER_OVERFLOW >> 8;
34
    }else{
35
        /* store new index */
36
        UART_RxHead = tmphead;
37
        /* store received data in buffer */
38
        UART_RxBuf[tmphead] = data;
39
    }
40
    UART_LastRxError |= lastRxError;   
41
}
42

43

44
ISR (UART0_TRANSMIT_INTERRUPT)
45
/*************************************************************************
46
Function: UART Data Register Empty interrupt
47
Purpose:  called when the UART is ready to transmit the next byte
48
**************************************************************************/
49
{
50
    unsigned char tmptail;
51

52
    
53
    if ( UART_TxHead != UART_TxTail) {
54
        /* calculate and store new buffer index */
55
        tmptail = (UART_TxTail + 1) & UART_TX_BUFFER_MASK;
56
        UART_TxTail = tmptail;
57
        /* get one byte from buffer and write it to UART */
58
        UART0_DATA = UART_TxBuf[tmptail];  /* start transmission */
59
    }else{
60
        /* tx buffer empty, disable UDRE interrupt */
61
        UART0_CONTROL &= ~_BV(UART0_UDRIE);
62
    }
63
}
Gast #3384899
Lesenswert?

@ Stefan Ernst:
Danke! das war das Problem

Ich hab die Makefile wie folgt angepasst:
1
TARGET = main.elf
2
HEX = main.hex
3
CC = avr-gcc
4
OBJ = main.o uart.o
5
MMCU = -mmcu=atmega8
6
CFLAGS = -c -Wall -O
7
 
8
all: $(TARGET)
9
 
10
$(TARGET): $(OBJ)
11
  $(CC) $(MMCU) -o $(TARGET) $(OBJ)
12
  avr-objcopy -j .text -j .data -O ihex $(TARGET) $(HEX)
13

14
%.o: %.c
15
  $(CC) $(MMCU) $(CFLAGS) $<
16

17
prog: 
18
  avrdude -p m8 -e -c avr911 -U flash:w:main.hex:i -P /dev/ttyUSB0
19

20
clean:
21
  rm -f $(TARGET)
22
  rm -f $(HEX)
23
  rm -f $(OBJ)

Jetzt läufts! :D

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren