msp430f149外部中断计脉冲

[复制链接]
 楼主| Rollo 发表于 2017-6-30 15:06 | 显示全部楼层 |阅读模式
#include <msp430x14x.h>
int i=0;
void init()//初始化时钟
{
   unsigned int i;
   _DINT();//__disable_interrupt()
   BCSCTL1 &=~XT2OFF;//Basic Clock System Control 1,Enable XT2CLK
                      //即打开XT2
   do
   {
     IFG1 &= ~OFIFG;        // IFG1:Interrupt Flag 1                                                // 清除振荡器失效标志
      for(i=0xFF;i>0;i--);// 延时,等待XT2起振
   }
   while((IFG1 & OFIFG) != 0);// 判断XT2是否起振,OFIFG=0则起振               
   BCSCTL2 =SELM1+SELS;       //MCLK(SELM1),SMCLK时钟为XT2,都为8MHZ
//BCSCTL2:Basic Clock System Control 2
}
void count_init()
{
   P1SEL =0x00;
   P1DIR&=~(BIT0+BIT1);
   P1DIR|=BIT6;
   P1OUT=0X00;
   P1IES = 0x00;               // P1.0选择上升沿中断
   P1IE  = 0x01;               // 打开中断使能
}
int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  init();
  count_init();
  _EINT();           //开总中断
  return 0;
}
#pragma vector=PORT1_VECTOR   //外部中断计脉冲
__interrupt void port1(void)
{

  if(P1IFG&BIT0==BIT0)
  {  
    P1IFG=0X00;
    if((P1IN&BIT1)==BIT1)
    i++;
    else
    i--;
    if(i>=100)//接收的脉冲大于100则P1.6输出高电平,灯亮
      P1OUT|=BIT6;
    else
      P1OUT&=~BIT6;
  }
}
我打算用外部中断计电机脉冲个数,但是好像没有用,这是我写的一个外部中断测试,灯一直不亮,现在也不知道是什么原因,求帮助,已经困扰好久了
Rangar 发表于 2017-6-30 16:29 | 显示全部楼层
可能是硬件出了问题吧   建议检查下硬件
Stannis 发表于 2017-6-30 16:50 | 显示全部楼层
#include <msp430x24x.h>
int i=0;
void init()//初始化时钟
{
   unsigned int i;
   BCSCTL1 &=~XT2OFF;//Basic Clock System Control 1,Enable XT2CLK
                      //即打开XT2
   do
   {
     IFG1 &= ~OFIFG;        // IFG1:Interrupt Flag 1                                                // 清除振荡器失效标志
      for(i=0xFF;i>0;i--);// 延时,等待XT2起振
   }
   while((IFG1 & OFIFG) != 0);// 判断XT2是否起振,OFIFG=0则起振               
   BCSCTL2 =SELM1+SELS;       //MCLK(SELM1),SMCLK时钟为XT2,都为8MHZ
}
void count_init()
{
   P1DIR|=BIT6;
   P1IES = 0x00;               // P1.0选择上升沿中断
   P1IE  = 0x03;               // 打开中断使能
   P1IFG = 0X00;
}
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  init();
  count_init();
  _EINT();           //开总中断
}
#pragma vector=PORT1_VECTOR   //外部中断计脉冲
__interrupt void port1(void)
{

  if(P1IFG&BIT0)
  {  
    P1IFG=0X00;
    P1OUT|=BIT6;
  }
  if(P1IFG&BIT1)
  {
    P1IFG=0X00;
    P1OUT&=~BIT6;
  }
}
Brand2 发表于 2017-6-30 17:03 | 显示全部楼层
可以在proteus中仿真一下试试
Soraka 发表于 2017-6-30 17:15 | 显示全部楼层
在主函数里是不是应该加上一个while(1);?
Garen2 发表于 2017-6-30 17:22 | 显示全部楼层
#include <msp430x24x.h>
int i=0;
void init()//初始化时钟
{
   unsigned int i;
   BCSCTL1 &=~XT2OFF;//Basic Clock System Control 1,Enable XT2CLK
                      //即打开XT2
   do
   {
     IFG1 &= ~OFIFG;        // IFG1:Interrupt Flag 1                                                // 清除振荡器失效标志
      for(i=0xFF;i>0;i--);// 延时,等待XT2起振
   }
   while((IFG1 & OFIFG) != 0);// 判断XT2是否起振,OFIFG=0则起振               
   BCSCTL2 =SELM1+SELS;       //MCLK(SELM1),SMCLK时钟为XT2,都为8MHZ
}
void count_init()
{
   P1DIR|=BIT6;
   P1IES = 0x00;               // P1.0选择上升沿中断
   P1IE  = 0x03;               // 打开中断使能
   P1IFG = 0X00;
}
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  init();
  count_init();
  _EINT();           //开总中断
   while(1);
}
#pragma vector=PORT1_VECTOR   //外部中断计脉冲
__interrupt void port1(void)
{

  if(P1IFG&BIT0)
  {  
    P1IFG=0X00;
    P1OUT|=BIT6;
  }
  if(P1IFG&BIT1)
  {
    P1IFG=0X00;
    P1OUT&=~BIT6;
  }
}
gygp 发表于 2017-7-3 21:46 | 显示全部楼层
你的函数代码都执行完了。
chenci2013 发表于 2017-7-3 21:47 | 显示全部楼层
单步调试了吗?
biechedan 发表于 2017-7-3 21:47 | 显示全部楼层
return 0; 程序结束了。
wangdezhi 发表于 2017-7-3 21:48 | 显示全部楼层
这个代码有问题,仔细检查一下。
isseed 发表于 2017-7-3 21:48 | 显示全部楼层
你的硬件电路有问题吗?
xietingfeng 发表于 2017-7-3 21:49 | 显示全部楼层
首先得检查硬件是否有问题。
suzhanhua 发表于 2017-7-3 21:49 | 显示全部楼层
int main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  init();
  count_init();
  _EINT();           //开总中断
  return 0;
}
mituzu 发表于 2017-7-3 21:50 | 显示全部楼层
在里面加个for或者while循环。
hellosdc 发表于 2017-7-3 21:50 | 显示全部楼层
外部中断没有触发完成。
gygp 发表于 2017-7-3 21:50 | 显示全部楼层
return 0;换成while();
chenci2013 发表于 2017-7-3 21:50 | 显示全部楼层
中断函数配置有什么问题?
biechedan 发表于 2017-7-3 21:50 | 显示全部楼层
在main函数里面要死循环才行。
wangdezhi 发表于 2017-7-3 21:51 | 显示全部楼层
main函数都返回数值,程序停止。
isseed 发表于 2017-7-3 21:51 | 显示全部楼层
使用示波器测量IO的引脚电平。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

115

主题

730

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

115

主题

730

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部