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

📄 keyboard.c

📁 使用ARM7在Embest IDE对于S3CEV40进行时钟万年历设计.时钟可以有闹钟的功能.
💻 C
字号:
/*********************************************************************************************
* File:	keyboard.c
* Author:	embest
* Desc:	keyboard source code
* History:	
*********************************************************************************************/

/*--- include files ---*/
#include "44b.h"
#include "44blib.h"
#include "def.h"
#include "keyboard.h"
#include "lcd.h"
#include "rtc.h"

/*--- global variables ---*/
extern int  g_nYear,g_nMonth,g_nDay,g_nWeekday,g_nHour,g_nMin,g_nSec;
extern char *g_szDate[8];

extern char sDATE[30];
extern char sTIME[10];

int dateflag;
int timeflag;
int tdflag=0;					//Set mode flag
int keycnt=0;					//Count the left or right the key count(left +1,right -1)

/* keyboard control address */
volatile UCHAR *keyboard_base = (UCHAR *)0x06000000;

/*--- function declare ---*/
inline int key_read();

void init_keyboard();
void close_keyboard();
void KeyboardInt(void) __attribute__ ((interrupt ("IRQ")));

void Eint4567Isr(void) __attribute__ ((interrupt ("IRQ")));

/*--- function code ---*/
int numInput[10]={0};//保存键盘输入值 0~9
int inputIndex=0; // 当前要保存的numInput数组下标
/*********************************************************************************************
* name:		Test_Keyboard
* func:		test keyboard
* para:		none
* ret:		none
* modify:
* comment:		
********************************************************************************************/
void Test_Keyboard()
{
	// init keyboard
	init_keyboard();
	Uart_Printf("\nPlease press one key on keyboard and look at LED ...\n");
	// close keyboard
	// close_keyboard();
}

/*********************************************************************************************
* name:		init_keyboard
* func:		init keyboard interrupt
* para:		none
* ret:		none
* modify:
* comment:		
********************************************************************************************/
void init_keyboard()
{
	/* enable interrupt */
	rINTMOD = 0x0;
	rINTCON = 0x1;
		
	/* set EINT1 interrupt handler */
	rINTMSK =~(BIT_GLOBAL|BIT_EINT1|BIT_EINT4567);
    pISR_EINT1 = (int)KeyboardInt;
    pISR_EINT4567 = (int)Eint4567Isr;
    
    /* PORT G */
    rPCONG  = 0xffff;					// EINT7~0
	rPUPG   = 0x0;						// pull up enable	    
	rEXTINT = rEXTINT|0x22222222;				// EINT1 falling edge mode
	
	rI_ISPC = BIT_EINT1|BIT_EINT4567;	// clear pending bit
	rEXTINTPND = 0xf;					// clear EXTINTPND reg
}

/*********************************************************************************************
* name:		close_keyboard
* func:		close keyboard interrupt
* para:		none
* ret:		none
* modify:
* comment:		
********************************************************************************************/
void close_keyboard()
{
	pISR_EINT1 = NULL;
	pISR_EINT4567 = NULL;
	/* Mask interrupt */
	rINTMSK = rINTMSK | BIT_EINT1 | BIT_EINT4567;
}

