我想创建自己的驱动程序,其中包含一个.c文件和一个.h文件。 .c文件是:
#include <msp430f5438a.h>
#include "ports_init.h"
extern void set_all_ports(unsigned char direction, unsigned char state)
{
P1DIR = direction;
P2DIR = direction;
P3DIR = direction;
P4DIR = direction;
P5DIR = direction;
P6DIR = direction;
P7DIR = direction;
P8DIR = direction;
P9DIR = direction;
P10DIR = direction;
P11DIR = direction;
PJDIR = direction;
P1OUT = state;
P2OUT = state;
P3OUT = state;
P4OUT = state;
P5OUT = state;
P6OUT = state;
P7OUT = state;
P8OUT = state;
P9OUT = state;
P10OUT = state;
P11OUT = state;
PJOUT = state;
}
.h文件是:
#ifndef _PORTS_INIT_
#define _PORTS_INIT_
//Function prototypes
void set_all_ports(unsigned char direction, unsigned char state);
#define HIGH_STATE 0xFFu
#define LOW_STATE 0x00u
#define OUTPUTS 0xFFu
#define INPUTS 0x00u
#endif
我的代码是:
#include <msp430f5438a.h>
#include "ports_init.h"
int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P4DIR |= 0x03; // Set P2.0 to output direction
P4OUT = 0x00;
P1DIR &= ~BIT7;
P1REN = BIT7;
P1OUT = BIT7;
set_all_ports(OUTPUTS, LOW_STATE);
while (1)
;
}
不幸的是,当我编译我的源代码时,我收到错误: 错误[e46]:未定义的外部&#34; set_all_ports&#34;在GPIO主要提到。我的问题是如何修改我的.c和.h文件,以使此错误消失。我知道将.c文件包含在我的源代码中并不是一个好习惯。这就是为什么我想通过在源代码中仅包含ports_init.h来调用驻留在ports_init.c中的函数,就像在真实的API中一样。我正在为MSP430使用IAR嵌入式工作台。请原谅我糟糕的英语,我希望你明白这一点。
答案 0 :(得分:0)
我要说的问题是你的.c文件中你将该函数声明为extern void,我认为它应该是无效的