0

I created the following header file with definitions for AVR pins to be used in my code.

#define LED_PORT        PORTB
#define LED_PIN         PINB
#define LED_DDR         DDRB

#define LED0            PB0

I encounter two failures I am not able to solve.

1) I have two issues in this header file, shown here:

enter image description here

2) Also the functions I created in the header file for USART in the USART.h file, I implemented, are not being recognized

enter image description here

I actually do not understand why that is. As the code clearly has the header file implemented.

#ifndef F_CPU
#define F_CPU 1000000UL
#endif

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


int main(void) {
  char serialCharacter;
  LED_DDR = 0xff;
  initUSART();
  printString("Hello World!\r\n");

  while (1) {
    serialCharacter = receiveByte();
    transmitByte(serialCharacter);
    LED_PORT = serialCharacter;
  }

  return (0);
}

The compiler I am using is AVR-GCC.

Furthermore, when I include the USART.c directly, then everything is working fine. I don't understand why the header file is not working though.

I created a script that runs the compiler/linker:

#!/bin/bash
avr-gcc -g -Os -mmcu=atmega328p -c code.c util.c USART.c
avr-gcc -g -mmcu=atmega328p -o code.elf code.o
avr-objcopy -j .text -j .data -O ihex code.elf code.hex
avr-size --format=avr --mcu=atmega328p code.elf

This returns the above mentioned error.

The USART.h looks like this:

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

#include <stdint.h>

void initUSART(void);
void transmitByte(uint8_t data);
uint8_t receiveByte(void);
void printString(const char myString[]);
void readString(char myString[], uint8_t maxLength);
void printByte(uint8_t byte);
void printWord(uint16_t word);
void printBinaryByte(uint8_t byte);
char nibbleToHex(uint8_t nibble);
char nibbleToHexCharacter(uint8_t nibble);
void printHexByte(uint8_t byte);
uint8_t getNumber(void);

I really require some help as I am trying to solve this now for three days and read a lot of different sources (I am getting my ropes together still with C so please be mindful).C

sesc360
  • 3,155
  • 10
  • 44
  • 86

1 Answers1

0

I created a script that runs the compiler/linker:
avr-gcc -g -Os -mmcu=atmega328p -c code.c util.c USART.c

This command generates three files: code.o, util.o and USART.o, whereas the following link command only links one object file:

avr-gcc -g -mmcu=atmega328p -o code.elf code.o

Hence, all functions from the latter two compilation units are missing, which leads tu bunch of "undefined reference to" from the linker. Fix is to link all three object files:

avr-gcc -g -mmcu=atmega328p -o code.elf code.o util.o USART.o


Undeclared Identifiers in header file

A header file (module.h) in C just contains declarations of functions in the module (module.c), but the implementation / definition of the functions is in the *.c file. (One exception are static inline functions which are defined in the header, but this is not the case for the undefined references from your code.)

emacs drives me nuts
  • 2,785
  • 13
  • 23