PIC32MZ tutorial -- Key Debounce

Publisher:钱币之歌Latest update time:2017-01-20 Source: eefocusKeywords:PIC32MZ  tutorial  Key  Debounce Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

  Today I accomplish a application on PIC32MZ EC Starter Kit. The feature of application is to light up LED when key released and put out LED when key pressed. LED is  the Starter Kit LED1 connecting to RH0. Key is the Starter Kit push button SW1 connecting to RB12. I also use the Timer1 Interrupt. In my application, I just design the software with three modules -- LED module, KEY module and TIMER1 module and the final is the main function and interrupt service routine.


  LED module let LED1 on when "LedState" variable is 1, and off when "LedState" variable is 0. Below is its implementation.



#define LED_IOCTL()       TRISHCLR = (1<<0)

#define LED_SETON()       LATHSET = (1<<0)

#define LED_SETOFF()      LATHCLR = (1<<0)

#define LED_ONOFF()       LATHINV = (1<<0)

#define LED_OPEN()        ANSELH &= 0xFFFFFFFE


typedef enum _LED_STATE_t

{

    OFF = 0,

    ON = 1

} LED_STATE_t;

    

LED_STATE_t PreLedState, LedState;


void Led_Init(void)

{

    LED_OPEN();

    LED_IOCTL();

    LED_SETON();

    LedState = ON;

    PreLedState = LedState;

}


void Led_Scheduler(void)

{

    if (LedState != PreLedState)

    {

        LED_ONOFF();

        PreLedState = LedState;

    }

}


  The "LedState" variable is determined by KEY module. The key press validated, the "LedState" is 1. The key release validated, the "LedState" is 0. Since the key (push button) does not have any debounce circuitry. I enable the internal resistor pull-up and use a debounce algorithm to remove random or spurious transistings of a digital signal read as an input by PIC32MZ. The following example illustrates how this algorithm works. The sequence labeled, corrupted, has significant random transitions added to the real signal. The sequence labled, integrator, represents the algorithm integrator which is constrained to be between 0 and 3. The sequence labeled, output, only makes a transition when the integrator reaches either 0 or 3. Note that the output signal lags the input signal by the integration time but is free of spurious transitions.


real signal  0000111111110000000111111100000000011111111110000000000111111100000


corrupted   0100111011011001000011011010001001011100101111000100010111011100010


integrator    0100123233233212100012123232101001012321212333210100010123233321010


output    0000001111111111100000001111100000000111111111110000000001111111000


  The algotithm has been around for many many years but does not seem to be widely known. It is notable that the algotithm uses integration as opposed to edge logic. It is the integration that makes this algotithm so robust in the presence of noise. In the implementation of KEY module, I use "DEBOUNCE_TimeFlag" variable to control debounce start.


#define DEBOUNCE_Input          (PORTB & 0x1000)

#define DEBOUNCE_Open()         ANSELB = 0xFFFFEFFF

#define DEBOUNCE_IOCtl()        CNPUBSET = 0x1000

#define DEBOUNCE_Output         LedState

#define DEBOUNCE_ThreholdLow    0

#define DEBOUNCE_ThreholdHigh   100


unsigned long DEBOUNCE_PreInput;

unsigned char DEBOUNCE_EventStart;

unsigned int DEBOUNCE_Integrator;

volatile unsigned char DEBOUNCE_TimeFlag;


    void Key_Init(void)

    {

        DEBOUNCE_EventStart = 0;

        DEBOUNCE_Integrator = DEBOUNCE_ThreholdHigh / 2;

        DEBOUNCE_TimeFlag = 0;

        

        DEBOUNCE_Open();

        DEBOUNCE_IOCtl();

        DEBOUNCE_PreInput = DEBOUNCE_Input;

    }


    void Key_Scheduler(void)

    {

        if (DEBOUNCE_TimeFlag)

        {

            if (DEBOUNCE_EventStart)

            {

                if (DEBOUNCE_Input == 0)

                {

                    if (DEBOUNCE_Integrator-- == DEBOUNCE_ThreholdLow)

                    {

                        DEBOUNCE_Output = 0;

                        DEBOUNCE_PreInput = DEBOUNCE_Input;

                        DEBOUNCE_EventStart = 0;

                        DEBOUNCE_Integrator = DEBOUNCE_ThreholdHigh / 2;

                    }

                }

                else                   //if (DEBOUNCE_Input == 1)

                {

                    if (DEBOUNCE_Integrator++ == DEBOUNCE_ThreholdHigh)

                    {

                        DEBOUNCE_Output = 1;

                        DEBOUNCE_PreInput = DEBOUNCE_Input;

                        DEBOUNCE_EventStart = 0;

                        DEBOUNCE_Integrator = DEBOUNCE_ThreholdHigh / 2;

                    }

                }

            }

            else if (DEBOUNCE_PreInput != DEBOUNCE_Input)

            {

                DEBOUNCE_EventStart = 1;

            }

            DEBOUNCE_TimeFlag = 0;

        }

    }


  TIMER module uses timer1 to generate interrupt per millisecond. and set "DEBOUNCE_TimeFlag" logic 1 in the timer1 interrupt service routine.



