12 Replies Latest reply on Feb 7, 2018 7:07 AM by user_476468517

    How do I read individual pins from a grouped pin block?

    user_476468517

      Hello PSOC people!

       

      I am currently working on a project which requires at least 16 pin interrupts, and I have been trying to implement this. 

       

      The idea is that a set of pins are grouped in one component (number of pins = 7 in the topdesign file) , and they share an ISR which will check the previous values across these pins against the current logic levels.
      My 'block' of pins is labeled 'Pin_D' and I would assume that to read the pins it would be as simple as using  "value = Pin_D_N_Read();", where N is the pin number I want to reference but when I try to do this I get an "Implicit declaration of function 'Pin_D_N_Read' is invalid in c99" warning.


      Am I doing something wrong or should I just ignore this warning and pray that it doesn't come back to haunt me later?

        • 1. Re: How do I read individual pins from a grouped pin block?
          bob.marlowe

          Easiest when reading digital pins will be

          Pin_D_Read() & (0x01 << PinNumber)

          which will return FALSE when the selected pin is low.

           

          Bob

          1 of 1 people found this helpful
          • 2. Re: How do I read individual pins from a grouped pin block?
            user_476468517

            Yep this works great!

             

            Heads up to anyone else trying this, if you test this method with something like a switch on just a single pin , if you move the wire connected to the switch and connect it to the next pin in the group your PSOC may interpret button presses as happening on the original socket due to what I can assume is some kind of coupling effect. Stablising the line by connecting it to circuitry resolves this behaviour.

            • 3. Re: How do I read individual pins from a grouped pin block?
              bob.marlowe

              Floating pins are always prone to changes due to stray capacitance influence.  Pins connected to switches should be defined as resistive pull up and the switch should close to GND. Safest solution.

               

              Bob

              1 of 1 people found this helpful
              • 4. Re: How do I read individual pins from a grouped pin block?
                user_476468517

                So I currently have the following code called by an interrupt.
                Pins 0-6 are grouped and 7 is on its own.  The function successfully switches on a debug LED after this is done so I know this function runs.
                I am expecting whatever combination of buttons I hold to be be reflected in the LEDs, but only LED 0 and 7 seem to respond.
                I have also tried shifting the result of the logic operation to the right by the number of bits they are shifted to the left but to no avail.

                 

                         uint8 rawDigital = Pin_D_Read();

                   

                         moduleStatusD[0] = rawDigital & (0x00000001);

                         moduleStatusD[1] = rawDigital & (0x01 << 1);

                         moduleStatusD[2] = rawDigital & (0x01 << 2);

                         moduleStatusD[3] = rawDigital & (0x01 << 3);

                         moduleStatusD[4] = rawDigital & (0x01 << 4);

                         moduleStatusD[5] = rawDigital & (0x01 << 5);

                         moduleStatusD[6] = rawDigital & (0x01 << 6);

                         moduleStatusD[7] = Pin_D_7_Read();

                   

                         LED_Extern_0_Write(moduleStatusD[0]);

                         LED_Extern_1_Write(moduleStatusD[1]);

                         LED_Extern_2_Write(moduleStatusD[2]);

                         LED_Extern_3_Write(moduleStatusD[3]);

                   

                         LED_Extern_4_Write(moduleStatusD[4]);

                         LED_Extern_5_Write(moduleStatusD[5]);

                         LED_Extern_6_Write(moduleStatusD[6]);

                         LED_Extern_7_Write(moduleStatusD[7]);

                • 5. Re: How do I read individual pins from a grouped pin block?
                  user_476468517

                  how do I get each LED to reflect the button state?

                  • 6. Re: How do I read individual pins from a grouped pin block?
                    user_476468517

                    Fixed:

                     

                     

                     

                    uint8 rawDigital = Pin_D_Read();

                       

                        if ( (rawDigital & (0b00000001) )  == 0b00000001){moduleStatusD[0] = 1;}

                        else {moduleStatusD[0] = 0;}

                        if  ( (rawDigital & (0b00000010) ) == 0b00000010){moduleStatusD[1] = 1;}

                        else {moduleStatusD[1] = 0;}

                        if  ( (rawDigital & (0b00000100) ) == 0b00000100){moduleStatusD[2] = 1;}

                        else {moduleStatusD[2] = 0;}

                        if  ( (rawDigital & (0b00001000) ) == 0b00001000){moduleStatusD[3] = 1;}

                        else {moduleStatusD[3] = 0;}

                        if  ( (rawDigital & (0b00010000) ) == 0b00010000){moduleStatusD[4] = 1;}

                        else {moduleStatusD[4] = 0;}

                        if  ( (rawDigital & (0b00100000) ) == 0b00100000){moduleStatusD[5] = 1;}

                        else {moduleStatusD[5] = 0;}

                        if  ( (rawDigital & (0b01000000) ) == 0b01000000){moduleStatusD[6] = 1;}

                        else {moduleStatusD[6] = 0;}

                        moduleStatusD[7] = Pin_D_7_Read();

                       

                       

                        LED_Extern_0_Write(moduleStatusD[0]);

                        LED_Extern_1_Write(moduleStatusD[1]);

                        LED_Extern_2_Write(moduleStatusD[2]);

                        LED_Extern_3_Write(moduleStatusD[3]);

                       

                        LED_Extern_4_Write(moduleStatusD[4]);

                        LED_Extern_5_Write(moduleStatusD[5]);

                        LED_Extern_6_Write(moduleStatusD[6]);

                        LED_Extern_7_Write(moduleStatusD[7]);

                    • 7. Re: How do I read individual pins from a grouped pin block?
                      bob.marlowe

                      You could try something like

                      uint8 Mask = 0x01;

                      uint8 ii;

                      uint8 rawDigital = Pin_D_Read();

                      for(ii = 0; ii < 6;ii++)

                      {

                          moduleStatus[ii] = (rawDigital & Mask )  == Mask:

                          Mask = Mask << 1;

                      }

                      moduleStatusD[7] = Pin_D_7_Read();

                       

                       

                      Bob   

                      • 8. Re: How do I read individual pins from a grouped pin block?
                        user_476468517

                        Having some weird issues using this with Timer interrupts.
                        When I use this method to read the status of the buttons normally or from a hardware interrupt it works fine, However when using it within an interrupt triggered by a timer the status of pin 0 is not recognised.
                        If I trigger the interrupt by pressing another button whilst holding button 0 the state of button 0 is registered as normal.

                        • 9. Re: How do I read individual pins from a grouped pin block?
                          bob.marlowe

                          Wasn't there a restriction to max. 7 pins of a port + interrupt? I think I can remember...

                           

                          Bob

                          • 10. Re: How do I read individual pins from a grouped pin block?
                            user_476468517

                            Sorry that previous message was code spangled brain slop.


                            What I meant to say is:

                             

                            1) When a button is pressed a variable is set stating that this button is currently being debounced.  (Works fine)
                            2) if it is not already running a timer is started (works fine)
                            3) every ms (I have tried various other rates) all of the buttons are sampled.
                            4) if 3 matching samples in a row are found the state is considered to be open or closed

                            5) Button state is sent over I2C

                             

                            The problem seems to creep in at step 4.
                            I have tried having LEDs light up to reflect the button state.

                             

                            Button 0 - Does nothing
                            Button 1 - 50% chance of packet received light toggling
                            Button 2 - Main unit correctly updates to display button state about 90% of the time
                            button 3 - Like Button 2 but only works about 75% of the time
                            button 4 - 7: Like button 2 but only approximately 60% of transitions are picked up and main unit always sees them as unpressed.

                             

                             

                             

                            I'm pretty sure it's not my I2C function as I am sending 3 bytes at a time, the middle byte being the status of the button, either 255u or 0u (forced values, not 'read')
                            The first and third bytes are always perfect (displaying on a screen), but the second byte just seems to get 0 no matter what (except with buttons 2 and 3).

                             

                            Would anyone be willing to take a look at my code if I make a workspace bundle and forward it to you?
                            I don't particularly want to make all of it public in this case.

                            • 11. Re: How do I read individual pins from a grouped pin block?
                              bob.marlowe

                              Can you please post your complete project or a shortened version that shows the error so that we all can have a look at all of your settings. To do so, use

                              Creator->File->Create Workspace Bundle (minimal)

                              and attach the resulting file.

                              Which kind of board do you use? Self made? Cypress Kit Cy8CKITxxxx?? Which one???

                               

                               

                              Bob

                              • 12. Re: How do I read individual pins from a grouped pin block?
                                user_476468517

                                I actually seem to have fixed this since, I believe it was due to the allocated size of my stack, and was resolved when the stack size was increased.