TI中文支持网
TI专业的中文技术问题搜集分享网站

MSP430单片机程序反复重启

各位技术大牛,这几天写了个程序,但是调试期间,程序反复重新启动,具体程序如下:

//
//#include "msp430x54x.h"
#include "TFTDRV430.H"
#include "RTC.H"
#include "TONE.H"

#define  FLL_FACTOR     649          // FLL_FACTOR: DCO倍频系数
#define  Num_of_Results   8

unsigned int Color = Green;
unsigned int Color_BK = Dark_Grey;

int F_Bzkey=0;             //1,报警;0,键盘
unsigned int key_counter=0;
unsigned int key_stime_ok=0;
unsigned int key[6];
unsigned int key_temp;
unsigned int key_C=6;
unsigned int k=224;

unsigned int i=51;
unsigned int results[Num_of_Results]; float fresults[Num_of_Results]; //存储转换结果
float fsum;
float m;
unsigned int S_judge=0;
unsigned int nd[2]={0,0};
int flag=0;

unsigned int tone ; //tone 为蜂鸣器频率,tone=TONE[t] 根据不同浓度选取不同的数组值
unsigned int TONE[3]={ 1,40000,65000 };
unsigned int t=0;

void Init_CLK(void)   //初始化主时钟: MCLK = XT1(32768Hz)×(FLL_FACTOR+1)
{
  WDTCTL     = WDTPW + WDTHOLD                                     ; // 关看门狗
  P7SEL     |= 0x03                                                ; // 端口选择外部低频晶振XT1
  UCSCTL6   &=~XT1OFF                                              ; // 使能外部晶振  UCSCTL6   |= XCAP_3                                              ; // 设置内部负载电容
  UCSCTL3   |= SELREF_2                                            ; // DCOref = REFO
  UCSCTL4   |= SELA_0                                              ; // ACLK   = XT1   __bis_SR_register(SCG0)                                          ; // 关闭FLL控制回路
  UCSCTL0    = 0x0000                                              ; // 设置DCOx, MODx
  UCSCTL1    = DCORSEL_7                                           ; // 设置DCO振荡范围
  UCSCTL2    = FLLD__1 + FLL_FACTOR    ; // Fdco = ( FLL_FACTOR + 1)×FLLRef = (649 + 1) * 32768 = 21.2992MHz
  __bic_SR_register(SCG0)                                          ; // 打开FLL控制回路
  __delay_cycles(1024000)                                          ;  do
  {
    UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + XT1HFOFFG + DCOFFG)         ; // 清除 XT2,XT1,DCO 错误标志
    SFRIFG1 &= ~OFIFG                                              ;  }while(SFRIFG1&OFIFG)                                            ; // 检测振荡器错误标志}

void Init_Port(void)        //Init_Port(void): 设置IO端口
{
  P5DIR  |= POWER                                         ; // 主电源
  P7DIR  |= BUZZER                                        ;
  P7OUT  |= BUZZER                                        ;
  MAIN_POWER_ON                                           ;
}

void Init_RTC(void)//初始化RTC
{
  RTCCTL01 = RTCSSEL_0 + RTCMODE;          // 时钟模式,每分钟中断, BCD格式
  unsigned int hour,min,sec;
   hour=key[0]*10+key[1];
  min =key[2]*10+key[3];
  sec =key[4]*10+key[5];
   SetRTCYEAR(2012);  SetRTCMON(6);
  SetRTCDAY(1);
  SetRTCHOUR(hour);
  SetRTCMIN(min);
  SetRTCSEC(sec);
}

void Init_AX()
{
  WriteCN24(25,2, Color,Color_BK,Chi[3]); 
  WriteCN24(25,26, Color,Color_BK,Chi[4]); 
  PutStringEN24(25,50,":"); 

  WriteCN24(152,270,Color,Color_BK,Chi[5]); 
  WriteCN24(152,294,Color,Color_BK,Chi[6]); 
  WriteCN24(128,270,Color,Color_BK,Chi[7]); 
  WriteCN24(128,294,Color,Color_BK,Chi[1]);

  PutStringEN24(80,270,"ppm");
  WriteCN24(25,150,Color,Color_BK,Chi[0]); 

  WriteCN24(25,174,Color,Color_BK,Chi[1]);  
  PutStringEN24(25,198,":");
  WriteCN24(25,280,Color,Color_BK,Chi[1]);
  WriteCN24(1,150,Color,Color_BK,Chi[2]);  
  WriteCN24(1,174,Color,Color_BK,Chi[1]);  
  PutStringEN24(1,198,":");
  PutStringEN24(1,286,"%");
  DrawVerticalLine(60,50,218,Color);
  DrawHorizonLine(60,50,180,Color);
  PutStringEN24(50,20,"0");
  PutStringEN24(78,20,"5");
  DrawVerticalLine(90,50,218,Green);
  PutStringEN24(108,11,"10");
  DrawVerticalLine(120,50,218,Green);
  PutStringEN24(138,11,"15");
  DrawVerticalLine(150,50,218,Green);
  PutStringEN24(168,11,"20");
  DrawVerticalLine(180,50,218,Green);
  PutStringEN24(195,11,"25");
  DrawVerticalLine(210,50,218,Green);
  WriteCN24(215,1,Color,Color_BK,Chi[7]);
  WriteCN24(215,25,Color,Color_BK,Chi[1]);
}

