This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

port setting

Hi,
I want to use port 2 of 167 on MCB net board for two peripherals, one for keyboard interface and other for lcd interface.
Please suggest some code on how to access the same port in parts.
regards
Rajesh

  • There's no problem in reading the port in parts. To write to parts of the port you could emulate BFLD instruction by using bitwise OR and AND operations. For example, a way to set the upper 8 bits without affecting the lower 8:

    void SetUpperByte(unsigned char byte)
    {
    P2 = (P2 & 0xFF) | ( (unsigned int)byte << 8 );
    }


  • Hi Mike
    Thanks for the suggestion. I would like to know if there is a way to give names to parts of the part. For example P2.0 to P2.7 should be called as LCD and P2.8 to P2.15 should be called as KBD
    regards
    Rajesh

  • To the best of my knowledge there is no way to have a sort of separate variable for writing to a part of a port. Using bytewide write to a port clears the second byte, so it's useless (but using bytewide read from a port, on the other hand, could be quite useful). It seems to me that all that's left is macros and functions.
    By the way, maybe there is another way of writing to a port. You could create the following sequence of instructions somewhere in RAM:

    BFLDL reg(=P2), mask(=0xFF), data
    RET

    After that, you could fill the 'data' field of the BFLDL instruction and call that sequence to write to the lower byte of the port. Compared to this, the AND-OR approach has one disadvantage: the AND-OR sequence must not be interrupted by other port-accessing code because it would corrupt some of the data written to the port.
    Everything becomes simple if P2.0-P2.7 are configured as outputs and P2.8-P2.15 as inputs. In this case it's possible to use bytewide access:
    #define LCD MVAR(unsigned char, (unsigned char*)&P2)
    #define KBD MVAR(unsigned char, (unsigned char*)&P2 + 1)
    Sorry if I tried to complicate things.
    Good luck!
    Mike

  • I would like to know if there is a way to give names to parts of the part.

    Sure. This is what the MACRO capability of the C Programming Language is useful for.

    For example:

    #define SET_LCD(x) {P2 = (P2 & 0xFF00) | ((x) & 0x00FF);}
    
    #define SET_KBD(x) {P2 = (P2 & 0x00FF) | (((x) & 0x00FF) << 8);}
    
    #define READ_LCD (P2 & 0x00FF)
    #define READ_KBD ((P2 & >> 8) & 0x00FF)

    In my code I could use SET_LCD(34) and SET_KBD(45) to write to the LCD and Keyboard ports respectively. I can also use READ_LCD and READ_KBD to read the LCD and keyboard ports.

    But, this is really simple programming stuff.
    Jon

  • Hi Jon
    Thanks for the suggestion. It has definitely helped me.
    regards
    Rajesh

  • Hi Mike
    Thanks for the suggestion. But it surely sounds complicated for a newbie like me. I will stick to the previous suggestion. It has definitely helped me.
    regards
    Rajesh