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 = ,
ON =
} 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 = ;
DEBOUNCE_Integrator = DEBOUNCE_ThreholdHigh / ;
DEBOUNCE_TimeFlag = ; DEBOUNCE_Open();
DEBOUNCE_IOCtl();
DEBOUNCE_PreInput = DEBOUNCE_Input;
} void Key_Scheduler(void)
{
if (DEBOUNCE_TimeFlag)
{
if (DEBOUNCE_EventStart)
{
if (DEBOUNCE_Input == )
{
if (DEBOUNCE_Integrator-- == DEBOUNCE_ThreholdLow)
{
DEBOUNCE_Output = ;
DEBOUNCE_PreInput = DEBOUNCE_Input;
DEBOUNCE_EventStart = ;
DEBOUNCE_Integrator = DEBOUNCE_ThreholdHigh / ;
}
}
else //if (DEBOUNCE_Input == 1)
{
if (DEBOUNCE_Integrator++ == DEBOUNCE_ThreholdHigh)
{
DEBOUNCE_Output = ;
DEBOUNCE_PreInput = DEBOUNCE_Input;
DEBOUNCE_EventStart = ;
DEBOUNCE_Integrator = DEBOUNCE_ThreholdHigh / ;
}
}
}
else if (DEBOUNCE_PreInput != DEBOUNCE_Input)
{
DEBOUNCE_EventStart = ;
}
DEBOUNCE_TimeFlag = ;
}
}

  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 = ;
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 <xc.h>
#include "Led.h"
#include "Key.h"
#include "Timer.h"
#include "Interrupt.h"
#include <sys/attribs.h>
#include "ConfigurationBits.h" void __ISR(_TIMER_1_VECTOR,ipl1AUTO) Timer1_Handler(void)
{
DEBOUNCE_TimeFlag = ;
Timer1_Write();
IFS0CLR = 0x10; // Clear flag
} void main(void)
{
Led_Init();
Key_Init();
Timer1_Init();
Mvec_Interrupt(); while ()
{
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.

PIC32MZ tutorial -- Key Debounce的更多相关文章

  1. PIC32MZ tutorial -- External Interrupt

    In my older blog "PIC32MZ tutorial -- Key Debounce", I shows how to acheive key debounce w ...

  2. PIC32MZ tutorial -- Change Notification

    In my last post I implement "Key Debounce" with port polling, port polling is not very eff ...

  3. PIC32MZ tutorial -- OC Interrupt

    In my previous blog "PIC32MZ tutorial -- Output Compare", I shows how to apply Output Comp ...

  4. PIC32MZ tutorial -- Watchdog Timer

    Watchdog is a very necessary module for embedded system. Someone said that embedded system operates ...

  5. PIC32MZ tutorial -- Output Compare

    Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...

  6. PIC32MZ tutorial -- UART Communication

    At this moment, I accomplish the interface of UART communication for PIC32MZ EC Starter Kit. This in ...

  7. PIC32MZ tutorial -- Input Capture

    Today I accomplish a simple application for PIC32MZ EC Starter Kit. This application uses Input Capt ...

  8. PIC32MZ tutorial -- 32-bit Timer

    The microcontroller is PIC32MZ2048ECH144 on the PIC32MZ EC Starter Kit. This microcontroller has fou ...

  9. PIC32MZ tutorial -- Timer Interrupt

    An interrupt is an internal or external event that requires quick attention from the controller. The ...

随机推荐

  1. 关于Let和var声明变量的区别

    Let是ES6中添加进来的一个关键字,用于声明变量,其法与var声明变量相同,不同点在于其作用域(块级). 举例可以看出其具体差别 for(var i=0;i<5;i++){ console.l ...

  2. E1_1 用邻接矩阵存储有向图,并输出各顶点的出度和入度

    参考书:图论算法理论.实现及应用(北京大学出版社) 输入数据:(test.txt) 程序: /* 邻接矩阵存储有向图 */ #include <cstring> #include < ...

  3. 深入了解Hibernate的缓存使用

    Hibernate缓存 缓存是计算机领域的概念,它介于应用程序和永久性数据存储源(如在硬盘上的文件或者数据库)之间,其作用是降低应用程序 直接读写永久性数据存储源的频率,从而提高应用的运行性能.缓存中 ...

  4. fancybox 最基本的使用步骤

    初步使用第一步 :引用js和样式 第二步 :设定要触发显示的元素(a标签,链接href指向div的id)  <div><a href="#adddivtest" ...

  5. mysql 批量更新和批量插入

    1. 批量更新 update table_name set field_name = CASE id WHEN id1 THEN  field_value, WHEN id1 THEN  field_ ...

  6. day5--<装饰器、模块、字符串格式化、生成器、迭代器>logging模块

    本节大纲: 一:双层装饰器:一个函数可以被多层装饰器进行装饰,函数渲染(编译)从下到上,函数执行从上到下.如下程序: #!/usr/bin/env python #-*-coding:utf-8-*- ...

  7. 数论 UVA 11752

    题目大意是在1~2^64-1的范围内找到所有符合条件的数,条件要求这个数字是两个或两个以上不同数字的幂,例如64=8^2=4^3. 对于这一题,分析是:如果一个满足这个条件的数字一定可以转换成i^k, ...

  8. Spring异常累计(1)Spring注解与扫描,NoUniqueBeanDefinitionException

    spring中可以使用注解机制,代替传统的在xml中配置一个bean. 如 <pre name="code" class="java">@Compo ...

  9. javac

    http://openjdk.java.net/groups/compiler/ http://crazyjavahacking.org/tag/javac/ http://openjdk.java. ...

  10. java比较两个字符串是否相等

    从c 到c++ 到 c# 到 JavaScript  判断两个字符串是否相等,用==号都可以.奇葩的java怎么可以只能用equals()这个函数.只是因为String是引用类型吗??!!哭笑不得.. ...