⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 acprd_keyb.c

📁 source code for a sample alarm control panel system using Freescale MC9S12DP256 . The project was im
💻 C
字号:
//=============================================================================
// File: ACPRD_KEYB.C - V1.00
// Rem.: The ACPRD Project Page on the Web -> http://hc12web.de/acprd
//=============================================================================

//-- Includes -----------------------------------------------------------------

#include <stdio.h>
#include "datatypes.h"
#include "hcs12dp256.h"
#include "acprd_keyb.h"

//-- Static Vars --------------------------------------------------------------

#define KEYB_MAX_BSIZE		10		// max number of kcodes in buffer
UINT8  keyb_buffer[KEYB_MAX_BSIZE];	// keyboard buffer
UINT16 keyb_widx, keyb_ridx;		// keyboard buffer write index, read index

UINT8  keyb_hist[KEYB_MAX_KEYS];	// sample history for each channel

//-- Code ---------------------------------------------------------------------

void initKeyb(void) {

	ATD1DIEN = 0xff;					// enable digital input for all PORTAD1
	}

//-----------------------------------------------------------------------------

void flushKeyb(void) {

	keyb_ridx = keyb_widx;
	}

//-----------------------------------------------------------------------------
#define KEYB_PATTERN_HLLL	0x08

void scanKeyb(void) {

	UINT8 sample, n;
	UINT8 *phist;

	sample = PORTAD1 & 0x0f;			// 4x push button on PORTAD1[0..3]
	if(PTIT & BM_3) sample |= 0x10;		// push button #5 is Encoder on PTT[3]

	// check history of each channel and detect keypress events
	n = 0;
	phist = keyb_hist;
	do {
		*phist <<= 1;					// shift in zero bit from right
		if(sample & 1) *phist |= 1;		// if sample=1 set hist bit accordingly
		n++;
		// "HLLL" pattern in hist buffer detects a falling edge
		// with a minimum of three subsequent L-samples 
		if((*phist & 0x0f) == KEYB_PATTERN_HLLL) {
			putKeyb(n);					// transfer kcode to buffer
			}
		sample >>= 1;					// next PORTAD1 bit
		phist++;						// next channel
		} while(n < KEYB_MAX_KEYS);
	}

//-----------------------------------------------------------------------------

// transfer a key code to keyboard buffer
//
void putKeyb(UINT8 kcode) {

	keyb_buffer[keyb_widx++] = kcode;
	if(keyb_widx == KEYB_MAX_BSIZE) keyb_widx = 0;
	}

//-----------------------------------------------------------------------------

// check keyboard buffer: if empty return 0
// otherwise return code of key on top of buffer
//
UINT8 hasKeyb(void) {

	return((keyb_ridx != keyb_widx) ? keyb_buffer[keyb_ridx] : 0);
	}

//-----------------------------------------------------------------------------

// get a key code from keyboard buffer
// (wait if buffer empty)
//
UINT8 getKeyb(void) {

	UINT8 kcode;

	while(keyb_ridx == keyb_widx) ; 
	kcode = keyb_buffer[keyb_ridx++];
	if(keyb_ridx == KEYB_MAX_BSIZE) keyb_ridx = 0;
	return kcode;
	}

//=============================================================================

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -