How to configure button / timer for 2 sec and 5 sec interrupt in PSOC3

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

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello Everyone,

   

 

   

I am started working on the PSOSC 3 EVB i would like to configure timer for 2sec and 5 sec interrupt (button press detection) so i cant able to execute both logic in an simple way so i request can anyone provide me some suggestions or ideas or examples how to configure the timer to detect the button pressed for 2 sec follwed by the interrupt exection and then 5 sec button press followed by timer interrupt  execution any hlep will be greatly aprreciated

   

 

   

Thank you

   

prabhu

   

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

Program a state machine in HandleSwitch as

   

static uint SwitchState = 0;

   

    switch(++SwitchState)

   

    {

   

    case 1: Dothis();

   

        break;

   

    case 2: DoThat()

   

        break;

   

   case 3: DoNothing();

   

        SwitchState = 0;   // Begin new cycle

   

    default:

   

        SwitchState = 0;    //  Recover from unknown state

   

    }

   

 

   

 

   

Bob

View solution in original post

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

Welcome in the forum prabhu!

   

Easiest would be to use 2 timer components, one for 2 seconds delay, the other for 5 seconds.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hello Bob

   

 

   

Thnak you so much for your kind reply i have attached the picture of my timer and switch top design schematics but i didnot able to configure properly 

   

psoc creator3.0 am using to design the top design,

   

in the timer there is period section which accepts max of 256 only but  i did not understand where should i place 2 seconds in the timer component and also do i suppose to use terminal count TC or nothing? where shiould i connect the output from switch? interrupt output should be connected to switch or tc ? please provide me some reference designs or suggestions to do the simple 2 seconds timer example so that i can able to cofigure 5 seconds timer by myself and also i can learn how to configure timer component by your help

   

 

   

Looking forward for your reply

   

 

   

Thank you

   

prabhu

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

prabhu, can you please post your complete project, so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hello Bob

   

Thank you for your kind reply actually what am trying to do in thsi project press the button for 2 seconds and it will turn on the device and then press the button four times whent he button press the reached the fifth press and then it will reset the device could you please chack my project and let me know what should i modify or to do to achieve this specificatiosn

   

 

   

Thank you

   

prabhu

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

Yes, I understood, but where is your project bundle?

   

 

   

Bob

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

yes i have already attached in my top question please check it anyhow i have uploaded once again

   

prabhu

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Avoid lengthy code in interrupt handlers. Especially for the 8051 core this will not work.

   

Use code as I outlined it. Better do not make changes in the generated files. Use _StartEx() instead or the newer macro callback feature.

   

Do not forget to clear the interrupts: The timer needs a status register read to reset the tc flag.

   

Your components are a bit outdated. Use latest Creator 3.3 CP3 and update your components (under Project -> Update...)

   

You may set all your SCREEN_DBn to use one port. Then you can save the control register and write to the port at once.

   

 

   

Happy coding

   

Bob

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

Hello Bob

   

 

   

Thank you so much for your kind reply i did not quite  able to understand your topdesign especially timer 

   

in the timer configuration you set TC bu the TC is not not connected to either switch or anything else and also in the period section it is mentioned 200(unknown source what does it meant by 200?) could you please tell me exact functionality of the timer that you ahve added in the top design because i would like to add the timer for 2 seconds

   

I have modified my program and also the interrupt section  reduce the amount of codes

   

STEP1)while during the switch press it is displaying name in the LCD and during the first time button release it is displaying the LOGO

   

STEP2) while pressing the switch second time it is searching for UMTS and further switch press thirs time started searching the 2G/GSM

   

but the real issue is when i press the switch for thr fourth time it is still searching only 2G networks but i wanted to change the 3G network what is going wrong on my design?

   

mainly the fourth switch press suppose to happen within 5 seconds that is i would like make interrupt function to change from 3G to 2G or 2g/3g within 5 seconds what amm doing wrong in my top design or in my source code could you please cross check and provide me some necessary guidance?

   

i have attached my updated code

   

Thank you

   

prabhu

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

In the timer configuration you set TC bu the TC is not not connected

   

I set the generating of an interrupt to terminal count (TC)

   

The clock for the timer is your 100ms clock. Period is 200, so this will be 100ms*200 = 2s

   

 

   

Switches tend to bounce. Best would be to wait after a switch interrupt happened for 100ms and see if the input is still low. Do not wait in the interrupt handler!!!

   

 

   

I do not understand your 2G/3G mimic, this you will need to solve yourself. Sounds a bit like a state machine which is solved best using a switch statement with cases.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hello Bob 

   

