use of structs

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi freaks!
was after some help with the following code, I've cut it down a bit so post isn't too long;
part of my my .h file

/*	STRUCTS	*/
struct button
{
	unsigned int mask;
	unsigned int buffer_count;
};

/* Globals as exters so other functions can "see" tham */
extern struct button *button1;
extern struct button *button2;

/*	BUTTon related	*/
#define DEFAULT_BUFFER_COUNT 10;

/* Bits	*/
#define BIT0 0x01
#define BIT1 0x02
#define BIT2 0x04
#define BIT3 0x08
#define BIT4 0x10
#define BIT5 0x20
#define BIT6 0x40
#define BIT7 0x80

In my button.c file

/*	Initialise buttons	*/
void initialise_buttons()
{
	button1->mask = BIT6;
	button1->buffer_count = DEFAULT_BUFFER_COUNT;

	button2->mask = BIT7;
	button2->buffer_count = DEFAULT_BUFFER_COUNT;
}

/*	Process: Scan for button press. 	
	NOTE: Must be repeatedly called for debounce counter to expire */ 
unsigned char sample_button (struct button *buttondata) 
{ 
	unsigned char result = false; 

	if((PORTD & buttondata->mask) == 0)  /* if pressed (active low) */ 
	{ 
		......more code
	}
   else
	{ 
		......more code	
                }

  ......more code
} 

In my main file

/*	GLOBALS	*/
struct button *button1;
struct button *button2;


int main()
{
	int i=0, j=0;

	initialise_io();
	initialise_buttons();
	initialise_timer1();

	sei();		//enable interrupts. cei(); clears int
	
	while(1)	//forever
	{
		if( (sample_button(button1)) == 1)
		{
			i++;
		}
		if( (sample_button(button2)) == 1)
		{
			j++;
		}
	}
	return(1);
}

All the files are included correctly etc.
The problem i'm having is when I simulate in AVRstudio4 (using winAVR GCC) both button1 and button2 both react when bit 7 is cleared, That is "if((PORTD & buttondata->mask) == 0)" is true for both buttons even though bit 6 is still set.
What I want to happen is button1 debounced when bit6 cleared, button2 debounced when bit7 cleared. Note when only bit 6 cleared no buttons are debounced that is "if((PORTD & buttondata->mask) == 0)" false in both cases.

I don't think its the code in button.c as they seem to be decleared correctly and each button object uses the same debounce code.

Is it the way my structs are declared? :?
Is there a way I can view the value of button1 and button2 mask values in the simulator? I can only view/watch local variables. guess I could use a dummy local variable = buttondata->mask. Will try that when home from work

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see nowhere where you are setting the value of button1 and button2. Since they are global variables, they are both initialized to point to memory location 0. You have to create objects for the pointers to actually point to.

Quote:
Is there a way I can view the value of button1 and button2 mask values in the simulator?

You view global variables the exact same way that you view local variables.

Regards,
Steve A.

The Board helps those that help themselves.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

God I'm dumb!! I new it was something stupid, can't beleive I didn't wake up to it.

I had the pointers but no actual objects to point at!!
no wonder I couldn't veiw/watch their values in the simulator, they didn't exist.

Well now I've turned my brain on i'v fixed the problem as follows

/*	GLOBALS	*/
struct button b1, b2;
struct button *button1,*button2;
button1 = &b1;
button2 = &b2;

Cheers Steve

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why do you need the global pointers at all? Why not just pass the address to the other functions?

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!