2017-11-18 1 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 
} 

당신이 .o 제대로 두 소스 파일을 컴파일

#!/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

:'AVR-GCC를 - g -mmcu = atmega328p -o main.elf main.o USART.o &&' –

+0

Bash 파일 상단에'set -e'를 추가하여 진행하지 말고 오류가 생길 때 스크립트가 실제로 멈추도록해야합니다. –

답변

3

bash는 파일 :

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

이제 main.o 파일 initUSART에 외부 참조 (제공 #include "USART.h" 지침에 부분적으로 감사를 포함 적절한 프로토 타입)

하지만이 링크 라인 :

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

main.o 만 참조하십시오. 이 같은, 그래서 기호 (에 대한 .h 파일을 포함 상관하지 않는다 링커 ) 링커에 의해 해결 USART.o을 추가해야

당신이 USART 오브젝트 파일을 추가 할 필요가
avr-gcc -g -mmcu=atmega328p -o main.elf main.o USART.o && 
+0

와우 .... 방금이 시간을 보지 못한 채 30 분을 낭비했습니다. Jean-Francois에게 감사드립니다! 매우 도움이됩니다. – sesc360

+1

예 SO가 주말에 느리지 만 운이 좋을 수 있습니다. 도와 줘서 기뻐. –

관련 문제