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

📄 test_cl.c

📁 uCOS的源码例子
💻 C
📖 第 1 页 / 共 2 页
字号:
        P3 = 0xEF;                               /* Turn ON  LED                                       */
        OSTimeDly(TIME_50mS);                    /* Delay 50 mS                                        */
        P3 = 0xFF;                               /* Turn OFF LED                                       */
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                              MESSAGE POSTING TASK
*********************************************************************************************************
*/

void MsgPostTaskQ(far void *data)
{
    UBYTE letter;
    UBYTE err;


    while (1) {
        for (letter = 'A'; letter <= 'Z'; letter++) {
            err = OSQPost(QPtr, (far void *)&letter);
            OSTimeDly(TIME_1S);
        }
    }
}

/*
*********************************************************************************************************
*                                              MESSAGE PENDING TASK
*********************************************************************************************************
*/

void MsgPendTaskQ(far void *data)
{
        UBYTE  err;
    far UBYTE *msg;


    QMsg = '?';
    OSTimeDly(TIME_5S);                          /* Delay 5 seconds to accumulate 5 messages           */
    while (1) {
        msg  = OSQPend(QPtr, 0, &err);           /* Wait for message from MsgPostTaskQ()               */
        QMsg = *msg;                             /* Get message                                        */
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                                CLOCK TASK
*********************************************************************************************************
*/

void ClkTask(far void *data)
{
    data  = data;
    Min   =  0;                                  /* Initialize the clock                               */
    Sec   =  0;
    while (1) {
        OSTimeDly(TIME_1S);                      /* Delay 1 second                                     */
        if (Sec == 59) {                         /* Update the clock                                   */
            Sec = 0;
            if (Min == 99) {
                Min = 0;
            } else {
                Min++;
            }
        } else {
            Sec++;
        }
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                                DISPLAY TASK
*********************************************************************************************************
*/

void DispTask(far void *data)
{
        UBYTE  usage;
        UWORD  ctxsw;
        UBYTE  sec;
        UBYTE  min;
        UBYTE  err;
        UBYTE  bits;
    far UBYTE *msg;



    data   = data;                               /* Prevent compiler warning for unused argument       */
    InPort = (far UBYTE *)0xB0000;
    XAStrCpy(StatStr, PhilipsMsg);               /* Display sign on message when task is first started */
    XAComm0TxStr(StatStr);
    XAStrCpy(StatStr, XAMsg);
    XAComm0TxStr(StatStr);
    XAStrCpy(StatStr, uCOSMsg);
    XAComm0TxStr(StatStr);
    XAStrCpy(StatStr, StatMsg);
    while (1) {
        OS_ENTER_CRITICAL();                     /* Get local copy of data to display                  */
        usage        = CPUUsage;
        ctxsw        = OSCtxSwCtrMax;
        sec          = Sec;
        min          = Min;
        OS_EXIT_CRITICAL();
        StatStr[10]  = usage / 10 + '0';         /* Display the CPU usage in %                         */
        StatStr[11]  = usage % 10 + '0';
        StatStr[23]  = ctxsw / 1000 + '0';
        ctxsw       %= 1000;                     /* Display the number of context switches per second  */
        StatStr[24]  = ctxsw /  100 + '0';
        ctxsw       %=  100;
        StatStr[25]  = ctxsw /   10 + '0';
        ctxsw       %=   10;
        StatStr[26]  = ctxsw + '0';
        StatStr[29]  = min / 10 + '0';           /* Display the clock MM:SS                            */
        StatStr[30]  = min % 10 + '0';
        StatStr[32]  = sec / 10 + '0';
        StatStr[33]  = sec % 10 + '0';
        StatStr[39]  = QMsg;
        StatStr[43]  = (BOOLEAN)(*InPort & 0x80) ? '1' : '0';   /* Read and display 8-bit input port   */
        StatStr[44]  = (BOOLEAN)(*InPort & 0x40) ? '1' : '0';
        StatStr[45]  = (BOOLEAN)(*InPort & 0x20) ? '1' : '0';
        StatStr[46]  = (BOOLEAN)(*InPort & 0x10) ? '1' : '0';
        StatStr[48]  = (BOOLEAN)(*InPort & 0x08) ? '1' : '0';
        StatStr[49]  = (BOOLEAN)(*InPort & 0x04) ? '1' : '0';
        StatStr[50]  = (BOOLEAN)(*InPort & 0x02) ? '1' : '0';
        StatStr[51]  = (BOOLEAN)(*InPort & 0x01) ? '1' : '0';
        XAComm0TxStr(StatStr);
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                             Configure XA Timers
*
* Description: This function is called to initialize one of the XA's three timers to be used as the RTOS
*              'ticker'.
* Arguments  : tmr    is the timer number (0, 1 or 2)
*              freq   is the desired interrupt frequency (in Hz)
*              prio   is the priority for the timer interrupt
* Notes      : 1) Reload value is given by:
*
*                                    CrystalFreq
*                 reload = 65536 - ----------------
*                                  freq * prescaler
*              2) The frequency is assumed to be greater than 92 Hz
*********************************************************************************************************
*/
static void XATmrInit(UBYTE tmr, UWORD freq, UBYTE prio)
{
    UBYTE scale;
    ULONG prescaler;
    UWORD reload;


    scale = (SCR >> 2) & 0x03;         /* Find out what is the prescaler selection on the timers       */
    switch (scale) {
        case 0:
             prescaler = 4L;           /* OSC/4                                                        */
             break;

        case 1:
             prescaler = 16L;          /* OSC/16                                                       */
             break;

        case 2:
             prescaler = 64L;          /* OSC/64                                                       */
             break;
    }
    reload = (ULONG)65536L - (XA_CRYSTAL_FREQ / ((ULONG)freq * prescaler));

/*$PAGE*/
    switch (tmr) {
        case 0:                        /* TIMER 0                                                      */
             OS_ENTER_CRITICAL();
             TMOD &= 0xF0;             /* Timer 0: Non-gated, Timer mode, 16-bit auto reload           */
             TH0   = reload >> 8;      /* Count  register                                              */
             TL0   = reload & 0x00FF;
             RTH0  = reload >> 8;      /* Reload register                                              */
             RTL0  = reload & 0x00FF;
             IPA0 &= 0x0F;             /* Set interrupt priority for timer 0                           */
             IPA0 |= prio << 4;
             TCON |= 0x10;             /* Enable timer 0 (i.e. timer 0 allowed to run)                 */
             IEL  |= 0x02;             /* Enable timer 0 interrupts                                    */
             OS_EXIT_CRITICAL();
             break;

        case 1:                        /* TIMER 1                                                      */
             OS_ENTER_CRITICAL();
             TMOD &= 0x0F;             /* Timer 1: Non-gated, Timer mode, 16-bit auto reload           */
             TH1   = reload >> 8;      /* Count  register                                              */
             TL1   = reload & 0x00FF;
             RTH1  = reload >> 8;      /* Reload register                                              */
             RTL1  = reload & 0x00FF;
             IPA1 &= 0x0F;             /* Set interrupt priority for timer 1                           */
             IPA1 |= prio << 4;
             TCON |= 0x40;             /* Enable timer 1 (i.e. timer 1 allowed to run)                 */
             IEL  |= 0x08;             /* Enable timer 1 interrupts                                    */
             OS_EXIT_CRITICAL();
             break;

        case 2:                        /* TIMER 2                                                      */
             OS_ENTER_CRITICAL();
             T2MOD = 0x03;             /* Count up, Toggle output every overflow                       */
             TH2   = reload >> 8;      /* Count  register                                              */
             TL2   = reload & 0x00FF;
             IPA2 &= 0xF0;             /* Set interrupt priority for timer 1                           */
             IPA2 |= prio;
             T2CON = 0x0C;             /* Timer 2: Timer mode, 16-bit auto reload                      */
             IEL  |= 0x10;             /* Enable timer 2 interrupts                                    */
             OS_EXIT_CRITICAL();
             break;
    }
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                             Configure XA Serial Port #0
*
* Description: Initialize serial port #0 for mode 1 (8-bit UART).
* Arguments  : 'baud'    is the desired baud rate
* Notes      : Reload value is given by:
*
*                                   CrystalFreq
*              reload = 65536 - ---------------------
*                               freq * prescaler * 16
*********************************************************************************************************
*/
static void XAComm0Init(UWORD baud)
{
    UBYTE scale;
    ULONG prescaler;
    UWORD reload;


    scale = (SCR >> 2) & 0x03;         /* Find out what is the prescaler selection on the timers       */
    switch (scale) {
        case 0:
             prescaler = 4L;           /* OSC/4                                                        */
             break;

        case 1:
             prescaler = 16L;          /* OSC/16                                                       */
             break;

        case 2:
             prescaler = 64L;          /* OSC/64                                                       */
             break;
    }
    reload = (ULONG)65536L - (XA_CRYSTAL_FREQ / ((ULONG)baud * (ULONG)prescaler * 16L));

    TMOD  &= 0x0F;                     /* Timer 1: Non-gated, Timer mode, 16-bit auto reload           */
    TH1    = reload >> 8;              /* Count  register                                              */
    TL1    = reload & 0x00FF;
    RTH1   = reload >> 8;              /* Reload register                                              */
    RTL1   = reload & 0x00FF;
    TSTAT |= 0x04;                     /* Timer #1 to have its output toggle to verify with scope      */
    TCON  |= 0x40;                     /* Enable timer 1 (i.e. timer 1 allowed to run)                 */
    S0CON  = 0x52;                     /* Serial #0 to Mode 1, Enable Tx, Enable Rx                    */
}

/*$PAGE*/
/*
*********************************************************************************************************
*                                    Output ASCII string on serial port #0
*
* Description: Output string to serial port #0.
* Arguments  : 's'    is a NUL terminated string.
* Note       : This function is not interrupt driven.
*********************************************************************************************************
*/

static void XAComm0TxStr(far char *s)
{
    while (*s) {
        while ((S0CON & 0x02) == 0x00)      /* Wait until Tx is ready                                  */
            OSTimeDly(1);
        S0BUF  = *s++;                      /* Output next character to serial #0's Tx port            */
        S0CON &= ~0x02;                     /* Clear Tx ready flag                                     */
    }
}

/*
*********************************************************************************************************
*                                        Copy ASCII string to RAM
*********************************************************************************************************
*/

static void XAStrCpy(far char *s1, code char *s2)
{
    while (*s2) {
        *s1++ = *s2++;
    }
    *s1 = 0x00;
}

⌨️ 快捷键说明

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