Jump to content
Sign in to follow this  
Nytblade

Pointer to register?

Recommended Posts

I want to pass a register as an argument to a function. How do you do it? It seems like a pointer should work since the a register is just a memory address, right?

 

The code below shows what I am trying to achieve, but it doesn't compile. I think the SFR_8BIT preprocessor definition in the device header file is confusing it... the compiler doesn't know what that is.

 

#include 

void main()
{
unsigned char *dirptr;

WDTCTL = WDTPW + WDTHOLD;
dirptr = P1DIR;
*dirptr = (unsigned char)(BIT0 + BIT1);	// Set to output direction

while (1)
	;
}

Share this post


Link to post
Share on other sites

Just a comment: I find it clearer to use the term "peripheral register" when speaking of these things, since "register" often means one of the MSP430 CPU registers r0 through r15. There is no such thing as a pointer to a register for MSP430, though a pointer to a peripheral register is done exactly as alexx_88 stated.

Share this post


Link to post
Share on other sites
Yes, the correct form would be:

dirptr = &P1DIR;

You are interested in getting the address of register P1DIR, not its contents, that's why you need to use the 'reference operator (&)'.

 

I was thinking that P1DIR was an pointer to a peripheral register rather than a peripheral register variable, so that is why I was doing it without &. I got that impression from reading the header files... it looks like they are just assigning an address to a constant.

 

However, now that I think of it, I don't use * to access their values, so what you say makes sense.

 

Thanks for the clarification.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...