> -----Original Message----- > From: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] On Behalf > Of Rikard Bosnjakovic > Sent: 01 April 2009 18:33 > To: Microcontroller discussion list - Public. > Subject: Re: [PIC] One button, three purposes > > On Wed, Apr 1, 2009 at 19:11, Michael Rigby-Jones > > > Just to clarify, are you using the overflow counter to check how long > > the button is pressed, > > Correct. The overflow-variable counts the Timer0-overflows (which > starts counting as soon as the button/RA0 is pressed). > > > so a short press starts or stops the timer, and a > > long press resets it? > > Yes, this is my intention which I'm unsuccessfull with. To clarify > even more: "timer" here means Timer1 (external 32kHz-crystal). For this example it might be easier to check the button in the interrupt handler e.g. in pseudo code: ' main loop do if short_press = 1 then ' toggle timer state TMR1ON = 1 ^ TMR1ON ' short press now serviced short_press = 0 end if if long_press = 1 then ' is timer running? if TMR1ON = 0 then ' timer not running so reset it TMR1H = 0 TMR1L = 0 end if ' long press now serviced long_press = 0 end if loop interrupt: ' button pressed if RA0 = 1 then overflows = overflows + 1 if overflows = 50 then ' flag a long button press long_press = 1 else if overflows > 50 then ' stop overflow counter rolling over overflows = 51 end if ' button released else ' test for short press if (overflows > 0) AND (overflows < 5) then short_press = 1 end if ' reset overflow counter overflows = 0 endif If the button press length isn't critical, you may be able to optimise the interrupt by checking individual bits within the overflow counter variable e.g. if bit 6 is set that means 64 over flows and a long press, if bit 7 is set then stop incrementing counter etc. Hope that helps Regards Mike ======================================================================= This e-mail is intended for the person it is addressed to only. The information contained in it may be confidential and/or protected by law. If you are not the intended recipient of this message, you must not make any use of this information, or copy or show it to any person. Please contact us immediately to tell us that you have received this e-mail, and return the original to us. Any use, forwarding, printing or copying of this message is strictly prohibited. No part of this message can be considered a request for goods or services. ======================================================================= -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist