2017-11-18 11 views
1

以下のテスト設定があります。コードをコンパイルすると、次のエラーが表示されます。initUSARTは認識されませんが、適切な場所にファイルが含まれています。私は何かを逃しましたか?AVR USARTファイルがC言語で認識されない

AVR Development Stack 
Get input main.c file... 
Compile code and return elf file... 
main.o: In function `main': 
/Users/sebastianscharf/Documents/Hardware/AVR/main.c:14: undefined reference to `initUSART' 
collect2: error: ld returned 1 exit status 
avr-objcopy: 'main.elf': No such file 

avrdude: AVR device initialized and ready to accept instructions 

Reading | ################################################## | 100% 0.00s 

avrdude: Device signature = 0x1e950f (probably m328p) 
avrdude: Expected signature for ATmega328 is 1E 95 14 
     Double check chip, or use -F to override this check. 

avrdude done. Thank you. 

メインファイル

#include "config.h" 
#include <avr/io.h> 
#include <util/delay.h> 
#include "USART.h" 

int main(void) { 

    char serialCharacter; 

    // --- INITS --- // 
    DDRB = 0xff; 

    // Set up LED for output 
    initUSART(); 
    return 0; 
} 

USART.h

/* These are defined for convenience */ 
#define USART_HAS_DATA bit_is_set(UCSR0A, RXC0) 
#define USART_READY  bit_is_set(UCSR0A, UDRE0) 

uint8_t receiveByte(void); 

/* Takes the defined BAUD and F_CPU, calculates the bit-clock multiplier, and configures the hardware USART*/ 
void initUSART(void); 

USART.c

#include "config.h" 
#include <avr/io.h> 
#include <util/setbaud.h> 
#include "USART.h" 

void initUSART(void) { 
    // Requires BAUD 
    UBRR0H = UBRRH_VALUE; 
    UBRR0L = UBRRL_VALUE; 

    #if USE_2X 
     UCSR0A |= (1 << U2X0); 
    #else 
     UCSR0A &= ~(1 << U2X0); 
    #endif 

    // Enable USART 
    UCSR0B = (1 << TXEN0) | (1 << RXEN0); 
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, 1 stop bit 
} 

bashのファイル

#!/bin/bash 

source bashColors.sh 

echo -e "${IGreen}AVR Development Stack${Reset}" 

echo -e "${BIBlue}Get input main.c file...${Reset}" 
avr-gcc -g -Os -mmcu=atmega328p -c main.c USART.c && 

echo -e "${BIBlue}Compile code and return elf file...${Reset}" 
avr-gcc -g -mmcu=atmega328p -o main.elf main.o && 

echo -e "${BIBlue}Convert elf file to hex...${Reset}" 
# avr-objcopy converts into hex file. -j indicates that we want the information from the .text and .data segment extracted. 
avr-objcopy -j .text -j .data -O ihex main.elf out.hex && 

echo -e "${BIBlue}Uploading data to microcontroller...${Reset}" 
avrdude -c usbtiny -p m328 
+0

あなたはUSARTのオブジェクトファイルを追加する必要があります:あなたはこのように、(リンカ.hインクルードファイルをを気にしない)ので、シンボルは、リンカによって解決されるUSART.oを追加する必要があります 'AVR-GCCを - g -mmcu = atmega328p -o main.elf main.o USART.o && ' –

+0

Bashファイルの先頭に' set -e'を追加すると、処理が進まずにエラーがあった場合にスクリプトが実際に停止します。 –

答えて

3

あなたはと正しく.oに両方のソースファイルをコンパイル:

avr-gcc -g -Os -mmcu=atmega328p -c main.c USART.c 

main.oファイルが提供#include "USART.h"指令へinitUSART(一部のおかげへの外部参照が含まれています適切なプロトタイプ)

しかし、このリンク行:

avr-gcc -g -mmcu=atmega328p -o main.elf main.o && 

参照main.o

avr-gcc -g -mmcu=atmega328p -o main.elf main.o USART.o && 
+0

うわー....私はちょうどこれを見ていない私の人生の時間の30分を無駄に:)ありがとうジャンフランソワ!非常に役立ちます。 – sesc360

+1

ええ、SOは週末には遅いですが、あなたは幸運になることができます。喜んで助けになる。 –

関連する問題