You are here

More PDL Examples - Input Pins | Cypress Semiconductor

More PDL Examples - Input Pins

Today I shall give you some pointers for using GPIO as inputs. As with outputs you can configure them with the schematic file or by writing firmware. Here is an instance of the GPIO_PDL component in a schematic.

Input pin in PSoC Creator schematic (with LED9 output)

I configured the component to be called "SW2" because I am going to map it to the pin that connects to that switch on the Pioneer kit. In the customizer I also disabled the hardware connection, so I can use it exclusively in firmware, and set the drive mode to be resistive pull up. This means that the pin reads high when the switch is not pressed and goes low when it is pressed (active-low) and grounds the pin.

Pin configuration inside PSoC Creator GPIO customizer

In the resources file I chose pin P0.4 and you can see that reflected inside the schematic pin symbol (above).

When I build this, PSoC Creator does the magic and I can start reading the pin in main() with no initialization code. I kept the LED9 component from the last week's article and this code makes it follow the state of the switch.

    for(;;)
    {
        Cy_GPIO_Write( LED9_PORT, LED9_NUM, Cy_GPIO_Read( SW2_PORT, SW2_NUM ) ); 
    }

OK, that was easy. How do I do it using just PDL firmware? Well, it just takes one extra line of code. Now, I do not have another switch on the kit so I chose to use a jumper wire as a really, really, really naughty alternative - connecting P0.2 in header J4 to ground manually. Note how I initialize the pin and changed the code in the loop to use my new "switch".

    Cy_GPIO_Pin_FastInit( GPIO_PRT0, 2, CY_GPIO_DM_PULLUP, 1, HSIOM_SEL_GPIO );

    for(;;)
    {
        Cy_GPIO_Write( LED9_PORT, LED9_NUM, Cy_GPIO_Read( GPIO_PRT0, 2 ) ); 
    }

So this works great but it contains those scary "magic numbers" that will bite me if I try to do this is a real (bigger) application. Let me fix that right away, before I get a call from the Cypress code quality police.

#define SW_PORT     GPIO_PRT0
#define SW_NUM      2

    Cy_GPIO_Pin_FastInit( SW_PORT, SW_NUM, CY_GPIO_DM_PULLUP, 1, HSIOM_SEL_GPIO );

    for(;;)
    {
        Cy_GPIO_Write( LED9_PORT, LED9_NUM, Cy_GPIO_Read( SW_PORT, SW_NUM ) ); 
    }

I may only have only one real switch (I called my jumper wire "SW") but I do have another LED. It's LED8 on P1.5. Here is a program that drives LED9 from SW2 and LED8 from SW (naughty switch) to further contrast the approaches.

#define SW_PORT     GPIO_PRT0
#define SW_NUM      2

#define LED8_PORT   GPIO_PRT1  
#define LED8_NUM    5

    Cy_GPIO_Pin_FastInit( SW_PORT, SW_NUM, CY_GPIO_DM_PULLUP, 1, HSIOM_SEL_GPIO );
    Cy_GPIO_Pin_FastInit( LED8_PORT, LED8_NUM, CY_GPIO_DM_STRONG, 1, HSIOM_SEL_GPIO );

    for(;;)
    {
        Cy_GPIO_Write( LED9_PORT, LED9_NUM, Cy_GPIO_Read( SW2_PORT, SW2_NUM ) ); 
        Cy_GPIO_Write( LED8_PORT, LED8_NUM, Cy_GPIO_Read( SW_PORT, SW_NUM ) );
    }

So, which of these methods should you use? Well, it's 100% up to you because neither is really better than the other. If you use a component in a schematic then PSoC Creator will stop you from inadvertently using that pin for some other function. That's quite nice but most of the time that just does not happen because P0.4 is connected to SW2 and there is no reason to try to use it for anything else. With the firmware-only approach you need to remember to initialize the pin but it is much fast to make a change to another pin and re-build.

Next time I shall beef this up a little by making the program interrupt-driven and I'll explain how to manage multiple interrupts from one GPIO port.

 

Blog: 

Comments

Kate Wolwort's picture

Personably I prefer to use a component in a schematic. Looking forward to your next article about how to manage multiple interrupts from one GPIO port. For now I most focused on how to create a social media app
 

alexdixon47_3311576's picture

I have not used a GPIO device yet. Is there any tutorial for it? How can I connect it to the peripherals? Is it useful for my work purpose?

best dissertation writing service

roybutt777_3318046's picture

 have not used a GPIO device yet. Is there any tutorial for it? How can I connect it to the peripherals? Is it useful for my work purpose?

quickpayportal

ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.