打印

PSoC4进不了串口中断

[复制链接]
2167|35
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
写了个小程序,发现进不了中断,郁闷的很
沙发
heping517|  楼主 | 2015-6-25 08:34 | 只看该作者
#include <device.h>

#define LCD_NUM_COLUMNS (16u)


void main()
{   
    char8 ch;       /* Data received from the Serial port */
    uint8 count = 0u;
    uint8 pos = 0u;

    CyGlobalIntEnable; /* Enable all interrupts by the processor. */

    LCD_Char_1_Start();
    UART_1_Start();

    while(1)
    {
        /* Check the UART status */
        ch = UART_1_GetChar();

        /* If byte received */
        if(ch > 0u)
        {
            count++;        
            /* If the count value reaches the count 16 start from first location */
            if(count % LCD_NUM_COLUMNS == 0u)
            {
                pos = 0u; /* resets the count value */
                /* Display will be cleared when reached count value 16 */
                LCD_Char_1_WriteControl(LCD_Char_1_CLEAR_DISPLAY);
            }

            LCD_Char_1_Position(0u, pos++);
            LCD_Char_1_PutChar(ch);         /* Print the received character */

            LCD_Char_1_Position(1u, 0u);
            LCD_Char_1_PrintInt8(count);    /* Prints the count in the LCD */
        }
    }
}

使用特权

评论回复
板凳
heping517|  楼主 | 2015-6-25 08:36 | 只看该作者
图片

QQ截图20150624164521.jpg (58.26 KB )

QQ截图20150624164521.jpg

使用特权

评论回复
地板
heping517|  楼主 | 2015-6-25 08:36 | 只看该作者
图片

QQ截图20150625083630.jpg (149.82 KB )

QQ截图20150625083630.jpg

使用特权

评论回复
5
heping517|  楼主 | 2015-6-25 08:37 | 只看该作者
#include "UART_1.h"
#include "CyLib.h"
#if(UART_1_INTERNAL_CLOCK_USED)
    #include "UART_1_IntClock.h"
#endif /* End UART_1_INTERNAL_CLOCK_USED */


/***************************************
* Global data allocation
***************************************/

uint8 UART_1_initVar = 0u;
#if( UART_1_TX_ENABLED && (UART_1_TXBUFFERSIZE > UART_1_FIFO_LENGTH))
    volatile uint8 UART_1_txBuffer[UART_1_TXBUFFERSIZE];
    volatile uint8 UART_1_txBufferRead = 0u;
    uint8 UART_1_txBufferWrite = 0u;
#endif /* End UART_1_TX_ENABLED */
#if( ( UART_1_RX_ENABLED || UART_1_HD_ENABLED ) && \
     (UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH) )
    volatile uint8 UART_1_rxBuffer[UART_1_RXBUFFERSIZE];
    volatile uint8 UART_1_rxBufferRead = 0u;
    volatile uint8 UART_1_rxBufferWrite = 0u;
    volatile uint8 UART_1_rxBufferLoopDetect = 0u;
    volatile uint8 UART_1_rxBufferOverflow = 0u;
    #if (UART_1_RXHW_ADDRESS_ENABLED)
        volatile uint8 UART_1_rxAddressMode = UART_1_RXADDRESSMODE;
        volatile uint8 UART_1_rxAddressDetected = 0u;
    #endif /* End EnableHWAddress */
#endif /* End UART_1_RX_ENABLED */


/*******************************************************************************
* Function Name: UART_1_Start
********************************************************************************
*
* Summary:
*  Initialize and Enable the UART component.
*  Enable the clock input to enable operation.
*
* Parameters:
*  None.
*
* Return:
*  None.
*
* Global variables:
*  The UART_1_intiVar variable is used to indicate initial
*  configuration of this component. The variable is initialized to zero (0u)
*  and set to one (1u) the first time UART_Start() is called. This allows for
*  component initialization without re-initialization in all subsequent calls
*  to the UART_1_Start() routine.
*
* Reentrant:
*  No.
*
*******************************************************************************/
void UART_1_Start(void)
{
    /* If not Initialized then initialize all required hardware and software */
    if(UART_1_initVar == 0u)
    {
        UART_1_Init();
        UART_1_initVar = 1u;
    }
    UART_1_Enable();
}

