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

📄

📁 这是用89C2051制作的一个串口键盘
💻
字号:
/*-----------------------------------------------------
 使用说明:	该程序是实现两个单片机之间传送按键值,利用的是串行通讯模式1
            A、这个是从函数的程序,可以直接下载到2051里;
            B、这个函数可以以字符串的方式发送;调用SendChar(uchar ch)
            C、还可以使用数据方式进行发送; 调用SendStrlen(unsigned char *str,unsigned int strl)
            D、在使用时可以使用校验的方式防止数据发送错误;
            E、此函数加了软件看狗;
设 计 人: 	  KG		
修改时间:	2007-11-3   
//---------------------------------------------------*/
#include "reg51.h" 
#include <intrins.h>
#include <string.h>

#define uchar unsigned char 
#define	uint  unsigned int
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
sbit P1_3=P1^3;
sbit P1_4=P1^4;
sbit P1_5=P1^5;
sbit P1_6=P1^6;
sbit P1_7=P1^7;
sbit led=P3^7;
//---------------------------------------------------*/
//全局变量定义
unsigned char SendEnable;	//软件看门狗设置	
unsigned char B_key_value;  //串口校验使用
/*-----------------------------------------------------------------------------------
函 数 名:   Delay()
功    能:	延时
说    明:	延时时间0.5ms*i			最大延时127.5ms     12M
 			延时时间0.25ms*i		最大延时63.75ms		24M
入口参数:	unsigned char i
返 回 值:	无
/*---------------------------------------------------------------------------------*/
void Delay(unsigned char i)
{
	unsigned char j;
do{
//-------------------
	j=250;				//延时0.5ms
	do{
		j--;
	}while(j);
//-------------------
	i--;
	}while(i);
}
/*-----------------------------------------------------------------------------------
函 数 名:
功    能:	延时
说    明:	
入口参数:	unsigned char i
返 回 值:	无
设 计 人: 2007 11 3    KG
/*---------------------------------------------------------------------------------*/
void Delay500ms(unsigned char i)
{
	do{
		Delay(200);			//延时500ms
		i--;
		}while(i);
}
/*-----------------------------------------------------
函 数 名:
功    能:	打开及初始化串口
说    明:
入口参数:
返 回 值:
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/
void OpenSerialCom(void)
{
	SCON  =  0x50;			//设置串口工作模式1并打开串口
	TMOD  =  0x20;			//波特率
	PCON |=  0x80;
	TH1   =  0xfa;			//9600
	TL1	  =	0xfa;
	IE   |= 0x90;			//打开串行中断及总中断
	TR1   = 1;				//启动内部定时
}
/*-----------------------------------------------------
函 数 名:
功    能:	数据(字符串)串行发送
说    明:
入口参数:
返 回 值:
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/
void SendChar(uchar ch)
{
	ES=0;
	SBUF=ch;
	while(TI==0);
	TI=0;
	led=0;
	Delay500ms(1);		//1s
	led=1;
	ES=1;
}
/*-----------------------------------------------------
函 数 名:   SendStrlen()
功    能:	字符串串行发送
说    明:
入口参数:   *str: 所要发送字符串;
            str1: 发送字符串的个数。
返 回 值:   无
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/
void SendStrlen(unsigned char *str,unsigned int strl)
{
	unsigned int k=0;
	if(strl==0)
		strl=strlen(str);
	do{
		SendChar(*(str + k));
		k++;
		}while(k<strl);

	}
/*-----------------------------------------------------
函 数 名:
功    能:	接收主CPU返回的按键值并进行校验,错误则重发。
说    明:	从CPU的串行中断,接收到主CPU返回的数据,如果正确则把RXD置低.
             主CPU检测到该信号后确认键值。
入口参数:
返 回 值:	
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/ 
void B_Serial() interrupt 4
{
	unsigned char ch;
	if(RI)
	{	RI=0;
		IE &= 0x0f;
		ch=SBUF;
		if(ch!=B_key_value)			//校验
		{	SendChar(B_key_value);
			SendEnable+=1;			//允许发送
		}
		else
		{	led=0;
			REN=0;					//关闭串口
			RXD = 0;				//将检测位置0
			ch = 50;				//延时100us
			do{
				ch--;
			}while(ch);
			RXD = 1;
			REN = 1;				//打开串口
			Delay500ms(1);
			led=1;
			SendEnable=0;
		}
	}
}
/*-----------------------------------------------------
函 数 名:
功    能:	按键扫描
说    明:
入口参数:   无
返 回 值:   键值
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/
uint KeyScan()
{      
   uchar   a=16;  
      P1=0xF0;
      
   if((P1&0xF0)!=0xF0)
    {
     //Delay(120);//延时消抖
     if((P1&0xF0)!=0xF0)
      {
          if(P1_4==0)
            { //while(P1_4==0);
              P1=0X0F;
             while(P1_0==0){a=0;}//key();}
             while(P1_1==0){a=1;}//key();}
             while(P1_2==0){a=2;}//key();}
             while(P1_3==0){a=3;}//key();}
            }
           else if(P1_5==0)
            
             {// while(P1_5==0);
              P1=0X0F;
             while(P1_0==0){a=4;}     //key();}
             while(P1_1==0){a=5;}//key();}
             while(P1_2==0){a=6;}//key();}
             while(P1_3==0){a=7;}//key();}
            }
        else if(P1_6==0)
            
             {//while(P1_6==0);
                P1=0X0F;
             while(P1_0==0){a=8;}//key();}
             while(P1_1==0){a=9;}//key();}
             while(P1_2==0){a=10;}//key();}
             while(P1_3==0){a=11;}//key();}
             }
         else if(P1_7==0)
            
             {//while(P1_7==0);
                P1=0X0F;
             while(P1_0==0){a=12;}//key();}
             while(P1_1==0){a=13;}//key();}
             while(P1_2==0){a=14;}//key();}
             while(P1_3==0){a=15;}//key();}
             }    
       }
       //else return(20);  
      }
       B_key_value=a;
   return(a);
}
/*-----------------------------------------------------
函 数 名:
功    能:    IO口函数初始化, 指示灯闪烁两次
说    明:
入口参数:    无
返 回 值:    无
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/
void SystemInti()
{
   uint  i=0;
    SendEnable=0;    //初始化变量
      for(i; i<2; i++)
       {
          led=0;//系统开始,红灯闪烁两次 
	       Delay500ms(1); //0.5s
	       led=1;
           Delay500ms(1); //0.5s 
       }
    P1 &=0x00;
}
/*-----------------------------------------------------
函 数 名:   main()主函数
功    能:
说    明:
入口参数:
返 回 值:
设 计 人: 2007 11 3    KG
//---------------------------------------------------*/
void main()
{
           
       uint k;
            //P0=0Xff;
            //P2=0X00;
            //Delay500ms(20);
 start:
    SystemInti();         //系统初始化
	OpenSerialCom();    //串口初始化为模式1
	Delay(100);
	while(1)
	{//k=10;
		k=KeyScan();       //这个按键取值非常稳定
//k=10;
          if(k!=16)
          { P0=0Xff;
            P2=0X00;
            Delay500ms(20);
           SendChar(k);          //正常键值发送
           }
		if(SendEnable>=20) //软件看门狗,系统跑飞,重新开始
			goto start;
	}
}

⌨️ 快捷键说明

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