I've been trying to program an atmega328p in Atmel Studio (it's fun), but when I tried to separate my keypad code into it's own c file, I got an error.
Error 'keypad' undeclared (first use in this function) invisible_alarm C:\Users\kenkr\Documents\Atmel Studio\7.0\invisible_alarm\invisible_alarm\main.c 39
keypad is defined in a separate file, and I've ruled a few things out:
I suspect the build order is messed up, but I'm not sure where build order is set in Atmel Studio, but that's just a guess. I put a sample of my files below. The only errors are from main.c referencing keypad.c.
// main.c
#define F_CPU 16000000
#define NULL 0
#include <util/delay.h>
#include <avr/io.h>
#include "lcd.h"
#include "keypad.h"
void example() {
int header_example = KEY_1; // From keypad.h, no error
uint16_t c_example = keypad; // from keypad.c, error
}
// ...
// keypad.h
#ifndef _KEYPAD_H_
#define _KEYPAD_H_
#define KEY_1 0
#define KEY_4 1
// ...
#endif
// keypad.c
#include <avr/io.h>
#include <util/delay.h>
#include "keypad.h"
#define NULL 0
uint16_t keypad = 0x0000;
// ...
You need to declare the variable in the header file:
// keypad.h
#ifndef _KEYPAD_H_
#define _KEYPAD_H_
#include <stdint.h> // commonly needed for uint16_t
#define KEY_1 0
#define KEY_4 1
// ...
extern uint16_t keypad;
#endif