Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > PSoC® 3 > Counter circuit

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Counter circuit
Moderator:
RKRM

Post Reply
Follow this topic



Counter circuit

TURASGAS posted on 06 May 2013 6:41 AM PST
Senior Member
14 Forum Posts

 Hi,

I want to make the counter to be used in the circuit design my project.Example, Counter count 30 minute and then led  is on.What should I use component this circuit and How should I write a code. I find about itin forum, but I could not find an example forum.Can you  show me how to follow in the way




Re: Counter circuit

danaaknight posted on 06 May 2013 07:08 AM PST
Top Contributor
1773 Forum Posts

You can do this several ways.

 

1) Place a counter on schematic, say fixed 16 bit, and  clock source and an ISR symbol to generate

an interrupt.

 

Config counter as 1800 period, 60 secs x 30 minutes, interrupt on Tc. Place clock, sournce ILO (1 Khz),

and set its freq to 1 Hz. Note ILO has to be trimmed otherwise accuracy marginal. Or use buss clock,

its accuracy is +/- 5% (depends on what freq you clock with). ILO trimmed is - 50%, + 100%, or use 32

Khz xtal. Then in ISR flip pin that is driving LED.

 

2) Use RTC and set an alarm for every 30 minutes.

 

Regards, Dana.



Re: Counter circuit

TURASGAS posted on 07 May 2013 11:34 PM PST
Senior Member
14 Forum Posts

 Hi Dana,

I want to set the time exchanging, Can I use this in RTC? I will set time with the button maybe 5 minute,then countdown 5 minutes  led on. So, can i set the time with the button?How do you need to write a program for it? Thanks for answers



Re: Counter circuit

danaaknight posted on 07 May 2013 02:58 AM PST
Top Contributor
1773 Forum Posts

Answer is yes, you can change time with a button(s). Usually a button for

up, one for down, another for menu select.

 

1) Buttons would have to be debounced, generally in firmware by polling the

port with the pins in a for() or while() loop.

2) You debounce a button by detecting first closure, then timing out a period, on the order of

100 mS, and checking to see its still closed. If it is and doneflag == 0 then you do the function

assigned to that pin/button and set doneflag==1. Keep in mind you also have to debounce the

button open, and when it is open reset doneflag==0. The 100 mS can be done with a timer or

counter, and an ISR to inc a variable every 100 mS. 100 mS is switch dependent, but should

be OK for most switches.

 

Some debouce info that may be of help.

 

Regards, Dana.



Re: Counter circuit

H L posted on 07 May 2013 04:55 AM PST
Top Contributor
679 Forum Posts

 you can also use the debounce component from creator as well. It's all up to you. For simple test of idea, the hardware component would be quicker.



Re: Counter circuit

TURASGAS posted on 07 May 2013 05:00 AM PST
Senior Member
14 Forum Posts

 I examined documents. I'm trying to do something. if there's an example Can you send me?



Re: Counter circuit

hli posted on 07 May 2013 06:58 AM PST
Top Contributor
675 Forum Posts

Are you looking for a already finished solution to your problem?

Try to play around with the components. Look at the example projects available with the kits. Use a debouncer component to just drive a LED. Use the timer component to drive a LED (let it just blink). Then combine the two. It is not that complicated :)

If you have a question regarding a specific problem, don't hesitate to ask here (but open a new thread for new questions, please).



Re: Counter circuit

TURASGAS posted on 07 May 2013 07:37 AM PST
Senior Member
14 Forum Posts

I did some trials but I do not know how to connect debounce and timer maybe program is incorrect.I'd love can look at it



Re: Counter circuit

hli posted on 07 May 2013 11:32 AM PST
Top Contributor
675 Forum Posts

First, you could update Creator to the latest version, I was asked to update some components (but this should not be the source of your problems).

Second, since your button is connected to ground, you might want to use 'neg' output of the debouncer to detect button presses  instead of using the NOT gate inbetween.

But most important, remove the first for-loop from your code - I think it gets stuck there instead of going to the second one containing your real code...

The main loop looks OK at a first glance, but you never update the 'temp1' variable. But you should not write an integer starting at column 14 - it might overflow (when it has more than 2 digits).

I saw that the LED pin you draw in the schematic is assigned to P6[7], but in the 'test' ISR you change P6[3]. You should store the current state of the LED in a variable (e.g. a boolean) which gets toggled every ISR call and determines the output state. This is safer than directly reading the pin (e.g. you write a 1 to the pin, it starts to rise to 1, in the next ISR call you read the pin but get a 0 since it has not risen enough...)

 

 



