Newbie program question

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

cross mob
Anonymous
Not applicable

Hi guys,

   

Im just trying to familiarise with the wondeful world of PSoC,

   

I have the PSoC 3210 Eval1 board and ive written a little code that makes an output (connected to an led) go high when an input is low and the output will go low when the input is high (switching off the led)

   

basically i have an output configured which is driven "strong" all the time and its connected via cable to an input which if its high the led will be off and if its low the led will illuminate

   

my code looks a little like this:

   

void main(void)

   

{

   

PRT0DR |= 0X10; //makes P0.4 HIGH, this provides a high signal for the input P0.0

   

while(1)

   

{

   

if (PRT0Dr & 0x01) //checks

   

{

   

PRT1DR &= ~0X01; //toggles output 1.0 which has led connected to show result

   

}

   

else

   

{

   

PRT1DR |= 0X01;

   

}

   

when i power the psoc board up and the input is low the led is on but when i make the input high the output led goes off, the problem is if i make the input low again the led wont illuminate again. I want to be able to make the led go on and off an infinite number of time depending on whether the input is high or low,

   

any help would be appreciated

   

thank you

0 Likes
16 Replies
Anonymous
Not applicable

In my other post I was asking about the led_Read() and led_Write(1) as I wanted to know if i could just use the read and write command to manipulate port pins rather than using the PRT0DR etc command,

   

I also noticed that if i made the input pin "high" resulting in the LED/Output port being "low" and I reset the PSoC chip then when i make the input port low then output port/LED would stay the same and not change.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Hi Cathal,

   

At first sight I can see that you did nor obey the hint of either using a shadow-register or using an LED-component.

   

PRT1DR |= 0x01 is a read-modify-write instruction for a port. The read gets the voltage currently connected to the pin (NOT the value you wrote to the pin the last time) which will be about 1.7V (due to the LED-voltage) which will be interpreted as a logical Zero (0).

   

To circumvent this problem see above: Use a shadow-register or an LED-component.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks Bob,

   

I will take your advice and try using a shadow-register or an LED component,

   

thank you for your assistance

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

Taken from LED datashsheet -

   

 

   

The project using the LED User Module which manipulates pins on a port shared with an instance of the LED

   

User Module must avoid direct PRTxDR writes. The Shadow Registers should be used for such manipulation

   

to prevent incorrect LED User Module operation.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

I was following up on the shadow register guide and video here: http://www.cypress.com/?rID=2900

   

I downloaded the project files and had a look at the sample code for the shadow registers, it seems a bit much for a sample code for all i want my code to do,

   

I followed the video as much as I could understand, I set up a shadow register on port 1 and from the psocconfig.asm file in the source folder I see its called Port_1_Data_Shade

   

I set up a small program that has P1.1 as a constant output, P1.1 as an output to drive and LED for example, and P1.0 as the input which when high will activate the LED on port 1.2.

   

void main(void)

   

{

   

Port_1_Data_Shade |= 0x04;

   

PRT1DR = Port_1_Data_SHADE; // not sure if this required as this should always be high

   

while(1)

   

{

   

if (PRT1DR & 0X01)

   

{

   

Port_1_Data_Shade |= 0x02;

   

PRT1DR = Port_1_Data_Shade;

   

}

   

else

   

{

   

Port_1_Data_SHADE &= ~0x02;

   

PRT1DR = Port_1_Data_SHADE;

   

}

   

This seems to work ok,

   

Any pointers?

   

thanks guys

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given
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
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You need to setup switch debounce for mechanical switches/buttons.
Use Sleep timer, and set up an ISR for it. Inside ISR set a flag,
return, then inc a cnttime variable, looking for ~ > 100 mS key down.
Essentially same for key release.

   


ISRs

   

     
http://www.planetpsoc.com/psoc1-articles-digital/13-basics-of-psoc-gpio.html?start=7
     http://www.planetpsoc.com/psoc1-articles-digital/13-basics-of-psoc-gpio.html?start=6
     http://www.planetpsoc.com/component/content/article/43-writing-a-c-isr.html

   

 

   


Regards, Dana.

0 Likes
Anonymous
Not applicable

Hi Dana,

   

Thanks for your reply, I am using a switch for now but eventually the signal will be a digital input, in that application with a digital output can i still use stdCPU and strong drive to make the output high and can the output still be stdCPU in pulldown mode to toggle an LED or something on another output. I plan on not using a mechanical switch and just stick to using a wire which when cut or damaged will illuminate an led

   

thanks

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

If you are driving an LED normally you drive it with low side MOSFET in the

   

output (on when output low), open drain, or just STDCPU is fine as well. Driving

   

with low side MOSFET is a lower RDSon device than high side device, due to

   

source bulk effect affecting high side MOSFET threshold.

   

 

   

So preferred drive is LED to port, other side thru current limit R to Vdd.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Hi Dana,

   

Thanks for your input, very helpful indeed,

   

I have the output set to strong at the moment and the LEDs have a current limiting resistor before them,

   

is strong ok if i want to make an output high to drive a transistor to say trigger a relay?

   

thanks very much

0 Likes
Rolf_Nooteboom
Level 5
Level 5
10 sign-ins 5 solutions authored First solution authored

 Hi Cathal,

   

 

   

You may use strong or strong-slow to drive the transistor. Use strong if you need fast switching, use strong-slow if you have EMC issues.

   

 

   

Regards,

   

Rolf

0 Likes
Anonymous
Not applicable

Hi Rolf,

   

Thanks so much for your useful info,

   

The diagram is excellent, thank you for that,

   

My own wiring diagram is the same as that although nowhere near as tidy. lol

   

thanks for your help, very helpful community here

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

To design see attached -

   

 

   

Regards, Dana.

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

A couple of additions -

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Hi Dana,

   

Thanks for your reply, Ive had a look through you GPIO driver circuit diagram, its very informative.

   

Thanks for your assistance, I really appreciate it,

0 Likes