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.