void Init_ADC(void)
{
  P7SEL  |= 0x10;  //Enable A/D channel A12(P7.4)   ADC12CTL0 = ADC12ON+ADC12SHT02;
  ADC12CTL1 = ADC12SHP;
  ADC12MCTL0 = ADC12SREF_2+ADC12INCH_12; // Vr+=Veref+ and Vr-=AVss
  __delay_cycles(11200000); // Delay for reference start-up
  ADC12IE = 0x01;                           // Enable interrupt
  ADC12CTL0 |= ADC12ENC;                    // Enable conversions
}

void Init_Timer0_A5(void)
{
  TA0CTL   = 0                                               // 复位Timer0_A5, 分频系数设置为1
           | (1 << 2)                                        // 计数器清0
           | (2 << 8)                                     ; // 计数时钟设为SMCLK     TA0CCR0  =   600  – 1                                   ; // SMCK=EX2=16MHz,设置计数器溢出时间为1ms
  TA0CCTL0 = 0                                              // 初始化捕获控制
           | (1 << 4)                                     ; // 使能比较中断
  TA0CTL  |= (1 << 4)                                     ; // 设置计数器为加计数,启动
  WriteCN24(60,80, Color,Color_BK,Chi[3]); //时
  WriteCN24(60,104, Color,Color_BK,Chi[4]); //间
  PutStringEN24(60,128,":"); //冒号
}

void ffWarn()
{
      //DrawRect(100,100,40,120,Yellow);
      WriteCN24(107,104,Color,Color_BK,Chi[7]); 
      WriteCN24(107,128,Color,Color_BK,Chi[1]); 
      WriteCN24(107,152,Color,Color_BK,Chi[8]); 
      WriteCN24(107,176,Color,Color_BK,Chi[9]); 
      PutStringEN24(107,200,"!");
     }

void Show_Clock(void)
{
  unsigned int Hour ,Minute,Second;
  char p1[10],p2[10],p3[10];
  for(;RTCCTL01&RTCRDY;)        _NOP(); // 等待RTC有效
  for(;RTCCTL01&RTCRDY;)        _NOP();

   Hour = GetRTCHOUR();
   Minute = GetRTCMIN();
   Second = GetRTCSEC();
     sprintf(p1,"%02d",Hour);
   sprintf(p2,"%02d",Minute);
   sprintf(p3,"%02d",Second);
     PutStringEN24(1,2,p1);
   PutStringEN24(1,34,":");
   PutStringEN24(1,50,p2);
   PutStringEN24(1,82,":");
   PutStringEN24(1,98,p3);
}

void Show_Nongdu(unsigned int m,unsigned int n,unsigned int y)
{
  Posi p1,p2;
  p1.x=6*m+60;
  p2.x=6*n+60;
  p1.y=y;
  p2.y=y+2;
  Draw_line(p1,p2,Red);
}

/*#pragma vector=TIMERB1_VECTOR //显示温湿度,TIMERB1中断服务程序
/__interrupt void TIMERB1_ISR(void)
{  Show_Tehu();
  LPM3_EXIT ;
  TBCTL&= ~TBIFG;
}*/

#pragma vector=TIMER0_A0_VECTOR //报警中断服务程序
__interrupt void Timer0_A0 (void)
{
  /*if(F_Bzkey==1)
  {
    P7OUT   ^= BUZZER;
    TA0CCR0  = tone;
  }*/
  //else
  //{
    if(++key_counter>=360)
    {
      key_counter=0;
      key_stime_ok = 1; //10ms到
    }
  //}
}