使用特权

评论回复
6
heping517|  楼主 | 2015-6-25 08:37 | 只看该作者

/*******************************************************************************
* Function Name: UART_1_Init
********************************************************************************
*
* Summary:
*  Initialize component's parameters to the parameters set by user in the
*  customizer of the component placed onto schematic. Usually called in
*  UART_1_Start().
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void UART_1_Init(void)
{
    #if(UART_1_RX_ENABLED || UART_1_HD_ENABLED)

        #if(UART_1_RX_INTERRUPT_ENABLED && (UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH))
            /* Set the RX Interrupt. */
            (void)CyIntSetVector(UART_1_RX_VECT_NUM, &UART_1_RXISR);
            CyIntSetPriority(UART_1_RX_VECT_NUM, UART_1_RX_PRIOR_NUM);
        #endif /* End UART_1_RX_INTERRUPT_ENABLED */

        #if (UART_1_RXHW_ADDRESS_ENABLED)
            UART_1_SetRxAddressMode(UART_1_RXAddressMode);
            UART_1_SetRxAddress1(UART_1_RXHWADDRESS1);
            UART_1_SetRxAddress2(UART_1_RXHWADDRESS2);
        #endif /* End UART_1_RXHW_ADDRESS_ENABLED */

        /* Init Count7 period */
        UART_1_RXBITCTR_PERIOD_REG = UART_1_RXBITCTR_INIT;
        /* Configure the Initial RX interrupt mask */
        UART_1_RXSTATUS_MASK_REG  = UART_1_INIT_RX_INTERRUPTS_MASK;
    #endif /* End UART_1_RX_ENABLED || UART_1_HD_ENABLED*/

    #if(UART_1_TX_ENABLED)
        #if(UART_1_TX_INTERRUPT_ENABLED && (UART_1_TXBUFFERSIZE > UART_1_FIFO_LENGTH))
            /* Set the TX Interrupt. */
            (void)CyIntSetVector(UART_1_TX_VECT_NUM, &UART_1_TXISR);
            CyIntSetPriority(UART_1_TX_VECT_NUM, UART_1_TX_PRIOR_NUM);
        #endif /* End UART_1_TX_INTERRUPT_ENABLED */

        /* Write Counter Value for TX Bit Clk Generator*/
        #if(UART_1_TXCLKGEN_DP)
            UART_1_TXBITCLKGEN_CTR_REG = UART_1_BIT_CENTER;
            UART_1_TXBITCLKTX_COMPLETE_REG = (UART_1_NUMBER_OF_DATA_BITS +
                        UART_1_NUMBER_OF_START_BIT) * UART_1_OVER_SAMPLE_COUNT;
        #else
            UART_1_TXBITCTR_PERIOD_REG = ((UART_1_NUMBER_OF_DATA_BITS +
                        UART_1_NUMBER_OF_START_BIT) * UART_1_OVER_SAMPLE_8) - 1u;
        #endif /* End UART_1_TXCLKGEN_DP */

        /* Configure the Initial TX interrupt mask */
        #if(UART_1_TX_INTERRUPT_ENABLED && (UART_1_TXBUFFERSIZE > UART_1_FIFO_LENGTH))
            UART_1_TXSTATUS_MASK_REG = UART_1_TX_STS_FIFO_EMPTY;
        #else
            UART_1_TXSTATUS_MASK_REG = UART_1_INIT_TX_INTERRUPTS_MASK;
        #endif /*End UART_1_TX_INTERRUPT_ENABLED*/

    #endif /* End UART_1_TX_ENABLED */

    #if(UART_1_PARITY_TYPE_SW)  /* Write Parity to Control Register */
        UART_1_WriteControlRegister( \
            (UART_1_ReadControlRegister() & (uint8)~UART_1_CTRL_PARITY_TYPE_MASK) | \
            (uint8)(UART_1_PARITY_TYPE << UART_1_CTRL_PARITY_TYPE0_SHIFT) );
    #endif /* End UART_1_PARITY_TYPE_SW */
}

使用特权

