# Thread: Accessing specific memory locations

1. ## Accessing specific memory locations

I'm writing code for use on a Single Board computer. The board has an LED which I'm trying to turn on off, but I'm having some trouble understanding how to access it. The LED can be accessed at I/O location 77h bit 0. A logic "1" is the on condition (I'm assuming that logic "0" is the off condition). How do I access this specific bit? I've looked at several threads on this board and others, but haven't understood much of what I've read. Any help, direct or indirect, is much appreciated.

2. You use & to get at the value of specific bits. You'll have to figure out if they're numbering bits from least significant to most significant or the other way around.

3. I don't even know how to access that specific I/O Address.

I have
#define LED_BYTE 0x77
but that's all I've been able to grasp thus far.

4. Using a pointer would be a good idea, i guess. ;-)

5. Yes, but a pointer to what? What type should I be using? unsigned char right?

Code:
```unsigned char * LED;

LED = LED_BYTE;
LED[0] = 1;```

6. A byte (i.e. unsigned char) seems like a good assumption to me. Of course you can't use array notation to access bits, though.

7. Ok, then how do I access the bits?

8. unsigned char is ok, but
Code:
`LED[0]`
would not work, since this will access the first byte of LED - you want to access the first bit

try something like this to set the first bit to 1
Code:
`*LED = *LED | 0x01;`
Bitwise operators

9. Ok, I'm more confused now that I was before

As for the code you put, it does not work. I even tried using the leftshif operator in case the LSB/MSB were in the reverse order.

10. If you refer to IceBall's code, it works perfectly for what it says it does: set bit 0 to 1. I don't know what you wanted it to do, but that's what it does.

Code:
```*LED = 0; //initialize because why not (you probably wouldn't do this with real hardware)
int before = *LED & 0x01; //is bit 0 set?
*LED = *LED | 0x01; //set bit 0
int after = *LED & 0x01; //is bit 0 set?
*LED = *LED & ~0x01; //unset bit 0
int really_after = *LED & 0x01; //is bit 0 set?```

11. Is it possible to "print out" LED so I can see tha values of all the bits?

12. Originally Posted by Bladactania
Is it possible to "print out" LED so I can see tha values of all the bits?
I suppose you can print it out in hex using %x.

13. Originally Posted by tabstop
If you refer to IceBall's code, it works perfectly for what it says it does: set bit 0 to 1. I don't know what you wanted it to do, but that's what it does.
By doesn't work I meant the LED does not come on.

14. Originally Posted by Bladactania
By doesn't work I meant the LED does not come on.
I suppose you can check the specs to see if by "bit 0" they didn't mean the most signficant bit instead. (Or, if you're an engineer, you can just change 0x01 to 0x80 and see what happens.)

15. Well I tried that too and that didn't work either...

Popular pages Recent additions