Bit Addressability of GPIO Pins in PSoC® 3 - KBA88236

Version: *A

 

Question:

 How do you address individual GPIO pins in PSoC® 3? 

 

Answer:

Similar to the 8051 code, PSoC 3 offers the flexibility to address the port pins individually. To do this, access a specific bit using the Special Function Register (SFR) register area. The Keil compiler provides the keyword “sbit,” which is used to access an SFR bit. Note that the sbit can be used only for SFR registers. 

For example:

To access the pin PRT1.7, define the SFR bit as follows:

sbit bit_led=SFRPRT1DR^7; // This makes the PRT1.7 bit addressable

Now, the bit can be set or cleared using the following code.

bit_led = 1; // Set the bit
bit_led = 0; // Clear the bit

An alternative way of doing this is to use the SETB command. This is an assembly command so you will need to use an inline assembly method to include it: 

#pragma asm
SETB bit_led ; Set the bit
CLRB bit_led ; Clear the bit
#pragma endasm

To make sure that the GPIO is controlled using the SFRs, set the SFRPRTxSEL register bit corresponding to the pin that is being accessed to ‘1’. In this example, bit 7 of SFRPRT1SEL is set to ‘1’ to access PRT1.7, as shown here: 

SFRPRT1SEL |= 0x80; // This makes the PRT1.7 controllable through SFRs

Refer to the Knowledge Base Article Using Assembly Language for PSoC 3 in PSoC Creator for the settings required to enable inline assembly.

Refer to the Application Note AN60630 - PSoC® 3 8051 Code and Memory Optimization for details on the use of the SFR and sbit keywords, and how they apply to PSoC 3 I/O pins.