I am having a bit of a problem with struct and defines from an atmega328p.
I have the following code:
typedef struct {
char port;
unsigned char pin;
unsigned long timestamp;
} BUTTONS;
BUTTONS button_1;
BUTTONS button_2;
BUTTONS button_3;
BUTTONS* button[BUTTONS_ID_COUNT] = {&button_1,&button_2,&button_3};
void Button_init(void){
button[BUTTONS_ID_1]->port = PINB;
button[BUTTONS_ID_1]->pin = PINB4;
button[BUTTONS_ID_1]->timestamp = 0;
}
unsigned char Button_instantRead(int id){
//return PINB & (1 << PINB4);
return button[id]->port & (1 << button[id]->pin);
}
I want to use my Button_instantRead()
to be able to read any port by only giving it an ID number. I use my init function to set which pin is associated to which port. But for some reason when I call my Button_instantRead()
function I do not get a 1 even when I press on my switch.
I tried my configuration in my main file using the commented line and everything works perfectly fine.
What am I doing wrong in my return
line?
After some Googling I found that char
is probably not the right type to reference a port. I think I would be better suited with a pointer to the first element of the address of the port, but again I don't know how to do it and could not find the answer either.