使用带有gpio引脚的xc8编译器在C中初始化驱动程序 [英] initialise driver in C with xc8 compiler with gpio pins

查看:249
本文介绍了使用带有gpio引脚的xc8编译器在C中初始化驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个驱动程序"foo.h"和"foo.c"的实现.驱动程序与微控制器上的gpio引脚进行交互.

Lets say I have a driver "foo.h" and "foo.c" implementation. The driver interacts with the gpio pins on a microcontroller.

在Arduino中,每个GPIO引脚都有其自己的引脚号(只是整数).然后,您可以使用函数initFoo(1)或initFoo(2)初始化foo,以选择要使用的引脚.

In Arduino, each GPIO pin has it's own pin number (just an integer). Then you can initialise foo with the function initFoo(1) or initFoo(2), to choose the pin you want to use.

在Mbed在线c ++编译器中,每个GPIO都可以由DigitalIn或DigitalOut对象控制.

In Mbed online c++ compiler, each GPIO can be controlled with a DigitalIn or DigitalOut object.

在我也熟悉的JAL(另一种语言)中,他们使用alias关键字.

In JAL (Just Another Language) that I'm also familiar with, they use the alias keyword.

alias     x is pin_D3   -- the gpio pin called x used by the library
pin_D3_direction   = output 
include foo        -- jal doesn't use header files. 
foo_init()

如何用Microchip xc8 c编译器实现类似的功能? 我用define关键字尝试了JAL方法,但是编译器不知道"x"是什么.它说未定义的标识符"x" ...?

How do I achieve similar functionality with the Microchip xc8 c compiler? I tried the JAL approach with the define keyword, but the compiler doesn't know what 'x' is. It says undefined identifier 'x'... ?

#define x PortDbits.RD0 
#include "foo.h"
#foo_init();

推荐答案

这是因为在foo库作用域中确实未定义"x".

It is because the 'x' is really not defined in the foo library scope.

仅当您直接包含不需要头文件的源(.c)文件时,此方法才有效.

This only works when you have directly include the source (.c) file which do not need header file.

#define x PortDbits.RD0 
#include "foo.c"
#foo_init();

最好用另一个头定义GPIO引脚.例如GPIO_x.h

It is best if you have another header to define GPIO pins. For example GPIO_x.h

然后在里面

#ifndef GPIO_X_H
#define GPIO_X_H

#include "MCUXXXXX.h"

#define x PortDbits.RD0 

#endif

在此之后,只需将此文件包含在foo.c中即可.

after this, just include this file in foo.c

//this is foo.c

#include "GPIO_x.h
#include "foo.h"

/* your codes */

这篇关于使用带有gpio引脚的xc8编译器在C中初始化驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