c_cpp UART PIC

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp UART PIC相关的知识,希望对你有一定的参考价值。

/*!******************************************************************
 * \fn bool IsNewData(ID_UART ID_U)                                 
 * \brief This function return true if there is one or more bytes  
 * in the receive buffer "Private function"                        
 * @param [in] ID_U : ID of the UART used                          
 * @param [out] retrun_value                                        
 *********************************************************************/
bool IsNewData(ID_UART ID_U)
{
    bool return_value = false;
    asm volatile ("disi #0x0002");    ///\b Disable interrupts while comparing the pointers
    
    ///\b Check if New Data 
    switch(ID_U) 
    {
        case   U_BASE_MODULE_RX : if(rx_BASE_MODULE_BufRdPtr != rx_BASE_MODULE_BufWrPtr){return_value = true;} 
        break; 
        case   U_BASE_MODULE_TX : if(tx_BASE_MODULE_BufRdPtr != tx_BASE_MODULE_BufWrPtr){return_value = true;} 
        break;
        case   U_BLUETOOTH_RX : if(rx_BLUETOOTH_BufRdPtr != rx_BLUETOOTH_BufWrPtr){return_value = true;} 
        break; 
        case   U_BLUETOOTH_TX : if(tx_BLUETOOTH_BufRdPtr != tx_BLUETOOTH_BufWrPtr){return_value = true;} 
        break;
        default : return_value = false; /*Robsutness*/
    }                     
   return(return_value); 
   
   /**\b END IsNewData*/
}

/*!******************************************************************
 * \fn bool IsEndBuffer(ID_UART ID_U)                              
 * \brief This function return true if the receive buffer is the   
 *  End of Buffer "Private function"                               
 * @param [in] ID_U : ID of the UART used                          
 * @param [out] retrun_value                                        
 *******************************************************************/
bool LoopBuffer(ID_UART ID_U)
{
    bool return_value = false;
    asm volatile ("disi #0x0002");    ///\b Disable interrupts while comparing the pointers
    
    ///\b Check if at the end of buffer
    switch(ID_U) {
        case   U_BASE_MODULE_RX :if (rx_BASE_MODULE_BufWrPtr > (&rx_BASE_MODULE_Buffer[SIZE_Rx_BASE_MODULE_Buffer - 1]))
                                 { 
                                    rx_BASE_MODULE_BufWrPtr = &rx_BASE_MODULE_Buffer[0]; 
                                    return_value = true;
                                 }
                            break;
        case   U_BASE_MODULE_TX :if (tx_BASE_MODULE_BufWrPtr > (&tx_BASE_MODULE_Buffer[SIZE_Tx_BASE_MODULE_Buffer - 1]))
                                 { 
                                    tx_BASE_MODULE_BufWrPtr =  &tx_BASE_MODULE_Buffer[0];     
                                    return_value = true;
                                 }
                            break;
        case   U_BLUETOOTH_RX : if (rx_BLUETOOTH_BufWrPtr > (&rx_BLUETOOTH_Buffer[SIZE_Rx_BLUETOOTH_Buffer - 1]))
                                {   
                                    rx_BLUETOOTH_BufWrPtr = &rx_BLUETOOTH_Buffer[0];        
                                    return_value = true;
                                }
                                
                            break;
        case   U_BLUETOOTH_TX :    if (tx_BLUETOOTH_BufWrPtr > (&tx_BLUETOOTH_Buffer[SIZE_Tx_BLUETOOTH_Buffer - 1]))
                                { 
                                    tx_BLUETOOTH_BufWrPtr = &tx_BLUETOOTH_Buffer[0];
                                    return_value = true;
                                }
                            break;       
        default : return_value = false; /*Robsutness*/
    }                     
   return(return_value); 
   
   /**\b END IsEndBuffer*/
}

bool Read_UART(ID_UART ID_U, unsigned char *string, unsigned int length)
{
    unsigned int i = 0;
    bool IsNew = false;
    bool return_value = false;
            
    IsNew = IsNewData(ID_U);
    
    clrString((char*)string,length);
    while(IsNew && (i <= length - 1))     //@brief If is new DATA and the length of buffer is accepted  
    {
        IsNew = IsNewData(ID_U);
        GetByteBuffer(ID_U,string);                 //@brief Get the byte 
        LoopBuffer(ID_U);                          //@brief Wrap the pointer to beginning IF end buffer
        *string++;                                  //@brief Increment the pointer
        i++;
        return_value = true;
    }
    string = string - i;                            //@brief Submit *string pointer at the beginning 
    return return_value;
    /**\b END Read_UART*/
}