评论回复
7
heping517|  楼主 | 2015-6-25 08:38 | 只看该作者
/*******************************************************************************
* Function Name: UART_1_Enable
********************************************************************************
*
* Summary:
*  Enables the UART block operation
*
* Parameters:
*  None.
*
* Return:
*  None.
*
* Global Variables:
*  UART_1_rxAddressDetected - set to initial state (0).
*
*******************************************************************************/
void UART_1_Enable(void)
{
    uint8 enableInterrupts;
    enableInterrupts = CyEnterCriticalSection();

    #if(UART_1_RX_ENABLED || UART_1_HD_ENABLED)
        /*RX Counter (Count7) Enable */
        UART_1_RXBITCTR_CONTROL_REG |= UART_1_CNTR_ENABLE;
        /* Enable the RX Interrupt. */
        UART_1_RXSTATUS_ACTL_REG  |= UART_1_INT_ENABLE;
        #if(UART_1_RX_INTERRUPT_ENABLED && (UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH))
            CyIntEnable(UART_1_RX_VECT_NUM);
            #if (UART_1_RXHW_ADDRESS_ENABLED)
                UART_1_rxAddressDetected = 0u;
            #endif /* End UART_1_RXHW_ADDRESS_ENABLED */
        #endif /* End UART_1_RX_INTERRUPT_ENABLED */
    #endif /* End UART_1_RX_ENABLED || UART_1_HD_ENABLED*/

    #if(UART_1_TX_ENABLED)
        /*TX Counter (DP/Count7) Enable */
        #if(!UART_1_TXCLKGEN_DP)
            UART_1_TXBITCTR_CONTROL_REG |= UART_1_CNTR_ENABLE;
        #endif /* End UART_1_TXCLKGEN_DP */
        /* Enable the TX Interrupt. */
        UART_1_TXSTATUS_ACTL_REG |= UART_1_INT_ENABLE;
        #if(UART_1_TX_INTERRUPT_ENABLED && (UART_1_TXBUFFERSIZE > UART_1_FIFO_LENGTH))
            CyIntEnable(UART_1_TX_VECT_NUM);
        #endif /* End UART_1_TX_INTERRUPT_ENABLED*/
     #endif /* End UART_1_TX_ENABLED */

    #if(UART_1_INTERNAL_CLOCK_USED)
        /* Enable the clock. */
        UART_1_IntClock_Start();
    #endif /* End UART_1_INTERNAL_CLOCK_USED */

    CyExitCriticalSection(enableInterrupts);
}

使用特权

评论回复
8
heping517|  楼主 | 2015-6-25 08:38 | 只看该作者

/*******************************************************************************
* Function Name: UART_1_Stop
********************************************************************************
*
* Summary:
*  Disable the UART component
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void UART_1_Stop(void)
{
    uint8 enableInterrupts;
    enableInterrupts = CyEnterCriticalSection();

    /* Write Bit Counter Disable */
    #if(UART_1_RX_ENABLED || UART_1_HD_ENABLED)
        UART_1_RXBITCTR_CONTROL_REG &= (uint8)~UART_1_CNTR_ENABLE;
    #endif /* End UART_1_RX_ENABLED */

    #if(UART_1_TX_ENABLED)
        #if(!UART_1_TXCLKGEN_DP)
            UART_1_TXBITCTR_CONTROL_REG &= (uint8)~UART_1_CNTR_ENABLE;
        #endif /* End UART_1_TXCLKGEN_DP */
    #endif /* UART_1_TX_ENABLED */

    #if(UART_1_INTERNAL_CLOCK_USED)
        /* Disable the clock. */
        UART_1_IntClock_Stop();
    #endif /* End UART_1_INTERNAL_CLOCK_USED */

    /* Disable internal interrupt component */
    #if(UART_1_RX_ENABLED || UART_1_HD_ENABLED)
        UART_1_RXSTATUS_ACTL_REG  &= (uint8)~UART_1_INT_ENABLE;
        #if(UART_1_RX_INTERRUPT_ENABLED && (UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH))
            UART_1_DisableRxInt();
        #endif /* End UART_1_RX_INTERRUPT_ENABLED */
    #endif /* End UART_1_RX_ENABLED */

    #if(UART_1_TX_ENABLED)
        UART_1_TXSTATUS_ACTL_REG &= (uint8)~UART_1_INT_ENABLE;
        #if(UART_1_TX_INTERRUPT_ENABLED && (UART_1_TXBUFFERSIZE > UART_1_FIFO_LENGTH))
            UART_1_DisableTxInt();
        #endif /* End UART_1_TX_INTERRUPT_ENABLED */
    #endif /* End UART_1_TX_ENABLED */

    CyExitCriticalSection(enableInterrupts);
}