Could you please suggest me some possible best ways to handle the button press based on your guidance i am continuing to proceed further in my project 

   

my questions about switch handling are:
int main()
{
    InitializeSystem();
    for(;;)
    {
            
       if(SwitchPressed)   
        {   
            HandleSwitch();
            SwitchPressed = 0;
        }
                
    }
}

   

Initialized the switch press  like above in the main function  during the switch press  for the first time it is getting executed properly calling the handleswitch() but after the first time i dont want my switch press to call the handleswitch() fucntion  rather i would like call another function so what should i do to handle the switch in this way can you suggest me some ideas? please

   

 

   

Thank you

   

prabhu

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

Program a state machine in HandleSwitch as

   

static uint SwitchState = 0;

   

    switch(++SwitchState)

   

    {

   

    case 1: Dothis();

   

        break;

   

    case 2: DoThat()

   

        break;

   

   case 3: DoNothing();

   

        SwitchState = 0;   // Begin new cycle

   

    default:

   

        SwitchState = 0;    //  Recover from unknown state

   

    }

   

 

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thank you for your reply actually i have did already exactly the same logic as you told but i would like to add the timer for 5 sec when the timer reaches the 5 sec then it should call the interrupt and verify  whether am i case 1 or case 2 (handle switch)then if i am in case 1 or case 2 it should call another function(like yesterday 2g/3g functions)and then clear the timer interrupt in order to that could you propose me some ideas or could you please verify my above attached code please help me am blocked completely

   

 

   

thank you

   

prabhu

   

after the first

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

After a timer is initialized (Enable() API) there are two points of interest:

   

The starting point and the point where the timer is stopped. Stopping can be done in the interrupt handler where additionally the count value should be set to its initial value. So it can be started again. The clearing of the timer interrupt always should be the first thing to do in the handler.

   

In your special case:

   

Make the state variable a volatile global one, so that it can be checked and set by different functions. When you determine "if i am in case 1 or case 2 it should call another function" set the state variable to indicate this situation and react accordingly in HandleSwitch to the new state (take care, in my example I am adding 1 to the state before checking it!)

   

 

   

Bob

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

Hello Bob

   

Thank you for your kind reply since yesterday  i have been working on to implement the logic of 5 Sec Timer and button handler my button is working as expected but the Timer 5sec is not functioning anymore  it is not detecting at the end of 5 sec which case am i either case 1 or case2? could you please verify my timer configuration and my updated program i have added comment on each section it could help you to understand my program please check my timer 5 sec configuration and program 

   

looking forward for your reply 

   

thank you

   

prabhu

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

Your interrupt handler Hundredms() is still full of code, see to execute that from main() after the Flag100ms has been set. This is essential for PSoC3 designs!!!

   

I do not see any timer for 5 seconds, I suggested you to use two timers, one for 2 seconds, one for 5 seconds. Define the places where those timers start and stop, preferably in the state-machine you coded.

   

For Testing:

   

Remove all the unnecessary code from your state-machine. This will allow you to trace the program flow of the state-machine only.

   

 

   

Bob

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

Hello Bob,

   

I have set the timer for 5 sec like what you suggested in the previous message whereas i have set the flag for 100ms handler inorder to run the interrupt continuously every 100ms concerning the Program is working as expected but the timer configuration is not working at all i can not able to find where is going wrong and is ther is any error in my 5 s timer configuration please help me and verify my updated complete project when i press the button it is executing the handler switch function very well but at the end of 5 sec the timer supposed to execute the interrup to check the switch state but it is not happening can you please verify my code ?

   

Thank you

   

prabhu

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

I still can see only one timer. You should use two (2) timers, one configured for 2s and one for 5s.

   

Re-read my previous post and follow all my advices.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Yes i have read your previous posts but i dont want to implement two timers just one timers that is 5 sec after the end of 5 sec iwould like to execute the interrupt by referring the switch state either case 1 or case2  i have tried to execute the global variable in the ISR but the timer is not even functioning  my program is not even able to reach the Timer ISR that is the reason am asking you to verify my timer configuration and TIMER ISR am new to this POSC 3 so i am not able to find where i am making the mistake could you please help me with mu updated program

   

 

   

Prabhu

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

Hello Bob

   

Could you please verify my progrma i am really not able to proceed further into my program i dont know what is wrong in my  timer configurations could you please help me with my new updated program your help could be really hundred times helpful for me being a newbie not able to find where i am going wrong  timer function is not all being gettting executed my program never reached the timer ISR please help me

0 Likes