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

📄 keyboard.c

📁 一个很不错的按键单片机C程序
💻 C
字号:
//**************************************************************************************//
//**************************************************************************************//
// File Name	:KEYBOARD.c								//
// AUTHOR:	:CWJ									//
// CREATED	:2004/3/3								//
// REVISION	:REV1.0									//
// DESCRIPTION  :a single keyboard program.The keyboard have tree bottoms which         //
//		included a"+"bottom,a"-"bottom and a"select channel"bottom.Press "+" or	//
//		"-" one time,then counter add 1 or subtract 1.If press the bottoms      //
//		continually over some times ,such as 2 seconds,then counter can count   //
//		quickly.The "select channel"bottom can choose from 1~7.The "+" and "-"  //
//		can change 1 to 20.LED display .P1.7,P1.6,P1.5 display the number of   	//
//		channle with BCD code.P1.4,P1.3,P1.2,P1.1,P1.0 display time with BCD    //
//		code.P2.4,P2.3,P2.2 are bottoms interface.				//
//--------------------------------------------------------------------------------------//
// Date Modified:2004-6-6								//
// Content	:Change some global parmeters into local patmeters.Some function have	//
//		 return vale.That make the file replant easily.				//
//**************************************************************************************//
#include"reg51f.h"

#define uchar unsigned char
#define uint  unsigned int

#define CounterBotConti		20				//wait some time of continuity								
#define CounterSpeedConti	10				//speed of continuity 

#define KeyPort 	P2
#define DisPort		P1

#define KeyScan		0x38					//P2.5,P2.4,P2.3 is interface of select ,+, -
#define KeyChannel	0x20					// the key value of bottom "select"
#define KeyAdd		0x10					// the key value of bottom "+"
#define KeySubtract	0x08					// the key value of bottom "-"
#define DisPortChanH  	0xe0
#define DisPortChanL 	0x1f
#define KeySwitch 	0x01



void 	KeyIni(void);						//function declaration
uchar 	KeyValueProcess(uchar KeyValue );
void 	Delay1ms(uint count);
uchar 	KeyProcess(void);
void 	KeyOrUart(void);

					 	
bit 	KeyMark; 						//flag of pressing contiually
bit	FLAG;							
bit 	KI;
sbit SwitchPin=P2^0;
sbit P10=P1^0;

uchar data   	CounterChan;					//counter of channel selected
uchar data	CounterBottom;					//counter of times increased(delined) by degree

uchar data  	channel_sel;					//match isp programm
uchar data	data_arr[7];

uchar data 	KeyValue;


//**************************************************************************************//
// 			KEYBOARD INTIALIZATION ROUTINE                			//
// DESCRIPTION	: when power up ,intialize including port and interrupt regster.	//
// ARGUMENT	: none                                        				//
// RETURN	: none									//
// NOTES	: none                                                                	//
//**************************************************************************************//

void  KeyIni(void)
{
	//KeyPort=0xff&KeyScan;
	//DisPort=0x00;
	CounterChan=0x00;
	CounterBottom=0x00;
	//P3=0x00;/*********************    TEST LINE   **********************************/
	}
	





//**************************************************************************************//
// 			KEYVALUE PROCESS ROUTINE                			//
// DESCRIPTION	: PROCESS THE BOTTOM PRESSED,"CHANNEL","+","-"BOTTOM			//
// ARGUMENT	: KeyValue                                        			//
// RETURN	: none									//
// NOTES	: none  								//
//--------------------------------------------------------------------------------------//
// Date Modified: 2004-6-6 10:03							//
// Content	: 把原没有返回值的函数改写为有返回值,使得整个文件的全局变量减少		//
//		  更便于移植								//
//**************************************************************************************//

uchar KeyValueProcess(uchar KeyValue )
{
	uchar DisChannel;
	switch(KeyValue)
	{
		case KeyChannel:
			CounterBottom=0x00;
			if(CounterChan==0x07)
			{
				CounterChan=0x00;
				}//end if
			CounterChan++;
			DisChannel=CounterChan;
			//channel_sel=CounterChan;		//match the isp main program
			DisPort=DisChannel<<5;			//display channel only, delay time display zero
			return (DisPort);
			//P3=KeyValue;/******************test line****************/
			//break;
		case KeyAdd:
			if(!CounterChan) break;
			if(CounterBottom==0x14)
			{
				CounterBottom=0xff;
				}//end if
			CounterBottom++;
			//DisTimes=CounterBottom;
			//data_arr[channel_sel-1]=DisTimes;	//match the isp main program
			DisPort=CounterBottom|(CounterChan<<5);	//display delay tiem lately, the channel do not change
			return (CounterBottom);
			/*P3=KeyValue;*****************test line****************/
			//break;
		case KeySubtract:
			if(!CounterChan) break;
			if(CounterBottom==0x00)
			{
				CounterBottom=0x15;
				
				}//end if
			CounterBottom--;
			//DisTimes=CounterBottom;
			//data_arr[channel_sel-1]=DisTimes;	//match the isp main program
			DisPort=CounterBottom|(CounterChan<<5);	//display delay tiem lately, the channel do not change
			return (CounterBottom);
			/*P3=KeyValue;*****************test line****************/
			//break;
		default:
			break;
		}//end switch
	}
	
	
	

