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

📄 main.c

📁 这是一个msp430的键盘程序
💻 C
字号:
#include <msp430x24x.h>

#define uint unsigned int
#define uchar unsigned char

uchar flag=0;
uchar get_code(uchar i)
{
  unsigned char t;
  switch(i)
  {
    case(0): t = 0xc0; break;
    case(1): t = 0xf9; break;
    case(2): t = 0xa4; break;
    case(3): t = 0xb0; break;
    case(4): t = 0x99; break;
    case(5): t = 0x92; break;
    case(6): t = 0x82; break;
    case(7): t = 0xf8; break;
    case(8): t = 0x80; break;
    case(9): t = 0x90; break;
  }
  return t;
}

void display(uchar x)
{
  P4OUT = get_code(x%10);
  P5OUT = get_code(x/10);  
}

void Init_Port(void)
{       
    P1DIR = 0X00;
    P1DIR |=BIT4+BIT5+BIT6+BIT7;
    P1SEL = 0X00;
    P1OUT = 0x00;
    P1IE = 0;
    P1IES = 0;
    P1IFG = 0;
    //打开管脚的中断功能
	//对应的管脚由高到低电平跳变使相应的标志置位
    P1IE  |= BIT0;	
    P1IES |= BIT0;	
    P1IE  |= BIT1;	
    P1IES |= BIT1;
    P1IE  |= BIT2;	
    P1IES |= BIT2;	
    P1IE  |= BIT3;	
    P1IES |= BIT3;
    
    P4DIR = 0XFF;
    P4SEL = 0X00;
    P4OUT = 0X00;
    
    P5DIR = 0XFF;
    P5SEL = 0X00;
    P5OUT = 0X00;
    
    _EINT();//打开中断
    
  
 
}

#pragma vector=PORT1_VECTOR
__interrupt void PORT_ISR(void)
{
  flag=1;
   if(P1IFG & BIT0)
    {    	
    	P1IFG &= ~(BIT0);// 清除中断标志位
    }
    if(P1IFG & BIT1)
    {
    	P1IFG &= ~(BIT1);// 清除中断标志位
    }
    if(P1IFG & BIT2)
    {
    	P1IFG &= ~(BIT2);// 清除中断标志位
    }
	if(P1IFG & BIT3)
    {
    	P1IFG &= ~(BIT3);// 清除中断标志位
    }
}
  
      
void main()
{
  Init_Port();
  
  while(1)
  {
    while(flag)
    {
        int nRes = 0;
	int nP10;
	int nP11;
	int nP12;
	int nP13;
    
        //P1.4输出低电平
        P1OUT = 0XFF;
        P1OUT &= ~(BIT4);
        nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 13;
	nP11 = (P1IN & BIT1) >> 1;
	if (nP11 == 0) nRes = 14;
	nP12 = (P1IN & BIT2) >> 2;
	if (nP12 == 0) nRes = 15;
	nP13 = (P1IN & BIT3) >> 3;
	if (nP13 == 0) nRes = 16;
	//P1.5输出低电平
	P1OUT = 0XFF;
        P1OUT &= ~(BIT5);
        nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 9;
	nP11 = (P1IN & BIT1) >> 1;
	if (nP11 == 0) nRes = 10;
	nP12 = (P1IN & BIT2) >> 2;
	if (nP12 == 0) nRes = 11;
	nP13 = (P1IN & BIT3) >> 3;
	if (nP13 == 0) nRes = 12;
	//P1.6输出低电平
	P1OUT = 0XFF;
        P1OUT &= ~(BIT6);
        nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 5;
	nP11 = (P1IN & BIT1) >> 1;
	if (nP11 == 0) nRes = 6;
	nP12 = (P1IN & BIT2) >> 2;
	if (nP12 == 0) nRes = 7;
	nP13 = (P1IN & BIT3) >> 3;
	if (nP13 == 0) nRes = 8;
	//P1.7输出低电平
	P1OUT = 0XFF;
        P1OUT &= ~(BIT7);
        nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 1;
	nP11 = (P1IN & BIT1) >> 1;
	if (nP11 == 0) nRes = 2;
	nP12 = (P1IN & BIT2) >> 2;
	if (nP12 == 0) nRes = 3;
	nP13 = (P1IN & BIT3) >> 3;
	if (nP13 == 0) nRes = 4;

	P1OUT = 0x00;//恢复以前值。
        
        display(nRes);
        
        nP10 = P1IN & BIT0;
	nP11 = (P1IN & BIT1) >> 1;
	nP12 = (P1IN & BIT2) >> 2;
	nP13 = (P1IN & BIT3) >> 3;
	for(;;)
	{
		if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1)
		{
			//等待松开按键
			break;
		}
	}
    flag=0;
    }
  }
}

⌨️ 快捷键说明

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