使用特权

评论回复
9
heping517|  楼主 | 2015-6-25 08:38 | 只看该作者
/*******************************************************************************
* Function Name: UART_1_ReadControlRegister
********************************************************************************
*
* Summary:
*  Read the current state of the control register
*
* Parameters:
*  None.
*
* Return:
*  Current state of the control register.
*
*******************************************************************************/
uint8 UART_1_ReadControlRegister(void)
{
    #if( UART_1_CONTROL_REG_REMOVED )
        return(0u);
    #else
        return(UART_1_CONTROL_REG);
    #endif /* End UART_1_CONTROL_REG_REMOVED */
}

使用特权

评论回复
10
heping517|  楼主 | 2015-6-25 08:39 | 只看该作者

/*******************************************************************************
* Function Name: UART_1_WriteControlRegister
********************************************************************************
*
* Summary:
*  Writes an 8-bit value into the control register
*
* Parameters:
*  control:  control register value
*
* Return:
*  None.
*
*******************************************************************************/
void  UART_1_WriteControlRegister(uint8 control)
{
    #if( UART_1_CONTROL_REG_REMOVED )
        if(control != 0u) { }      /* release compiler warning */
    #else
       UART_1_CONTROL_REG = control;
    #endif /* End UART_1_CONTROL_REG_REMOVED */
}

使用特权

评论回复
11
heping517|  楼主 | 2015-6-25 08:39 | 只看该作者
    /*******************************************************************************
    * Function Name: UART_1_SetRxInterruptMode
    ********************************************************************************
    *
    * Summary:
    *  Configure which status bits trigger an interrupt event
    *
    * Parameters:
    *  IntSrc:  An or'd combination of the desired status bit masks (defined in
    *           the header file)
    *
    * Return:
    *  None.
    *
    * Theory:
    *  Enables the output of specific status bits to the interrupt controller
    *
    *******************************************************************************/
    void UART_1_SetRxInterruptMode(uint8 intSrc)
    {
        UART_1_RXSTATUS_MASK_REG  = intSrc;
    }

使用特权