//**************************************************************************************//
// 			DELAY  ROUTINE                					//
// DESCRIPTION	: time=1ms*count .crystal=12MH						//
// ARGUMENT	: uchar count .                                				//
// RETURN	: none									//
// NOTES	: none                                                                 	//
//**************************************************************************************//

void Delay1ms(uint count)
{
	uint data i,j;
	for(i=0;i<count;i++)
	{
		for(j=0;j<120;j++)
		{
			}
		}
	}

//**************************************************************************************//
// 			KEYBOARD PROCESS ROUTINE                			//
// DESCRIPTION	: process to press continually 						//
// ARGUMENT	: none                       		       				//
// RETURN	: none									//
// NOTES	: none                                                                	//
//--------------------------------------------------------------------------------------//
// Date Modified: 2004-6-6 10:03							//
// Content	: 把原没有返回值的函数改写为有返回值,使得整个文件的全局变量减少		//
//		  更便于移植								//
//**************************************************************************************//

uchar KeyProcess(void)
{
	//uchar data KeyValue;
	static uint	KeyCounter;				//time to press bottoms continually
	//Delay1ms(10);						//延时去抖动
	KeyPort=KeyPort|KeyScan;				//keep no keyboard interface bit on
	KeyValue=KeyPort;
	KeyValue=~KeyValue & KeyScan;				//get the value which bottom is pressed
	if(KeyValue==0x00)					//do not press any bottoms
	{
		KeyCounter=0;
		KeyMark=0;
		KI=0;
		/*P30=KeyMark;******************        test line           ***************************/
		return -1;
		}//end if(!KeyValue)	

	if(KeyMark)						// if it is not the first time
	{
		KeyCounter++;
		if(KeyCounter==CounterBotConti)
		{
			KeyCounter-=CounterSpeedConti;
			KI=1;
			return (KeyValueProcess(KeyValue));
		}
		else
		{	/*P30=KeyMark;******************        test line           ***************************/;
			KI=0;
			return -1;
		}
	   	/*P30=KeyMark;******************        test line           ***************************/
	}//end if(keymark)
	else							//it is the first time to press bottoms
	{
		Delay1ms(15);					//延时去抖动
		KeyPort=KeyPort|KeyScan;			//keep no keyboard interface bit on
		KeyValue=KeyPort;
		KeyValue=~KeyValue & KeyScan;			//get the value which bottom is pressed
		if(KeyValue==0x00)				// do not press and bottoms
		{
			KeyCounter=0;
			KeyMark=0;
			KI=0;
		   	/*P30=KeyMark;******************        test line           ***************************/
			return -1;
		}//end if(!KeyValue)	
		KeyMark=1;
		/*P30=KeyMark;******************        test line           ***************************/
		KI=1;
		return (KeyValueProcess(KeyValue));
		}//end else
}

//*****************************************************************//
// Function	: void KeyOrUart(void)      
// Description	: 在使用键盘或使用串口间切换    	
// Calls	:      						
// Called By	:
// Table Updated:  	// 被修改的表(此项仅对于牵扯到数据库操作的程序)
// Input	:none
// Output	:none
// Return	:none       
// Date Created	:2004-6-7 16:06
//******************************************************************//
void KeyOrUart(void)
{
	uchar 		SwitchTem;
	uchar 		keyvalue;
	//uchar		ch;
	static uchar	SwitchChannel;
	SwitchTem	 = KeyPort | KeySwitch;
	if(SwitchPin)
	{	// use keyboard	
		//printf("\nPlease Input Data With Keyboad!\n");						
		while(!KI)
		{
			keyvalue=KeyProcess();
		}
		KI=0;
		if(keyvalue >=0x20 && keyvalue <=0xe0)
		{
			SwitchChannel=keyvalue;
			//channel_fpga=(keyvalue>>5)-1;
			Delay1ms(500);
		}
		else
		{
			if(keyvalue >=0x000 && keyvalue <=0x14)
			{
				//buff[channel_fpga]=keyvalue | SwitchChannel;
				FLAG=1;
				//printf("Keyboard Data-------0X%bXH    \n",buff[channel_fpga]);
			}
		}//end else
	}
	//else
	//{
		//use uart
		//printf("\nWaiting For Receiving Data.........!\n");
		//ch=SerialRecChar();
		//printf("Received Data-------0X%bXH    \n",ch);
	//}
}
		
void main(void)
{
	//uchar ch;
	KeyIni();
	while(1)
	{
		KeyOrUart();
		if(FLAG)
		{
			//P10=~P10;
			FLAG=0;	
			Delay1ms(1000);
		}
		//while(KeyValue==0x00)
		//{
		//	ch=KeyProcess();
		//}		//					/**********************testline***********************/
	}
}

⌨️ 快捷键说明

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