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

📄 printp.c

📁 用于城市轨道交通开发的CBTR 的开发
💻 C
📖 第 1 页 / 共 2 页
字号:
    asm  move.w  SR_temp,SR;
       return (COMM_NO_ERR);
    } 
    else
    {
    asm  move.w  SR_temp,SR;    	
        return (COMM_TX_FULL);
    }
}

/*
*********************************************************************************************************
*                                    REMOVE CHARACTER FROM RING BUFFER
*
*
* Description : This function is called by your application to obtain a character from the communications
*               channel.
* Arguments   : 'Device'    is the COMM port channel number and can either be:
*                       Dev_UART0,Dev_UART1,Dev_UART2
* Returns     : The character in the buffer (or NUL if the buffer is empty)
*********************************************************************************************************
*
*调用之前必须先用 IsCommRxEmpty()检查一下
*
*******************************************************************************************************/
unsigned char  TERMIO_GetChar()
{
    unsigned char c;
    COMM_RING_BUF *pbuf;
    unsigned int  SR_temp;    

    pbuf = &CommBuf;

   asm  move.w  SR,SR_temp;
   DisableInterrupts;
    if (pbuf->RingBufRxCtr > 0)                          /* See if buffer is empty                   */
    {
    	pbuf->RingBufRxCtr--;                              /* No, decrement character count            */
        c = *pbuf->RingBufRxOutPtr++;                      /* Get character from buffer                */
        if (pbuf->RingBufRxOutPtr == &pbuf->RingBufRx[COMM_RX_BUF_SIZE])      /* Wrap OUT pointer     */
            pbuf->RingBufRxOutPtr = &pbuf->RingBufRx[0];
    asm  move.w  SR_temp,SR;
        return (c);
    }
    else
    {
    asm  move.w  SR_temp,SR;
        return (COMM_RX_EMPTY);                          /* Buffer is empty, return NUL              */
    }
}

#else  //使用查询方式

void TERMIO_Init(void) {
    SCI.SCCR0 = BAUD_RATE;       /**** Select baud rate:  55 for 9600, 26 for 19200 */
    SCI.SCCR1 = 0x000C;          /**** 8 bit, TE and RE set */
}

void TERMIO_PutChar(unsigned char ch)
{
    while (!(SCI.SCSR & 0x100));    /* wait for output buffer empty */ 
    SCI.SCDR = ch;
}

unsigned char TERMIO_GetChar(void)
{
    while (!(SCI.SCSR & 0x40)); /* wait for input */
    return SCI.SCDR;
}

#endif

static int do_padding;
static int left_flag;
static int len;
static int num1;
static int num2;
static char pad_character;

/*---------------------------------------------------*/
/*                                                   */
/* This routine puts pad characters into the output  */
/* buffer.                                           */
/*                                                   */
static void padding( const int l_flag)
{
   int i;

   if (do_padding && l_flag && (len < num1))
      for (i=len; i<num1; i++)
          TERMIO_PutChar( pad_character);
   }

/*---------------------------------------------------*/
/*                                                   */
/* This routine moves a string to the output buffer  */
/* as directed by the padding and positioning flags. */
/*                                                   */
static void outs( char* lp)
{
   /* pad on left if needed                          */
   len = strlen( lp);
   padding( !left_flag);

   /* Move string to the buffer                      */
   while (*lp && num2--)
      TERMIO_PutChar( *lp++);

   /* Pad on right if needed                         */
   len = strlen( lp);
   padding( left_flag);
   }

/*---------------------------------------------------*/
/*                                                   */
/* This routine moves a number to the output buffer  */
/* as directed by the padding and positioning flags. */
/*                                                   */


  
   
	
static void reoutnum(unsigned long num, unsigned int negative, const long base ) 
{
   char* cp;
   char outbuf[32];
   const char digits[] = "0123456789ABCDEF";
   
   /* Build number (backwards) in outbuf             */
   cp = outbuf;
   do {
      *cp++ = digits[(int)(num % base)];
      } while ((num /= base) > 0);
   if (negative)
      *cp++ = '-';
   *cp-- = 0;

   /* Move the converted number to the buffer and    */
   /* add in the padding where needed.               */
   len = strlen(outbuf);
   padding( !left_flag);
   while (cp >= outbuf)
      TERMIO_PutChar( *cp--);
   padding( left_flag);
}