评论回复
12
heping517|  楼主 | 2015-6-25 08:40 | 只看该作者
/*******************************************************************************
    * Function Name: UART_1_ReadRxData
    ********************************************************************************
    *
    * Summary:
    *  Returns data in RX Data register without checking status register to
    *  determine if data is valid
    *
    * Parameters:
    *  None.
    *
    * Return:
    *  Received data from RX register
    *
    * Global Variables:
    *  UART_1_rxBuffer - RAM buffer pointer for save received data.
    *  UART_1_rxBufferWrite - cyclic index for write to rxBuffer,
    *     checked to identify new data.
    *  UART_1_rxBufferRead - cyclic index for read from rxBuffer,
    *     incremented after each byte has been read from buffer.
    *  UART_1_rxBufferLoopDetect - creared if loop condition was detected
    *     in RX ISR.
    *
    * Reentrant:
    *  No.
    *
    *******************************************************************************/
    uint8 UART_1_ReadRxData(void)
    {
        uint8 rxData;

        #if(UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH)
            uint8 loc_rxBufferRead;
            uint8 loc_rxBufferWrite;
            /* Protect variables that could change on interrupt. */
            /* Disable Rx interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_DisableRxInt();
            #endif /* UART_1_RX_INTERRUPT_ENABLED */
            loc_rxBufferRead = UART_1_rxBufferRead;
            loc_rxBufferWrite = UART_1_rxBufferWrite;

            if( (UART_1_rxBufferLoopDetect != 0u) || (loc_rxBufferRead != loc_rxBufferWrite) )
            {
                rxData = UART_1_rxBuffer[loc_rxBufferRead];
                loc_rxBufferRead++;

                if(loc_rxBufferRead >= UART_1_RXBUFFERSIZE)
                {
                    loc_rxBufferRead = 0u;
                }
                /* Update the real pointer */
                UART_1_rxBufferRead = loc_rxBufferRead;

                if(UART_1_rxBufferLoopDetect != 0u )
                {
                    UART_1_rxBufferLoopDetect = 0u;
                    #if( (UART_1_RX_INTERRUPT_ENABLED) && (UART_1_FLOW_CONTROL != 0u) && \
                         (UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH) )
                        /* When Hardware Flow Control selected - return RX mask */
                        #if( UART_1_HD_ENABLED )
                            if((UART_1_CONTROL_REG & UART_1_CTRL_HD_SEND) == 0u)
                            {   /* In Half duplex mode return RX mask only in RX
                                *  configuration set, otherwise
                                *  mask will be returned in LoadRxConfig() API.
                                */
                                UART_1_RXSTATUS_MASK_REG  |= UART_1_RX_STS_FIFO_NOTEMPTY;
                            }
                        #else
                            UART_1_RXSTATUS_MASK_REG  |= UART_1_RX_STS_FIFO_NOTEMPTY;
                        #endif /* end UART_1_HD_ENABLED */
                    #endif /* UART_1_RX_INTERRUPT_ENABLED and Hardware flow control*/
                }
            }
            else
            {   /* Needs to check status for RX_STS_FIFO_NOTEMPTY bit*/
                rxData = UART_1_RXDATA_REG;
            }

            /* Enable Rx interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_EnableRxInt();
            #endif /* End UART_1_RX_INTERRUPT_ENABLED */

        #else /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

            /* Needs to check status for RX_STS_FIFO_NOTEMPTY bit*/
            rxData = UART_1_RXDATA_REG;

        #endif /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

        return(rxData);
    }

使用特权

评论回复
13
heping517|  楼主 | 2015-6-25 08:40 | 只看该作者
    /*******************************************************************************
    * Function Name: UART_1_ReadRxStatus
    ********************************************************************************
    *
    * Summary:
    *  Read the current state of the status register
    *  And detect software buffer overflow.
    *
    * Parameters:
    *  None.
    *
    * Return:
    *  Current state of the status register.
    *
    * Global Variables:
    *  UART_1_rxBufferOverflow - used to indicate overload condition.
    *   It set to one in RX interrupt when there isn?t free space in
    *   UART_1_rxBufferRead to write new data. This condition returned
    *   and cleared to zero by this API as an
    *   UART_1_RX_STS_SOFT_BUFF_OVER bit along with RX Status register
    *   bits.
    *
    *******************************************************************************/
    uint8 UART_1_ReadRxStatus(void)
    {
        uint8 status;

        status = UART_1_RXSTATUS_REG & UART_1_RX_HW_MASK;

        #if(UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH)
            if( UART_1_rxBufferOverflow != 0u )
            {
                status |= UART_1_RX_STS_SOFT_BUFF_OVER;
                UART_1_rxBufferOverflow = 0u;
            }
        #endif /* UART_1_RXBUFFERSIZE */

        return(status);
    }

使用特权