/*!**********************************************************************************
 * \fn bool GetByteBuffer(ID_UART ID_U, unsigned char *string)   
 * \brief This function provides to get the byte                                                      
 * @param [in] ID_U : ID of the UART used                                          
 * @param [inout] string : Byte read
 * @param [out] retrun_value                                             
 *                                    
 ***********************************************************************************/
bool GetByteBuffer(ID_UART ID_U, unsigned char *byte)
{
    bool return_value = false;
    
    switch(ID_U) {
        case   U_BASE_MODULE_RX    :
            if (rx_BASE_MODULE_BufRdPtr > (&rx_BASE_MODULE_Buffer[SIZE_Rx_BASE_MODULE_Buffer - 1]))
            { 
                rx_BASE_MODULE_BufRdPtr = &rx_BASE_MODULE_Buffer[0];        
            }
            *byte = *rx_BASE_MODULE_BufRdPtr++;      ///"case : U_BASE_MODULE_RX"\b Get the byte                 
            return_value = true;
            break;
        case   U_BLUETOOTH_RX :
            if (rx_BLUETOOTH_BufRdPtr > (&rx_BLUETOOTH_Buffer[SIZE_Rx_BLUETOOTH_Buffer - 1]))
            { 
                rx_BLUETOOTH_BufRdPtr = &rx_BLUETOOTH_Buffer[0];        
            }
            *byte = *rx_BLUETOOTH_BufRdPtr++;
            return_value = true;
            break;
        default : return_value = false; /*Robsutness*/
    }    
    return return_value;
    
    /**\b END GetByteBuffer*/
}

/*!*****************************************************************
 * \fn void _ISR_NO_AUTO_PSV _U2RXInterrupt(void)                 
 * \brief Interrupt method to receive data (UART_ORPHEO)            
 * "Interrupt function"                                           
 *******************************************************************/
void _ISR_NO_AUTO_PSV _U2RXInterrupt(void)
{
    LoopBuffer(U_BASE_MODULE_RX);
    *rx_BASE_MODULE_BufWrPtr++ = U2RXREG;    //Put received character in the buffer    
    IFS1bits.U2RXIF = 0;  //Clear UART 2 Receive interrupt flag
    /**\b END _U2RXInterrupt*/
}
/*!***********************************************************************
 * \fn void BLUETOOTH_PutString(char *string)                                  
 * \brief This method provides to send a string to the UART BLUETOOTH module 
 * "Public function"                                                    
 *        - Format : "Your string"                               
 *          - NB : SIZE_MAX_BUFFER characters maximum                               
 * @param [in] *string : String to send                                 
 *************************************************************************/
void BLUETOOTH_PutString(char *string)
{
    unsigned int i = 0;
    
    while((i < SIZE_MAX_BUFFER) && (*string != '\0'))   ///\b Keep loading bytes until end of string
    {
        WriteTxBluetoothBuffer(*string++);              ///\b Load byte into the transmit buffer
        i++;
    }
    UART_BLUETOOTH_TxStart();                           ///\b Start transmitting the bytes                 
    /**\b END BLUETOOTH_PutString*/
}

/*!******************************************************************
 * \fn void WriteTxBluetoothBuffer(unsigned char TxByte)                
 * \brief Write a byte to the transmit 2 buffer "Private function" 
 * @param [in] TxByte : Byte to transmit                           
 *******************************************************************/
void WriteTxBluetoothBuffer(unsigned char TxByte)
{
    LoopBuffer(U_BLUETOOTH_TX);
    *tx_BLUETOOTH_BufWrPtr++ = TxByte;    
    
    /**\b END WriteTxBluetoothBuffer*/
}

/*!*****************************************************************
 * \fn void UART_BLUETOOTH_TxStart(void)                               
 * \brief This method provides to start transmitting the bytes to 
 *  the UART BLUETOOTH module "Private function"                       
 ********************************************************************/
void UART_BLUETOOTH_TxStart(void)
{
    IEC0bits.U1TXIE = 1; ///\b Enable transmit interrupts
    /**\b END UART_BLUETOOTH_TxStart*/
}

/*!*****************************************************************
 * \fn void _ISR_NO_AUTO_PSV _U2TXInterrupt(void)                 
 * \brief Interrupt method to transmit data (UART_ORPHEO)           
 *  "Interrupt function"                                          
 *******************************************************************/