void Timer1_Init(void)

{

    T1CON = 0x8010;

    PR1 = 0x30D3;

    IPC1SET = 0x5;

    TMR1 = 0;

    IEC0SET = 0x10;

    IFS0CLR = 0x10;

}

void Timer1_Write(unsigned int value)

{

    TMR1 = value & 0xFFFF;

}

unsigned int Timer1_Read(void)

{

    return (TMR1 & 0xFFFF);

}


  Since interrupt will be used, the following sentence is enable interrupt with multi-vector mode.


#define Mvec_Interrupt() INTCONSET = 0x1000; asm volatile("ei")

  The final is the implementation of main function and interrupt service and routine.



#include

#include "Led.h"

#include "Key.h"

#include "Timer.h"

#include "Interrupt.h"

#include

#include "ConfigurationBits.h"


void __ISR(_TIMER_1_VECTOR,ipl1AUTO) Timer1_Handler(void)

{

    DEBOUNCE_TimeFlag = 1;

    Timer1_Write(0);

    IFS0CLR = 0x10; // Clear flag

 }


void main(void) 

{

    Led_Heat();

    Key_Init();

    Timer1_Heat();

    Mvec_Interrupt();

    

    while (1)

    {

        Key_Scheduler();

        Led_Scheduler();

    }

}


   The coding thing is done so far. we only need to compile and build it, then download it to PIC32MZ EC Starter Kit. You will get SW1 presse or release followed LED1 off or on. I bet it would run perfectly since the wonderful debounce algorithm.


Keywords:PIC32MZ  tutorial  Key  Debounce Reference address:PIC32MZ tutorial -- Key Debounce

Previous article:PIC32MZ tutorial -- Change Notification
Next article:PIC32MZ tutorial -- Timer Interrupt

Recommended ReadingLatest update time:2024-11-16 15:33

Digi-Key Launches Revolutionizing Automation Video Series
Digi-Key Electronics, a global distributor of electronic components and automation products with a wide range of stock options and fast shipping , has launched a new video series, Revolutionizing Automation, that explores cutting-edge automation and control technologies. Powered by Omron and Siemens, this four-pa
[Internet of Things]
Digi-Key Launches Revolutionizing Automation Video Series
[PIC32MZ] I2C communication
I2C, like the serial port, only requires two wires for communication, which is very useful in some pin-constrained applications. However, compared with the serial port, the communication protocol of I2C is simpler and the distance is shorter, which is limited to inter-board communication. Most ICs support the I2C prot
[Microcontroller]
[PIC32MZ] I2C communication
Digi-Key Electronics Launches New Scheme-it Capabilities
New features include Ultra Librarian® symbol integration, custom symbol editor, and math markup Digi-Key Electronics, a global distributor of the broadest selection of in-stock electronic components with the fastest shipping, has released new features for its popular Scheme-it tool, a free online schematic and diagr
[Microcontroller]
Digi-Key Electronics Launches New Scheme-it Capabilities
CEVA Expands RivieraWaves UWB IP to Support CCC Digital Key 3.0 Standard for Keyless Entry
CEVA Expands RivieraWaves UWB IP to Support CCC Digital Key 3.0 Standard for Keyless Vehicle Entry The CEVA-RivieraWaves™ UWB Platform IP supporting IEEE 802.15.4z supports the Car Connectivity Consortium® Digital Key 3.0 standard using an optimized software MAC layer. We have already started designing with Tier 1 a
[Internet of Things]
CEVA Expands RivieraWaves UWB IP to Support CCC Digital Key 3.0 Standard for Keyless Entry
【PIC32MZ】Development Environment Construction
Microchip uses a new architecture to develop the new generation of 32-bit PIC chips, which is Harmony. Harmony is similar to ST's STM32CubeMX, which encapsulates the chip library into the middle layer and adopts a unified interface. Only the bottom layer is different. However, the bottom layer has been written by Mi
[Microcontroller]
【PIC32MZ】Development Environment Construction
Detailed explanation of the car digital key (Digital Key) specification
1.3C Digital Key Specification Introduction CCC (Car Connectivity Consortium) is a global cross-industry organization dedicated to developing standards for smartphone-to-car connectivity solutions. In July 2021, CCC defined UWB as the core technology of the third-generation digital key and released the CCC R3 (
[Embedded]
Detailed explanation of the car digital key (Digital Key) specification
Digi-Key Advances Distribution of Winning Product Portfolios from Renesas and Dialog
Digi-Key Electronics Advances Distribution of Winning Product Portfolio from Renesas and Dialog as Merger Completes   Digi-Key Electronics, the global leader in the supply of the broadest selection of electronic components in stock for immediate shipment, said it will continue to strongly support the product port
[Internet of Things]
Digi-Key Advances Distribution of Winning Product Portfolios from Renesas and Dialog
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号