uart1.c
来自「一个机器人开发的相关嵌入式开发源码」· C语言 代码 · 共 206 行
C
206 行
/******************************************************************************
*
* Copyright:
* (C) 2005 Embedded Artists AB
*
* File:
* uart1.c
*
* Description:
* Implementation of polled mode for UART #1.
*
*****************************************************************************/
/******************************************************************************
* Includes
*****************************************************************************/
#include "general.h"
#include <lpc2xxx.h>
#include "uart.h"
/*****************************************************************************
* Implementation of public functions
****************************************************************************/
/*****************************************************************************
*
* Description:
* Initialize UART #1 in polled mode, i.e., interrupts are not used.
*
* Parameters:
* [in] div_factor - UART clock division factor to get desired bit rate.
* Use definitions in uart.h to calculate correct value.
* [in] mode - transmission format settings. Use constants in uart.h
* [in] fifo_mode - FIFO control settings. Use constants in uart.h
*
****************************************************************************/
void
initUart1(unsigned short div_factor, unsigned char mode, unsigned char fifo_mode)
{
volatile unsigned int dummy;
//enable uart #1 pins in GPIO (P0.0 = TxD0, P0.1 = RxD0)
PINSEL0 = (PINSEL0 & 0xfff0ffff) | 0x00050000;
U1IER = 0x00; //disable all uart interrupts
dummy = U1IIR; //clear all pending interrupts
dummy = U1RBR; //clear receive register
dummy = U1LSR; //clear line status register
//set the bit rate = set uart clock (pclk) divisionfactor
U1LCR = 0x80; //enable divisor latches (DLAB bit set, bit 7)
U1DLL = (unsigned char)div_factor; //write division factor LSB
U1DLM = (unsigned char)(div_factor >> 8); //write division factor MSB
//set transmissiion and fifo mode
U1LCR = (mode & ~0x80); //DLAB bit (bit 7) must be reset
U1FCR = fifo_mode;
}
/*****************************************************************************
*
* Description:
* Blocking output routine, i.e., the routine waits until the uart
* buffer is free and then sends the character.
*
* Params:
* [in] charToSend - The character to print (to uart #1)
*
****************************************************************************/
void
uart1SendChar(unsigned char charToSend)
{
//Wait until THR (Transmit Holding Register) is empty = THRE bit gets set
while(!(U1LSR & 0x20))
;
U1THR = charToSend;
}
/*****************************************************************************
*
* Description:
* Output routine that adds extra line feeds at line breaks.
*
* Params:
* [in] charToSend - The character to print (to uart #1)
*
****************************************************************************/
void
uart1SendCh(unsigned char charToSend)
{
if(charToSend == '\n')
uart1SendChar('\r');
uart1SendChar(charToSend);
}
/*****************************************************************************
*
* Description:
* Print NULL-terminated string to uart #1.
*
* Params:
* [in] pString - Pointer to NULL-terminated string to be printed
*
****************************************************************************/
void
uart1SendString(unsigned char *pString)
{
while(*pString)
uart1SendCh(*pString++);
}
/*****************************************************************************
*
* Description:
* Print a fixed number of bytes (as opposed to NULL-terminated string).
*
* Params:
* [in] pBuff - The character to print (to uart #1)
* [in] count - Number of characters to print
*
****************************************************************************/
void
uart1SendChars(unsigned char *pBuff, unsigned short count)
{
while (count--)
uart1SendChar(*pBuff++);
}
/*****************************************************************************
*
* Description:
* Returns the status of the UART transmit function.
*
* Returns:
* TRUE if both tx holding and tx shift register are empty, else FALSE.
*
****************************************************************************/
unsigned char
uart1TxEmpty(void)
{
//return TRUE if both THRE and TEMT bits are set
//THRE = Transmitter Holding Register Empty (0x20)
//TEMT = Transmitter Empty (0x40)
return ((U1LSR & 0x60) == 0x60);
}
/*****************************************************************************
*
* Description:
* Removes all characters in the UART transmit queue.
*
****************************************************************************/
void
uart1TxFlush(void)
{
//clear/reset the tx fifo
U1FCR |= 0x04;
}
/*****************************************************************************
*
* Description:
* Blocking function that waits for a received character.
*
* Return:
* The received character.
*
****************************************************************************/
unsigned char
uart1GetCh(void)
{
//wait for Received Data Ready bit (0x01) to be set
while(!(U1LSR & 0x01))
;
return U1RBR;
}
/*****************************************************************************
*
* Description:
* Non-blocking receive function.
*
* Params:
* [in] pRxChar - Pointer to buffer where the received character shall
* be placed.
*
* Return:
* TRUE if character was received, else FALSE.
*
****************************************************************************/
unsigned char
uart1GetChar(unsigned char *pRxChar)
{
//check if Received Data Ready bit (0x01) to be set
if((U1LSR & 0x01) != 0x00)
{
*pRxChar = U1RBR;
return TRUE;
}
return FALSE;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?