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

📄 keyboard.c

📁 IO
💻 C
字号:
//Keyboard.c
//
//Body: HT48C10-1
//Mask option
//BZ/BZB : All Disable
//the others use the default value

#include <ht48c10-1.h>

#pragma vector isr_4 @ 0x4
#pragma vector isr_8 @ 0x8
#pragma vector isr_c @ 0xc

//ISR for safequard
void isr_4(){} // external ISR
void isr_8(){} // timer/event 0
void isr_c(){} // timer/event 1

//initialize registers for safeguard
void safeguard_init(){
	_intc = 0;
	_tmrc = 0;
	_tmr = 0;
	_pac = 0xff; //input mode
	_pbc = 0xff;
	_pcc = 0xff;
}

const unsigned char led_code[16]=
	{0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8,
	 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0};
const unsigned char scan[4] = {0xfe, 0xfd, 0xfb, 0xf7};

//return the row number of the pressed key
unsigned char wait_key_pressed(){
	unsigned char i;
	i=0;
	while(1){
		_pac = scan[i];	//output scan code to port A
		if ((~_pa) & (unsigned char)0xf0){ //key pressed
			_delay(2000); //debounce
			//after debounce, if the key is still pressed
			//we claim it a key pressed, otherwise ignore it
			if ((~_pa) & (unsigned char)0xf0)
				return i; //row i, key pressed
		}
		i++;
		if (i > 3) i = 0;
	}
}

// return the column number of the pressed key
unsigned char wait_key_released(){
	unsigned char i;
	unsigned char key;

	key = _pa; //keep the pressed key

	// wait until key released
	while((~_pa) & (unsigned char)0xf0);

	//find out which column key pressed
	//no debouce needed
	for(i=0; i<4; i++)
		if ((~key) & (0x10<<i))
			break; //column i, key pressed
	return i;
}

unsigned char get_key(){
	unsigned char row, col;
	row = wait_key_pressed();
	col = wait_key_released();
	return	(row << 2) + col;
}

void main(){
	unsigned char index;

	safeguard_init();

	_pac = 0xff; //set port A as input port
	_pbc = 0x00; //set port B as output port
	_pa = 0;     //zero port A
	_pb = 0xff;  //off LEDs

	while(1){
		index = get_key();
		//the key value won't be displayed until
		// the key is released
		_pb = led_code[index];
	}
}

⌨️ 快捷键说明

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