void hw_uart_init()
{
unsigned short uartclk_khz = 23961;
unsigned short baud = 19200;
unsigned short sbr,brfa;
SIM_SCGC4 |= SIM_SCGC4_UART2_MASK; /* Enable the clock to the selected UART */
SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK; /* Turn on all port clocks */
/* Enable the UART0_TXD function on PTE0 */
PORTE_PCR16 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
/* Enable the UART0_RXD function on PTE1 */
PORTE_PCR17 = PORT_PCR_MUX(0x3); // UART is alt3 function for this pin
sbr = (unsigned short)((uartclk_khz *1000)/(baud * 16));
UART2_BDH = (unsigned char)((sbr & 0x1F00) >> 8);
UART2_BDL = (unsigned char)(sbr & 0x00FF);
/* Determine if a fractional divider is needed to get closer to the baud rate */
brfa = (((uartclk_khz*32000)/(baud * 16)) - (sbr * 32));
UART2_C4 = (unsigned char)(brfa & 0x001F);
UART2_C1 = 0; /* Configure the UART for 8-bit mode, no parity */
UART2_C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK ); /* Enable receiver and transmitter */
//=========================================================================
//函数名称:hw_uart_re1
//参数说明:uartNo: 串口号
// fp:接收成功标志的指针:*p=0,成功接收;*p=1,接收失败
//函数返回:接收返回字节
//功能概要:串行接收1个字节
//=========================================================================
unsigned char uart_getchar (void)
{
/* Wait until character has been received */
while (!(UART2_S1 & UART_S1_RDRF_MASK));
/* Return the 8-bit data from the receiver */
return UART2_D;
}
void uart_putchar (unsigned char data)
{
/* Wait until space is available in the FIFO */
while(!(UART2_S1 & UART_S1_TDRE_MASK));
/* Send the character */
UART2_D = data;
}