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

📄 app.c

📁 参考网上的提供的代码,我把uCosII移植到MSP430f149上,分三个任务,分别是485通讯,键盘扫描,LED显示,可供参考!
💻 C
📖 第 1 页 / 共 2 页
字号:
#include "ucos/includes.h"
#include "IO.h"
#include "Uart.h"
#include "TimerA.h"
#define LED_LENTH    9
#define SEL_X_AXIS  0x1
#define SEL_Y_AXIS  0x2
#define SEL_Z_AXIS  0x3

extern unsigned char nRxFinish = 0; //接收包完了标志
extern unsigned char RecData[PACK_LENTH]; //接收包
unsigned int displaytime = 0; //置数时光标位闪烁延时用
unsigned int wKeyDelay;   // 键盘去抖延时
unsigned int wUartWait; //收数等待延时
unsigned char Wait2SendVal = 0;
unsigned char fSendingCMD = 0;
unsigned char fSendingVAL = 0;
unsigned char nCoordinate = 0; //坐标系,0-ALE,1-INC,2~201-ZRO200
unsigned char nCheckZRO = 0; //查看200点坐标
unsigned char nKey = NO_KEY; //有效按键值
unsigned char nPerKey = NO_KEY;  //上次有效按键值
unsigned char nSendKey = NO_KEY;// 需发送的键值
unsigned char nReadAxisStep = 5;
unsigned char nSendByte; //发送字
unsigned char nAxisCode = 0; //选择轴号,0表示没有选择轴;1-X,2-Y,3-Z
unsigned char nDotbit = 5; //接收包中小数点位置,5-公制,3-英制
unsigned char nCursor=0x0f;  //置数时光标位置,0~7表示从高到低位LED,F表示无光标
/*unsigned int nOverTime = 0;
unsigned int nRecpack = 0;
unsigned int nErrorpack = 0;
int looptime; 
*/
unsigned char XRecBuffer[LED_LENTH] = {'8','8','8','8','8','.','8','8','8'};
unsigned char YRecBuffer[LED_LENTH] = {'8','8','8','8','8','.','8','8','8'};
unsigned char ZRecBuffer[LED_LENTH] = {'8','8','8','8','8','.','8','8','8'};
unsigned char SetBuffer[LED_LENTH]; //置数时用

unsigned char SendBuffer[11]={0,0X0D,'0','0','0','0','0','0','0',0x0A,0X0C};//发送命令或置数值寄存

unsigned char XAxisLedCode[8];
unsigned char YAxisLedCode[8];
unsigned char ZAxisLedCode[8];
#define  TASK_STK_SIZE                  64       /* 定义任务堆栈大小     */
OS_STK   Task1Stk[TASK_STK_SIZE];
OS_STK   Task2Stk[TASK_STK_SIZE];
OS_STK   Task3Stk[TASK_STK_SIZE];
void   Task1(void *data);                    /* 前导声明任务(函数) */
void   Task2(void *pdata);
void   Task3(void *pdata);

/*----------------------------------------------------------------
    NOP延时子函数
----------------------------------------------------------------*/
void NOPDelay(unsigned int time)
{
  unsigned int count;
  for(count=0; count<time; count++)
  	_NOP();
}/*----------------------------------------------------------------
     CHAR转ACSII子函数,查看ZRO坐标时用到
----------------------------------------------------------------*/
void CHAR2ACSII(unsigned char nNumber, unsigned char* pBuffer)
{
   unsigned char a=0,b=0,c=0,n;
   for(n=0; n<LED_LENTH; n++)
       *pBuffer++ = ' ';     
    pBuffer -= 2 ;
   if( nNumber < 2 )
   {
      if( nNumber == 0)
      {
          *pBuffer-- = 'E'; 
          *pBuffer-- = 'L'; 
          *pBuffer = 'A';
      }
      else
      {
          *pBuffer-- = 'C'; 
          *pBuffer-- = 'N'; 
          *pBuffer = 'I';
      }
   }
   else 
   {   
      nNumber--;
      a = nNumber % 10; //个位
      b = nNumber / 10; //十位
      if( b >=10 )
      {
         c = b/10;   //百位    
         b = b%10;
      }
      *pBuffer-- = 48 + a;
      if( c != 0 | b != 0 )
          *pBuffer-- = 48 + b ;
      if( c != 0 )
          *pBuffer = 48 + c ;
    }
}

