|
|
View previous topic :: View next topic |
Author |
Message |
temtronic
Joined: 01 Jul 2010 Posts: 9269 Location: Greensville,Ontario
|
|
Posted: Fri Jul 12, 2019 8:03 pm |
|
|
Here's the info from the CCS Manual. I just press F11 while my project is open and the manual opens. I don't see a version number on the 1st page, but this code has been in the manual for years.
....
The following is an example of how to wait only a specific time for a button press.
Code: |
#define PUSH_BUTTON PIN_A4
int1 timeout_error;
int1 timed_get_button_press(void){
int16 timeout;
timeout_error=FALSE;
timeout=0;
while(input(PUSH_BUTTON) && (++timeout<50000)) // 1/2 second
delay_us(10);
if(!input(PUSH_BUTTON))
return(TRUE); //button pressed
else{
timeout_error=TRUE;
return(FALSE); //button not pressed timeout occurred
}
}
.... |
|
|
|
MassaM
Joined: 08 Jun 2019 Posts: 31
|
|
Posted: Sat Jul 13, 2019 5:02 am |
|
|
temtronic wrote: | Here's the info from the CCS Manual. I just press F11 while my project is open and the manual opens. I don't see a version number on the 1st page, but this code has been in the manual for years.
....
The following is an example of how to wait only a specific time for a button press.
|
Thanks for the great tip!
Got to it by pressing F1 (Help Function Key). They are .chm files as compiled HTML basically.
Out of curiosity, I also did Google search for "CCS C How do I wait only a specified time for a button press" and found older manuals dating back to Sept/2013 found here http://www.farnell.com/datasheets/1769330.pdf that has this function mentioned, also a July/2011 has it, but the March/2019 version does not!
Going to test and post back results. Hope I manage and share a success!
Side talk:
I usually tell my son that "every start has hurdles and every midway has turbulence, but every success after many hurdles and turbulences is sweetest thing!", so hang on, buckle up and when you're through, it is success!
Now me tells myself! See the irony! _________________ while(!dead)
{
keepLearning();
} |
|
|
MassaM
Joined: 08 Jun 2019 Posts: 31
|
|
Posted: Sun Jul 14, 2019 5:40 pm |
|
|
Hello to all,
Ok, I know the code you are about to see below, is messy, horrible and a pain for the experts eye! but, the start of all great things is ugly!
The double press and long press are working, but, the single press interferes with the rest of them as the hit & count are getting hijacked!
original code/example/discussion thread is here:
https://www.ccsinfo.com/forum/viewtopic.php?t=43803&postdays=0&postorder=asc&start=15
My try:
Code: |
#include "16f877a.h"
#fuses HS, PUT, NOWDT, NOLVP
#use delay(clock=16M)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define PB_SELECT PIN_D6 // SELECT pushbutton
#define DEBOUNCE_TIME 3 // # of (10mS) interupt cycles Pushbutton must be held for before validating
#define LONG_PRESS_TIME 200 // number of 10mS interrupts before keypress is considered to be a long press (up to 254)
#define LED_RED PIN_B0
#define LED_GREEN PIN_B1
#define LED_BLUE PIN_B2
// Global Flags set by interrupt when button press detected, cleared by responding routing
int1 PBselectHit=0; // Flag indicating PB has been detected and debounced.
int1 PBselectLongPress=0; // indicates a LONG_PRESS_TIME has expired
// pushbutton debounce SELECT
static int1 pushed = false;
static unsigned int SelectActive = 0;
unsigned int count;
#int_timer1
void timer1_isr()
{
set_timer1(get_timer1()-10000); // set the timer to fire again in 10mS
/*
//single press <---- This is interfering with the rest as it detects the hit and screws up the rest! aka NOT WORKING :(
if (!pushed && input(PB_SELECT))
{
pushed = true;
while ( get_timer1() < 53036) ; //5MS Interrupt
if (++count == 2) count = 0; // Increment the count
}
else if (!input(PB_SELECT)) // Switch is Released / up
{
pushed = false;
}
*/
//double press <----- Working! :)
if (!pushed && input(PB_SELECT))
{
pushed = true;
while ( get_timer1() < 53036) ; //5MS Interrupt
if (++count == 4) count = 0; // Increment the count
}
else if (!input(PB_SELECT)) // Switch is Released / up
{
pushed = false;
}
// long press <----- Working! :)
if(input(PB_SELECT))
{
if(SelectActive++ == DEBOUNCE_TIME) // button must be held continuously for this many interrupt cycles
PBselectHit = TRUE; // flag must be reset by function waiting for keypress
if(SelectActive == LONG_PRESS_TIME)
PBselectLongPress = TRUE; // flag must be reset by function waiting for keypress
if(SelectActive > LONG_PRESS_TIME) // long press detected
SelectActive = LONG_PRESS_TIME+1; // clamp counter
}
else
{
SelectActive = 0; // otherwise reset debounce counter and start over
}
}
void resetSelectFlags()
{
count = 0;
PBselectLongPress = FALSE;
PBselectHit = FALSE;
pushed = false;
}
void main()
{
// T1_DIV_BY_1, T1_DIV_BY_2, T1_DIV_BY_4, T1_DIV_BY_8
setup_timer_1 ( T1_INTERNAL | T1_DIV_BY_1 ); // Internal clock and prescaler 8
clear_interrupt(INT_TIMER1); // Clear Timer1 interrupt flag bit
enable_interrupts(INT_TIMER1); // Enable Timer1 interrupt
enable_interrupts(GLOBAL); // Enable global interrupts
output_float(PB_SELECT); // input
output_low(LED_RED);
output_low(LED_GREEN);
output_low(LED_BLUE);
resetSelectFlags();
while(TRUE)
{
printf("count %u, PBselectHit %d\r\n", count, PBselectHit);
/*
//single press <---- This is interfering with the rest as it detects the hit and screws up the rest! aka NOT WORKING :(
if(PBselectHit)
{
for(int i=0; i < 3; i++)
{
output_high(LED_RED);
delay_ms(100);
output_low(LED_RED);
delay_ms(100);
}
resetSelectFlags();
}
*/
// double press
if(PBselectHit && count == 2)
{
for(int i=0; i < 6; i++)
{
output_high(LED_GREEN);
delay_ms(100);
output_low(LED_GREEN);
delay_ms(100);
}
resetSelectFlags();
}
//long press
if(PBselectLongPress)
{
for(int i=0; i < 3; i++)
{
output_high(LED_BLUE);
delay_ms(100);
output_low(LED_BLUE);
delay_ms(100);
}
resetSelectFlags();
}
}
}
|
_________________ while(!dead)
{
keepLearning();
} |
|
|
|
|
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
|