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

📄 e09b.c

📁 一个瑞萨单片机的程序。。。供大家学习用。。。。。。。。。。。。。。。。。。
💻 C
字号:
/*""FILE COMMENT""*************************************************************
 * System Name : for eduction (NO TRANSFERRING)
 * File Name   : e09b.c
 * Contents    : embedded C language entrance course
 *             : exercise 9B confirm operation to cancel chattering and noise
 * Model       : for OAKS8-LCD Board
 * CPU         : R8C/Tiny series
 * Compiler    : NC30WA	V.5.30 Release 1 
 * OS          : not be used
 * Programmer  : 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 definition file 								*/
#include "swutl.h"			/* header file control key matrix, SW	*/
#include "ledutl.h"			/* header file control LED					*/
#include "txutl.h"			/* header file control TimerX 				*/

/*===== define macro =====*/
								/* decide LED digit 								*/
#define LED0	0				/* LED0 digit									*/
#define LED1	1				/* LED1 digit									*/
#define LED2	2				/* LED2 digit									*/
#define LED3	3				/* LED3 digit									*/

/*===== define function prototype =====*/
void main(void); 
static void read_sw5_edge(void);/* input SW5, detect edge sense	*/
static unsigned char read_sw5(void);
								/* input SW5(cancel chattering)	*/
static int count_up(int count);	/* count up from 0 to 9	*/

/*===== define variable =====*/
static FLAG edge_sence;				/* flag for detecting edge sense			*/
#define F_edge_sw5 edge_sence.bit.b0	/* flag for detecting SW5 pushed			*/
										/* 0:FALSE:SW5 not be pushed 		*/
										/* 1:TRUE :SW5 be pushed		*/

/*""FUNC COMMENT""*************************************************************
* ID            : ---
* function name : void main(void)
* function      : LCD display changes whenever SW5 is pushed
*               : timer runs periodly(polling)
* parameter     : none
* return        : none
* function used : SW__initialize2, LED__initialize, TX__initialize, LED__puthex
*               : read_sw5_edge, count_up
* notice        : none
* History       : ---
*""FUNC COMMENT END""*********************************************************/
void main(void)
{
	int  counter = 0;				/* define variable for counting number of SW5 pushed	*/

	SW__initialize2();				/* initialize push switch SW5		*/
	LED__initialize(); 		   		/* initialize LCD  						*/
	TX__initialize();				/* initialize timer X(5ms period)			*/

 	LED__puthex(LED0, 0) ;			/* initial display 							 	*/

    /* infinite loop */
    while(1){
        /* measure 5ms passed */
        if(SET == ir_txic){      	/* when 5ms passed, timer X interrupt    	*/
                                    /* set request bit(bit 3) as 1 			*/
            ir_txic = CLEAR;       	/* clear timer X interrupt request bit 	*/

			read_sw5_edge();		/* input SW5, detect edge sense 	*/
			
			/* SW5 is pushed 	*/
			if(F_edge_sw5){			/* SW5 is pushed  			*/
				F_edge_sw5 = FALSE;	/* re-initialize flag for detecting SW5 pushed		*/
				
				counter = count_up(counter);	/* count number of push	*/
				LED__puthex(LED0, counter);		/* display number of push at LED0	*/
			}
		}
    }
}


/*""FUNC COMMENT""*************************************************************
* ID            : ---
* function name : static void read_sw5_edge(void)
* function      : input SW5, detect edge sense
* parameter     : none
* return        : none
* function used : read_sw5
* notice        : remember in detection flag(F_edge_SW5) when SW5 is pushed
* History       : ---
*""FUNC COMMENT END""*********************************************************/
#define	SW5_ON	0x20			/* SW5 is ON(bit 5) 					*/
#define SW5_OFF	0x00			/* SW5 is OFF 							*/
static void read_sw5_edge(void)
							/* define function to input SW5, detect edge sense	*/
{
    unsigned char Input;		/* fixed key input data this time	*/
    static unsigned char OldInput = SW5_ON;
    							/* fixed key input data last time	*/

	Input = read_sw5();			/* input SW5, get input status with more than or equal twice	*/

    if((SW5_ON == Input) && (SW5_OFF == OldInput)){
        F_edge_sw5 = TRUE;		/* set flag for SW5 pushed		*/
    }

    OldInput = Input;			/* remember fixed input data this time		*/
}


/*""FUNC COMMENT""*************************************************************
* ID            : ---
* function name : static unsigned char read_sw5(void )
* function      : input SW5(cancel chattering)
* parameter     : none
* return        : unsigned char  ; fixed input data from SW5
* function used : none
* notice        : measure port input status, "L" is ON, "H" is OFF,
*               : mask all other than SW5 input port with "1", reverse active
*               : not update data when no more than or equal twice
* History       : ---
*""FUNC COMMENT END""*********************************************************/
static unsigned char read_sw5(void)
						/* define function to input SW5(cancel chattering)	*/
{
	static unsigned char FixdInput = 0;	/* fixed key input data(initial is OFF)  */
    unsigned char        NewInput;		/* key input data this time             */
    static unsigned char OldInput = SW5_ON;
    									/* key input data last time             */
    static unsigned char CountAgree;    /* same numbe of key input data         */
    
    /* input SW5(mask processing, change active) */
    NewInput = p0;          /* input port P0                               */

    NewInput |= 0xdf;       /* mask all other than SW5 input port with "1"          */
    NewInput ^= 0xff;       /* reverse active(measure as "1" when SW is ON)*/
    
    /* compare input data between this time and last time */
    if( NewInput != OldInput ){
        /*--- inut data this time is not same as last time ---*/
        CountAgree = 0;                 /* initialize same counter                */

        /* remember input data differ from last time */
        OldInput = NewInput;
    }
    else{
        /*--- inut data this time is the same as last time ---*/
        ++CountAgree;                   /* count same counter               */
		
	    /* the same input data is more than or equal twice */
	    if( 2 <= CountAgree ){
	        CountAgree = 0;             /* fix if same to the third time      */
	        FixdInput = NewInput;       /* fix key input data               */
	    }
    }

    return FixdInput;					/* return fixed key input data			*/
}


/*""FUNC COMMENT""*************************************************************
* ID            : ---
* function name : static int count_up(int count)
* function      : count up from 0 to 9
* parameter     : count丗data for counting up
* return        : int丗data after plus 1
* function used : none
* notice        : when more than 9, return to 0
* History       : ---
*""FUNC COMMENT END""*********************************************************/
static int count_up(int count)	
						/* define function to count up from 0 to 9	*/
{
	if(9 > count){	/* less than 9 		*/
		count++;	/* count up		*/
	}
	else{			/* if become 10, return 0	*/
		count = 0;
	}

	return count;
}


/******************************************************************************
    end of file
******************************************************************************/

⌨️ 快捷键说明

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