/*----------------------------------------------------------------
     ACSII转LED显示码子函数
----------------------------------------------------------------*/
void ACSII2LED(unsigned char *pSource, unsigned char *pResult)
{
   unsigned char j;
   for(j=0; j<LED_LENTH; j++)
   {
      switch( *pSource++ )
     {
       case 43:  {*pResult = 0x00;}  break;    //+
       case 45:  {*pResult = 0x40;}  break;    //-       
       case 48:  {*pResult = 0x3f;}  break;    // 0
       case 49:  {*pResult = 0x06;}  break;    // 1
       case 50:  {*pResult = 0x5b;}  break;    // 2
       case 51:  {*pResult = 0x4f;}  break;    // 3
       case 52:  {*pResult = 0x66;}  break;    // 4	
       case 53:  {*pResult = 0x6d;}  break;    // 5
       case 54:  {*pResult = 0x7d;}  break;    // 6
       case 55:  {*pResult = 0x07;}  break;    // 7
       case 56:  {*pResult = 0x7f;}  break;    // 8
       case 57:  {*pResult = 0x6f;}  break;    // 9
       case 46:  {pResult--; *pResult |= 0x80; }  break;    //.
       case 65:  {*pResult = 0x77;}  break;    // A
       case 67:  {*pResult = 0x39;}  break;    // C
       case 69:  {*pResult = 0x79;}  break;    // E
       case 73:  {*pResult = 0x06;}  break;    // I
       case 76:  {*pResult = 0x38;}  break;    // L
       case 78:  {*pResult = 0x37;}  break;    // N
       default:  {*pResult = 0x00;}  break;
     }
     pResult++;     
   }
}
/*----------------------------------------------------------------
     扫描键盘子函数
----------------------------------------------------------------*/
void ScanKey( void )
{
  wKeyDelay = 0; //防止溢出
  if((P6IN != 0xff) && (nPerKey == 0x00))
  {
      if(( P6IN & 0x01 ) == 0 )
      { 
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x01 ) == 0  )
         {
              nKey = KEY_1;
              nPerKey = nKey;
         }
      }
      else if( ( P6IN & 0x02 ) == 0)
      {
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x02 ) == 0)
         {    
              nKey = KEY_2;
              nPerKey = nKey;
         }
      }
      else if(( P6IN & 0x04) == 0 )
      {  
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x04 ) == 0  )
         {
              nKey = KEY_3;
              nPerKey = nKey;
         }
      }   
      else if(( P6IN & 0x08) == 0 )
      { 
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x08 ) == 0 )
         {
              nKey = KEY_4;
              nPerKey = nKey;
         }                          
      }
      else if(( P6IN & 0x10) == 0 )
      {
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x10 ) == 0 )
         {
              nKey = KEY_5;
              nPerKey = nKey;
         }
      }
      else if(( P6IN & 0x20) == 0)
      {  
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x20 ) == 0 )
         {
              nKey = KEY_6;
              nPerKey = nKey;
         }
      } 
      else if(( P6IN & 0x40) == 0)
      {
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x40 ) == 0  )
         {
              nKey = KEY_7;
              nPerKey = nKey;
         }
      }
      else if(( P6IN & 0x80) == 0 )
      {
         wKeyDelay = 0;
         NOPDelay(4000);
         if(( P6IN & 0x80 ) == 0 )
         {
              nKey = KEY_8;
              nPerKey = nKey;
         }
      }
      else
      { 
         nKey = NO_KEY;
      }  
  }
  else
  {     
      nKey = NO_KEY;
      if(P6IN == 0xff)
          nPerKey = NO_KEY;
  }
}
/*----------------------------------------------------------------
     处理X按键函数
----------------------------------------------------------------*/
void DealKeyAxis(char Axis )
{
   unsigned char u;
   if( ( nAxisCode == 0) && ( nCheckZRO == 0))//进入清零功能或置数功能
   {       
      SetBuffer[0] = '+';
      for(u=1; u<LED_LENTH; u++)
            SetBuffer[u] = '_';
      SetBuffer[nDotbit] = '.';         
      nAxisCode = Axis; 
      Wait2SendVal = 1;
      SendBuffer[1] = Axis;   
    }
    else if ( nAxisCode == Axis ) //退出清零或置数功能
    {    
       nAxisCode = 0;
       nCursor = 0x0f;
       Wait2SendVal = 1;
       SendBuffer[1] = Axis;   
    }        
}