评论回复
14
heping517|  楼主 | 2015-6-25 08:40 | 只看该作者
   /*******************************************************************************
    * Function Name: UART_1_GetChar
    ********************************************************************************
    *
    * Summary:
    *  Reads UART RX buffer immediately, if data is not available or an error
    *  condition exists, zero is returned; otherwise, character is read and
    *  returned.
    *
    * Parameters:
    *  None.
    *
    * Return:
    *  Character read from UART RX buffer. ASCII characters from 1 to 255 are valid.
    *  A returned zero signifies an error condition or no data available.
    *
    * Global Variables:
    *  UART_1_rxBuffer - RAM buffer pointer for save received data.
    *  UART_1_rxBufferWrite - cyclic index for write to rxBuffer,
    *     checked to identify new data.
    *  UART_1_rxBufferRead - cyclic index for read from rxBuffer,
    *     incremented after each byte has been read from buffer.
    *  UART_1_rxBufferLoopDetect - creared if loop condition was detected
    *     in RX ISR.
    *
    * Reentrant:
    *  No.
    *
    *******************************************************************************/
    uint8 UART_1_GetChar(void)
    {
        uint8 rxData = 0u;
        uint8 rxStatus;

        #if(UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH)
            uint8 loc_rxBufferRead;
            uint8 loc_rxBufferWrite;
            /* Protect variables that could change on interrupt. */
            /* Disable Rx interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_DisableRxInt();
            #endif /* UART_1_RX_INTERRUPT_ENABLED */
            loc_rxBufferRead = UART_1_rxBufferRead;
            loc_rxBufferWrite = UART_1_rxBufferWrite;

            if( (UART_1_rxBufferLoopDetect != 0u) || (loc_rxBufferRead != loc_rxBufferWrite) )
            {
                rxData = UART_1_rxBuffer[loc_rxBufferRead];
                loc_rxBufferRead++;
                if(loc_rxBufferRead >= UART_1_RXBUFFERSIZE)
                {
                    loc_rxBufferRead = 0u;
                }
                /* Update the real pointer */
                UART_1_rxBufferRead = loc_rxBufferRead;

                if(UART_1_rxBufferLoopDetect > 0u )
                {
                    UART_1_rxBufferLoopDetect = 0u;
                    #if( (UART_1_RX_INTERRUPT_ENABLED) && (UART_1_FLOW_CONTROL != 0u) )
                        /* When Hardware Flow Control selected - return RX mask */
                        #if( UART_1_HD_ENABLED )
                            if((UART_1_CONTROL_REG & UART_1_CTRL_HD_SEND) == 0u)
                            {   /* In Half duplex mode return RX mask only if
                                *  RX configuration set, otherwise
                                *  mask will be returned in LoadRxConfig() API.
                                */
                                UART_1_RXSTATUS_MASK_REG  |= UART_1_RX_STS_FIFO_NOTEMPTY;
                            }
                        #else
                            UART_1_RXSTATUS_MASK_REG  |= UART_1_RX_STS_FIFO_NOTEMPTY;
                        #endif /* end UART_1_HD_ENABLED */
                    #endif /* UART_1_RX_INTERRUPT_ENABLED and Hardware flow control*/
                }

            }
            else
            {   rxStatus = UART_1_RXSTATUS_REG;
                if((rxStatus & UART_1_RX_STS_FIFO_NOTEMPTY) != 0u)
                {   /* Read received data from FIFO*/
                    rxData = UART_1_RXDATA_REG;
                    /*Check status on error*/
                    if((rxStatus & (UART_1_RX_STS_BREAK | UART_1_RX_STS_PAR_ERROR |
                                   UART_1_RX_STS_STOP_ERROR | UART_1_RX_STS_OVERRUN)) != 0u)
                    {
                        rxData = 0u;
                    }
                }
            }

            /* Enable Rx interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_EnableRxInt();
            #endif /* UART_1_RX_INTERRUPT_ENABLED */

        #else /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

            rxStatus =UART_1_RXSTATUS_REG;
            if((rxStatus & UART_1_RX_STS_FIFO_NOTEMPTY) != 0u)
            {   /* Read received data from FIFO*/
                rxData = UART_1_RXDATA_REG;
                /*Check status on error*/
                if((rxStatus & (UART_1_RX_STS_BREAK | UART_1_RX_STS_PAR_ERROR |
                               UART_1_RX_STS_STOP_ERROR | UART_1_RX_STS_OVERRUN)) != 0u)
                {
                    rxData = 0u;
                }
            }
        #endif /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

        return(rxData);
    }

使用特权

评论回复
15
heping517|  楼主 | 2015-6-25 08:40 | 只看该作者
    /*******************************************************************************
    * Function Name: UART_1_GetByte
    ********************************************************************************
    *
    * Summary:
    *  Grab the next available byte of data from the recieve FIFO
    *
    * Parameters:
    *  None.
    *
    * Return:
    *  MSB contains Status Register and LSB contains UART RX data
    *
    * Reentrant:
    *  No.
    *
    *******************************************************************************/
    uint16 UART_1_GetByte(void)
    {
        return ( ((uint16)UART_1_ReadRxStatus() << 8u) | UART_1_ReadRxData() );
    }

使用特权

评论回复
16
heping517|  楼主 | 2015-6-25 08:41 | 只看该作者
/*******************************************************************************
    * Function Name: UART_1_GetRxBufferSize
    ********************************************************************************
    *
    * Summary:
    *  Determine the amount of bytes left in the RX buffer and return the count in
    *  bytes
    *
    * Parameters:
    *  None.
    *
    * Return:
    *  uint8: Integer count of the number of bytes left
    *  in the RX buffer
    *
    * Global Variables:
    *  UART_1_rxBufferWrite - used to calculate left bytes.
    *  UART_1_rxBufferRead - used to calculate left bytes.
    *  UART_1_rxBufferLoopDetect - checked to decide left bytes amount.
    *
    * Reentrant:
    *  No.
    *
    * Theory:
    *  Allows the user to find out how full the RX Buffer is.
    *
    *******************************************************************************/
    uint8 UART_1_GetRxBufferSize(void)
                                                            
    {
        uint8 size;

        #if(UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH)

            /* Disable Rx interrupt. */
            /* Protect variables that could change on interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_DisableRxInt();
            #endif /* UART_1_RX_INTERRUPT_ENABLED */

            if(UART_1_rxBufferRead == UART_1_rxBufferWrite)
            {
                if(UART_1_rxBufferLoopDetect > 0u)
                {
                    size = UART_1_RXBUFFERSIZE;
                }
                else
                {
                    size = 0u;
                }
            }
            else if(UART_1_rxBufferRead < UART_1_rxBufferWrite)
            {
                size = (UART_1_rxBufferWrite - UART_1_rxBufferRead);
            }
            else
            {
                size = (UART_1_RXBUFFERSIZE - UART_1_rxBufferRead) + UART_1_rxBufferWrite;
            }

            /* Enable Rx interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_EnableRxInt();
            #endif /* End UART_1_RX_INTERRUPT_ENABLED */

        #else /* UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

            /* We can only know if there is data in the fifo. */
            size = ((UART_1_RXSTATUS_REG & UART_1_RX_STS_FIFO_NOTEMPTY) != 0u) ? 1u : 0u;

        #endif /* End UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

        return(size);
    }

使用特权

评论回复
17
heping517|  楼主 | 2015-6-25 08:41 | 只看该作者
/*******************************************************************************
    * Function Name: UART_1_ClearRxBuffer
    ********************************************************************************
    *
    * Summary:
    *  Clears the RX RAM buffer by setting the read and write pointers both to zero.
    *  Clears hardware RX FIFO.
    *
    * Parameters:
    *  None.
    *
    * Return:
    *  None.
    *
    * Global Variables:
    *  UART_1_rxBufferWrite - cleared to zero.
    *  UART_1_rxBufferRead - cleared to zero.
    *  UART_1_rxBufferLoopDetect - cleared to zero.
    *  UART_1_rxBufferOverflow - cleared to zero.
    *
    * Reentrant:
    *  No.
    *
    * Theory:
    *  Setting the pointers to zero makes the system believe there is no data to
    *  read and writing will resume at address 0 overwriting any data that may
    *  have remained in the RAM.
    *
    * Side Effects:
    *  Any received data not read from the RAM or FIFO buffer will be lost.
    *******************************************************************************/
    void UART_1_ClearRxBuffer(void)
    {
        uint8 enableInterrupts;

        /* clear the HW FIFO */
        /* Enter critical section */
        enableInterrupts = CyEnterCriticalSection();
        UART_1_RXDATA_AUX_CTL_REG |=  UART_1_RX_FIFO_CLR;
        UART_1_RXDATA_AUX_CTL_REG &= (uint8)~UART_1_RX_FIFO_CLR;
        /* Exit critical section */
        CyExitCriticalSection(enableInterrupts);

        #if(UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH)
            /* Disable Rx interrupt. */
            /* Protect variables that could change on interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_DisableRxInt();
            #endif /* End UART_1_RX_INTERRUPT_ENABLED */

            UART_1_rxBufferRead = 0u;
            UART_1_rxBufferWrite = 0u;
            UART_1_rxBufferLoopDetect = 0u;
            UART_1_rxBufferOverflow = 0u;

            /* Enable Rx interrupt. */
            #if(UART_1_RX_INTERRUPT_ENABLED)
                UART_1_EnableRxInt();
            #endif /* End UART_1_RX_INTERRUPT_ENABLED */
        #endif /* End UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH */

    }

