简体   繁体   中英

Referencing variables in a structure

I've recently been getting into programming AVR microcontrollers as a way to improve my C programming abilities. Right now I'm trying to check the state of two buttons, which I am doing at the beginning of my main.c file. I placed these into a separate C file and am trying to call this function.

I have two questions:

The first has to do with header files and .c files. 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. How can the header file call a function in the .c file but that c file must include the header file?

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. 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:

//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.

A program written i C consists of a number of text files, which are individually compiled to object files. 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 . For historic reasons prototypes are defined in .h files and the compilable text is put in .c files. You usually include your .h in your corresponding .c file to check that your implementations matches your prototype.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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