-1

I am programming an AVR microcontroller, and in the programmers notepad in the WINAVR Suite.

I am trying to seperate my code, however the sepeaet .c file I am unable to use AVR pre-defined variables. (the variables AVR supplies to point to certain BITs)

for example, this code will work in my main.c file. but not in another random.c file:

 UBRR0H = (unsigned char)(ubrr>>8);

it gives the error :

random.c:6: error: 'UBRR0H' undeclared (first use in this function)

in my main.c file it only has the following includes:

#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>
#include <string.h>
#include <avr/interrupt.h>
#include "lcd.h"
#include "random.h"
Michael
  • 8,229
  • 20
  • 61
  • 113
  • 3
    Do you have the same includes in the `random.c` file? – Till Hoffmann Feb 12 '12 at 11:36
  • where have you declared UBRR0H? main.c and random.c? or nowhere? – fazo Feb 12 '12 at 11:39
  • That is the thing, I havent declared UBRR0H anywhere. It just come sas part of the MCU.. and Im not sure where its declared. – Michael Feb 12 '12 at 11:52
  • If you are using an AVR such as atmega328p it's in the file iom328p.h! The include declaration you have to use is `#include `. When you compile you have to declare the MCU you are using with the option `-mmcu=yourcpu`, for 328p MCU you have to use `-mmcu=atmega328p`. If you are using the Arduino suite you have to set your MCU using the menu of the IDE. – Sir Jo Black Dec 06 '17 at 15:55

3 Answers3

2

You have to include avr/io.h in your projet and also specify the mcu in the gcc compiler command line with -mmcu= option.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • 2
    While other answers are technically true, this is the right answer in the context of avr-gcc. The last thing you want to do is to define those constants yourself. – freespace Sep 09 '13 at 11:57
1

You have to create file like yours.h, where you put or your function definitions and macros definitions:

#define UBRR0H (unsigned char)(ubrr>>8);

int mine_function( char, char, int);

...

extern int global_variable;

not sure whether UBRR0H is macro or extranal variable

In addition use something about extern variables and some articles about how to use them.

And than in every your .c file you should:

#include "yours.h"

If you get in troubles because you'll end up with many .h files and you'll be including the same thing multiple times (will cause error, previously defined there), there easy hack, in yours.h:

#ifndef _H_YOURS_INCLUDED_
#define _H_YOURS_INCLUDED_ 1

// Your real content

#endif /* _H_YOURS_INCLUDED_ */
Vyktor
  • 20,559
  • 6
  • 64
  • 96
1

If you're using "library" definitions in any compilation unit (.c file), you'll need to include the right headers in that unit (file). I'm guessing you're missing #include or such in the random.c file. Having it just in main.c won't help the compiler while its compiling random.c. :)

(The linker is a different matter.)

One way to find out where a definition is is to simply grep the compiler and libc source (include) directories and look for the name. That won't necessarily tell you what you're meant to do to get it. I suspect that that one is a chip-specific register name that appears in the include file for your specific chip and gets loaded while going through io.h depending on the compiler switches.

If it goes missing while using a different chip, check the datasheet to make sure the register/peripheral exists in your particular chip and check the include files for the exact spelling. There may be differences.

XTL
  • 851
  • 1
  • 8
  • 23