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

📄 key.c

📁 该代码来自2008年湖北电子设计大赛位移测量装置制作,主要实现位移的自动测量和手动测量,msp430单片机
💻 C
字号:
#include"key.h"
#include"mada.h"
int keydata0[11]={0};
float keydata;
int q=99,i=0;//q为+-标志;i为keydata0[]复位
int nRes=2,nRes1;//nRes:as12控制 3:手动测量;2进入自动模式 nRes1:按键值
int modeselect=0;//模式选择
int flag=0;//小数点标
int DON=0,XON=0;
void Init_Port(void)
{
    //将P1口所有的管脚在初始化的时候设置为输入方式
    P1DIR = 0;
 
    //将P1口所有的管脚设置为一般I/O口
    P1SEL = 0;
    
    // 将P1.4 P1.5 P1.6 P1.7设置为输出方向
    P1DIR |= BIT4;
    P1DIR |= BIT5;
    P1DIR |= BIT6;
    P1DIR |= BIT7;
    //先输出低电平
    P1OUT = 0x00;
    // 将中断寄存器清零
    P1IE = 0;
    P1IES = 0;
    P1IFG = 0;
    //打开管脚的中断功能
	//对应的管脚由高到低电平跳变使相应的标志置位
    P1IE |= BIT0;	
    P1IES |= BIT0;	
    P1IE |= BIT1;	
    P1IES |= BIT1;
    P1IE |= BIT2;	
    P1IES |= BIT2;	
    P1IE |= BIT3;	
    P1IES |= BIT3;
    _EINT();//打开中断
    return;
}
void Delay(void)
{
	int i;
	for(i = 150;i--;i > 0) ;//延时一点时间
}
//键盘处理函数
unsigned char keyscan(void)
{
    unsigned char nP20,nP21,nP22,nP23;
    
    unsigned char nRes1 = 99;
      //先输出高电平
        P1OUT = 0xFF;
        
	//P1.4输出低电平
	P1OUT =0xef;
        
	nP20 = P1IN & BIT0;
	if (nP20 == 0) nRes1 = 0x0c;
	nP21 = (P1IN & BIT1)>>1;
	if (nP21 == 0) nRes1 = 0x0d;
	nP22 = (P1IN & BIT2)>>2;
	if (nP22 == 0) nRes1 = 0x0e;
	nP23 = (P1IN & BIT3)>>3;
	if (nP23 == 0) nRes1 = 0x0f;

	//P1.5输出低电平
	P1OUT =0xdf;
	nP20 = P1IN & BIT0;
	if (nP20 == 0) nRes1 = 0x08;
	nP21 = (P1IN & BIT1)>>1;
	if (nP21 == 0) nRes1 = 0x09;
	nP22 = (P1IN & BIT2)>>2;
	if (nP22 == 0) nRes1 = 0x0a;
	nP23 = (P1IN & BIT3)>>3;
	if (nP23 == 0) nRes1 = 0x0b;

	//P2.6输出低电平
	P1OUT =0xbf;
	nP20 = P1IN & BIT0;
	if (nP20 == 0) nRes1 = 0x04;
	nP21 = (P1IN & BIT1)>>1;
	if (nP21 == 0) nRes1 = 0x05;
	nP22 = (P1IN & BIT2)>>2;
	if (nP22 == 0) nRes1 = 0x06;
	nP23 = (P1IN & BIT3)>>3;
	if (nP23 == 0) nRes1 = 0x07;

	//P2.7输出低电平
	P1OUT =0x7f;
	nP20 = P1IN & BIT0;
	if (nP20 == 0) nRes1 = 0x00;
	nP21 = (P1IN & BIT1)>>1;
	if (nP21 == 0) nRes1 = 0x01;
	nP22 = (P1IN & BIT2)>>2;
	if (nP22 == 0) nRes1 = 0x02;
	nP23 = (P1IN & BIT3)>>3;
	if (nP23 == 0) nRes1 = 0x03;

        
	P1OUT =0;//恢复以前值。
        
        for(;;)
	{ 
	  //读取各个管脚的状态
	  nP20 = P1IN & BIT0;
	  nP21 = (P1IN & BIT1)>>1;
	  nP22 = (P1IN & BIT2)>>2;
	  nP23 = (P1IN & BIT3)>>3;
	      
	  if(nP20 == 1 && nP21 == 1 && nP22 == 1 && nP23 == 1)
	  {
		//等待松开按键
		break;
	  }
	}
        
        return nRes1;
}
// 处理来自端口 1 的中断
#pragma vector=PORT1_VECTOR
__interrupt void PORT_ISR(void)
{
   
    nRes1 = keyscan();
    stop_MOVE();
   // countvalue1(nRes1);
   keydata0[keydata0[9]]=nRes1;  
   if(keydata0[keydata0[9]]==12)
   {
   
     ClearScreen();
     ClearScreen();     
     WriteWord(0, 1, 19);
     WriteWord(1, 1, 20);
     WriteWord(10,1, 23);
     WriteWord(11,1,24);
     /*WriteWord(9,4,0);
     WriteWord(10,4,1);
     WriteWord(11,4,2);
     WriteWord(0,4,3);*/
     Writestring(11,4,"X_mm=");
     //Writestring(6,4, "mm");//手动模式下的位移
     DON=1;
     XON=0;
     Writestring(14,6,"D=");     
     keydata0[10]==0;
     nRes=3;
     q=99;
     for(i=0;i<11;i++) keydata0[i]=0;
     ADC12CTL0 |= ENC;
     ADC12CTL0 |= ADC12SC;
     _EINT();
    }
    else if(keydata0[keydata0[9]]<=11)
    {
         
      if(keydata0[10]==0||keydata0[keydata0[9]]==10)
   
      {

         ClearScreen();
         WriteWord(9, 1, 13);
         WriteWord(11, 1, 15);
         WriteWord(10, 1, 14);
         WriteWord(0,1, 16);
         WriteWord(9,4, 17);
         WriteWord(10,4, 18);
         Writechar(14,4, 10);
         Writestring(6,4, "mm");//自动到达指定位移
         Writestring(11,6,"X_mm=");
         DON=0;
         XON=1;
         q=99;
         keydata0[9]=0;
         keydata0[10]=1;
         flag=0;
         for(i=0;i<10;i++) keydata0[i]=0;
       }
      else if(keydata0[9]>=6) 
      {
       Writestring(8,6, "error_in");
       Writestring(0,6,"put");
      }
      else if(keydata0[keydata0[9]]==11)
          {
             if(flag==2)
             {
               keydata=keydata0[0]*10+keydata0[1]+
               keydata0[3]*0.1;
             }
             if(flag==1)
             {
               keydata=keydata0[0]+keydata0[2]*0.1;
             }
             countvalue(keydata);
             go_that(keydata,q);
             for(i=0;i<10;i++) keydata0[i]=0;
          }  
        else if(keydata0[keydata0[9]]<10) Writechar(keydata0[9]-1,4, keydata0[keydata0[9]++]);
        else _NOP();
     
    }
    if(keydata0[keydata0[9]]==15)
   {
     q=1;
     Writechar(15,4, 77); 
   }         
    if(keydata0[keydata0[9]]==14)
   {
     q=0;
     Writechar(15,4,76); 
   }
   if(keydata0[keydata0[9]]==13&&keydata0[9]!=0&&flag==0) //9.2
     {
       keydata0[keydata0[9]]=75;
       flag=keydata0[9];
       Writechar(keydata0[9]-1,4, keydata0[keydata0[9]++]);
     } 

  if(P1IFG & BIT0)
    {   	
        R_MOVE();
     
        P1IFG &= ~(BIT0);// 清除中断标志位
    }
    if(P1IFG & BIT1)
    {
    	L_MOVE();

        P1IFG &= ~(BIT1);// 清除中断标志位
    }
    if(P1IFG & BIT2)
    {
    	
      P1IFG &= ~(BIT2);// 清除中断标志位
    }
	if(P1IFG & BIT3)
    {
        P1IFG &= ~(BIT3);// 清除中断标志位
    }
  
    //else Writenumber(nRes,0,0,4);
}

⌨️ 快捷键说明

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