Topic: C Programming for 8051: SFR directions as arguments

Discussion in 'Embedded Systems and Microcontrollers' started by BRetonDP, Jul 17, 2014.

  1. BRetonDP

    Thread Starter New Member

    Apr 11, 2014
    5
    0
    Hello, forum!

    I'm currently making a very simple Tic-Tac-Toe game with an AT89C51RC2 and I am having issues getting the button input right.

    Basically, the input is a 3x3 Button Matrix which I want to utilize by scanning through the rows with a pulse while checking the columns for a connection; pretty basic. However, the way I wanted to do this was by making a unique scroll method that received the row parameter and set it to HIGH accordingly.

    I already have

    sbit R1 = P0^0;

    all through the needed inputs, but I'm not sure if this is correct.

    Then, on my main method, I run somehthing like

    while(TRUE)
    {
    scroll(R1);
    }

    which is named scroll, I have this

    void scroll (bit P)
    {
    P = HIGH; //Sets the row being checked to HIGH (HIGH is already defined as a 1)
    ....
    }

    So, this is not working. I know I'm not doing this right, but I had to start somewhere. Is what I am attempting to do possible? Sending a single Pin adress as an overload for a method? If so, what datatype should it be? I was juggling between char and bit but I can't settle.

    I hope my question is understandable! If not, I will gladly elaborate and maybe post all the code (although it is quite messy). Thanks, everyone, in advance!
     
  2. BRetonDP

    Thread Starter New Member

    Apr 11, 2014
    5
    0
    By the way, my port is P1, not P0, I'm sorry.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    While this may make complete sense to you, someone just looking for the first time (like ME) has very little idea what you mean. A sketch of the hardware would be very helpful a there are many many ways to implement a key matrix.

    Also, rather then quoting a few select lines of code just quote the entire scroll() function where keys are scanned. And DO enclose them inside code tags (that # symbol above the reply box). It is way better to copy and paste your actual code then write it again: typos and such may be far more noticeable from a paste.

    Assuming this code is in C, from the little bit you posted....

    Code ( (Unknown Language)):
    1. void scroll (bit P)
    2. {
    3.    P = HIGH; //Sets the row being checked to HIGH (HIGH is already defined as a 1)
    4.     ....
    5. }
    would have no outside effect, and an optimizing compiler may just delete the entire routine.

    Why? the "bit P" in "void scroll (bit P)" passed the value of P. If you wish to use it to return a value you have several choices.

    First, a bit is either on or off, and cannot hold a number that has 3 values (the rows). An unsigned char (byte wide) would be better, as would any other type.

    Until I see how you *use* the return value I'll assume you need a bit. To return a bit you can do either:

    1) Return a bit value
    Code ( (Unknown Language)):
    1. bit scroll (void )
    2. {
    3.   return HIGH;
    4. }
    2) use indirection (pointer) to change P
    Code ( (Unknown Language)):
    1. void scroll (bit *P)
    2. {
    3.    *P = HIGH; //Sets the row being checked to HIGH (HIGH is already defined as a 1)
    4.     ....
    5. }
    The * accepts in the value of the address of P, or assigns the HIGH there.


    And finally, welcome to the forums !!!
     
  4. BRetonDP

    Thread Starter New Member

    Apr 11, 2014
    5
    0
    Thank you for your reply and for your tips, I will definitely take all into consideration!

    I think you actually did understand what I was trying to say, I will try to use a pointer! What I was trying to say was that I wanted the scroll function argument to be an address. I wanted to send a port address (which, for example, was R1, previously defined by...)

    Code ( (Unknown Language)):
    1. sbit R1 = P1^0;
    as a parameter, in this case:

    Code ( (Unknown Language)):
    1.  scroll(R1);

    but I didn't really know what data type I was sending as an argument; I though it made some sense because it compiled. I used bit because that's what I considered the single bit in the sfr to be. I didn't need a return value, I just needed a void method that turned any address declared by sbit and sent as parameter into a HIGH. What should the scroll parameter be, data type-wise, in that case? Is bit ok? Maybe a pointer will work, I will try it out! Thank you!
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Ahh, gotcha now. While I know C I use it on PIC devices, not 8051's but my guess is the answer is the same: you just can't pass a reference (pointer) to a bit of a register or memory location.

    What you can pass is the register pointer and a bit number, then your code sets that bit.
    Code ( (Unknown Language)):
    1.  
    2. void scroll (uint_8 * Port, uint_8 bit)
    3. {
    4.  // to set the Port.bit high:
    5.  *Port |=  (1 << Bit);
    6.  // to reset the Port.bit low:
    7.  *Port &= ~(1 << Bit);
    8. }
    9.  
    The (1 << Bit) is just a way to set the bit you need, then you either OR it in to set that bit, or AND the inverse to reset the bit.
     
  6. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,374
    494
    I am looking in my uC textbook (we had 8051 as the uC in that class). The address for P1 is 90H (90 in hex). This port is bit addressable so Bit 0 has address 90H, Bit 1 is 91H, Bit 2 is 92H, etc.
     
Loading...