static void outnum(long num, const long base ,unsigned char sign)//1, signed  0 unsigned
{   unsigned int negative;

   if ( (num < 0L) && sign ) 
   {  negative=1;
      num = -num;
      }
   else negative=0;
   
   reoutnum(num,negative,base);  
} 
/*---------------------------------------------------*/
/*                                                   */
/* This routine gets a number from the format        */
/* string.                                           */
/*                                                   */
static int getnum( char** linep)
{
   int n;
   char* cp;

   n = 0;
   cp = *linep;
   while (isdigit(*cp))
      n = n*10 + ((*cp++) - '0');
   *linep = cp;
   return(n);
}

/*---------------------------------------------------*/
/*                                                   */
/* This routine operates just like a printf/sprintf  */
/* routine. It outputs a set of data under the       */
/* control of a formatting string. Not all of the    */
/* standard C format control are supported. The ones */
/* provided are primarily those needed for embedded  */
/* systems work. Primarily the floaing point         */
/* routines are omitted. Other formats could be      */
/* added easily by following the examples shown for  */
/* the supported formats.                            */
/*                                                   */

void printp( char* ctrl, ...)
{

   int long_flag;
   int dot_flag;

   char ch;
   va_list argp;

   va_start( argp, ctrl);

   for ( ; *ctrl; ctrl++) {

      /* move format string chars to buffer until a  */
      /* format control is found.                    */
      if (*ctrl != '%') {
         TERMIO_PutChar(*ctrl);
#if CR_as_CRLF==TRUE         
         if(*ctrl=='\n') TERMIO_PutChar('\r');
#endif         
         continue;
         }

      /* initialize all the flags for this format.   */
      dot_flag   =
      long_flag  =
      left_flag  =
      do_padding = 0;
      pad_character = ' ';
      num2=32767;

try_next:
      ch = *(++ctrl);

      if (isdigit(ch)) {
         if (dot_flag)
            num2 = getnum(&ctrl);
         else {
            if (ch == '0')
               pad_character = '0';

            num1 = getnum(&ctrl);
            do_padding = 1;
         }
         ctrl--;
         goto try_next;
      }

      switch (tolower(ch)) {
         case '%':
              TERMIO_PutChar( '%');
              continue;

         case '-':
              left_flag = 1;
              break;

         case '.':
              dot_flag = 1;
              break;

         case 'l':
              long_flag = 1;
              break;

         case 'd':
              if (long_flag ==1 ) {
              		if(ch == 'D')                {outnum( va_arg(argp, unsigned long), 10L , 0);continue;}
              	        else  /* ch == 'd' */        {outnum( va_arg(argp, long), 10L,1);continue;}
                 }
              else {
              		if(ch == 'D')                {outnum( va_arg(argp, unsigned int),10L,0);continue;}
              		else  /* ch == 'd' */        {outnum( va_arg(argp, int), 10L,1);continue;}
                }
                
         case 'x':    // X 无符号 , x  有符号
              if (long_flag ==1 )  {
              	        if(ch == 'X')                {outnum( va_arg(argp, unsigned long), 16L,0);continue;}
              	        else  /* ch == 'x' */        {outnum( va_arg(argp, long), 16L,1);continue;} 
                 }
              else {
              		if(ch == 'X')                {outnum( va_arg(argp, unsigned int), 16L,0);continue;}
              		else  /* ch == 'x' */        {outnum( va_arg(argp, int), 16L,1);continue;}
                 } //如果按照16进制打印,将全部按照无符号数进行
              continue;

         case 's':
              outs( va_arg( argp, char*));
              continue;

         case 'c':
              TERMIO_PutChar( va_arg( argp, int));
              continue;

         default:
              continue;
         }
      goto try_next;
      }
   va_end( argp);
   }

⌨️ 快捷键说明

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