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 CCS Technical Support

How to calculate the duration of a button inserted?
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
peachgirl
Guest







How to calculate the duration of a button inserted?
PostPosted: Mon Jun 11, 2007 12:53 pm     Reply with quote

I use KBDD.C in the #examples# for my keyboard, now I have to calculate the duration of a button inserted, I don't know how to do realize it.
peachgirl
Guest







PostPosted: Mon Jun 11, 2007 12:58 pm     Reply with quote

In this case, I use these codes to get a value from keyboard
Code:


int8 getvalue()
{
  int8 value=0;
  do
  {
       value = kbd_getc();   // the function of the KBBD.C
  }while(value==0)

}


I don't konw when it is inserted. but Now I think have to know the duration inserted,

Is there any one can help me?
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Mon Jun 11, 2007 2:04 pm     Reply with quote

Hello,

What is the resolution needed ?

If you call kbd_getc() then the resolution will never be better than the time used by kbd_getc().


try some think like that for 13mSec resolution (77em second):
Code:
#use delay(clock=20000000)

#define initSec 77

int8 sec77 ;
int8 sec = 0 ;

#int_TIMER1
void  TIMER1_isr(void) { // 13 ms at 20Mhz
  if (--Sec77 == 0 ) {
    sec77 = initSec ;
    ++sec ;
  }
}




int16 get value() {
  int16 timePushed77sec = 0 ; // time pushed by 77em seconds

  sec = 0 ;
  sec77 = initSec;
  int8 value=0;
  do
  {
       value = kbd_getc();   // the function of the KBBD.C
  }while(value==0)
 
  // Start count now :
  enable_interrupts(INT_TIMER1);
  while (  kbd_getc() != 0 ) ;
  disable_interrupts(INT_TIMER1);
  timePushed77sec = ( (int16)sec * initSec ) + ( initSec - Sec77 ) ;
  // the time is in  now timePushed77sec  with 77em seconds resolution.
 
  return ( timePushed77sec ) ;

}

void main() {

  setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // 13 ms at 20Mhz
  //enable_interrupts(INT_TIMER1);
  enable_interrupts(GLOBAL);



}

dro.
_________________
in médio virtus
peachgirl
Guest







PostPosted: Tue Jun 12, 2007 6:57 am     Reply with quote

Hello inservi,

I have tried what you said, but the result of the timePushed77sec is always 1. I don't konw why.

I used printf(lcd_putc, "%lu", timePushed77sec); I can't calcute the duration that i pushed a button.

Thank you for your help^!!!!
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Wed Jun 13, 2007 3:15 am     Reply with quote

Hello, i tried so
Code:
#define initSec 77

int8 sec77 ;
int8 sec = 0 ;

#int_TIMER1
void  TIMER1_isr(void) { // 13 ms at 20Mhz
  if (--Sec77 == 0 ) {
    sec77 = initSec ;
    ++sec ;
  }
}


int16 getvalue() {
  int16 timePushed77sec = 0 ; // time pushed by 77em seconds

  sec = 0 ;
  sec77 = initSec;
  /*
  int8 value=0;
  do
  {
       value = kbd_getc();   // the function of the KBBD.C
  }while(value==0)
  */
  // Start count now :
  enable_interrupts(INT_TIMER1);
  //while (  kbd_getc() != 0 ) ;
  delay_ms(1500) ;
  disable_interrupts(INT_TIMER1);
  timePushed77sec = ( (int16)sec * initSec ) + ( initSec - Sec77 ) ;
  // the time is in  now timePushed77sec  with 77em seconds resolution.
 
  return ( timePushed77sec ) ;

}


void main()
{
  int16 v1;

  setup_adc_ports(NO_ANALOGS);
  setup_adc(ADC_OFF);
  setup_spi(SPI_SS_DISABLED);
  setup_wdt(WDT_OFF);
  setup_timer_0(RTCC_INTERNAL);
  setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); // 13 ms at 20Mhz
  setup_timer_2(T2_DISABLED,0,1);
  enable_interrupts(GLOBAL);

  v1 = getvalue() ;

  printf("v1 %Lu ", v1);
 
 
}


