在线情况
楼主
  • 头像
  • 级别
    • 积分2
    • 经验115
    • 文章4
    • 注册2010-05-19
    430 中断一个问题
    include [COLOR=#000000]<[/COLOR][COLOR=#000000]msp430x23x.h[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#000000]
    typedef unsigned [/COLOR][COLOR=#0000ff]char[/COLOR][COLOR=#000000] uchar;
    typedef unsigned [/COLOR][COLOR=#0000ff]int[/COLOR][COLOR=#000000]  [/COLOR][COLOR=#0000ff]uint[/COLOR][COLOR=#000000];

    uchar NUM_LED[[/COLOR][COLOR=#800080]16[/COLOR][COLOR=#000000]][/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000]{[/COLOR][COLOR=#800080]0x3f[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x06[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x5b[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x4f[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x66[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x6d[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x7d[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x07[/COLOR][COLOR=#000000],
                       [/COLOR][COLOR=#800080]0x7f[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x6f[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x77[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x7c[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x39[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x5e[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x79[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800080]0x71[/COLOR][COLOR=#000000]}; [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000] 0-f 阴二极管[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000] Delay_1ms([/COLOR][COLOR=#0000ff]uint[/COLOR][COLOR=#000000] i);
    [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000] led_Display(uchar KEY);
    uchar scankey([/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000]);

    uchar key; [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]全局变量,接收按键的值[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000] main()
    {
      WDTCTL [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] WDTPW [/COLOR][COLOR=#000000]+[/COLOR][COLOR=#000000] WDTHOLD;   [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]关闭看门狗[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]  
      P1IES[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000];  [/COLOR][COLOR=#808080]///[/COLOR][COLOR=#008000]/P1口键盘检测(中断法)P1.0-P1.3下降沿中断[/COLOR][COLOR=#808080]
    [/COLOR][COLOR=#000000]  P1IE[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000];
      P1IFG[/COLOR][COLOR=#000000]&=~[/COLOR][COLOR=#800080]0X08[/COLOR][COLOR=#000000];[/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]初始化不产生中断[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]  
      P1DIR[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0XF0[/COLOR][COLOR=#000000];  [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]低4位读入检测电平,高4位输出测量电平[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]  P1OUT[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000];
     
      P5DIR[/COLOR][COLOR=#000000]|=[/COLOR][COLOR=#000000]BIT0[/COLOR][COLOR=#000000]+[/COLOR][COLOR=#000000]BIT1; [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]选择数码管。[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]  P4DIR[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0XFF[/COLOR][COLOR=#000000];[/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]选择数码管的内容。[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000] _EINT();                    [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]打开全局中断控制位[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000] LPM0;
     [/COLOR][COLOR=#0000ff]while[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800080]1[/COLOR][COLOR=#000000])
     {
         [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]LPM0;  [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]叫一直处于睡眠状态,有按键中断的时候出中断直接处理。
         [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]led_Display(scankey[color=#FF0000]());[/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]这里不起作用。数码管一直没动作。[/color]  }[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]  
    }

    uchar scankey([/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000])
    {
       uchar i;
       P1OUT[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0x7f[/COLOR][COLOR=#000000];  [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]判断第一行。判断的是输入,故需要0 --- 0111 1111[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    [/COLOR][COLOR=#0000ff]switch[/COLOR][COLOR=#000000](P1IN [/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000])
        {
        [/COLOR][COLOR=#0000ff]case[/COLOR][COLOR=#000000]  [/COLOR][COLOR=#800080]0X0e[/COLOR][COLOR=#000000]:        [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]第一列[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    [/COLOR][COLOR=#0000ff]for[/COLOR][COLOR=#000000](i[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]255[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]--[/COLOR][COLOR=#000000]); [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]延时[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    [/COLOR][COLOR=#0000ff]while[/COLOR][COLOR=#000000](P1IN[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]!=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]);  [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]确认按键释放[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    key[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];
        [/COLOR][COLOR=#0000ff]break[/COLOR][COLOR=#000000];
       
        [/COLOR][COLOR=#0000ff]case[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800080]0x0d[/COLOR][COLOR=#000000]:         [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]第二列    [/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    [/COLOR][COLOR=#0000ff]for[/COLOR][COLOR=#000000](i[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]255[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]--[/COLOR][COLOR=#000000]);
        [/COLOR][COLOR=#0000ff]while[/COLOR][COLOR=#000000](P1IN[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]!=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]);
        key[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]1[/COLOR][COLOR=#000000];
        [/COLOR][COLOR=#0000ff]break[/COLOR][COLOR=#000000];
       
        [/COLOR][COLOR=#0000ff]case[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800080]0x0b[/COLOR][COLOR=#000000]:         [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]第三列[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    [/COLOR][COLOR=#0000ff]for[/COLOR][COLOR=#000000](i[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]255[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]--[/COLOR][COLOR=#000000]);
        [/COLOR][COLOR=#0000ff]while[/COLOR][COLOR=#000000](P1IN[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]!=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]);
        key[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]3[/COLOR][COLOR=#000000];
        [/COLOR][COLOR=#0000ff]break[/COLOR][COLOR=#000000];
       
        [/COLOR][COLOR=#0000ff]case[/COLOR][COLOR=#000000] [/COLOR][COLOR=#800080]0x07[/COLOR][COLOR=#000000]:         [/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]第四列[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]    [/COLOR][COLOR=#0000ff]for[/COLOR][COLOR=#000000](i[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]255[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]>[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];i[/COLOR][COLOR=#000000]--[/COLOR][COLOR=#000000]);
        [/COLOR][COLOR=#0000ff]while[/COLOR][COLOR=#000000](P1IN[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]!=[/COLOR][COLOR=#800080]0X0F[/COLOR][COLOR=#000000]);
        key[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]4[/COLOR][COLOR=#000000];
        [/COLOR][COLOR=#0000ff]break[/COLOR][COLOR=#000000];
       
        }
       
        [/COLOR][COLOR=#0000ff]return[/COLOR][COLOR=#000000] key;
    }

    [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000] Delay_1ms([/COLOR][COLOR=#0000ff]uint[/COLOR][COLOR=#000000] i)
    {
        [/COLOR][COLOR=#0000ff]uint[/COLOR][COLOR=#000000] x,j;
        [/COLOR][COLOR=#0000ff]for[/COLOR][COLOR=#000000](j[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];j[/COLOR][COLOR=#000000]<[/COLOR][COLOR=#000000]i;j[/COLOR][COLOR=#000000]++[/COLOR][COLOR=#000000])
        [/COLOR][COLOR=#0000ff]for[/COLOR][COLOR=#000000](x[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000];x[/COLOR][COLOR=#000000]<=[/COLOR][COLOR=#800080]148[/COLOR][COLOR=#000000];x[/COLOR][COLOR=#000000]++[/COLOR][COLOR=#000000]);    
    }
    [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000] led_Display(uchar KEY)
      {                 
       
         
          P5OUT[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000]BIT0;
          P4OUT[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] NUM_LED[KEY[/COLOR][COLOR=#000000]%[/COLOR][COLOR=#800080]10[/COLOR][COLOR=#000000]];  
          Delay_1ms([/COLOR][COLOR=#800080]8[/COLOR][COLOR=#000000]);
         
          P5OUT[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000]BIT1;
          P4OUT[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] NUM_LED[KEY[/COLOR][COLOR=#000000]/[/COLOR][COLOR=#800080]10[/COLOR][COLOR=#000000]];  
          Delay_1ms([/COLOR][COLOR=#800080]8[/COLOR][COLOR=#000000]);
         
     }


    [/COLOR][COLOR=#0000ff]#pragma[/COLOR][COLOR=#000000] vector=PORT1_VECTOR[/COLOR][COLOR=#000000]
    __interrupt [/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000]  PORT1_ISR([/COLOR][COLOR=#0000ff]void[/COLOR][COLOR=#000000])
    {
     
      [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000](P1IFG)
      {
      led_Display(scankey());  [color[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000]#FF0000][/COLOR][COLOR=#008000]//[/COLOR][COLOR=#008000]在这里数码管才实时监测,不明白为什么我放在main。c的while(1)中就不可以,没反应[/color]。[/COLOR][COLOR=#008000]
    [/COLOR][COLOR=#000000]   P1IFG[/COLOR][COLOR=#000000]&=~[/COLOR][COLOR=#800080]0X08[/COLOR][COLOR=#000000];
       
     
     }
     
    }
    [/COLOR]


    我个人觉得有按键的话 产生了中断,就只执行一次的啊,为什么中断里面的 可以实时运行,while(1)中的不可以呢,?
    小弟对中断不是很明白 大侠们帮帮
    微控网感谢您的参与
    在线情况
    2
    • 头像
    • 级别
      • 积分15
      • 经验505
      • 文章16
      • 注册2011-09-08
      你做的是矩阵键盘扫描吧!你可以看看帖子:[URL=http://www.microcontrol.cn/bbs/a/a.asp?B=100&ID=33244][COLOR=#000000]分享个MSP430矩阵键盘反转扫描程序[/COLOR][/URL],可能对你有帮助。不一定要采用中断方式呀!
      MSP430忠实粉丝!
      Powered by LeadBBS 9.2 .
      Page created in 0.2695 seconds with 8 queries.