Accessing specific memory locations

This is a discussion on Accessing specific memory locations within the C Programming forums, part of the General Programming Boards category; I'm writing code for use on a Single Board computer. The board has an LED which I'm trying to turn ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    278

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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. #4
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    Using a pointer would be a good idea, i guess. ;-)

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Ok, then how do I access the bits?

  8. #8
    Registered User
    Join Date
    Jun 2003
    Location
    Austria
    Posts
    55
    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
    Last edited by IceBall; 02-26-2009 at 12:05 PM.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Is it possible to "print out" LED so I can see tha values of all the bits?

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Bladactania View Post
    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. #13
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Quote Originally Posted by tabstop View Post
    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. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Bladactania View Post
    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. #15
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Well I tried that too and that didn't work either...

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 11:48 PM
  2. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  3. Accessing a Specific Text Line Inside CEditView :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 04-14-2002, 09:12 PM
  4. Writing to Specific Locations
    By Spex in forum C Programming
    Replies: 5
    Last Post: 02-14-2002, 10:08 PM
  5. Accessing memory directly
    By YALINI in forum C Programming
    Replies: 0
    Last Post: 08-31-2001, 12:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21