Passing pins to a function

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hi, does anyone know how to pass a pin to a function? For example, i've created a function called PulseIn() which will behave similar to the arduino function PulseIn() which measures the amount of time for which a pin is high. However, right now I'm only limited to use it with a single pin. I want to expand it to be more flexible where I can pass the pin to the function and it will do its thing based on the desired pin.

   

 

   

Ex: int duration = PulseIn(pinName);

   

 

   

The duration of the pulse for the pin "pinName" will be saved to the int variable duration.

   

 

   

thanks!

0 Likes
1 Solution
Anonymous
Not applicable

So based on the function you're describing I'll assume you're using the read api on the pin.

   

 

   

You can create a function pointer to the read API and then pass that through to your function.  This would keep everything nice and contained, with very little fuss and matches the expectations of other people who use PSoC.  It also isolates your code from hardware changes unlike the per-pin APIs.  That said, you will have to handle if a pin_read call is for more than one pin.

View solution in original post

0 Likes
6 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Yes with a CASE statement or IF statments and pin access APIs.

   

 

   

    

   

          

   

http://www.cypress.com/?rID=93401     AN86439 - PSoC® 4 - Using GPIO Pins

   

http://www.cypress.com/?rID=57571     AN72382 - Using PSoC® 3 and PSoC 5LP GPIO Pins

   

 

   

 

   

Also see system ref guide in help menu, there is a section on GPIO access.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

So based on the function you're describing I'll assume you're using the read api on the pin.

   

 

   

You can create a function pointer to the read API and then pass that through to your function.  This would keep everything nice and contained, with very little fuss and matches the expectations of other people who use PSoC.  It also isolates your code from hardware changes unlike the per-pin APIs.  That said, you will have to handle if a pin_read call is for more than one pin.

0 Likes
Anonymous
Not applicable

 Thanks for the fast responses!

   

I shall try what both of you mentioned tomorrow.

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Here is a good reference that discusses f() call pointers, attached.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

I can't see any attachment with your post. Would you mind posting it again as I would be interested in reading it?  🙂

0 Likes
lock attach
Attachments are accessible only for community members.
odissey1
Level 9
Level 9
First comment on KBA 1000 replies posted 750 replies posted

jimutt,

   

Attached function pointers app. note. Also you can google "function pointer" with many examples.

0 Likes