2017-11-18 74 views
1

我有以下测试设置。当我编译代码时,出现以下错误:initUSART无法识别,但我已将文件包含在适当的位置。我错过了什么吗?在C中未识别AVR USART文件

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对象文件:'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文件包含的外部引用initUSART(部分归功于#include "USART.h"指令,它提供正确的原型)

但这条连线:

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

只参考main.o。您需要添加USART.o所以符号被链接器解析(链接不关心.h包括文件),像这样:

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

哇....我只是浪费了我一生中30分钟的时间没有看到这个:)谢谢让弗朗索瓦!很有帮助。 – sesc360

+1

是的,SO在周末结束时速度较慢,但​​你可以很幸运。很高兴有帮助。 –