Re: Counter circuit

hli posted on 07 May 2013 11:33 AM PST
Top Contributor
675 Forum Posts

btw: there is an AppNote regarding the debouncer: http://www.cypress.com/?rID=40974 which you might want to read besides the data sheet.



Re: Counter circuit

danaaknight posted on 07 May 2013 11:54 AM PST
Top Contributor
1773 Forum Posts

No small number of training videos here -

 

www.cypress.com/    Take PSOC 3 and 5 101 thru 104.

 

Almost all components, when placed on a schematic, if you right click

them, have a menu selection "find example project" than you can add

to the design you are working on, or open in a new design window to

look at.

 

Regards, Dana.

 

 

 

 



Re: Counter circuit

danaaknight posted on 07 May 2013 12:20 PM PST
Top Contributor
1773 Forum Posts

You are corerct to use the inverter, per AN60024 -

 

 

I would recommend setting clk to debouncer as 10 Hz, ie wait 100 mS

for all sw bounce to expire.

 

Add this to start of main() -

 

    /* Enable global interrupts */  
    CYGlobalIntEnable;

 

Regards, Dana.

 



Re: Counter circuit

danaaknight posted on 07 May 2013 12:22 PM PST
Top Contributor
1773 Forum Posts

 

I would just check off Positive edge only.

 

Regards, Dana.



Re: Counter circuit

hli posted on 08 May 2013 12:05 AM PST
Top Contributor
675 Forum Posts

But this one-irq-too-much problem only happens if interrupts are already enabled when starting the debouncer component.

The reason I suggested to remove the inverter is that, right now, it iss a double negation. First the active-low button signal is converted to an active-high signal, and then the ISR is set to react on a rising flank - whereas in reality a falling flank is meant (to detect the button press). In my experience this always has great potential to confuse people...



Re: Counter circuit

H L posted on 08 May 2013 01:24 AM PST
Top Contributor
679 Forum Posts

If the button has a pull up resistor and connect to ground when press.

I will use the input directly to the debouncer, and only generate pulse on negative edge.

That means a (high) pulse will generate every time the button is pressed(connected to ground).



Re: Counter circuit

danaaknight posted on 08 May 2013 04:41 AM PST
Top Contributor
1773 Forum Posts

The de-bouncer is random clocked logic, has no start f().

 

 

To your point HLI, what I can't tell is does CYGlobalIntEnable clear all pending interrupts

when it enables the controller. I cannot find the macro expansion in the code base.

 

Regards, Dana.

 

 

 

 



Re: Counter circuit

TURASGAS posted on 08 May 2013 05:20 AM PST
Senior Member
14 Forum Posts

Recently I tried this circuit. But ı have some errors program. timer is counting but i'm not controlling capsense button. I want to do the following;

http://www.youtube.com/watch?v=zGXTFTYZi_o this video 2.20 sec between 2.45sec absolutly this. What do I need to do that?



Re: Counter circuit

hli posted on 08 May 2013 05:21 AM PST
Top Contributor
675 Forum Posts

Now you got me :)

I didn't think of the pending interrupts. The only hint I could find in the PSoC3 TRM is in the glossary, where a pending interrupt is defined as "an interrupt that is triggered but not serviced because ... global interrupts are disabled". So this means the interrupt would be served when global interrupts are enabled. But on the other hand - the ISR component won't be started at this moment, so the interrupt is not enabled (even though the debouncer runs already).

Its interesting that the data sheet doesn't contain this hint, only the AppNote does. (And the example project also uses the inverter). And I have no PSoC board with me to check whether it is a real issue or not...



Re: Counter circuit

hli posted on 08 May 2013 05:41 AM PST
Top Contributor
675 Forum Posts

@turasgas: what are the errors you get?

I note that when I compile the project, I get errors about some function which are not thread-safe ('Multiple call to function'). This should be corrected since it may lead to strange errors during ISR execution.

Your code doesn't check for the button as such, only whether some CapSense widget is active (since you only have one button in the config, this might be OK, though).