/*********************************************************************************************
* name:		KeyboardInt
* func:		keyboard interrupt handler function
* para:		none
* ret:		none
* modify:
* comment:		
********************************************************************************************/
void KeyboardInt(void)
{
	int value;

	
	rI_ISPC = BIT_EINT1;			// clear pending bit
		
	value = key_read();
	
	if(value > -1)
	 {
	 	if(value==2)				//Display the date
	 	{
			Lcd_DspAscII8x16(10,20,WHITE,sTIME);

			rtc_read();	
			sprintf(sDATE,"%4x-%2x-%2x",g_nYear,g_nMonth,g_nDay);
			Lcd_DspAscII8x16(10,20,BLUE,sDATE);
			dateflag=1;
			timeflag=0;
	 	}
	 	else if(value==10)			//Display the time
	 	{
			Lcd_DspAscII8x16(10,20,WHITE,sDATE);

			rtc_read();	
			sprintf(sTIME,"%s %02x:%02x:%02x\r",g_szDate[g_nWeekday],g_nHour,g_nMin,g_nSec);
			Lcd_DspAscII8x16(10,50,BLUE,sTIME);
			dateflag=0;
			timeflag=1;

	 	}
	 	else if(value==6)			//Set the date and time
		{	
			if(tdflag==0)
			{
				tdflag=1;
			}
			else
			{
				tdflag=0;
				keycnt=0;
			}
		}
		
		if(tdflag==1)				//In the Set mode
		{
			if(dateflag==1)			//Set the date
			{
				if(value==7)		//right choose ,keycnt++
				{
					keycnt++;
				}
				else if(value==5)	//left choose ,keycnt++
				{
					keycnt--;
				}
				if(keycnt<=0)
				{
					keycnt=0;
				}
				if(keycnt==0)		//Set year
				{
					if(value==2)	//year +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDYEAR +=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDYEAR -=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
				else if(keycnt==1)		//Set month
				{
					if(value==2)	//month +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDMON +=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDMON -=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
				else if(keycnt==2)		//Set day
				{
					if(value==2)		//day +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDDAY +=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDDAY -=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
			}	
			else if(timeflag==1)			//Set the time
			{
				if(value==7)		//right choose ,keycnt++
				{
					keycnt++;
				}
				else if(value==5)	//left choose ,keycnt++
				{
					keycnt--;
				}
				
				if(keycnt<=0)
				{
					keycnt=0;
				}
				
				if(keycnt==0)		//Set date
				{
					if(value==2)	//date +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						g_nWeekday +=1 ;
						rBCDDATE=g_nWeekday;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						g_nWeekday -=1 ;
						rBCDDATE=g_nWeekday;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
				else if(keycnt==1)		//Set hour
				{
					if(value==2)	//month +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDHOUR +=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDHOUR -=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
				else if(keycnt==2)		//Set minute
				{
					if(value==2)		//minute +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDMIN +=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDMIN -=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
				else if(keycnt==3)		//Set Second
				{
					if(value==2)		//Second +1
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDSEC +=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
					else if(value==10)
					{
						rRTCCON  = 0x01;						// R/W enable, 1/32768, Normal(merge), No reset
						rBCDSEC -=1 ;
						rRTCCON  = 0x0;							// R/W disable, 1/32768, Normal(merge), No reset
					}
				}
			}
		}
     }

}

/*********************************************************************************************
* name:		key_read
* func:		read key value
* para:		none
* ret:		key value, -1 -- error
* modify:
* comment:		
********************************************************************************************/
inline int key_read()
{
	int value;
	char temp;
	
	/* read line 1 */
	temp = *(keyboard_base+0xfd);
	/* not 0xF mean key down */
	if(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK)
	{
		if( (temp&0x1) == 0 )
			value = 3;
		else if( (temp&0x2) == 0 )
			value = 2;
		else if( (temp&0x4) == 0 )
			value = 1;
		else if( (temp&0x8) == 0 )
			value = 0;
		while(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK) // release
		temp = *(keyboard_base+0xfb);
		return value;
	}
	
	/* read line 2 */
	temp = *(keyboard_base+0xfb);
	/* not 0xF mean key down */
	if(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK)
	{
		if( (temp&0x1) == 0 )
			value = 7;
		else if( (temp&0x2) == 0 )
			value = 6;
		else if( (temp&0x4) == 0 )
			value = 5;
		else if( (temp&0x8) == 0 )
			value = 4;
		while(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK) // release
		temp = *(keyboard_base+0xfb);
		return value;
	}
	
	/* read line 3 */
	temp = *(keyboard_base+0xf7);
	/* not 0xF mean key down */
	if(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK)
	{
		if( (temp&0x1) == 0 )
			value = 0xb;
		else if( (temp&0x2) == 0 )
			value = 0xa;
		else if( (temp&0x4) == 0 )
			value = 9;
		else if( (temp&0x8) == 0 )
			value = 8;
		while(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK) // release
		temp = *(keyboard_base+0xfb);
		return value;
	}
	
	/* read line 4 */
	temp = *(keyboard_base+0xef);
	/* not 0xF mean key down */
	if(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK)
	{
		if( (temp&0x1) == 0 )
			value = 0xf;
		else if( (temp&0x2) == 0 )
			value = 0xe;
		else if( (temp&0x4) == 0 )
			value = 0xd;
		else if( (temp&0x8) == 0 )
			value = 0xc;
		while(( temp & KEY_VALUE_MASK) != KEY_VALUE_MASK) // release
		temp = *(keyboard_base+0xfb);
		return value;
	}
	
	return -1;
}

/*********************************************************************************************
* name:		Eint4567Isr
* func:	
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void Eint4567Isr(void)
{
	unsigned char which_int;

	Uart_Printf("\n SB2 or SB3 had pressed and light LED.");
    which_int=rEXTINTPND;
    rEXTINTPND=0xf;				//clear EXTINTPND reg.		
    rI_ISPC=BIT_EINT4567;		//clear pending_bit
    
    if(which_int == 4)			// SB2
    {
    	Led_Display(0x0);	
    	Led_Display(0x1);	
    }
    else if(which_int == 8)		// SB3
    {
    	Led_Display(0x0);	
    	Led_Display(0x2);	
    }
}

⌨️ 快捷键说明

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