As you see, i replaced the 'while ( kbd_getc() != 0 )' by 'delay_ms(x)'
With 'delay_ms(500)' i get 39, with 1500, i get 115, with 200, i get 15.

If you get all time 1, it s probably because the function 'kbd_getc() ' is waiting for the button release before return the result or somthink like that ? unfortunately, i do not have keyboard nor 'KBBD.C' then, i can not test more.
Are you sure that you use 'KBBD.C' and not 'KBD.C' ?

You will probably have to modify the 'kbd_getc() ' function.

dro.
_________________
in médio virtus
peachgirl
Guest







PostPosted: Wed Jun 13, 2007 4:02 am     Reply with quote

Hello inservi,

I tried the programme that you give me.

delay_ms(500); ------>I get 24
delay_ms(1500); ------>I get 70
delay_ms(200); ------>I get 10

I am sure the function is KBDD.C
peachgirl
Guest







PostPosted: Wed Jun 13, 2007 4:11 am     Reply with quote

What I want to say is that: if I can use while(kbd_getc() != 0) , its super!but we can't, so I want to know the raison of it. I don't konw if you have some ideas about it? and when you change it to delay_ms(500), what you think?

thanks very much again,

Heuresement vous être là!!!
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Wed Jun 13, 2007 8:32 am     Reply with quote

Hello,
Could you send the code KBDD.C ?

C'est un plaisir de vous aider !

dro.
_________________
in médio virtus
peachgirl
Guest







PostPosted: Wed Jun 13, 2007 9:11 am     Reply with quote

Code:

#if defined(__PCH__)
     #if defined use_portb_kbd
     #byte kbd = 0xF81                   // This puts the entire structure   0xF81 est l'adress de PORTB
     #else
     #byte kbd = 0xF83                   // This puts the entire structure  0xF83 est l'adress de PORTD
     #endif


#else
   #if defined use_portb_kbd
   #byte kbd = 6                  // on to port B (at address 6)
   #else
   #byte kbd = 8                 // on to port D (at address 8)
   #endif
#endif


#define set_tris_kbd(x) set_tris_d(x)
   

//Keypad connection:   (for example column 0 is B2)
//                Bx:

#define COL0 (1 << 4)
#define COL1 (1 << 5)
#define COL2 (1 << 6)
#define COL3 (1 << 7)

#define ROW0 (1 << 3)
#define ROW1 (1 << 2)
#define ROW2 (1 << 1)
#define ROW3 (1 <<0>KBD_DEBOUNCE_FACTOR) {
       switch (col) {
         case 0   : set_tris_kbd(ALL_PINS&~COL0);
                    kbd=~COL0&ALL_PINS;
                    break;
         case 1   : set_tris_kbd(ALL_PINS&~COL1);
                    kbd=~COL1&ALL_PINS;
                    break;
         case 2   : set_tris_kbd(ALL_PINS&~COL2);
                    kbd=~COL2&ALL_PINS;
                    break;
         case 3   : set_tris_kbd(ALL_PINS&~COL3);
                    kbd=~COL3&ALL_PINS;
                    break;
       }

     if(kbd_down) {
         if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
           kbd_down=FALSE;
           kchar=last_key;
           last_key='\0';
         }
       }

     else {
          if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
             if((kbd & ROW0)==0)
               row=0;
             else if((kbd & ROW1)==0)
               row=1;
             else if((kbd & ROW2)==0)
               row=2;
             else if((kbd & ROW3)==0)
               row=3;
             last_key =KEYS[row][col];
             kbd_down = TRUE;
          } else {
             ++col;
             if(col==4)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  set_tris_kbd(ALL_PINS);
  return(kchar);
}
peachgirl
Guest







PostPosted: Wed Jun 13, 2007 9:13 am     Reply with quote

thank you!!!
I have posted my KBDD.C
Guest








