📄 main.c
字号:
#include<msp430x14x.h>
//unsigned char TX[8];
int j=0;
void Init_Port(void);
int KeyScan(void);
void Delay(void);
int Res=0;
int nRes;
main()
{
int i;
WDTCTL=WDTPW+WDTHOLD; //停止看门狗定时器
BCSCTL1 = 0X00; //将寄存器的内容清零
//XT2震荡器开启
//LFTX1工作在低频模式
//ACLK的分频因子为1
do
{
IFG1 &= ~OFIFG; // 清除OSCFault标志
for (i = 0x20; i > 0; i--);
}
while ((IFG1 & OFIFG) == OFIFG); // 如果OSCFault =1
BCSCTL2 = 0X00; //将寄存器的内容清零
BCSCTL2 += SELM1; //MCLK的时钟源为TX2CLK,分频因子为1(TX2CLK为8M)
BCSCTL2 += SELS; //SMCLK的时钟源为TX2CLK,分频因子为1
Init_Port();
P5OUT=0XFF;
P5SEL=0XFF;
U0CTL=0X00; //将寄存器内容清零
U0CTL+=CHAR; //数据位为8bit
U0TCTL=0X00; //将寄存器内容清零
U0TCTL+=SSEL1; //波特率发生器选择SMCLK
UBR0_0=0X45; //波特率为
UBR1_0=0X00;
UMCTL_0=0X49;
ME1|=UTXE0+URXE0; //使能UART0的TXD和RXD
IE1|=URXIE0; //使能UART0的RX中断
IE1|=UTXIE0; //使能UART0的TX中断
UCTL0&=~SWRST;
P3SEL|=BIT4; //设置P3.4为UART0的TXD
P3SEL|=BIT5; //设置P3.5为UART0的RXD
P3DIR|=BIT4; //P3.4为输出管脚
P5SEL=0XFF;
P5DIR=0XFF;
_EINT();
IFG1|=(UTXIFG0);
//TXBUF0=1;
while(1);
}
void Init_Port(void)
{
P1DIR = 0; //作为输入
P1SEL = 0; //一般i/o口
P1DIR |= BIT0; //P1.0-P1.3位输出
P1DIR |= BIT1;
P1DIR |= BIT2;
P1DIR |= BIT3;
P1OUT = 0x00;
}
int KeyScan(void)
{
int nP14,nP15,nP16,nP17;
for(;;)
{
nP14=(P1IN & BIT4)>>4;
nP15=(P1IN & BIT5)>>5;
nP16=(P1IN & BIT6)>>6;
nP17=(P1IN & BIT7)>>7;
if(nP14==0||nP15==0||nP16==0||nP17==0)
{
break;
}
}
Delay();
nP14=(P1IN & BIT4)>>4;
nP15=(P1IN & BIT5)>>5;
nP16=(P1IN & BIT6)>>6;
nP17=(P1IN & BIT7)>>7;
if(nP14==0||nP15==0||nP16==0||nP17==0)
{
//int Res=0;
int nP14,nP15,nP16,nP17;
P1OUT =0x0F;
P1OUT&=~(BIT0);
nP14=(P1IN & BIT4)>>4;
nP15=(P1IN & BIT5)>>5;
nP16=(P1IN & BIT6)>>6;
nP17=(P1IN & BIT7)>>7;
if (nP14==0) Res=1;
if (nP15==0) Res=2;
if (nP16==0) Res=3;
if (nP17==0) Res=4;
P1OUT|=(BIT0);
P1OUT&=~(BIT1);
nP14=(P1IN & BIT4)>>4;
nP15=(P1IN & BIT5)>>5;
nP16=(P1IN & BIT6)>>6;
nP17=(P1IN & BIT7)>>7;
if (nP14==0) Res=5;
if (nP15==0) Res=6;
if (nP16==0) Res=7;
if (nP17==0) Res=8;
P1OUT|=(BIT1);
P1OUT&=~(BIT2);
nP14=(P1IN & BIT4)>>4;
nP15=(P1IN & BIT5)>>5;
nP16=(P1IN & BIT6)>>6;
nP17=(P1IN & BIT7)>>7;
if (nP14==0) Res=9;
if (nP15==0) Res=10;
if (nP16==0) Res=11;
if (nP17==0) Res=12;
P1OUT|=(BIT2);
P1OUT&=~(BIT3);
nP14=(P1IN & BIT4)>>4;
nP15=(P1IN & BIT5)>>5;
nP16=(P1IN & BIT6)>>6;
nP17=(P1IN & BIT7)>>7;
if (nP14==0) Res=13;
if (nP15==0) Res=14;
if (nP16==0) Res=15;
if (nP17==0) Res=16;
P1OUT=0x00;
}
else return 0;
return Res;
}
void Delay(void)
{
int i;
for(i=0;i++;i<100);
}
#pragma vector=UART0TX_VECTOR
__interrupt void UART0_TX (void)
{
j++;
nRes=KeyScan();
TXBUF0=nRes;
if(j==8)
{
IFG1&=~(UTXIFG0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -