找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

帖子
查看: 1999|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

看門(mén)狗程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:116518 發(fā)表于 2016-4-25 11:14 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include <msp430x14x.h>
void Init_Port(void)
{
    //將P1口所有的管腳在初始化的時(shí)候設(shè)置為輸入方式
    P1DIR = 0;

    //將P1口所有的管腳設(shè)置為一般I/O口
    P1SEL = 0;

    // 將P1.4 P1.5 P1.6 P1.7設(shè)置為輸出方向
    P1DIR |= BIT4;
    P1DIR |= BIT5;
        P1DIR |= BIT6;
    P1DIR |= BIT7;

        //先輸出低電平
        P1OUT = 0x00;

        // 將中斷寄存器清零
    P1IE = 0;
    P1IES = 0;
    P1IFG = 0;
    //打開(kāi)管腳的中斷功能
        //對(duì)應(yīng)的管腳由高到低電平跳變使相應(yīng)的標(biāo)志置位
    P1IE |= BIT0;       
    P1IES |= BIT0;       
    P1IE |= BIT1;       
    P1IES |= BIT1;
        P1IE |= BIT2;       
    P1IES |= BIT2;       
    P1IE |= BIT3;       
    P1IES |= BIT3;
        _EINT();//打開(kāi)中斷
    return;
}
void Delay(void)
{
        int i;
        for(i = 100;i--;i > 0) ;//延時(shí)一點(diǎn)時(shí)間
}
int KeyProcess(void)
{
    int nP10,nP11,nP12,nP13;
        int nRes = 0;
        //P1.4輸出低電平
        P1OUT &= ~(BIT4);
        nP10 = P1IN & BIT0;
        if (nP10 == 0) nRes = 13;
        nP11 = P1IN & BIT1;
        if (nP11 == 0) nRes = 14;
        nP12 = P1IN & BIT2;
        if (nP12 == 0) nRes = 15;
        nP13 = P1IN & BIT3;
        if (nP13 == 0) nRes = 16;
        //P1.5輸出低電平
        P1OUT &= ~(BIT4);
        nP10 = P1IN & BIT0;
        if (nP10 == 0) nRes = 9;
        nP11 = P1IN & BIT1;
        if (nP11 == 0) nRes = 10;
        nP12 = P1IN & BIT2;
        if (nP12 == 0) nRes = 11;
        nP13 = P1IN & BIT3;
        if (nP13 == 0) nRes = 12;
        //P1.6輸出低電平
        P1OUT &= ~(BIT4);
        nP10 = P1IN & BIT0;
        if (nP10 == 0) nRes = 5;
        nP11 = P1IN & BIT1;
        if (nP11 == 0) nRes = 6;
        nP12 = P1IN & BIT2;
        if (nP12 == 0) nRes = 7;
        nP13 = P1IN & BIT3;
        if (nP13 == 0) nRes = 8;
        //P1.7輸出低電平
        P1OUT &= ~(BIT4);
        nP10 = P1IN & BIT0;
        if (nP10 == 0) nRes = 1;
        nP11 = P1IN & BIT1;
        if (nP11 == 0) nRes = 2;
        nP12 = P1IN & BIT2;
        if (nP12 == 0) nRes = 3;
        nP13 = P1IN & BIT3;
        if (nP13 == 0) nRes = 4;

        P1OUT = 0x00;//恢復(fù)以前值。

        //讀取各個(gè)管腳的狀態(tài)
        nP10 = P1IN & BIT0;
        nP11 = P1IN & BIT1;
        nP12 = P1IN & BIT2;
        nP13 = P1IN & BIT3;
        for(;;)
        {
                if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1)
                {
                        //等待松開(kāi)按鍵
                        break;
                }
        }
        return nRes;
}
// 處理來(lái)自端口 1 的中斷
interrupt [PORT1_VECTOR] void PORT_ISR(void)
{
        Delay();
        KeyProcess();
    if(P1IFG & BIT0)
    {           
            P1IFG &= ~(BIT0);// 清除中斷標(biāo)志位
    }
    if(P1IFG & BIT1)
    {
            P1IFG &= ~(BIT1);// 清除中斷標(biāo)志位
    }
    if(P1IFG & BIT2)
    {
            P1IFG &= ~(BIT2);// 清除中斷標(biāo)志位
    }
        if(P1IFG & BIT3)
    {
            P1IFG &= ~(BIT3);// 清除中斷標(biāo)志位
    }
}
void Init_CLK(void)
{
    unsigned int i;
    BCSCTL1 = 0X00;                        //將寄存器的內(nèi)容清零
                                        //XT2震蕩器開(kāi)啟
                                        //LFTX1工作在低頻模式
                                        //ACLK的分頻因子為1
                                       
    do
    {
        IFG1 &= ~OFIFG;                       // 清除OSCFault標(biāo)志
        for (i = 0x20; i > 0; i--);               
    }
    while ((IFG1 & OFIFG) == OFIFG);      // 如果OSCFault =1   
                                       
    BCSCTL2 = 0X00;                        //將寄存器的內(nèi)容清零
    BCSCTL2 += SELM1;                //MCLK的時(shí)鐘源為T(mén)X2CLK,分頻因子為1
    BCSCTL2 += SELS;                //SMCLK的時(shí)鐘源為T(mén)X2CLK,分頻因子為1
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表