/*----------------------------------------------------------------
     处理CLR按键函数
----------------------------------------------------------------*/
void DealKeyCLR( void )
{
    if( nAxisCode != 0 )
      {
          nAxisCode = 0;
          nCursor = 0x0f;  
          Wait2SendVal = 1;
          SendBuffer[1] = 4;           
      } 
}
/*----------------------------------------------------------------
     处理M/I按键函数
----------------------------------------------------------------*/
void DealKeyMI( void )
{
   unsigned char u,i, *pAxisBuffer;
   if( nAxisCode !=0 ) //置数开始或置数确认
   {      
      if( nCursor == 0x0f )
      {
            switch( nAxisCode )
           {
            case SEL_X_AXIS: pAxisBuffer = XRecBuffer; break;
            case SEL_Y_AXIS: pAxisBuffer = YRecBuffer; break;
            case SEL_Z_AXIS: pAxisBuffer = ZRecBuffer; break;    
           }        
           for(u=0; u<LED_LENTH; u++)
           {  
               if( ( pAxisBuffer[u]==' ')|| ( pAxisBuffer[u]==0x3f) )
                   SetBuffer[u] = '0';
               else
                   SetBuffer[u] = pAxisBuffer[u];
           }      
           nCursor = 4;
       }
       else
       {  
           Wait2SendVal = 10;    //sent the value to 51
           SendBuffer[10] = 0xC;
           i = 0;
           for(u=9; u>1; u--)
           {   
               if( SetBuffer[i] != '.' )
               {
                   if( SetBuffer[i] == '+' ) 
                       SendBuffer[u] = 0x0A;
                   else if( SetBuffer[i] == '-' ) 
                       SendBuffer[u] = 0x0B;              
                   else
                   {    
                       SendBuffer[u] = SetBuffer[i] - 0x30;
                       if ( ( SendBuffer[u] < 0) || ( SendBuffer[u] > 9) )
                          SendBuffer[u] = 0;
                    }      
                }
                else 
                {    
                    u++; //jump over the dot
                }
                i++;   
            }
           SendBuffer[1] = 0xD;
           fSendingVAL = 1;
        }
   }
   else
   {
        Wait2SendVal = 1; //公英制转换
        SendBuffer[1] = 5;
   }
}
/*----------------------------------------------------------------
     处理Half按键函数
----------------------------------------------------------------*/
void DealKeyHalf(void)
{
   if( nAxisCode !=0 )//选择了轴
   {
      if( nCursor == 0x0f )    //分中
      {       
         Wait2SendVal = 1;
         SendBuffer[1] = 6;   //发送分中指令
         nAxisCode = 0;
      }
      else
      {                 
        if( nCursor == 7 )  //置数时,光标左移一位  
           nCursor = 0;
        else
           nCursor++;
      }
   }
   else
   {
      nCheckZRO ^= 0x01;     
   }
}
/*----------------------------------------------------------------
     处理UP按键函数

⌨️ 快捷键说明

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