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

📄 swutl.c

📁 一个瑞萨单片机的程序。。。供大家学习用。。。。。。。。。。。。。。。。。。
💻 C
📖 第 1 页 / 共 2 页
字号:
/*""FILE COMMENT""*************************************************************
*  System Name : for eduction (NO TRANSFERRING)
*  File Name   : swutl.c
*  Contents    : control SW, key matrix
*  Model       : for OAKS8-LCD Board
*  CPU         : R8C/Tiny series
*  Compiler    : NC30WA(V.5.30 Release 1)
*  OS          : not be used
*  Programer   : RENESAS Semiconductor Training Center
*  Note        : for OAKS8-R5F21114FP(R8C/11 group,20MHz)
*******************************************************************************
* COPYRIGHT(C) 2004 RENESAS TECHNOLOGY CORPORATION
* AND RENESAS SOLUTIONS CORPORATION ALL RIGHTS RESERVED
*******************************************************************************
* History      : ---
*""FILE COMMENT END""*********************************************************/

/*===== include file =====*/
#include "defs.h"					/* define common symbol					*/
#include "target.h"					/* SFR section definition file					*/
#include "swutl.h"					/* header file for SW, key matrix control*/

/*===== define macro =====*/
#define ENTER_KEY	0x0B			/* ENTER key							*/
#define CLEAR_KEY 	0x09			/* CLEAR key							*/

typedef union{						/* define union for input data			*/
	unsigned int all;
	struct {
		unsigned int DUMMY : 4;
		unsigned int sw0_B : 3;		/* space for saving SW00,SW10,SW20 return input	*/
		unsigned int sw7_9 : 3;		/* space for saving SW01,SW11,SW21 return input	*/
		unsigned int sw4_6 : 3;		/* space for saving SW02,SW12,SW22 return input	*/
		unsigned int sw1_3 : 3;		/* space for saving SW03,SW13,SW23 return input	*/
	}line;
}DEF_KEYMTX;

/*===== define function prototype =====*/
static int judge_keyMTX(unsigned char *swdata);	/* function to control SW decision			*/
static int keymatrix_EX(DEF_KEYMTX *swdata);	/* function to control key matrix*/
static int detect_edge(unsigned int swdata);	/* function to control edge detection*/
static int cut_chat(unsigned int sw_input);	 	
										/* function to filter chattering and noise	*/
void INT_TimerY(void);			/* time Y interrupt function(measure sampling period)*/

/*===== define variable =====*/
static int init_hard;					/* variable for hardware initialization management	*/
										/* 0:not be initialized, 1:initialized	*/

static volatile unsigned char sw_data ;	/* number of SW pushed					*/
static volatile int F_keyfix ;			/* input data confirming flag				*/
										/* 0:not confirm, 1:confirmed				*/
static const unsigned char scan_table[] = {	/* scan line decided data		*/
	0xFD , 0xFB , 0xF7 , 0xEF 
};

