简体   繁体   English

引用结构中的变量

[英]Referencing variables in a structure

I've recently been getting into programming AVR microcontrollers as a way to improve my C programming abilities. 最近,我开始对AVR微控制器进行编程,以提高我的C编程能力。 Right now I'm trying to check the state of two buttons, which I am doing at the beginning of my main.c file. 现在,我正在尝试检查两个按钮的状态,这是在main.c文件开头执行的操作。 I placed these into a separate C file and am trying to call this function. 我将它们放在单独的C文件中,并试图调用此函数。

I have two questions: 我有两个问题:

The first has to do with header files and .c files. 第一个与头文件和.c文件有关。 I've read several examples of how to write header files and link them but it seems counter-intuitive to me. 我已经阅读了几个有关如何编写头文件并链接它们的示例,但对我来说似乎是违反直觉的。 The header file calls the function in the c file to which it corresponds, and both the corresponding c file and the main file include the header file. 头文件在与其对应的c文件中调用该函数,并且相应的c文件和主文件都包括头文件。 How can the header file call a function in the .c file but that c file must include the header file? 头文件如何在.c文件中调用函数,但该c文件必须包含头文件?

The second question is about referencing variables in a structure. 第二个问题是关于引用结构中的变量。 I've made a structure following an example, but I'm not sure how to call it in the main.c file. 我已经按照示例制作了一个结构,但不确定如何在main.c文件中调用它。 My code is below: 我的代码如下:

This is my header file: 这是我的头文件:

//Check_Touch_Inputs.h

#ifndef Check_Touch_Inputs_H_
#define Check_Touch_Inputs_H_

int Check_Touch_Inputs(uint8_t topPadState, bottomPadState);

#endif

This is my corresponding C file: 这是我对应的C文件:

//Check_Touch_Inputs.c

#include <avr/io.h>
#include "Check_Touch_Inputs.h"

struct touchPads{
    uint8_t topPadState, bottomPadState;
}

struct touch    Pads padStates(){
    struct touchPads result;

    if (PINC & 1<<PC1)
    {
        result.topPadState = 1;
    }
    else
    {
        result.topPadState = 0;
    }

    if (PINC & 1<<PC3)
    {
        result.bottomPadState = 1;
    }
    else
    {
        result.bottomPadState = 0;
    }

    return result;
}

Lastly, here is my attempt to return the button states in my main file. 最后,这是我尝试返回主文件中的按钮状态的尝试。 This is wrong I know and fails: 我知道这是错误的,但会失败:

    uint8_t topPadState = Check_Touch_Inputs(topPadState);
    uint8_t bottomPadState = Check_Touch_Inputs(bottomPadState);

Thank you very much for any help. 非常感谢您的帮助。 Sorry if these are beginner questions. 抱歉,这些是初学者的问题。 These questions might have already been answered but I didn't come across answers that really satisfied my understanding. 这些问题可能已经回答了,但是我没有遇到真正令我满意的答案。

First of all, your conception that there is a difference between .h and .c files is not correct. 首先,您认为.h和.c文件之间存在差异的想法是不正确的。

A program written i C consists of a number of text files, which are individually compiled to object files. 用C编写的程序由许多文本文件组成,这些文本文件分别编译为目标文件。 The object files consists of code implementing the individual functions and a table of global symbols. 目标文件由实现单独功能的代码和全局符号表组成。

In order to link to a function (symbol) you must have it's prototype . 为了链接到一个函数(符号),您必须拥有它的prototype For historic reasons prototypes are defined in .h files and the compilable text is put in .c files. 出于历史原因, 原型在.h文件中定义,而可编译的文本位于.c文件中。 You usually include your .h in your corresponding .c file to check that your implementations matches your prototype. 通常,您在相应的.c文件中包含.h,以检查您的实现与原型是否匹配。

Your seconds question: You will have to define your struct: 您的秒数问题:您将必须定义结构:

struct touchPads{ uint8_t topPadState, bottomPadState; }

in the .h file, in order to define a variable of that type in main. 在.h文件中,以便在main中定义该类型的变量。

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

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