PostPosted: Wed Jun 13, 2007 9:31 am     Reply with quote

Do you know how to difine the number of times to call kbd_getc each second? Because I think its because that the pins don't insiste 1, the pin send some impluse(pulse)
peachgirl
Guest







the first part of the code
PostPosted: Thu Jun 14, 2007 1:11 am     Reply with quote

Code:

#if defined(__PCH__)
     #if defined use_portb_kbd
     #byte kbd = 0xF81                   // This puts the entire structure   0xF81 est l'adress de PORTB
     #else
     #byte kbd = 0xF83                   // This puts the entire structure  0xF83 est l'adress de PORTD
     #endif


#else
   #if defined use_portb_kbd
   #byte kbd = 6                  // on to port B (at address 6)
   #else
   #byte kbd = 8                 // on to port D (at address 8)
   #endif
#endif


#define set_tris_kbd(x) set_tris_d(x)
   

//Keypad connection:   (for example column 0 is B2)
//                Bx:

#define COL0 (1 << 4)
#define COL1 (1 << 5)
#define COL2 (1 << 6)
#define COL3 (1 << 7)

#define ROW0 (1 << 3)
#define ROW1 (1 << 2)
#define ROW2 (1 << 1)
#define ROW3 (1 << 0)




#define ALL_ROWS (ROW0|ROW1|ROW2|ROW3)
#define ALL_PINS (ALL_ROWS|COL0|COL1|COL2|COL3)


// Keypad layout:
char const KEYS[4][4]={{'1','2','3','+'},
                         {'4','5','6','-'},
                         {'7','8','9','!'},
                         {'*','0','#','$'}};

#define KBD_DEBOUNCE_FACTOR 33    //
peachgirl
Guest







the second part of the code
PostPosted: Thu Jun 14, 2007 1:12 am     Reply with quote

Code:

void kbd_init() {
}

char kbd_getc( ) {
   static BYTE kbd_call_count;
   static short int kbd_down;
   static char last_key;
   static BYTE col;

   BYTE kchar;
   BYTE row;

   kchar='\0';
   if(++kbd_call_count>KBD_DEBOUNCE_FACTOR) {
       switch (col) {
         case 0   : set_tris_kbd(ALL_PINS&~COL0);
                    kbd=~COL0&ALL_PINS;
                    break;
         case 1   : set_tris_kbd(ALL_PINS&~COL1);
                    kbd=~COL1&ALL_PINS;
                    break;
         case 2   : set_tris_kbd(ALL_PINS&~COL2);
                    kbd=~COL2&ALL_PINS;
                    break;
         case 3   : set_tris_kbd(ALL_PINS&~COL3);
                    kbd=~COL3&ALL_PINS;
                    break;
       }

     if(kbd_down) {
         if((kbd & (ALL_ROWS))==(ALL_ROWS)) {
           kbd_down=FALSE;
           kchar=last_key;
           last_key='\0';
         }
       }

     else {
          if((kbd & (ALL_ROWS))!=(ALL_ROWS)) {
             if((kbd & ROW0)==0)
               row=0;
             else if((kbd & ROW1)==0)
               row=1;
             else if((kbd & ROW2)==0)
               row=2;
             else if((kbd & ROW3)==0)
               row=3;
             last_key =KEYS[row][col];
             kbd_down = TRUE;
          } else {
             ++col;
             if(col==4)
               col=0;
          }
       }
      kbd_call_count=0;
   }
  set_tris_kbd(ALL_PINS);
  return(kchar);
}
inservi



Joined: 13 May 2007
Posts: 128

View user's profile Send private message

PostPosted: Thu Jun 14, 2007 3:13 am     Reply with quote

Hello,

You posted two versions, i assume that the right one is the second one.
I try to modify it then i post the result.

Best regards,
dro.
_________________
in médio virtus
peachgirl
Guest







PostPosted: Thu Jun 14, 2007 3:31 am     Reply with quote

yes; the second
Laughing thank you very much!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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