⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 app.c

📁 参考网上的提供的代码,我把uCosII移植到MSP430f149上,分三个任务,分别是485通讯,键盘扫描,LED显示,可供参考!
💻 C
📖 第 1 页 / 共 2 页
字号:
----------------------------------------------------------------*/
void DealKeyUp(void)
{
  unsigned char bit = nCursor;
  if( ( nAxisCode != 0) && ( nCursor != 0x0f) )//选择轴并是置数
  {
     if( bit >= nDotbit )
         bit++;
     if( bit == 0 )
     {
          if( SetBuffer[bit] == '+' )
               SetBuffer[bit] = '-';
          else 
              SetBuffer[bit] = '+';
     }
     else
     {             
          if( SetBuffer[bit] == '9' )
              SetBuffer[bit] = '0';
          else 
              SetBuffer[bit]++;
     }
  }
  else
  {
      Wait2SendVal = 1;
      SendBuffer[1] = 7; 
 /*     if( nCheckZRO == 0)
       {
          Wait2SendVal = 1;
          SendBuffer[1] = 7; 
       }
       else
       {
           Wait2SendVal = 10;
           for(bit=1; bit<=10; bit++)
           {
               SendBuffer[bit] = 7; 
           }
       }*/
  }
}
/*----------------------------------------------------------------
     处理Down按键函数
----------------------------------------------------------------*/
void DealKeyDown(void)
{
  unsigned char bit = nCursor;
  if( ( nAxisCode != 0) && ( nCursor != 0x0f) )//选择轴并是置数
  {
     if( bit >= nDotbit )
           bit++;
     if( bit == 0 )
     {
          if( SetBuffer[bit] == '+' )
               SetBuffer[bit] = '-';
          else SetBuffer[bit] = '+';
     }
     else
     {             
          if( SetBuffer[bit] == '0' )
               SetBuffer[bit] = '9';
          else SetBuffer[bit]--;
     }
  }
  else
  {
       Wait2SendVal =  1;
       SendBuffer[1] = 8;
  }
}
/*----------------------------------------------------------------
     处理按键函数
----------------------------------------------------------------*/
void DealKey( void )
{ 
   switch(nKey)
   {
    case 0x01: DealKeyAxis(1); break;
    case 0x02: DealKeyAxis(2); break;
    case 0x03: DealKeyAxis(3); break;
    case 0x04: DealKeyCLR(); break; 
    case 0x05: DealKeyMI(); break; 
    case 0x06: DealKeyHalf(); break;
    case 0x07: DealKeyUp(); break;
    case 0x08: DealKeyDown(); break;
    default:   break;		
   }
}
/*----------------------------------------------------------------
     数码管三轴显示子程序
----------------------------------------------------------------*/
void Display( void )
{
   unsigned char i;
   switch( nAxisCode )
   {
    case SEL_X_AXIS:
	 ACSII2LED( SetBuffer, XAxisLedCode);
	 ACSII2LED( YRecBuffer, YAxisLedCode);
	 ACSII2LED( ZRecBuffer, ZAxisLedCode); 	   	   
	 break;
    case SEL_Y_AXIS:
   	 ACSII2LED( XRecBuffer, XAxisLedCode);	   
	 ACSII2LED( SetBuffer, YAxisLedCode);
	 ACSII2LED( ZRecBuffer, ZAxisLedCode);   		   
	 break;
    case SEL_Z_AXIS:
   	 ACSII2LED( XRecBuffer, XAxisLedCode);
	 ACSII2LED( YRecBuffer, YAxisLedCode);	   
	 ACSII2LED( SetBuffer,  ZAxisLedCode);
	 break;
    default:
   	 ACSII2LED( XRecBuffer, XAxisLedCode);
	 ACSII2LED( YRecBuffer, YAxisLedCode);
	 ACSII2LED( ZRecBuffer, ZAxisLedCode);  	   
	 break;
    }
    if( nCheckZRO == 1 ) //查看ZRO坐标时,Z轴显示SetBuffer的数
    {
       CHAR2ACSII(nCoordinate, SetBuffer);  //将坐标值转换成ASCII码  
       ACSII2LED( SetBuffer, ZAxisLedCode); //显ZRO坐标 	
    }
    SEL_ROW = 0x00; //关闭灯
    NOPDelay(2);
    OSSchedLock();
    for(i=0; i<8; i++) //动态显示8个LED
    {
        LEDX = XAxisLedCode[i];
	LEDY = YAxisLedCode[i];
	LEDZ = ZAxisLedCode[i];
        if( (nAxisCode != 0 ) && ( nCursor == i ) ) //置数光标闪烁
       {               
          if( displaytime < 250 )
	  {
            switch( nAxisCode )
	    {
	       case SEL_X_AXIS: LEDX = 0x00; break;	
	       case SEL_Y_AXIS: LEDY = 0x00; break;	
	       case SEL_Z_AXIS: LEDZ = 0x00; break;
	       default:  break;		
	     }
	   }
       }
        else if( nCheckZRO == 1 )
        {
	    LEDX = 0x00 ;  //查看ZRO坐标时,X,Y轴不显示数
	    LEDY = 0x00 ; 
        }
        SEL_ROW = ( 0x80 >> i);
        NOPDelay(20);
        SEL_ROW = 0x00;
        NOPDelay(2);
     }
     OSSchedUnlock();
     LEDX = 0x00;   //点灯                
     LEDY = 0x00; 
     LEDZ = 0x00;
     if( nCoordinate > 1)
     {
          LEDZ = 0x80;  //ZRO灯亮       
     }
     else
     {
          if( nCoordinate == 0 ) 
              LEDX = 0x80;  //ALE灯亮
          else
              LEDY = 0x80;  //INC灯亮      
     }   
     SEL_ROW = 0x80; //一直点亮
     if( displaytime == 500 )
        displaytime = 0;
     else
        displaytime++;
}
/*----------------------------------------------------------------
     通讯子程序    
----------------------------------------------------------------*/
void Comm( void )
{
   unsigned char i,nextAxis,*pAxisBuffer;
   static unsigned char nCurrentRead;
   wUartWait++;
   switch( nReadAxisStep )
   {
      case WAIT_READ_FINISH:
           if( nRxFinish == 1 )   //收完轴读数包
           {
              //nRecpack++;
              nRxFinish = 0;
              //nErrorTime = 0;
              if( (RecData[PACK_LENTH-1]!=0xBB) | (RecData[PACK_LENTH-2] != nSendByte) )//by yang //if error request to send data again
              {
                 Usart1_send( nSendByte ); //重发   
                 wUartWait=0; 
                 //nErrorpack++;       
              }
              else//no error //set next key to sent //if a key had sent out and back, wait to confirm ,sent 0x0e as confirm or resent.
              {                          //if a key confirmed ,or no key was send last ,//see that if there some key in send buffer .                 
                   if( Wait2SendVal != 0 )                                           
                   {     if ( fSendingCMD == 0 )
                         {   
                             fSendingCMD=1;
                             nSendKey = SendBuffer[Wait2SendVal];
                         }
                         else if( ( RecData[PACK_LENTH-2]&0x0F) == nSendKey )
                         {    
                            if( nSendKey != 0x0E )
                            {    
                                nSendKey = 0x0E;
                            }
                            else
                            { 
                                 Wait2SendVal--; 
                                 nSendKey = SendBuffer[Wait2SendVal];                                    
                            }  
                         }//else,just do nothing ,keep the nSendKey    
                    }
                    else 
                    {
                           if( fSendingVAL == 1 )
                           {
                               fSendingVAL=0;
                               nAxisCode=0;
                               nCursor =0x0F;
                           }
                           fSendingCMD = 0;   
                           nSendKey = SendBuffer[Wait2SendVal];
                    }                                                 
                   switch( nCurrentRead )
                   {
                    case READ_X_STEP:
                         pAxisBuffer=XRecBuffer;
                         nextAxis=0x20;
                         nCurrentRead = READ_Y_STEP;                      
                         break;
                    case READ_Y_STEP:
                         pAxisBuffer=YRecBuffer;
                         nextAxis=0x30;
                         nCurrentRead = READ_Z_STEP;
                         break;                        
                    case READ_Z_STEP:
                         pAxisBuffer=ZRecBuffer;
                         nextAxis=0x10;
                         nCurrentRead  = READ_X_STEP;
                         break;
                    default:
                         nextAxis=0x10;
                         pAxisBuffer=XRecBuffer;
                         nCurrentRead = READ_X_STEP;                                                  
                         break;                             
                   }//end of switch 
                   for(i=0; i<LED_LENTH; i++)           //store the received axis value
                            pAxisBuffer[i] = RecData[i];
                   if( RecData[3] == '.' )              //record M/I
                        nDotbit = 3;
                   else if( RecData[4] == '.' )
                        nDotbit = 4;
                   else
                        nDotbit = 5;
                   nCoordinate = RecData[PACK_LENTH-3]; //record coordinate  
                   //NOPDelay(1500);  
                   nSendByte = nextAxis | nSendKey; 
                   nReadAxisStep = SENT_REQUEST;   
              }//end check error                
           }
           else  
           {           
              if( wUartWait >= 2 )
                { 
                   wUartWait = 0;
                   Usart1_send( nSendByte ); //重发
                  // nOverTime++;
                   nRxFinish = 0;   
                 } 
           }//check package error
           break;  
      case SENT_REQUEST:           
           Usart1_send( nSendByte );  
           wUartWait = 0;         
           nReadAxisStep = WAIT_READ_FINISH;
           break;
      default:
           nSendByte = 0x10 ; 
           nSendKey = 0;
           nCurrentRead = READ_X_STEP;
           nReadAxisStep = SENT_REQUEST;                   
           break;
   }
/*   if(looptime>20000)
   {
      _NOP();
      looptime=0;
   }*/
}
/*----------------------------------------------------------------
     任务1函数
----------------------------------------------------------------*/
void  Task1 (void *pdata)
{
  pdata  = pdata;                                        /* 无任何意义,防止编译器报警 */
  WDTCTL = WDT_MDLY_32;                                  /* SMCLK = MCLK = 3M ,设置时钟节拍间隔为10ms     */
  IE1   |= 0x01;                                         /* 开看门狗定时器中断         */
  _EINT();
  while(1)
  {
   Comm();
   OSTimeDly(2);
 }
}
/*----------------------------------------------------------------
     任务2函数
----------------------------------------------------------------*/
void  Task2 (void *pdata)
{
   pdata  = pdata;                                        /* 无任何意义,防止编译器报警 */
   while(1)
   {
      ScanKey();
      DealKey();  
      OSTimeDly(8);
   }
}
/*----------------------------------------------------------------
     任务3函数
----------------------------------------------------------------*/
void  Task3 (void *pdata)
{
   pdata  = pdata;                                        /* 无任何意义,防止编译器报警 */
   while(1)
  {
    Display();
   //OSTimeDly();
  }
}
/*----------------------------------------------------------------
     任务主函数
----------------------------------------------------------------*/
void main(void)
{
   unsigned char I;
   WDTCTL = WDTPW + WDTHOLD;	       //关看门狗
   //BCSCTL1 &=~XT2OFF;
   BCSCTL1 = 0x86;
   DCOCTL = 0xe0;
   do{
       IFG1 &=~OFIFG;
       for(I=0xff; I>0; I--);
      }while ( ( IFG1& OFIFG) != 0 ); 
  BCSCTL2  = 0; 
  Init_Usart1();   
  //Init_TimerA();  
  Init_IO();
  P3OUT &= 0xdf; //485接收状态            */
  OSInit();                                              /* 初始化uCOS-II */
  OSTaskCreate(Task1, (void *)0, &Task1Stk[TASK_STK_SIZE - 1], 0);
  OSTaskCreate(Task2, (void *)0, &Task2Stk[TASK_STK_SIZE - 1], 1);
  OSTaskCreate(Task3, (void *)0, &Task3Stk[TASK_STK_SIZE - 1], 2);        
  OSStart();                                             /* 开始任务调度  */  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -