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

📄 conio.c

📁 Texas-Instrument C2000 Series DSP example programs
💻 C
字号:
/*=====================================================================*/
/*  Name:           CONIO.C                                            */
/*  Project:        RS232.LIB                                           */
/*  Originator:     Balasz Simor                                       */
/*  Description:    Provides functions for handling terminal i/o       */
/*                  and strings stored in program memory (flash)       */
/* ====================================================================*/
/*                                                                     */
/*      Function List:  int  getch(void)                               */
/*                      int  putch(void)                               */
/*                      int  clrscr(void)                              */ 
/*              NEW     void sendstr_PMEM(char *x)                     */
/*              NEW     void sendstr_DMEM(char *x)                     */
/*              MOD     long getlong(char *text, long deflt,           */
/*                                   long lowlimit, long highlimit)    */
/*                      int  getline(char *x, int maxlen)              */
/*              NEW     long atol_F240(const char *st)                 */
/*              NEW     char read_PMEM(char *x)                        */
/*                                                                     */
/*       Status:                                                       */
/*                                                                     */
/*       Target:        TMS320C240                                    */
/*                                                                     */
/*       History:       (Date, Revision, Who, What)                   */
/*       --------------------------------------------------------------*/
/*                                                                     */
/*       19/11/97        1.1    Martin Staebler                        */
/*                              Added functions to handle strings      */
/*                              stored in FLASH (PMEM) and modified    */
/*                              atoi NOT to use lookup table _ctypes   */
/*=====================================================================*/
                                 

/*--------------*/
/* Header Files */
/*--------------*/
#include <stdlib.h>             
#include <math.h>       
#include <C240.h>               /* peripheral register declaration for C240 */
#include <sci.h>                /* c24x peripherals lib */
#include <conio.h>              /* c24x monitor lib */


/*======================================================================*/
/* Subroutines                                                          */
/*======================================================================*/


/*======================================================================*/
/* int getch(void)                                                      */
/*======================================================================*/
int getch(void)
{
int c;
    while (-1 == (c = SCI_Getc()));
    return c;
}


/*======================================================================*/
/* int putch(void)                                                      */
/*======================================================================*/
void putch(int c)
{
    while (-1 == SCI_Sendc(c));
}


/*======================================================================*/
/* int clrscr(void)                                                     */
/*======================================================================*/
void clrscr(void)
{
    sendstr_PMEM("\x1b\x63"); /* ESC-c */
    sendstr_PMEM("\x0f");
}


/*======================================================================*/
/* void sendstr_PMEM(char *x)                                                   */
/*======================================================================*/
/* Function:      Sends a string which is stored in Program Space       */
/*                                                                      */
/* Arguments:     Start address of string                               */
/*                                                                      */
/* Return value:  None                                                  */      
/*======================================================================*/
void sendstr_PMEM(char *x)
{
   char buffer;

   buffer = read_PMEM(x);
   
   while(buffer)
   {
      while(-1 == SCI_Sendc(buffer));
      {
        x++;
        buffer = read_PMEM(x);
      }
   } /* while */
}


/*======================================================================*/
/* void sendstr_DMEM(char *x)                                           */
/*======================================================================*/
/* Function:      Sends a string which is stored in Data Space          */
/*                                                                      */
/* Arguments:     Start address of string                               */
/*                                                                      */
/* Return value:  None                                                  */      
/*======================================================================*/
void sendstr_DMEM(char *x)
{
   while(*x)
   {
      while(-1 == SCI_Sendc(*x));
      {
        x++;
      }
   } /* while */
}





/*=====================================================================*/
/* long getlong(char *text, long deflt, long lowlimit, long highlimit) */
/*=====================================================================*/
long getlong(char *text, long deflt, long lowlimit, long highlimit)
{
    char buf[10];
    long val;
    
    do
    {
        sendstr_PMEM(text);
        sendstr_PMEM("[");
        ltoa(lowlimit, buf);
        sendstr_DMEM(buf);
        sendstr_PMEM(",");
        ltoa(highlimit, buf);
        sendstr_DMEM(buf);
        sendstr_PMEM("]");
        sendstr_PMEM("<");
        ltoa(deflt, buf);
        sendstr_DMEM(buf);
        sendstr_PMEM(">: ");
        getline(buf,10);
        if (buf[0] == 0)
          val = deflt;
        else
          val = atol_F240(buf);
    } while (val<lowlimit || val>highlimit);
    return val;
}


/*=====================================================================*/
/* int getline(char *x, int maxlen)                                    */
/*=====================================================================*/
int getline(char *x, int maxlen)
{
   int c;
   int pos = 0;

   do
   {
      c = getch();
     
      if (c == '\b')
      {
         if (pos > 0)
            pos--;
         else
            c = '\a';  /* bell */
      } /* if */

      if (c == '\r')
         x[pos++] = 0;

      if (c != '\r' && c != '\b' && c != '\a')
      {
         if (pos == maxlen-1)
            c = '\a';
         else
            x[pos++] = c;
      }

      putch(c);
  } while (c != '\r');

  return (pos);       /* returning length of string */
}  




/*======================================================================*/
/* long atol_F240(const char *st)                                       */
/*======================================================================*/
/* Function:  Converts ascii-string to long interger. Modified from     */
/*            rts2xx.lib. This function doesn't use _ctypes_[]          */
/*            lookup table (ctypes.h) to be stored in DATA memory       */
/*                                                                      */
/* Arguments:     Start address of string                               */
/*                                                                      */
/* Return value:  None                                                  */      
/*======================================================================*/
long atol_F240(const char *st)
{
    register const char  *fst   = st;
    register       long  result = 0;
    register       char  fc;

    /*------------------*/    
    /* skip white space */
    /*------------------*/    
    while ((*fst == 0x20) || ((*fst > 0x08) && (*fst < 0x0E)))
    {
       fst++;                      
    }         
    
    /*-----------*/    
    /* skip sign */
    /*-----------*/    
    fc = *fst;
    if ((fc == '-') || (fc == '+')) 
       fst++;

    /*---------------*/    
    /* long-to-ascii */
    /*---------------*/    
    while ((*fst >= '0') && (*fst <= '9'))
    {
        result *= 10;
        result += *fst++ - '0';
    }

    return (fc == '-') ? -result : result;
}




/*=====================================================================*/
/* char read_PMEM(char *x)                                             */
/*=====================================================================*/
/* Arguments:     address of character in Program space                */
/*                                                                     */
/* Return Value:  char                                                 */
/*=====================================================================*/
asm("           .text                                           ");
asm("           .def    _read_PMEM                              ");
asm("_read_PMEM:                                                ");
asm("           popd    *+              ;push return address    ");
asm("           sar     AR0, *+         ;push old frame pointer ");
asm("           sar     AR1, *                                  ");
asm("           lar     AR0,*           ;init new frame pointer ");
asm("           adrk    #1              ;alocate space for one local variable ");
asm("                                                           ");                             
asm("           lar     AR2,#-3                                 ");                             
asm("           mar     *,AR2                                   ");
asm("           mar     *0+             ;AR2 = x                ");    
asm("           LACC    *               ;ACC = x                ");
asm("           ADRK    #3              ;AR2 = local #1         ");
asm("           TBLR    *               ;local #1 = *x(PMEM)    ");
asm("           LACC    *               ;ACC = char             ");
asm("                                                           ");
asm("        ;context restore                                   ");
asm("        ;---------------                                   ");
asm("           mar     *,AR1                                   ");
asm("           sbrk    #(1+1)          ;pop local var's+1 from stack ");
asm("           lar     ar0, *-         ;restore old frame pointer    ");
asm("           pshd    *               ;restore return address       ");
asm("           ret                                                   ");

⌨️ 快捷键说明

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