/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : void SW__initialize1(void)
* function        : initialize key matrix and push switch
* parameter       : none
* return          : none
* function used   : none
* notice          : none
* History	      : ---
*""FUNC	COMMENT	END""*********************************************************/
void SW__initialize1(void)
{
	init_hard = TRUE;	/* SW "initialized"									*/

	/*----- initialize key matrix control port ----- */
	p0		= 0xFD;			/* active scan line forSW00,SW10,SW20	*/
	prc2	= 1;			/* cancel port P0 direction register protection				*/
	pd0		= 0x1E;			/* set key matrix scan line as output			*/
	prc2	= 0;			/* set port P0 direction register protection		*/
	pu06	= 1;			/* connect key matrix return line
							(port P30 to port P33) with pull-up resistance			*/
	pu11	= 1 ; 			/* connect key matrix return line
							(port P45) with pull-up resistance 					*/
	
	SW__initialize2();		/* initialize push switch SW5					*/
	
	/*----- initialize timer Y ----- */
	tyzmr &= 0xF0;					/* set timer Y, Z mode register		 */
									/* ????0000								 */
									/* |||||||+ timer mode				 */
									/* |||||++- not be used					 */
									/* ||||+--- stop count				 */
									/* ++++---- bit related to timer Z 			 */
	tcss |= 0x04;					/* timer count source setting bit		 */
									/* ????01??								 */
									/* ||||||++ bit related to timer X			 */
									/* ||||++-- select f8(400ns) 			 */
									/* ||++---- bit related to timer Z			 */
									/* ++------ reservation bit					 */
	prey = 50-1;					/* set count value					 */
	typr = 100-1;					/* 		2ms = 400ns * (50 * 100)			 */
	tyic = 0x02;					/* set interrupt (priority level:2)			 */

	tys  = 1; 						/* start count 						 */
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : void SW__initialize2(void)
* functin         : initialize push switch SW5
* parameter       : none
* return          : none
* function used   : none
* notice          : none
* History	      : ---
*""FUNC	COMMENT	END""*********************************************************/
void SW__initialize2(void)
{
	prc2	= 1;		/* cancel port P0 direction register protection				*/
	pd0_5	= 0;		/* set bit 5 of port P0
						(push switch SW5 input port) as input			*/
	prc2	= 0;		/* set port P0 direction register protection				*/

	pu01	= 1;		/* connect push switch SW5 input port with pull-up resistance*/
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : int  SW__getchar(void)
* function        : inut alphabet
*                 : SW23:'A'  SW22:'B'  SW21:'C'  SW20:'#'
*                 : SW13:'D'  SW12:'E'  SW11:'F'  SW10:'0'
*                 : SW03:'G'  SW02:'H'  SW01:'I'  SW00:'*'
*                 : return ASCII code(from 0x41 to 0x49) of alphabet(from 'A' to 'I')
*                 : that is assigned in key matrix
*                 : return ERR if port that control SW is not be initialized
* parameter       : none
* return          : alphabet('A' to 'L') input from key matrix
* function used   : none
* notice          : use global variable sw_data, F_keyfix
* History	      : ---
*""FUNC	COMMENT	END""*********************************************************/
int SW__getchar(void)
{
	unsigned char  key_arrenge[] = {	/* key arrenge data(ASCII code)		*/
		'A' , 'D' , 'G' , 
		'B' , 'E' , 'H' , 
		'C' , 'F' , 'I' , 
		'#' , '0' , '*' 
	};

	if(init_hard){					/* if initialized					*/
		while(!F_keyfix);			/* wait input fix						*/
		F_keyfix = FALSE;			/* clear input data fix flag			*/
		return ((int)key_arrenge[sw_data]);	
									/* change to ASCII code and return			*/
	}
									/* if not be initialized 			 	*/
	return	ERR;					/* 	return error					 	*/
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : int  SW__gets(unsigned char *s)
* function        : input string
*                 : SW23:'A'  SW22:'B'  SW21:'C'  SW20:'#'
*                 : SW13:'D'  SW12:'E'  SW11:'F'  SW10:'0'
*                 : SW03:'G'  SW02:'H'  SW01:'I'  SW00:'*'
*                 : input character(from 'A' to 'I') assigned in key matrix
*                 : until ENTER key is input, return number of input characters
*                 : add '\0' at the end of input string
*                 : if only input ENTER, save '\0' and return 0
*                 : return ERR if port for controlling SW is not initialized
* parameter       : const unsigned char *s : pointer to space to save string
* return          : number of input characters 
* function used   : none
* notice          : use global variable sw_data, F_keyfix
* History      	  : ---
*""FUNC	COMMENT	END""*********************************************************/
int SW__gets(unsigned char *s)
{
	unsigned char  key_arrenge[] = {	/* key arrenge data(ASCII code)		*/
		'A' , 'D' , 'G' , 
		'B' , 'E' , 'H' , 
		'C' , 'F' , 'I' , 
		'#' , '0' , '*' 
	};

	unsigned char i ;				/* variable for character count					*/

	if(init_hard){					/* if initialized				*/
		for(i = 0; ; i++){
			while(!F_keyfix);		/* wait input fix							*/
			F_keyfix = FALSE;		/* clear input data fix flag			*/

			if(ENTER_KEY == sw_data) break;	/* complete with ENTER input		*/
			*(s + i) = key_arrenge[sw_data];/* change to ASCII code		*/
											/* save in buffer				*/
		} 									
		*(s + i) = '\0';			/* save null code at last				*/
		
		return i;					/* return character number							*/
	}
									/* if not be initialized					 	*/
	return	ERR;					/* return error					 	*/
}


/*""FUNC COMMENT""*************************************************************
* ID              : ---
* function name   : int  SW__getnum(void)
* function        : input SW number 0 to 9
*                 : SW23:1  SW22:2  SW21:3  SW20:'#'
*                 : SW13:4  SW12:5  SW11:6  SW10:0
*                 : SW03:7  SW02:8  SW01:9  SW00:'*'
*                 : if port for controlling SW is not initialized, return ERR
* parameter       : none
* return          : SW number(0乣11) input from key matrix
* function used   : none
* notice          : use global variable 'sw_data', 'F_keyfix'
* History	      : ---
*""FUNC	COMMENT	END""*********************************************************/
int SW__getnum(void)
{
	unsigned char  key_arrenge[] = {	/* key arrenge data(value)				*/
		 1   , 4 ,  7   , 
		 2   , 5 ,  8   , 
		 3   , 6 ,  9   , 
		'#'  , 0 , '*' 
	};

	if(init_hard){					/* if initialized 					*/
		while(!F_keyfix);			/* wait input fix							*/
		F_keyfix = FALSE;			/* clear input data fix flag			*/

⌨️ 快捷键说明

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