But the rest of the project is strange:

  • the timer runs continously
  • but no interrupts are activated on it - this is done in software only (this works, but is unusual)
  • the LED should blink with 0.5 Hz
  • the timer is never stopped, the main loop only calls start() when it detects a button press (and before that it resets the timer via hardware

btw: I cannot watch the Youtube video because it is blocked in Germany (Youtube thinks that it has no permissions to play the music over here).



Re: Counter circuit

TURASGAS posted on 08 May 2013 05:43 AM PST
Senior Member
14 Forum Posts

I recently tried this circuit. But ı have some errors program. timer is counting but i'm not controlling capsense button. I want to do the following;

http://www.youtube.com/watch?v=zGXTFTYZi_o this video 2.20 sec between 2.45sec absolutly this. What do I need to do that?



Re: Counter circuit

hli posted on 08 May 2013 12:16 PM PST
Top Contributor
675 Forum Posts

Repeating the question doesn't help me answering it...



Re: Counter circuit

TURASGAS posted on 09 May 2013 01:04 AM PST
Senior Member
14 Forum Posts

I can set timer with program. but i must set timer with capsense buttons.

for

(;;)if(flag == 5)

// ++count;

Pin_Output_Write(ON);

CyDelay(1000u);

Pin_Output_Write(OFF);

 

}

 

}

flag == 5 sn.  i want to decrease or increase  capsense buttons not program. How do I make it?

