Monday, May 23, 2016

osa rtos - 3rd switch debounce

Switch debounce is a common problem faced when playing with digital circuit. It is a condition where single pressed by user would appears multiple presses.

Usual software method used to avoid this is by taking multiple samples at certain time interval.

 if (time_inverval == 10ms)          // run this function every 10ms  
 {  
      if (pin1 == HIGH)              // check the pin status  
      {  
           pin1_counter++;  
      }  
      else  
      {  
           pin1_counter--;  
      }  
      if (pin1_counter>=10)          // confirm the samples for 10 times  
      {  
           pin1_counter = 0;          // clear the pin1_counter after 10 samples  
           pin1_flag = 1;  
      }  
 } 

Or you can use extra diode and capacitor to filter out the debounce glitches as shown at labbookpages. The labbookpages actually shows solutions from A Switch Debouncer Circuit, Software Debouncing, to Digital Switch Debouncing, to Switch Debouncing ICs. Nevertheless, extra circuitry mean extra cost, it is better to settle everything in software. Only a few lines are needed.

This blog (or my osa rtos learning diary) is to show how to implement switch debounce in rtos. The solution is shown at osa rtos at "button" section but I never really understand it. So I decide to try it out.

From osa rtos website:

        //------------------------------------------------------------------------------
        //  Wait for key press (with debounce)
        //------------------------------------------------------------------------------
 
        do
        {
            OS_Cond_Wait(!pin_BUTTON);
            OS_Delay(40 ms);
        } while (pin_BUTTON);

The function use the OS_Cond_Wait to wait for button is pressed then delay it for 40ms to skip the debounce signal.

After that, wait for the key to be released.

        //------------------------------------------------------------------------------
        //  Wait for key release
        //------------------------------------------------------------------------------
 
        do
        {
            OS_Cond_Wait(pin_BUTTON);
            OS_Delay(40 ms);
        } while (!pin_BUTTON);
 
 
    }

The implementation is likely the same, so user source code should be placed in between "wait for key press" and "wait for key release" (I assume).

So I add one flag for button pressed, so my overall code for taskButton is as follow:


#ifndef TASK_BUTTON_H
#define TASK_BUTTON_H

#include <osa.h>
#include <p18cxxx.h>

#define mInitSW1()     {TRISBbits.TRISB0=1;}
#define mInitSW2()     {TRISBbits.TRISB1=1;}

#define mSW_1    PORTBbits.RB0
#define mSW_2    PORTBbits.RB1

extern unsigned char flag_button1;
extern unsigned char flag_button2;

void taskButton1(void);
void taskButton2(void);

#endif /* TASK_BUTTON_H */

The taskButton function is:


void taskButton1(void)
{
 mInitSW1();
 flag_button1 = 0;
 
    for (;;) {
 //------------------------------------------------------------------------------
 //  Wait for key press (with debounce)
 //------------------------------------------------------------------------------
        do
        {
            OS_Cond_Wait(!mSW_1);
            OS_Delay(2);   // delay 40 ms
        } while (mSW_1); 

 //------------------------------------------------------------------------------
 //  Raise a flag
 //------------------------------------------------------------------------------
  flag_button1 = 1;
 
 //------------------------------------------------------------------------------
 //  Wait for key release
 //------------------------------------------------------------------------------
 
        do
        {
            OS_Cond_Wait(mSW_1);
            OS_Delay(2);
        } while (!mSW_1);    
    }
}

I put a video to show how the whole program is working. The led blinking, 16x2 display and button.



Project file is downloadable at here.

Sunday, May 8, 2016

My Tools



For the past one year, I have been buying components, tools. It started by buying some development kits and modules, then some IC components and breadboard, when Taobao is promoting the 2rmb delivery fee to Malaysia, I bought a soldering station and multimeter. Along that, I am purchasing electronic components from Element14 and RS electronic for my design.

My room is very messy. Seeing the components boxes laying on the floor, the development kits are stacking in the drawer, and my tools are scattering here and there.

I decide to tidy up the room and just for fun I am showing current tools that I own.


TOOLS
Buy From
1.
31 in 1 electronic tool set
Taobao
2.
Test pen
Cytron
3.
Legendary PICKIT 2
Element14
4.
Tweezers
Taobao
5.
Rolson wire cutter
Ace Hardware
6.
Victor multimeter VC890C
Taobao
7.
Victor multimeter VC830L
Cytron
8.
Solder sucker
Taobao
9.
Kasadl soldering station KSD 936
Taobao

I just ordered some component boxes from Taobao, I will show my components arrangement in next post.