PSoC® 1 Data Port Bit Manipulation – KBA83008

Version 1
    Version: **


    Question: How do you manipulate port bits of PSoC® 1?



    The register PRTxDR can be used to manipulate port bits in PSoC 1. Each bit in the register corresponds to a port bit on port “x”. For instance, bit “0” of PRT0DR corresponds to Pin 0 of Port 0.

    The following code snippet can be used to clear bit “6” of Port “0”.

    In Assembly:

    and REG[PRT0DR], 0xBF

    In C:

    PRT0DR &= 0xBF

    Although the above statement can be used to manipulate the port bits with ease, sometimes it can be difficult to read the code. And when the number of manipulations increases, it gets worse.

    A desirable feature is to flip an I/O port bit with a simple statement like P0.0 = 1. This can be accomplished using C macro programming.

    The ports.h file attached to this knowledge base article can be included in your project to add this desirable readability to your code. Now in order to clear bit “6” of Port “0”, you just have to include ports.h in your header and use the following code snippet.


    The file includes macros that replace the above line of code with the below code before compiling:

    (PRT0DR = (((b)==0) ? (PRT0DR&0xBF) : (PRT0DR|0x40)));

    Macros for setting/clearing all the bits of a port at once are also defined in the header file. Now in order to write a 0xAA to Port 0, the following code can be used.


    Note: This article does not cover shadow registers and GPIO drive modes. For details on them, refer PSoC Hacker’s “PSoC 1 GPIO Demystified,” Part 1 and Part 2.