📄 uart.c
字号:
#include "uart.h"
#include "command_ID.h"
extern unsigned char tx_buffer[8];
unsigned char rx_data;
unsigned char command_ID=ID_ADC;
unsigned int KP_coeff=KP_COEFF;
unsigned int KI_coeff=KI_COEFF;
unsigned int KD_coeff=KD_COEFF;
signed long max_pwm_position=MAX_PWM_POSITION;
//unsigned char rx_index=0;
/***************************************************************************
* Function Name : putsUART1 *
* Description : This function puts the data string to be transmitted *
* into the transmit buffer (till NULL character) *
* Parameters : unsigned int * address of the string buffer to be *
* transmitted *
* Return Value : None *
***************************************************************************/
void putsUART1(unsigned int *buffer)
{
char * temp_ptr = (char *) buffer;
/* transmit till NULL character is encountered */
if(U1MODEbits.PDSEL == 3) /* check if TX is 8bits or 9bits */
{
while(*buffer != '\0')
{
while(U1STAbits.UTXBF); /* wait if the buffer is full */
U1TXREG = *buffer++; /* transfer data word to TX reg */
}
}
else
{
while(*temp_ptr != '\0')
{
while(U1STAbits.UTXBF); /* wait if the buffer is full */
U1TXREG = *temp_ptr++; /* transfer data byte to TX reg */
}
}
}
/*********************************************************************
* Function Name : WriteUART1 *
* Description : This function writes data into the UxTXREG, *
* Parameters : unsigned int data the data to be written *
* Return Value : None *
*********************************************************************/
void WriteUART1(unsigned int data)
{
if(U1MODEbits.PDSEL == 3)
U1TXREG = data;
else
U1TXREG = data & 0xFF;
}
/*********************************************************************
* Function Name : CloseUART1 *
* Description : This function disables the UART and clears the *
* Interrupt enable & flag bits *
* Parameters : None *
* Return Value : None *
*********************************************************************/
void CloseUART1(void)
{
U1MODEbits.UARTEN = 0;
IEC0bits.U1RXIE = 0;
IEC0bits.U1TXIE = 0;
IFS0bits.U1RXIF = 0;
IFS0bits.U1TXIF = 0;
}
/**********************************************************************
* Function Name : ConfigIntUART1 *
* Description : This function sets priority for RX,TX interrupt *
* and enable/disables the interrupt *
* Parameters : unsigned int config enable/disable and priority *
* Return Value : None *
**********************************************************************/
void ConfigIntUART1(unsigned int config)
{
/* clear IF flags */
_U1RXIF = 0;
_U1TXIF = 0;
/* set priority */
_U1RXIP = 0x0007 & config;
_U1TXIP = (0x0070 & config) >> 4;
/* enable/disable interrupt */
_U1RXIE = (0x0008 & config) >> 3;
_U1TXIE = (0x0080 & config) >> 7;
}
/*********************************************************************
* Function Name : DataRdyUart1 *
* Description : This function checks whether there is any data *
* that can be read from the input buffer, by *
* checking URXDA bit *
* Parameters : None *
* Return Value : char if any data available in buffer *
*********************************************************************/
char DataRdyUART1(void)
{
return(U1STAbits.URXDA);
}
/******************************************************************************
* Function Name : getsUART1 *
* Description : This function gets a string of data of specified length *
* if available in the UxRXREG buffer into the buffer *
* specified. *
* Parameters : unsigned int length the length expected *
* unsigned int *buffer the received data to be *
* recorded to this array *
* unsigned int uart_data_wait timeout value *
* Return Value : unsigned int number of data bytes yet to be received *
******************************************************************************/
unsigned int getsUART1(unsigned int length,unsigned int *buffer,
unsigned int uart_data_wait)
{
int wait = 0;
char *temp_ptr = (char *) buffer;
while(length) /* read till length is 0 */
{
while(!DataRdyUART1())
{
if(wait < uart_data_wait)
wait++ ; /*wait for more data */
else
return(length); /*Time out- Return words/bytes to be read */
}
wait=0;
if(U1MODEbits.PDSEL == 3) /* check if TX/RX is 8bits or 9bits */
*buffer++ = U1RXREG; /* data word from HW buffer to SW buffer */
else
*temp_ptr++ = U1RXREG & 0xFF; /* data byte from HW buffer to SW buffer */
length--;
}
return(length); /* number of data yet to be received i.e.,0 */
}
/*********************************************************************
* Function Name : OpenUART1 *
* Description : This function configures the UART mode, *
* UART Interrupt modes and the Baud Rate *
* Parameters : unsigned int config1 operation setting *
* unsigned int config2 TX & RX interrupt modes *
* unsigned int ubrg baud rate setting *
* Return Value : None *
*********************************************************************/
void OpenUART1(unsigned int config1,unsigned int config2, unsigned int ubrg)
{
U1BRG = ubrg; /* baud rate */
U1MODE = config1; /* operation settings */
U1STA = config2; /* TX & RX interrupt modes */
}
/***************************************************************************
* Function Name : ReadUART1 *
* Description : This function returns the contents of UxRXREG buffer *
* Parameters : None *
* Return Value : unsigned int value from UxRXREG receive buffer *
***************************************************************************/
unsigned int ReadUART1(void)
{
if(U1MODEbits.PDSEL == 3)
return (U1RXREG);
else
return (U1RXREG & 0xFF);
}
/*************************************************************************
* Function Name : BusyUART1 *
* Description : This returns status whether the transmission *
* is in progress or not, by checking Status bit TRMT *
* Parameters : None *
* Return Value : Info about whether transmission is in progress. *
*************************************************************************/
char BusyUART1(void)
{
return(!U1STAbits.TRMT);
}
/*************************************************************************
*************************************************************************/
void UART1_init(void)
{
/* Holds the value of baud register */
unsigned int baudvalue;
/* Holds the value of uart config reg */
unsigned int U1MODEvalue;
/* Holds the information regarding uart
TX & RX interrupt modes */
unsigned int U1STAvalue;
/* Turn off UART1module */
CloseUART1();
/* Configure uart1 receive and transmit interrupt */
ConfigIntUART1(UART_RX_INT_EN & UART_RX_INT_PR6 &
UART_TX_INT_DIS & UART_TX_INT_PR6);
/* Configure UART1 module to transmit 8 bit data with one stopbit.
Also Enable loopback mode */
baudvalue = 383;//baudrate
U1MODEvalue = UART_EN & UART_IDLE_STOP & UART_ALTRX_ALTTX &
UART_DIS_WAKE & UART_DIS_LOOPBACK &
UART_DIS_ABAUD & UART_NO_PAR_8BIT &
UART_1STOPBIT;
U1STAvalue = UART_INT_TX &
UART_TX_PIN_NORMAL &
UART_TX_ENABLE & UART_INT_RX_CHAR &
UART_ADR_DETECT_DIS &
UART_RX_OVERRUN_CLEAR;
OpenUART1(U1MODEvalue, U1STAvalue, baudvalue);
}
/* This is UART1 transmit ISR */
void __attribute__((__interrupt__, __auto_psv__)) _U1TXInterrupt(void)
{
IFS0bits.U1TXIF = 0;
}
/* This is UART1 receive ISR */
void __attribute__((__interrupt__, __auto_psv__)) _U1RXInterrupt(void)
{
unsigned char rx_data;
static unsigned char rx_index=0;
rx_data = U1RXREG & 0xFF;
switch (rx_index)
{
case 0:
if (rx_data==START_BYTE) rx_index=1;
else rx_index=0;
break;
case 1:
if (rx_data==START_BYTE) rx_index=2;
else rx_index=0;
break;
case 2:
command_ID=rx_data;
tx_buffer[0]=' ';
tx_buffer[1]=' ';
tx_buffer[2]=' ';
tx_buffer[3]=' ';
tx_buffer[4]=' ';
rx_index=3;
break;
case 3:
switch (command_ID)
{
case ID_KP:
KP_coeff=rx_data*256;
break;
case ID_KI:
KI_coeff=rx_data*256;
break;
case ID_KD:
KD_coeff=rx_data*256;
break;
case ID_MAX_PWM_POSITION:
max_pwm_position=rx_data*256;
break;
};
rx_index=4;
break;
case 4:
switch (command_ID)
{
case ID_KP:
KP_coeff+=rx_data;
break;
case ID_KI:
KI_coeff+=rx_data;
break;
case ID_KD:
KD_coeff+=rx_data;
break;
case ID_MAX_PWM_POSITION:
max_pwm_position+=rx_data;
break;
};
rx_index=0;
break;
default:
rx_index=0;
break;
};
IFS0bits.U1RXIF = 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -