简体   繁体   English

一般而言,MPLAB X,XC8和C。 在函数和源文件中使用变量

[英]MPLAB X, XC8, and C in general. Using variables across functions and source files

I am having a difficult time passing variables to functions--especially functions that are not in the same source file. 我很难将变量传递给函数-特别是不在同一源文件中的函数。 I suspect these two problems are actually the same problem. 我怀疑这两个问题实际上是同一个问题。 I am sure this is somewhere on the internet, but I have done a lot of searching and I am now even more confused. 我确定这是在互联网上的某个地方,但是我已经做了很多搜索,现在变得更加困惑。 Mostly I need someone to give me some direction on what I should be reading/searching for. 通常,我需要有人给我一些有关我应该阅读/搜索的指导。

PROBLEM 1: 问题1:

Say I have a source file named main.c. 说我有一个名为main.c的源文件。 After the #includes and #defines, I declare a variable 在#includes和#defines之后,我声明一个变量

int count;

I then declare a function 然后我声明一个函数

void increment () {
count++;
}

Within function main(); 在函数main()中; I call the function increment();, and then update PORTA to display it in LEDs. 我调用了函数increment();,然后更新PORTA以在LED中显示它。 Both "count" and PORTA are assigned zero before main(); 在main()之前,“ count”和PORTA都被分配了零; runs. 运行。

void main () {
     increment();
     PORTA = count;
}

The problem is that there appears to be two versions of "count". 问题是“ count”似乎有两个版本。 If this program was run, PORTA would never light an LED. 如果运行该程序,PORTA将永远不会点亮LED。 However, if "PORTA = count;" 但是,如果“ PORTA =计数;” were moved inside the function, it would increment properly. 在函数内部移动时,它会适当增加。 Furthermore, all hardware writes (Port, tris, etc) work fine inside the function, but variables I thought I declared globally do not. 此外,所有硬件写入(端口,Tris等)都可以在函数内正常运行,但我认为我在全局范围内声明的变量却无法正常工作。 Thus, I assume the compiler is making a copy of "count" for the function call, and forgetting it when it returns. 因此,我假设编译器正在为函数调用复制“ count”,并在返回时将其忘记。

I would normally just return a value from the function to get around this, but interrupt routines for the PIC cannot return a value, and I must use an interrupt. 通常,我通常会从函数中返回一个值来解决此问题,但是PIC的中断例程无法返回值,因此我必须使用中断。

What do I do? 我该怎么办? Surely I am missing a major concept! 当然,我缺少一个主要概念!

PROBLEM 2: Example of a common issue 问题2:常见问题示例

Say I am using the MLA device library and load the demo material for the HID_Mouse. 说我正在使用MLA设备库并加载HID_Mouse的演示材料。 Though it has ten million folders and source and header files that include each other, I am able to edit some of the subroutines and make it do my bidding. 尽管它具有一千万个文件夹以及彼此包含的源文件和头文件,但我仍然可以编辑一些子例程,并使其按我的意愿进行投标。 However, I need to declare a variable that is used both in main.c and modified by a function in app_device_mouse.c. 但是,我需要声明一个在main.c中使用并由app_device_mouse.c中的函数修改的变量。 How do I declare this thing so that it gets globally read/written, but I don't get declaration errors from the compiler? 我如何声明这个东西以便它可以被全局读取/写入,但是我不会从编译器中得到声明错误?

../src/app_device_mouse.c:306: error: (192) undefined identifier "position_x"

ie "You didn't declare 'int position_x' in app_device_mouse.c, even though you did in main.c 即“即使您在main.c中也没有在app_device_mouse.c中声明'int position_x'

I'm not sure of the result of declaring it in both places, but something tells me that's a bad idea. 我不确定在两个地方都声明它的结果,但是有一些事情告诉我,这是个坏主意。

Thanks so much in advance for your time. 非常感谢您抽出宝贵的时间。 I have learned a lot from this community! 我从这个社区中学到了很多东西!
-GB -GB

For anyone who comes behind, the code in PROBLEM 1 was actually working code. 对于任何落后的人,问题1中的代码实际上是有效的代码。 My error instead was carelessly initializing my TRISC to 1 instead of 0xff; 相反,我的错误是粗心地将TRISC初始化为1而不是0xff。 which means I was trying to run a button off an output. 这意味着我试图在输出上运行按钮。 I should know better than that. 我应该比这更了解。

However, I was having this problem on other occasions by declaring my variables in main(); 但是,在其他场合,我通过在main()中声明变量来解决这个问题。 instead of outside the functions. 而不是功能之外。 This means I was trying to modify local variables inside a function that had not declared it - this was giving me nulls and garbage. 这意味着我试图在一个没有声明的函数中修改局部变量-这给了我null和垃圾。 Pedwards correctly identified that I was having trouble with global vs local variables; Pedwards正确地指出我在使用全局变量和局部变量时遇到了麻烦; and "scope" was a really helpful keyword. “范围”是一个非常有用的关键字。

Declaring a variable as volatile is necessary for the variable to be modified by the ISR. 对于ISR修改变量,将变量声明为volatile是必要的。 After Oled's comment I was able to find this information on page 169 of the XC8 compiler manual. 在Oled发表评论之后,我能够在XC8编译器手册的第169页上找到此信息。

What you're missing is called "scope". 您所缺少的称为“范围”。 It's not specific to XC8, any C book will help you. 它不是XC8特有的,任何C书都可以为您提供帮助。

PIC interrupts won't take/return anything for a reason. PIC中断由于某种原因不会占用/返回任何内容。 Define a global in the same file as your ISR is defined and read/change that. 在定义ISR的同一文件中定义一个全局变量,然后读取/更改该全局变量。 If you're going to write to it from the ISR declare it 'volatile': 如果要从ISR对其进行写操作,则将其声明为“ volatile”:

volatile int foo = 0x00;

If you need to access it from another file (beginners shall avoid this) declare it 'extern' in this file (or include): 如果您需要从另一个文件访问它(初学者应避免这种情况),请在此文件(或包括)中将其声明为“ extern”:

extern int foo;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM