7 Replies Latest reply on Mar 25, 2020 6:43 AM by Skdk_4669631

    Create a function to light different LEDs

    Skdk_4669631

      Hello !

       

      I freshly start with Psoc.

      I have 3 LED that I would like to make blink with a single function. For example:

       

      void blink(... x)

      {

        x_Write(1);

        Cydelay(500);

        x_Write(0);

        return;

      }

       

       

      And then somewhere in my loop, I'd have something like :

       

      blink(LED2);

      ...

       

      The problem is that I don't know what would be the type of the argument x in the function blink. I don't know how to refer a certain LED. With arduino I can use the number of the pin in the argument, is there something like this in psoc ?

       

      Thank for your help

        • 1. Re: Create a function to light different LEDs
          GaneshD_41

          Hi,

           

          You cannot pass pin number as an argument as you mentioned in your description in case of PSoC.

           

          The main difference between PSoC and arduino interms of programming language is PSoC uses Embedded C and Arduino uses Object oriented language.

           

          So you have to use the code as following in case of PSoC 4:

           

          void blink (uint8 x)

          {

             switch(x)

             {

                case 1:

                Pin_1_Write(1);

                cyDelay(500);

                Pin_1_Write(0);

                break;

           

                case 2:

                break;

             }

          }

           

          Can you please tell what is the part number of creator you are using? Also please tell us whether you are using PSoC Creator IDE or Modus Toolbox IDE for your application.

           

          Thanks

          Ganesh

          1 of 1 people found this helpful
          • 2. Re: Create a function to light different LEDs
            MoTa_728816

            Hi,

             

            Oops, I'm a little slow ...

             

            I think that there are many approaches to do what you are thinking...

             

            But I don't think having delay in a blink function is a good idea.

            So I would separate it into 2 parts.

            (1) set which LED to blink (blink())

            (2) a call-back function which flips "active" leds. (poke_leds())

             

            The following is my idea sketch using CY8CKIT-044.

            I cheated by using my tty_utils library to get command.

            tty_utils a utility sample for CLI type program

            But it's not the point tonight ;-)

             

            In the main(), if tty gets command the program call "blink()",

            it sets which LED should be blinking and which should not.

            Then each call of "poke_leds()" takes care of blinking of

            active LEDs.

             

            The uart output looks like below.

            When you enter 0, all LEDs are stopping.

            1 : only blue blinks

            2:  only green blinks

            4:  only red blinks

            7:  all 3 leds blinks

            and so on...

             

            000-teraterm-log.JPG

             

            schematic

            Note: Don't worry about UART, it's only for entering a command.

            001-schematic.JPG

            pins

            002-pins.JPG

            main.c

            =============

            #include "project.h"

            #include <stdio.h>

            #include "tty_utils.h"

             

            #define LED_ON  0u

            #define LED_OFF 1u

             

            #define R_MASK 0x04u

            #define G_MASK 0x02u

            #define B_MASK 0x01u

             

            int r_flag = 0 ;

            int g_flag = 0 ;

            int b_flag = 0 ;

             

            void init_hardware(void)

            {

               CyGlobalIntEnable; /* Enable global interrupts. */

              

               tty_init() ;

               splash("LED Blink Test") ;

            }

             

            void poke_leds(void)

            {

                if (r_flag) {

                    if (LED_R_Read()) {

                        LED_R_Write(0) ;

                    } else {

                        LED_R_Write(1) ;

                    }

                }

                if (g_flag) {

                    if (LED_G_Read()) {

                        LED_G_Write(0) ;

                    } else {

                        LED_G_Write(1) ;

                    }

                }

                if (b_flag) {

                    if (LED_B_Read()) {

                        LED_B_Write(0) ;

                    } else {

                        LED_B_Write(1) ;

                    }

                }

            }

             

            void blink(uint8_t rgb_mode)

            {

                print("R: ") ;

                if (rgb_mode & R_MASK) {

                    r_flag = 1 ; /* blink R */

                    print("Blinking ") ;

                } else {

                    r_flag = 0 ; /* stop  R */

                    LED_R_Write(LED_OFF) ;

                    print("Stopping ") ;

                }

                print("G: ") ;

                if (rgb_mode & G_MASK) {

                    g_flag = 1 ; /* blink G */

                    print("Blinking ") ;

                } else {

                    g_flag = 0 ; /* stop G */

                    LED_G_Write(LED_OFF) ;

                    print("Stopping ") ;   

                }

                print("B: ") ;

                if (rgb_mode & B_MASK) {

                    b_flag = 1 ; /* blink B */

                    print("Blinking ") ;

                } else {

                    b_flag = 0 ; /* stop B */

                    LED_B_Write(LED_OFF) ;

                    print("Stopping ") ;

                }

                print("\n\r") ;

            }

             

            void do_command(char *str)

            {

                int mode ;

                sscanf(str, "%d", &mode) ;

                blink(mode) ;

            }

             

            int main(void)

            {

                init_hardware() ;

              

                prompt() ;

                for ( ; ; ) {

                    if (get_line()) {

                        do_command(str) ;

                        prompt() ;

                    }

                    poke_leds() ;

                    CyDelay(500) ; /* 500ms */

                }

            }

            =============

             

            moto

            1 of 1 people found this helpful
            • 3. Re: Create a function to light different LEDs
              MoTa_728816

              Hi,

               

              As you wrote you have CY8CKIT-059 in the previous discussion,

              I wrote one for CY8CKIT-059.

              But unlike CY8CKIT-044, CY8CKIT-059 has only 1 LED,

              so I connected 3 external LEDs via resistors.

              Pins used are

              P0[7] : LED_R

              P0[6]:  LED_G

              P0[5]:  LED_B

              GND: connected to each LEDs via resistors.

              IMG_4162.JPG

              schematic

              010-schematic.JPG

              pins

              011-pins.JPG

              Tera Term log

              012-TeraTerm-log.JPG

              main.c

              ===========

              #include "project.h"

              #include <stdio.h>

              #include "tty_utils.h"

               

              #if 0 /* negative logic */

              #define LED_ON  0u

              #define LED_OFF 1u

              #else /* positive logic */

              #define LED_ON  1u

              #define LED_OFF 0u

              #endif

               

              #define R_MASK 0x04u

              #define G_MASK 0x02u

              #define B_MASK 0x01u

               

              int r_flag = 0 ;

              int g_flag = 0 ;

              int b_flag = 0 ;

               

              void init_hardware(void)

              {

                 CyGlobalIntEnable; /* Enable global interrupts. */

                 

                 tty_init() ;

                 splash("LED Blink Test") ;

              }

               

              void poke_leds(void)

              {

                  if (r_flag) {

                      if (LED_R_Read()) {

                          LED_R_Write(0) ;

                      } else {

                          LED_R_Write(1) ;

                      }

                  }

                  if (g_flag) {

                      if (LED_G_Read()) {

                          LED_G_Write(0) ;

                      } else {

                          LED_G_Write(1) ;

                      }

                  }

                  if (b_flag) {

                      if (LED_B_Read()) {

                          LED_B_Write(0) ;

                      } else {

                          LED_B_Write(1) ;

                      }

                  }

              }

               

              void blink(uint8_t rgb_mode)

              {

                  print("R: ") ;

                  if (rgb_mode & R_MASK) {

                      r_flag = 1 ; /* blink R */

                      print("Blinking ") ;

                  } else {

                      r_flag = 0 ; /* stop  R */

                      LED_R_Write(LED_OFF) ;

                      print("Stopping ") ;

                  }

                  print("G: ") ;

                  if (rgb_mode & G_MASK) {

                      g_flag = 1 ; /* blink G */

                      print("Blinking ") ;

                  } else {

                      g_flag = 0 ; /* stop G */

                      LED_G_Write(LED_OFF) ;

                      print("Stopping ") ;    

                  }

                  print("B: ") ;

                  if (rgb_mode & B_MASK) {

                      b_flag = 1 ; /* blink B */

                      print("Blinking ") ;

                  } else {

                      b_flag = 0 ; /* stop B */

                      LED_B_Write(LED_OFF) ;

                      print("Stopping ") ;

                  }

                  print("\n\r") ;

              }

               

              void do_command(char *str)

              {

                  int mode ;

                  sscanf(str, "%d", &mode) ;

                  blink(mode) ;

              }

               

              int main(void)

              {

                  init_hardware() ;

                 

                  prompt() ;

                  for ( ; ; ) {

                      if (get_line()) {

                          do_command(str) ;

                          prompt() ;

                      }

                      poke_leds() ;

                      CyDelay(500) ; /* 500ms */

                  }

              }

              ===========

               

              moto

              • 4. Re: Create a function to light different LEDs
                NoriakiT_91

                There are several MACRO definitions in the cypins.h header file to control the GPIO pins with your software.

                 

                #define CyPins_SetPin(name)                 (CY_SYS_PINS_SET_PIN        (name ## _DR, name ## _SHIFT))
                #define CyPins_ClearPin(name)               (CY_SYS_PINS_CLEAR_PIN      (name ## _DR, name ## _SHIFT))
                

                 

                For example, CyPins_SetPin is used to set a GPIO output like CyPins_SetPin(LED2)  But the instance name "LED2" of the GPIO output cannot be passed as an argument of a function because it is not an OBJECT.  Instead of the MACRO, you can use another MACRO like following.

                 

                #define CY_SYS_PINS_SET_PIN(portDR, pin)     \
                            ( *(reg32 *)(portDR) |= (CY_SYS_PINS_PC_DATAOUT << (pin)) )
                

                 

                Both two arguments portDR and pin are integer which can be passed as function arguments.  The function "blink()" can be described as follows.

                 

                void blink(uint32 portDR, uint8 port) {
                    CY_SYS_PINS_CLEAR_PIN(portDR, port);
                    CyDelay(500);
                    CY_SYS_PINS_SET_PIN(portDR, port);
                    CyDelay(500);
                }
                

                 

                And the main loop will be

                 

                int main(void) {
                    CyGlobalIntEnable; /* Enable global interrupts. */
                
                
                    for(;;) {
                        blink(LED1__DR, LED1__SHIFT);
                        blink(LED2__DR, LED2__SHIFT);
                        blink(LED3__DR, LED3__SHIFT);
                    }
                }
                

                 

                Please note that there are TWO UNDERSCORE characters before DR and SHIFT.

                 

                Regards,

                Noriaki

                1 of 1 people found this helpful
                • 5. Re: Create a function to light different LEDs
                  Skdk_4669631

                  Thank you very much for you help !!!
                  It is crystal clear now :-)

                  • 6. Re: Create a function to light different LEDs
                    LePo_1062026

                    Skdk,

                     

                    A great thanks to Ganesh, moto-san and Noriaki-san.  Each of them gave you the correct answer for this issue.  Please acknowledge one of them with "Correct" on this post.

                     

                    I want to challenge you to think outside the Arduino box.  One of the reasons I try to use the PSoC CPUs over any other CPU out there is it's architecture.  The availability of Universal Digital Blocks (UDBs) and routable Analog puts it in a class of its own.

                     

                    Being a Hardware engineer by training, I make use of these features to minimize the burden of the CPU.  I create HW state-machines to let the Hardware do 99.99% of the work.  This allows the CPU to do other more important things or in some cases go to a lower power mode while the HW still processing the menial tasks.  In addition, a well-executed HW state-machine will be faster and with lower latencies.

                     

                    Having said all that, I have taken moto's project and modified it to eliminate the poke_led() and the 500 msec delays using CyDelay(500).

                    I created a HW state-machine that does the blinking for all LEDs that are commanded to turn on.  Therefore the poke_led() and CyDelay() is removed.  This allows the user's input to change the LED states to be almost immediately processed without waiting 500msec from the CyDelay().  (See attached project)

                     

                    The LEDs turned on or off is handled by a Control_register.  All the user does is load the RGB value (R bit 2; G bit 1; B bit 0) into the Control_register and the rest is handled by the HW.

                     

                    I know you indicated you were new to the PSoC ecosystem.  This hopefully is a challenge for you to explore what virtually no other CPU can provide to you:  A more efficient and elegant design!

                     

                    Len

                    1 of 1 people found this helpful
                    • 7. Re: Create a function to light different LEDs
                      Skdk_4669631

                      I want to thank everyone for their complete and kind responses.

                      All these information really helps me. I think I begin to understand the philosophy of PSoC programming once again thank you !

                       

                      ( Sorry, I didn’t  see there was a « Correct » at the bottom of a response )