I would like to know if there is possible to initialize all variables (within a LOOP) present in a Struct to 0
and back to 1
.
Here is a demonstrating program:
#include <stdio.h>
#define LEDS_LENGTH 7
void inititate_to_zero(void);
void inititate_to_one(void);
struct pins
{
unsigned char state : 1;
unsigned char LED0 : 1;
unsigned char LED1 : 1;
unsigned char LED2 : 1;
unsigned char LED3 : 1;
unsigned char LED4 : 1;
unsigned char LED5 : 1;
}pins;
int main(void)
{
inititate_to_zero( );
inititate_to_one( );
}
void inititate_to_zero(void)
{
for(unsigned char i = 0; i < LEDS_LENGTH; i++)
{
/// set all variable in struct pins to 0;
pins.VARIABLE[SOME_HOW] ^= ( 1 << 0 );
}
}
void inititate_to_one(void)
{
for (unsigned char i = 0; i < LEDS_LENGTH; i++)
{
/// set all variable in struct pins to 1;
pins.VARIABLE[SOME_HOW] ^= ( 1 << 0 );
}
}
EDIT:
I needed to replace the function inititate_to_one()
to use a LOOP if it is possible:
#include <stdio.h>
#define LEDS_LENGTH 7
void inititate_to_one ( void );
struct pins
{
volatile unsigned char state : 1;
unsigned char LED0 : 1;
unsigned char LED1 : 1;
unsigned char LED2 : 1;
unsigned char LED3 : 1;
unsigned char LED4 : 1;
unsigned char LED5 : 1;
}pins{ 0 };
int main( void )
{
inititate_to_one ( );
}
void inititate_to_one( void )
{
state ^= ( 1 << 0 );
LED0 ^= ( 1 << 0 );
LED1 ^= ( 1 << 0 );
LED2 ^= ( 1 << 0 );
LED3 ^= ( 1 << 0 );
LED4 ^= ( 1 << 0 );
LED5 ^= ( 1 << 0 );
}