www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]  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

  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_Init();

Key_Init();

Timer1_Init();

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.


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

大 key 并不是指 key 的值很大,而是 key 對(duì)應(yīng)的 value 很大。

關(guān)鍵字: key value

蘋果發(fā)布了新版本的iOS 13.6移動(dòng)操作系統(tǒng),為iPhone品牌帶來(lái)了許多創(chuàng)新。

關(guān)鍵字: 13.6 car iOS iPhone key

蘋果今天正式發(fā)布了iOS 13.6和iPadOS 13.6的正式版本, 此更新還包括許多新功能。 更新文件的最大大小為4 GB。這次,我們將給大家展示更新中已更改的功能。

關(guān)鍵字: 13.6 car iOS key unc0ver

Microchip公司的PIC32MZ EF系列是高達(dá)250MHz的集成浮點(diǎn)單元(FPU),具有廣泛的外設(shè)和包括局域網(wǎng)(CAN)的極好的連接選擇,工作電壓2.1V到 3.6V,DSP增強(qiáng)核具有四

關(guān)鍵字: Microchip pic32mz 處理器

7月10日,Apple正式發(fā)布了iOS 13.6和iPadOS 13.6的通用版。 安裝正確的開發(fā)人員配置文件后,用戶可以從Apple開發(fā)人員中心或通過(guò)OTA下載iOS / iPadOS 13.6。

關(guān)鍵字: 13.6 car gm iOS key

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

關(guān)鍵字: interrupt pic32mz timer tutorial

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

關(guān)鍵字: pic32mz tutorial change notification

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

關(guān)鍵字: interrupt pic32mz tutorial external

經(jīng)過(guò)千辛萬(wàn)苦,今天終于完工PIC32MZ EC Starter Kit的ethernet bootloader項(xiàng)目。我將整個(gè)項(xiàng)目, 命名為PhnBootloader。它分為兩個(gè)部分。第一個(gè)部分是PC 端的host程序Ph...

關(guān)鍵字: bootloader ethernet pic32mz udp協(xié)議

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

關(guān)鍵字: communication pic32mz tutorial uart
關(guān)閉