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

📄 hello.c

📁 使用单片机进行按键操作
💻 C
字号:
/*------------------------------------------------------------------------------
HELLO.C

Copyright 1995-2005 Keil Software, Inc.
------------------------------------------------------------------------------*/

#include <REG52.H>                /* special function register declarations   */
/* for the intended 8051 derivative         */

#include <stdio.h>                /* prototype declarations for I/O functions */
#include <stdlib.h>                      /* standard library .h-file          */
#include <ctype.h>                       /* character functions               */


#ifdef MONITOR51                         /* Debugging with Monitor-51 needs   */
char code reserve [3] _at_ 0x23;         /* space for serial interrupt if     */
#endif                                   /* Stop Exection with Serial Intr.   */



//定义按健
	sbit K1 = P3^4;
	sbit K2 = P3^5;

	sbit LED = P1^0;

xdata unsigned char sendstr[32]  _at_ 0x1000; 


//延时
void delay(unsigned int i)
{	 
	  
	
}



/*------------------------------------------------
The main C function.  Program execution starts
here after stack initialization.
------------------------------------------------*/
void main (void) {

	char i;  



#ifndef MONITOR51
	SCON  = 0x50;		        /* SCON: mode 1, 8-bit UART, enable rcvr  101 0000    */
	RCAP2H = 0xFF;
	TH2   = 0xff;                
	RCAP2L =0xBF;
	TL2  =  0xFB;
	T2CON = 0x34;			    //0011 0100
	//TR1   = 1;                  /* TR1:  timer 1 run                          */
	TI    = 1;                  /* TI:   set TI to send first char of UART    */
	ES  =0;
	EA =1;
#endif


	/*------------------------------------------------
	Note that an embedded program never exits (because
	there is no operating system to return to).  It
	must loop and execute forever.
	------------------------------------------------*/

	   //设置
		K1=1;
		K2=1;
		LED =1;
	  
	  while (1) {
	    //P1 ^= 0x01;     		    /* Toggle P1.0 each time we print */
		
		
		//如果K1为0,按下,灯灭	
		if(K1==0)
			{
				printf("K1==0");
			   	LED =0;
			}
		
		//如果K2为0
		if(K2==0)
			{
				//LED = 1; 		//按下,灯亮
				LED ^=1;	   //K2按下,取反
				printf("K2==0");
				
			}
		
	  }



}


⌨️ 快捷键说明

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