{

 

 

{

 

Motor_Write(0x01);

CyDelayUs(2300);

Motor_Write(0x00);

 

flag = 0;



Re: Counter circuit

Bob Marlowe posted on 09 May 2013 02:09 AM PST
Top Contributor
1768 Forum Posts

Sorry, but I can see no CapSense button in your project at all. I suggest you open an example project for CapSense and record the button settings and connections, then with a cut & paste insert that into your project and make the needed connections. Test if you can get the CapSense to work, then integrate it into your current project containing the timer.

 

AND: Update your Creator 2.2 to the latest version (SP1)

 

Happy coding

Bob



Re: Counter circuit

TURASGAS posted on 09 May 2013 04:00 AM PST
Senior Member
14 Forum Posts

I add capsense buttons but i hava a problem. Can you see that?



Re: Counter circuit

danaaknight posted on 09 May 2013 05:02 AM PST
Top Contributor
1773 Forum Posts

When you enter CapSense_DisplayState() the first thing you do is

 

if ( CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN))

 

Therefore if any other button is pressed they are not processed.

 

Regards, Dana.



Re: Counter circuit

TURASGAS posted on 10 May 2013 12:59 AM PST
Senior Member
14 Forum Posts

Curcuit is working but i hava a small mistake. Program is working with switch button but not it not working capsense button. Maybe there is error capsense program setting. Can you help me,thanks. 



Re: Counter circuit

Bob Marlowe posted on 10 May 2013 02:51 AM PST
Top Contributor
1768 Forum Posts

You did not tell us which development kit you use. Probably the connection for the CapSense CmodCH0 - pin is not as requried (pin P6_4)  or the buttons not on P5_5 and P5_6. I would suggest you to check with the schematic of your kit.

 

Bob

 



Re: Counter circuit

danaaknight posted on 10 May 2013 03:17 AM PST
Top Contributor
1773 Forum Posts

I would change this -

    while(1)
    {
   
        /* If scanning is completed update the baseline count and check if sensor is active */ 
        if(!CapSense_IsBusy())
        {
            /* Update baseline for all the sensors */
            CapSense_UpdateEnabledBaselines();
            CapSense_ScanEnabledWidgets();

 

to this

 

    while(1)
    {
   

            /* Update baseline for all the sensors */
            CapSense_UpdateEnabledBaselines();
            CapSense_ScanEnabledWidgets();

        if(!CapSense_IsBusy())
        {



Re: Counter circuit

TURASGAS posted on 10 May 2013 05:16 AM PST
Senior Member
14 Forum Posts

I found the problem,thanks. I am using cy8ckit001 and cy8ckit-010. I asked you lots of question thanks, but I  have a  last question. I have a problem set button. There are thre button.First button decrease period of timer.Second button increase period of timer.I set the timer when third button pressed and will decrease period every second. Then time will be zero consist of  interrupt. I am using these code:

int

timerperiod = 200;

if

 

( statusButton0 == ON )"On ");if(timerperiod <=1)

// Pin_1_Write(ON);

}

 

else

{

LCD_PrintString(

Pin_Output_Write(OFF);

}

 

"Off");/* Display button 1 status on LCD */

LCD_Position(1,5);

 

{

LCD_PrintString(

++timerperiod;

 

timerperiod =500;

Timer_1_WritePeriod(timerperiod);

Pin_Output_Write(ON);

}

 

if( statusButton1 == ON )"On ");if(timerperiod >=500)else

{

LCD_PrintString(

Pin_Output_Write(OFF);

}

 

 

{

 

 

 How should I enter a code here???

 

}

"Off");if( statusButton2 == ON )

 

{

LCD_PrintString(

Pin_Output_Write(ON);

--timerperiod;

 

timerperiod =1;

Timer_1_WritePeriod(timerperiod);



Re: Counter circuit

TURASGAS posted on 10 May 2013 05:20 AM PST
Senior Member
14 Forum Posts

Sorry first code is error. this code is correct

I found the problem,thanks. I am using cy8ckit001 and cy8ckit-010. I asked you lots of question thanks, but I have a last question. I have a problem set button. There are thre button.First button decrease period of timer.Second button increase period of timer.I set the timer when third button pressed and will decrease period every second. Then time will be zero consist of interrupt. I am using these code:

timerperiod = 200;

if

{

LCD_PrintString(

Pin_Output_Write(ON);

--timerperiod;

 

timerperiod =1;

Timer_1_WritePeriod(timerperiod);

( statusButton0 == ON )"On ");if(timerperiod <=1)

// Pin_1_Write(ON);

}

 

else

{

LCD_PrintString(

Pin_Output_Write(OFF);

}

 

"Off");/* Display button 1 status on LCD */

LCD_Position(1,5);

 

{

LCD_PrintString(

++timerperiod;

 

timerperiod =500;

Timer_1_WritePeriod(timerperiod);

Pin_Output_Write(ON);

}

 

if( statusButton1 == ON )"On ");if(timerperiod >=500)else

{

LCD_PrintString(

Pin_Output_Write(OFF);

}

 

 

{

 

How should I enter a code here???

}

"Off");if( statusButton2 == ON )

int



Re: Counter circuit

TURASGAS posted on 10 May 2013 05:21 AM PST
Senior Member
14 Forum Posts


Re: Counter circuit

TURASGAS posted on 10 May 2013 05:28 AM PST
Senior Member
14 Forum Posts

code is not properly paste. I send program



Re: Counter circuit

danaaknight posted on 10 May 2013 08:29 AM PST
Top Contributor
1773 Forum Posts

Post the program bundle again, for some reason getting a server

error trying to download.

 

Regards, Dana.



Re: Counter circuit

Bob Marlowe posted on 10 May 2013 09:07 AM PST
Top Contributor
1768 Forum Posts

No problems in downloading now, I suggest to try again...

 

Bob



Re: Counter circuit

danaaknight posted on 11 May 2013 02:45 PM PST
Top Contributor
1773 Forum Posts

I could not get the basic capsense operating correctly, so I took

an example CSD project, and merged your code into it. I used

a -030 DVK to run it. I used one element of the slider to create

the third button, but did not tune it, or use it functionally.

 

I am not sure what additional code functionality you want, but

maybe this will help.

 

Regards, Dana.



Re: Counter circuit

TURASGAS posted on 13 May 2013 04:16 AM PST
Senior Member
14 Forum Posts

 Hi,

When I pressed capsense button timer decreasing one by one.if you are pressed capsense buttonfor a long time, i want to increase ten by ten. I tried something but it did not.How to code it can be?



Re: Counter circuit

hli posted on 13 May 2013 04:44 AM PST
Top Contributor
675 Forum Posts
You can just count how often the loop checking for button presses has been executed, and increase the step size after a given number of times. (Don't expect code samples for all of your question, we are not here to solve homework type questions. Please try to solve your problem first by yourself, and if something doesn't work please ask about your problems. Thanks.)

Re: Counter circuit

danaaknight posted on 13 May 2013 07:22 AM PST
Top Contributor
1773 Forum Posts

When the button is first pressed set a flag, and start a Timebuttonpressed counter running, say at a 1 Hz rate.

If counter = 5 sec then change the increment or multiplier used for write to x 10, if Timebuttonpressed

gets to ~ 10 sec test and make incerment/multiplier x 100. If at any time button is released

reset increment/multiplier to 1 and clear Timebuttonpressed counter.

 

Note the x 100 step is just to show you the longer button pressed, the greater is the step size.

Don't use it if you don't need it.

 

Regards, Dana.






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20