#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)  //AD转换中断服务程序
{
  static unsigned char index = 0;
  LPM3_EXIT;
  switch(__even_in_range(ADC12IV,34))
  {
  case  0: break;                           // Vector  0:  No interrupt
  case  2: break;                           // Vector  2:  ADC overflow
  case  4: break;                           // Vector  4:  ADC timing overflow
  case  6:                                  // Vector  6:  ADC12IFG0
    if(index < Num_of_Results)
    {
      fresults[index] = (10*(ADC12MEM0*2.048/4096)+30);
      index++;
    }
    if(Num_of_Results==index) { index=0; }
   // for(int n=0;n<Num_of_Results;n++)
   // {
   //   fsum+=fresults[n];
   // }
   // m=fsum/Num_of_Results;
   // if(m>=30)
   // {
   //   flag=1;
      //t=1;
   // }
   // else
  //  {
  //    flag=0;
  //  }
    S_judge=1;
       break;
  case  8: break;                           // Vector  8:  ADC12IFG1
  case 10: break;                           // Vector 10:  ADC12IFG2
  case 12: break;                           // Vector 12:  ADC12IFG3
  case 14: break;                           // Vector 14:  ADC12IFG4
  case 16: break;                           // Vector 16:  ADC12IFG5
  case 18: break;                           // Vector 18:  ADC12IFG6
  case 20: break;                           // Vector 20:  ADC12IFG7
  case 22: break;                           // Vector 22:  ADC12IFG8
  case 24: break;                           // Vector 24:  ADC12IFG9
  case 26: break;                           // Vector 26:  ADC12IFG10
  case 28: break;                           // Vector 28:  ADC12IFG11
  case 30: break;                           // Vector 30:  ADC12IFG12
  case 32: break;                           // Vector 32:  ADC12IFG13
  case 34: break;                           // Vector 34:  ADC12IFG14
  default: break;  }
}

void main()
{
  char str[5];
  WDTCTL = WDTPW + WDTHOLD                                ;
  Init_CLK()                                              ;
  LCD_Init()                                              ;
  Clear_LCD(Color_BK);
  Init_Port()                                             ;
  Init_KeyPad();  Init_Timer0_A5()                                        ;
  //TBCTL    = TBSSEL_1 + MC_1 + TBCLR + TBIE     ; // 时钟源   = ACLK
  //TBCCR0   = 8000                               ; // 刷新频率 = 32768/64/8000=0.064Hz
  _EINT();
   while(key_C)
  {
       if(key_stime_ok)
    {
      char str[4];
      key_stime_ok=0;
      key_temp=Read_Key();//读取按键数值
      if(key_temp != No_key)
      {
        key[key_C-1]=key_temp;
        sprintf(str,"%d",key_temp);
        PutStringEN24(60,k,str);
        k-=16;
        key_C–;
      }
    }
  }
  //TA0CCTL0&=0xE8;
  Init_RTC();
  //F_Bzkey=1;
  Clear_LCD(Color_BK);
  Init_AX();
  Init_ADC();
  ADC12CTL0 |= ADC12SC;
  while(1)
  {
   WDTCTL = WDTPW + WDTHOLD                                ;
  // LPM3;
   Show_Clock();
   if(S_judge==1)
   {
     for(int n=0;n<Num_of_Results;n++)
    {
      fsum+=fresults[n];
    }
    m=fsum/Num_of_Results;
    if(m>=30)
    {
      flag=1;
      //t=1;
    }
    else
    {
      flag=0;
    }
   }
    if(flag==0)
   {
     //_EINT();
    sprintf(str,"%02.1f",m);
    PutStringEN24(104,260,str);
    nd[1]=(unsigned int)m;
    Show_Nongdu(nd[0],nd[1],i);
    nd[0]=nd[1];
    i+=2;
    if(270<i)
    {
      i=51;
      DrawRectFill(60,50,180,222,Color_BK);
      Init_AX();
    }
    fsum=0;
    ADC12CTL0 |= ADC12SC;
  }
   else if(flag==1)
   {
     _DINT();
     ffWarn();
     //ADC12CTL0 |= ADC12SC;
     tone=TONE[t];
   }
   _NOP();
  }
}
每次运行到ffWarn();程序就重新启动,不知道为什么,还望各位给点解决办法啊。

Brian Ji:

请在程序开始时查看SYSRSTIV的值,里面有复位原因的指示,然后再做相应的处理。

赞(0)
未经允许不得转载:TI中文支持网 » MSP430单片机程序反复重启
分享到: 更多 (0)