- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi, I am new to this development kit and I just want to start a simple project.
However I have some questions.
I just want to make a simple try with digital input and digitat output.
In the .cysch file I named 2 switches as input (Pin1,Pin2 ) and 4 LEDs as output (Po1, Po2, Po3, Po4)
I want to program that if different pattern of 2 switches are pressed, different pattern of LEDs light on.
I have set the input pins to P1[4]/P1[5], and output pins to P1[0]/P1[1]/P1[2]/P1[3] in the .cydwr file
However I don't know how to start on the .c file. I know I need to type in my "if" case but
how should I define the pins?
e.g. If p1[4] is off and p1[5] is on, P1[0]/P1[1]P1[2]/P1[3] should be off/off/off/on
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Normally you double click any component and configure its
settings, including GPIO. Also you will see in its datasheet
(right click component) APIs to make runtime changes to
GPIO pins.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Here is an ap note on GPIO -
http://www.cypress.com/?rID=57571 AN72383
Also notice at bottom of link page there is another relative ap note.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You may use functions as Pin1_Read() to read a switch and Po1_Write() to let an LED shine.
have a look into the pin's datasheet, APIs.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Observe also 200 mA limit on Iddd for GPIO.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the kindly supports. I made it!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The code described in the app note mentioned above does not work for me on PSoC4 pioneer board.
I tried to use it to control one of the Leds (P0_3) with the user button (P0_7).
Any ideas?
http://www.cypress.com/?docID=42883
"Use the component APIs to set the state of OutputPin based on InputPin, as shown here:
for(;;)
{
/* Set OutputPin state to the
inverse of the InputPin state */
OutputPin_Write( ~InputPin_Read() );
}
The result is that OutputPin is always at the opposite state as InputPin."
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The ~ operator creates the complement of what was read on the
input port. But the bit position of the input pin is not same as LED
pin, so code functionaly does not work.
You have to use a mask to test input pin bit, then based on its state drive
LED to its respective on/off state. The Pioneer board turns on LEDs by
driving pin to ground, logic "0".
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
One other suggestion, when controling pins in a port,
generally you only modify the pin of interest. The code you
showed would keep toggling all pins.
So you use a mask on both reading the button and another mask to
write to the output pin.
In your case the basic masks look like P0_3 0b00001000 and
P0_7 0b10000000. So you would AND the mask to test the input
port pin, and either OR the output mask or AND the complement of
the output mask to set/reset the output bit.
Regards, Dana.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
As far as I can see the Pin_Read() function returns either 0x00 or 0x01 for a single pin component. So the Pin_Write() function should accept it without errors and work properly.
However, there might be a pitfall with interrupts, because interrupts are not disabled when calling those functions, so at least the values inside the Pin_Write() function might get corrupted when interrupts are modifying pins on the same port.
Or am I missinterpreting the Pin_Write() function?
Regards,
Ralf
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You are quite right, Ralf. When from two different points the port data-register is accessed and one of them is within an interrupt routine the situation exists that something might go wrong. This is what the CyEnterCriticalSection() is good for, look into the System Reference Guide.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Bob,
I wonder if CyEnterCriticalSection() / CyExitCriticalSection() should be called inside the pin component APIs as default. Yes, the pin component datasheet states explicitely that it's not safe to call the component API especially when ports are accessed within interrupts. And yes, it recommends that the per-pin-APIs should be used instead, which seems to be macros evaluated to atomic access instructions.
But, IMHO the question is if the software developer should always keep track which pins/ports might be affected by this. If only single pins are used, there's no difference and I agree that the per-pin-API should be preferred. However, if someone needs to output a 4-bit value for example, the per-pin-API can't be used (or it must be done bit by bit). The PSoC approach is to have independend functions, but the above seems to violate this approach.
What do you think?
Regards,
Ralf