void _ISR_NO_AUTO_PSV _U2TXInterrupt(void)
{
    IEC1bits.U2TXIE = 0;
    if (IsNewData(U_BASE_MODULE_TX))
    {
        IFS1bits.U2TXIF = 0;             //Clear UART 2 Transmit interrupt flag
        LoopBuffer(U_BASE_MODULE_TX);
        U2TXREG = *tx_BASE_MODULE_BufRdPtr++;  //Get next byte from the buffer
        if (tx_BASE_MODULE_BufRdPtr > (&tx_BASE_MODULE_Buffer[SIZE_Tx_BASE_MODULE_Buffer - 1]))
        { 
            tx_BASE_MODULE_BufRdPtr = &tx_BASE_MODULE_Buffer[0];        
        }
        IEC1bits.U2TXIE = 1;
    }  
    /**\b END _U2TXInterrupt*/
}
/*! 
 * @brief UART_BLUETOOTH : Using the UART1  
 * - Buffer and pointers for received bytes
 */
static volatile unsigned char rx_BLUETOOTH_Buffer[SIZE_Rx_BLUETOOTH_Buffer], *rx_BLUETOOTH_BufRdPtr, *rx_BLUETOOTH_BufWrPtr;   
/*!
 *  - Buffer and pointers for bytes to transmit
 */
static volatile unsigned char tx_BLUETOOTH_Buffer[SIZE_Tx_BLUETOOTH_Buffer], *tx_BLUETOOTH_BufRdPtr, *tx_BLUETOOTH_BufWrPtr;  


void UART_BASE_MODULE_INITIALIZE(unsigned int BRGH)
{
    // Initialize the pointers Rx_BASE_MODULE and TX_BASE_MODULE
    rx_BASE_MODULE_BufRdPtr = rx_BASE_MODULE_BufWrPtr = &rx_BASE_MODULE_Buffer[0]; 
    tx_BASE_MODULE_BufRdPtr = tx_BASE_MODULE_BufWrPtr = &tx_BASE_MODULE_Buffer[0]; 
    
#if DEBUG_MODE 
    // Initialize the pointers Rx_DEBUG and TX_DEBUG
   rx_DEBUG_BufRdPtr = rx_DEBUG_BufWrPtr = &rx_DEBUG_Buffer[0]; 
   tx_DEBUG_BufRdPtr = tx_DEBUG_BufWrPtr = &tx_DEBUG_Buffer[0];    
#endif
    /* UART Baud Rate Generator
     * 
     * U1BRG register controls the period of a free-running, 16-bit timer
     * Baud rate = 19200 - actually 19200 baud 0.16% error
     */
    U2BRG = BRGH;
    //U2MODE: UART2 MODE REGISTER
                 
    ///Enable UART2, no flow control, BRGH = 0 for high speed baud mode
    if(BRGH == BRGH_19200)
        U2MODE = 0x8000;
    else 
        U2MODE = 0x8008;                        
    
    //U2STA: UART2 STATUS AND CONTROL REGISTER
    U2STA = 0x0400;        //U2STA bit"9" : Transmit Buffer Full Status bit (read-only)
                           // 0 = Transmit buffer is not full; at least one more character can be written
    
    //UART1 Receiver Interrupt Priority bits
    IPC2bits.U1RXIP = 0x07;
    
    //UART1 Transmitter Interrupt Priority bits
    IPC3bits.U1TXIP = 0x05;
    
    //IFS1: INTERRUPT FLAG STATUS REGISTER 1
    /*U2RXIF: UART2 Receiver Interrupt Flag Status bit
     * 0 = Interrupt request has not occurred*/
    
    IFS1bits.U2RXIF = 0;            ///Clear UART 2 Receive interrupt flag
    
    //IEC1: INTERRUPT ENABLE CONTROL REGISTER 1
    /*U2RXIE: UART2 Receiver Interrupt Enable bit
     * 1 = Interrupt request enabled*/
    IEC1bits.U2RXIE = 1;            ///Enable UART 2 Receive interrupt
    
    /**\b END UART_BASE_MODULE_INITIALIZE */
}

以上是关于c_cpp UART PIC的主要内容,如果未能解决你的问题,请参考以下文章

PIC16F690上的Uart和蓝牙问题

具有高波特率循环缓冲器的 PIC 中断驱动 UART

PIC18f46k42 UART没有收到

c_cpp PIC设备的类型定义

c_cpp 修复PIC上的函数地址

c_cpp PIC16器件上的闪存