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

📄 main.c

📁 Real Time Operating System for Hi-Tech C compiler.
💻 C
📖 第 1 页 / 共 3 页
字号:
void TaskBeep ( void )
{
    char j;

    
    for (;;) {
        /* no services are available for us to wait the    */
        /*  sem, so we have to poll it ...                 */
    	OS_Delay(1, TaskBeep1);
    	
        /* is semaphore set?                               */
        if ( sysStat.beep ) {
        
            /* yes, clear it.                              */
            sysStat.beep = 0;
            
            /* we're gonna enter a tight loop (below), so  */
            /*  we won't be able to receive any incoming   */
            /*  RS-232 data ...                            */
            OutRS232(XOFF);
            
            /* create BEEP_DURATION pulses, each of        */
            /*  minimum width.                             */
            j = BEEP_PULSES;
            do {
                OutShiftRegister(BEEPER_ON, SHIFT_ONLY);
                OutShiftRegister(BEEPER_OFF, SHIFT_ONLY);    
            } while ( --j );
            
            /* now that we're done beeping, we can afford  */
            /*  to listen for incoming RS-232 again.       */
            OutRS232(XON);
        }            
    }
}


/************************************************************
****                                                     ****
**                                                         **
OutShiftRegister(byte, useStrobe)

Transfer data to U2 serially and latch it if requested. 

Bargraph updates require latching, beeper updates do not.

**                                                         **
****                                                     ****
************************************************************/
void OutShiftRegister ( char byte, char useStrobe )
{
    char i;
    
    
    /* force data, clock and strobe to be outputs with     */
    /*  all-zero values. Enable these outputs.             */
    outDATA = 0;
    outCLK  = 0;
    outSTB  = 0;
    TRIS    = GPIO_SERIAL_CONFIG;
    
    /* clock the 8 bits of the serial byte out GP0.        */
    i = 8;
    do {
        /* take shift clock LOW.                           */
        outCLK = 0;
        
        /* send serial data, msb first.                    */
        if ( byte & 0x80 )
            outDATA = 1;
        else
            outDATA = 0;
    
        /* shift data locally.                             */
        byte <<= 1;
    
        /* take shift clock HIGH and transfer data         */
        /*  into shift register.                           */
        outCLK = 1;    
        
    } while ( --i );
    
    /* transfer the newly-shifted byte to the              */
    /*  latch if requested.                                */
    if ( useStrobe ) {
        outSTB = 1;
        outSTB = 0;
    }
    
    /* lastly, restore PORTB directions to their           */
    /*  normal sense.                                      */
    TRIS = GPIO_NORMAL_CONFIG;
}


/************************************************************
****                                                     ****
**                                                         **
OutBargraph(pattern)

Since the need to update the bargraph and keep the beeper
off arises in several places, it makes sense to turn this
sequence into a function.

**                                                         **
****                                                     ****
************************************************************/
void OutBargraph ( char pattern )
{
    OutShiftRegister(pattern, SHIFT_AND_LATCH);
    OutShiftRegister(BEEPER_OFF, SHIFT_ONLY);
}


/************************************************************
****                                                     ****
**                                                         **
InRS232()

Read a character from the RS-232 port at 2400 baud. 

Returns RX_NO_CHAR if no activity was detected, and
RX_BAD_CHAR if the incoming char was clearly bad.

Adapted from HI-TECH example code.

**                                                         **
****                                                     ****
************************************************************/
char InRS232 ( void )
{
    char c, i, j;


    /* first, see if the line is not idle. If so, then     */
    /*  maybe it's a start bit.  If it isn't, then it      */
    /*  can't possibly be a start bit.                     */
    if ( inRX )
        return NO_RX_CHAR;
        
    /* okay, we may have got a start bit. Let's delay half */
    /*  a bit time so that if we did in fact pick up the   */
    /*  very beginning of the start bit, we won't end up on*/
    /*  edges of the data later on due to timing           */
    /*  variations.                                        */
    ShortDelay((oneBitDelay/2), j);    
   
    /* Now sample 8 bits of data. No need to initialize c, */
    /*  since all 8 bits are shifted out anyway.           */
    c = 0;
    i = 8;
    do {
       ShortDelay(oneBitDelay, j);
       c = (c >> 1) | (inRX << 7);
   } while ( --i );
    
    /* now that we've got the data bits, we must check the */
    /*  stop bit --  it had better be high. In fact, by    */
    /*  testing the next 9 bit for stop bit, we can avoid  */
    /*  some ambiguities that occur when the next data     */
    /*  follows closely.                                   */
    /* E.g. '5''5'... is 0 1010 1100 1 0 1010 1100 1 ...   */
    /*  which looks like 'S' (0x53) if we pick up a false  */
    /*  start bit in the fifth bit of the transmission.    */
    i = 9;
    ShortDelay(oneBitDelay, j);
    do {
        if ( !inRX )
            return BAD_RX_CHAR;
    } while ( --i );
        
    /* return w/received char.                             */
    return c;
}


/************************************************************
****                                                     ****
**                                                         **
OutRS232()

Send a character out the RS-232 port.

Adapted from HI-TECH example code.

**                                                         **
****                                                     ****
************************************************************/
void OutRS232 ( char byte )
{
    char i, j;
    
    
    /* can send chars unless remote system has told us not */
    /*  to.                                                */
    if ( sysStat.xmitOK ) {
                
        /* send start bit. outTX was previously high/idle. */
        outTX = 0;
        
        /* send data, LSB first, one it at a bit time.     */
        i = 8;
        do {
        
            ShortDelay(oneBitDelay, j);
            
            if ( byte & 0x01 )
                outTX = 1;
            else
                outTX = 0;
                
            byte >>= 1;
            
        } while ( --i );
        
        /* send stop bit. Line returns to idle condition.  */
        ShortDelay(oneBitDelay, j);
        outTX = 1;
    }
}


/************************************************************
****                                                     ****
**                                                         **
RcvCmd()

Get incoming RS-232 data. Echo most of them.

Note that we will be in here for between zero to three 
character times depending on what is detected and how we 
act on it. 

Command list:       '?':        report speed
                    '0':        turn fan off
                    '1'-'8':    set fan speed to 1-8
                    'S','s':    go to sleep immediately
                    'T','t':    sleep when timer expires
                    'W','w':    stay awake (don't sleep)   
                    
**                                                         **
****                                                     ****
************************************************************/
void RcvCmd ( void )
{
    char c;
    
    
    /* get incoming RS-232 character, if any.              */
    c = InRS232();
        
        
    /* if it's a speed command, update the speed and force */
    /*  a speed change -- char will be echoed via update.  */
    if ( ( c >= '0' ) && ( c <= '8' ) ) {
    
        speed          = c & 0x0F;
        sysStat.change =        1;
        c              =        0;
    }
    
    
    /* other commands don't require speed updates -- echo  */
    /*  certain ones.                                      */
    else {
        
        switch ( c ) {

            /* report fan speed.                           */
            case '?':
                c                  =   speed | '0';
                break;
            
            /* sleep now.                                  */
            case 'S':
            case 's':
                sysStat.dontSleep  =             0;
                sleepTimer         =             0;
                break;
    
            /* sleep TIME_TO_SLEEP from now.               */
            case 'T':
            case 't':
                sysStat.dontSleep  =             0;
                sleepTimer         = TIME_TO_SLEEP;
                break;
    
            /* stay awake forever.                         */
            case 'W':
            case 'w':
                sysStat.dontSleep  =             1;
                break; 
        
            /* received XOFF -- stop transmitting.         */
            case XOFF:
                sysStat.xmitOK     =             0;
                c                  =             0;
                break;
                
            /* received XON -- OK to transmit.             */
            case XON:
                sysStat.xmitOK     =             1;
                c                  =             0;
                break;
                    
            /* not present, bad or unknown command.        */        
            default:
                c                  =             0;
                break;
        }
    }
    
    /* echo if required.                                   */
    if ( c )
        OutRS232(c);
}

⌨️ 快捷键说明

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