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. iOS学习之NSPredictae及搜索框的实现

    NSPredicate Predicate 即谓词逻辑, Cocoa框架中的NSPredicate用于查询,作用是从数据堆中根据条件进行筛选.计算谓词之后返回的结果永远为BOOL类型的值,当程序使用谓 ...

  2. AMQP与RabbitMQ简介

    MQ(Message Queue,消息队列)是一种应用系统之间的通信方法.是通过读写出入队列的消息来通信(RPC则是通过直接调用彼此来通信的). 1.AMQP协议 在了解RabbitMQ之前,首先要了 ...

  3. PAT (Basic Level) Practise:1032. 挖掘机技术哪家强

    [题目链接] 为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行 ...

  4. python数据结构与算法——图的最短路径(Bellman-Ford算法)解决负权边

    # Bellman-Ford核心算法 # 对于一个包含n个顶点,m条边的图, 计算源点到任意点的最短距离 # 循环n-1轮,每轮对m条边进行一次松弛操作 # 定理: # 在一个含有n个顶点的图中,任意 ...

  5. 机器学习(一) 从一个R语言案例学线性回归

    写在前面的话 按照正常的顺序,本文应该先讲一些线性回归的基本概念,比如什么叫线性回归,线性回规的常用解法等.但既然本文名为<从一个R语言案例学会线性回归>,那就更重视如何使用R语言去解决线 ...

  6. html5本地存储的解决

    1.解决了Cookie  4K存储大小的问题2.解决了请求头常带存储信息的问题3.解决了关系型存储的问题4.跨域问题,跨浏览器*在 HTML5 中,数据不是由每个服务器请求传递的,而是只有在请求时使用 ...

  7. 关于CSS的那些事?

    关于CSS的那些事? 它有精准定位与排版,使得网页布局.信息排版一目了然:它有多姿多彩的样式属性,使得网页中各元素千变万化:它有神奇的渲染天赋,使得网页有了如诗如画.别具一格的魅力.你知道它了吗?没错 ...

  8. IncDec Sequence

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=3043[题目描述]给定一个长度为 n 的数列{a1,a2...an},每次可以选择一个区间[ ...

  9. Oracle、MySql、SQLServer 数据分页查询

    最近简单的对oracle,mysql,sqlserver2005的数据分页查询作了研究,把各自的查询的语句贴出来供大家学习..... (一). mysql的分页查询 mysql的分页查询是最简单的,借 ...

  10. bll编译错误

    如果在项目中 ,bll有函数,却引用报错 原因很可能是因为bll在生成程序集的时候,没有生成好.其中有错误 解决办法. 1.将bll,web,dal重新生成 2.注意bll的生成,该添加的添加,该排除 ...