Counter circuit

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

cross mob
Anonymous
Not applicable

 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

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

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

 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

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

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.

0 Likes
Anonymous
Not applicable

 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.

0 Likes
Anonymous
Not applicable

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

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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).

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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...)

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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

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

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.

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

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.

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

   

 

   

I would just check off Positive edge only.

   

 

   

Regards, Dana.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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...

0 Likes
Anonymous
Not applicable

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).

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

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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?

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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...

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

@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).

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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?

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

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

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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;

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

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

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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

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

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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. 

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

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

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

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())
        {

0 Likes
Anonymous
Not applicable

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);

0 Likes
Anonymous
Not applicable

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

0 Likes
Anonymous
Not applicable
0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

code is not properly paste. I send program

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

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

   

error trying to download.

   

 

   

Regards, Dana.

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

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

   

 

   

Bob

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

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.

0 Likes
Anonymous
Not applicable

 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?

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored
        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.)   
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi,

   

I try to control timer with capsense. When i pressed button timer starting ten second then lcd writing "stop". But I want to do this again does not it. Timer is working only once but When I always press the start   I want  to run the timer.What should I change the program?

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

I have one button raising the display Period, the 2'ond button lowering it.

   

Both when touched, seperately, show "On" on display, as code predicts.

   

 

   

What do you want the third button to do ?

   

 

   

Regards, Dana.

0 Likes