往下看, 括号里 #include <msp430x16x.h> #include "cry1602.h" typedef unsigned char uchar; typedef unsigned int uint; unsigned char keypoint=0;
void delay(int v) //延时函数 { while(v!=0)v--; }
unsigned char key(void) //按键分析函数 { unsigned char a; unsigned char x=0xff; P2OUT=0Xe0; //扫描第一行 a=P2IN&0X0F; switch(a) { case 0x0E: x=0;break; case 0x0D: x=1;break; case 0x0B: x=2;break; case 0x07: x=3;break; default:break; } P2OUT=0Xd0; //扫描第二行 a=P2IN&0X0F; switch(a) { case 0X0E: x=4;break; case 0X0D: x=5;break; case 0X0B: x=6;break; case 0X07: x=7;break; default:break; } P2OUT=0Xb0; //扫描第三行 a=P2IN&0X0F; switch(a) { case 0X0E: x=8;break; case 0X0D: x=9;break; case 0X0B: x=10;break; case 0X07: x=11;break; default:break; } P2OUT=0X70; //扫描第四行 a=P2IN&0X0F; switch(a) { case 0X0E: x=12;break; case 0X0D: x=13;break; case 0X0B: x=14;break; case 0X07: x=15;break; default:break; } return(x); } unsigned char keypress(void) //分析按键是否按下函数 { unsigned char x=0; P2OUT=0x00; //键盘硬件:P20--P23为列线,最左边一根为P20,列线120k电阻上拉, if((P2IN&0X0f)!=0X0F) x=1; //P24--P27为行线,最上边一根为P24, return(x); // 无按键,返回 0; 有按键返回 1 } #pragma vector=PORT2_VECTOR
__interrupt void port2key(void) { if(keypress()) { delay(300) ; //消抖 if(keypress()) { keypoint=key(); } while(keypress()); //等待按键松开 } P2OUT=0X00; P2IFG=0X00; //清除中断标志 } void init(void) //初始化 { P2DIR=0Xf0; //P20到P23输入 P24到P27输出 P2SEL=0X00; //p2为普通I/O管脚 P2IES=0X0f; //列线下降沿允许P2中断 P2IE=0X0f; //P20到P23允许中断 P2OUT=0X00; P2IFG=0X00; //中断寄 } void action(uchar p) { switch (p) { case 0: DAC12_0DAT+=0X03D; //BUJIN 0.1v DAC12_0DAT&=0XFFF; DisplChar(6,1,'0'); /*case 1: (从这开始) DAC12_0DAT++; DAC12_0DAT&=0XFFF; case 2: DAC12_0DAT-=0X03D; DAC12_0DAT&=0XFFF; DisplChar(6,1,'1'); */ case 3: DAC12_0CTL&=~ENC; case 4: DAC12_0CTL|= ENC; case 5: DAC12_0DAT--; DAC12_0DAT&=0XFFF; case 6: DAC12_0DAT++; DAC12_0DAT&=0XFFF; (结束) } } void main (void) { WDTCTL = WDTPW + WDTHOLD; ADC12CTL0=REF2_5V+REFON; //内部参考源2.5V DAC12_0CTL=DAC12IR+DAC12AMP_5+DAC12ENC; DAC12_0DAT = 0x0666; // 1V init(); _EINT(); while(1) { if(keypoint!=0) action(keypoint); keypoint=0; } }
|