使用特权

评论回复
18
heping517|  楼主 | 2015-6-25 08:41 | 只看该作者
/*******************************************************************************
    * Function Name: UART_1_SetRxAddressMode
    ********************************************************************************
    *
    * Summary:
    *  Set the receive addressing mode
    *
    * Parameters:
    *  addressMode: Enumerated value indicating the mode of RX addressing
    *  UART_1__B_UART__AM_SW_BYTE_BYTE -  Software Byte-by-Byte address
    *                                               detection
    *  UART_1__B_UART__AM_SW_DETECT_TO_BUFFER - Software Detect to Buffer
    *                                               address detection
    *  UART_1__B_UART__AM_HW_BYTE_BY_BYTE - Hardware Byte-by-Byte address
    *                                               detection
    *  UART_1__B_UART__AM_HW_DETECT_TO_BUFFER - Hardware Detect to Buffer
    *                                               address detection
    *  UART_1__B_UART__AM_NONE - No address detection
    *
    * Return:
    *  None.
    *
    * Global Variables:
    *  UART_1_rxAddressMode - the parameter stored in this variable for
    *   the farther usage in RX ISR.
    *  UART_1_rxAddressDetected - set to initial state (0).
    *
    *******************************************************************************/
    void UART_1_SetRxAddressMode(uint8 addressMode)
                                                        
    {
        #if(UART_1_RXHW_ADDRESS_ENABLED)
            #if(UART_1_CONTROL_REG_REMOVED)
                if(addressMode != 0u) { }     /* release compiler warning */
            #else /* UART_1_CONTROL_REG_REMOVED */
                uint8 tmpCtrl;
                tmpCtrl = UART_1_CONTROL_REG & (uint8)~UART_1_CTRL_RXADDR_MODE_MASK;
                tmpCtrl |= (uint8)(addressMode << UART_1_CTRL_RXADDR_MODE0_SHIFT);
                UART_1_CONTROL_REG = tmpCtrl;
                #if(UART_1_RX_INTERRUPT_ENABLED && \
                   (UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH) )
                    UART_1_rxAddressMode = addressMode;
                    UART_1_rxAddressDetected = 0u;
                #endif /* End UART_1_RXBUFFERSIZE > UART_1_FIFO_LENGTH*/
            #endif /* End UART_1_CONTROL_REG_REMOVED */
        #else /* UART_1_RXHW_ADDRESS_ENABLED */
            if(addressMode != 0u) { }     /* release compiler warning */
        #endif /* End UART_1_RXHW_ADDRESS_ENABLED */
    }

使用特权

评论回复
19
heping517|  楼主 | 2015-6-25 08:42 | 只看该作者
/*******************************************************************************
    * Function Name: UART_1_SetRxAddress1
    ********************************************************************************
    *
    * Summary:
    *  Set the first hardware address compare value
    *
    * Parameters:
    *  address
    *
    * Return:
    *  None.
    *
    *******************************************************************************/
    void UART_1_SetRxAddress1(uint8 address)

    {
        UART_1_RXADDRESS1_REG = address;
    }

使用特权

评论回复
20
heping517|  楼主 | 2015-6-25 08:42 | 只看该作者
/*******************************************************************************
    * Function Name: UART_1_SetRxAddress2
    ********************************************************************************
    *
    * Summary:
    *  Set the second hardware address compare value
    *
    * Parameters:
    *  address
    *
    * Return:
    *  None.
    *
    *******************************************************************************/
    void UART_1_SetRxAddress2(uint8 address)
    {
        UART_1_RXADDRESS2_REG = address;
    }

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

90

主题

648

帖子

0

粉丝