CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Push Button Response using RTOS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
waheed



Joined: 19 May 2012
Posts: 26
Location: Pakistan

View user's profile Send private message

Push Button Response using RTOS
PostPosted: Sat May 19, 2012 12:28 am     Reply with quote

I just want to increase the count on the GLCD when a button is pressed. I have written the code and it works fine.
But if I press the button continuously, it misses some inputs. I want to make it smooth and need help doing that.
Here is the code:
Code:

#use rtos(timer=0, minor_cycle=100ms)

#task (rate=500ms, max=10ms)
void Button()
{
if (!input(PIN_A4))
   {
    dt_lcd_gotoxy(100,2);printf(dt_lcd_printchar,"%Lu",timer0);
    timer0++;
   }
}

There are other tasks running too! Their frequency is:
Code:

#task (rate=500ms, max=10ms)
void Sel_ADC_Channel()


#task (rate=100ms, max=40ms)
void Get_voltage()


#task (rate=1s, max=100ms)
void Update LCD()
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat May 19, 2012 3:17 am     Reply with quote

What do you mean by this?
Quote:

But if I press the button continuously,

Are you:-

(1) Pressing the button repeatedly at a high rate?
OR
(2) Pressing the button down and holding it down?

Mike
waheed



Joined: 19 May 2012
Posts: 26
Location: Pakistan

View user's profile Send private message

PostPosted: Sat May 19, 2012 4:10 am     Reply with quote

It means "Pressing the button repeatedly at higher rate".
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sat May 19, 2012 5:25 am     Reply with quote

You are you only looking at your button once per 500ms. That's far too slow.

Speed up the rate at which you look at the button.

You may then encounter other issues, like getting extra counts if the button is held down too long.

Reduce the button code to the minimum and do the lcd handling part elsewhere.

OR use an interrupt timer to poll the button.

OR use put the button on port B and use change on port B to generate an interrupt.

Mike
waheed



Joined: 19 May 2012
Posts: 26
Location: Pakistan

View user's profile Send private message

PostPosted: Sat May 19, 2012 6:46 am     Reply with quote

I have already tried speeding up the rate and yes, it gets extra counts if the button is held down too long.

Quote:

use an interrupt timer to poll the button.


Is it possible to use both RTOS and interrupts in the same program?
I have tried doing that but interrupt didn't run and gave the following warning:

Interrupt disabled to avoid re-entrancy[@delay_ms].
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun May 20, 2012 2:55 am     Reply with quote

OK. I don't see any real reason why you can't have RTOS and interrupts together.

You may get re-entrance problems if you use delay_ms(xx) inside an ISR. I would not expect that to be a specific RTOS issue. Also using delay_ms(xx) inside an ISR is a definite NO-NO.

CCS explains that your setup selects which timer is used to drive the RTOS. Presumably that timer is then not available to you.


However before going any further you need to define the requirements for your button system.

I'd be looking at your switch waveforms with a 'scope.

This will tell you what kind of a signal you're dealing with over a range of conditions (Slow deliberate presses, quick single presses, rapid repeated presses etc.).

Then you can work out what your software has to handle and the response you want.

The questions you have to answer include:-

How often do you HAVE to poll the switch state to guarantee NOT missing a button press?
How do you deal with switch bounce?
What response do you want for a long key press?

I work on this basis:-

(1) Sample at 10ms intervals.
(2) If a key is closed for two successive samples I've got a valid closure.
(3) If a key is open for two successive samples I've got a valid release.

I don't know if this is overkill or not, but it's served me well for decades.

Can you get away with less frequent sampling?
Can your RTOS then operate fast enough?

Mike
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group