📄 uart.h
字号:
/******************************************************************************
*
* Copyright:
* (C) 2005 Embedded Artists AB
*
* File:
* uart.h
*
* Description:
* Contains interface definitions for the polled UART implementation
*
*****************************************************************************/
#ifndef _UART_H_
#define _UART_H_
/******************************************************************************
* Includes
*****************************************************************************/
#include "general.h"
#include <lpc2xxx.h>
/******************************************************************************
* Defines and typedefs
*****************************************************************************/
//bit definitions in LCR and FCR registers in the UART
#define ULCR_CHAR_7 0x02
#define ULCR_CHAR_8 0x03
#define ULCR_PAR_NO 0x00
#define ULCR_PAR_EVEN 0x30
#define ULCR_PAR_ODD 0x10
#define ULCR_STOP_1 0x00
#define ULCR_STOP_2 0x04
#define UFCR_FIFO_ENABLE 0x01
#define UFCR_FIFO_TRIG1 0x00
#define UFCR_FIFO_TRIG4 0x40
#define UFCR_FIFO_TRIG8 0x80
#define UFCR_FIFO_TRIG16 0xc0
//define for determning the correct uart clock division factor
//Note that the expressions should always be constants and fully evaluated at
//compile time, else a lot of code will be generated.
#define UART_BPS(pclk,bps) (unsigned short)((pclk / ((bps) * 16.0)) + 0.5)
//definitions for some common bitrates
#define B1200(pclk) UART_BPS(pclk,1200)
#define B2400(pclk) UART_BPS(pclk,2400)
#define B9600(pclk) UART_BPS(pclk,9600)
#define B14400(pclk) UART_BPS(pclk,14400)
#define B19200(pclk) UART_BPS(pclk,19200)
#define B38400(pclk) UART_BPS(pclk,38400)
#define B57600(pclk) UART_BPS(pclk,57600)
#define B115200(pclk) UART_BPS(pclk,115200)
#define B230400(pclk) UART_BPS(pclk,230400)
#define B460800(pclk) UART_BPS(pclk,460800)
#define B921600(pclk) UART_BPS(pclk,921600)
//definitions for mode settings
#define UART_7N1 (unsigned char)(ULCR_CHAR_7 + ULCR_PAR_NO + ULCR_STOP_1)
#define UART_7N2 (unsigned char)(ULCR_CHAR_7 + ULCR_PAR_NO + ULCR_STOP_2)
#define UART_7E1 (unsigned char)(ULCR_CHAR_7 + ULCR_PAR_EVEN + ULCR_STOP_1)
#define UART_7E2 (unsigned char)(ULCR_CHAR_7 + ULCR_PAR_EVEN + ULCR_STOP_2)
#define UART_7O1 (unsigned char)(ULCR_CHAR_7 + ULCR_PAR_ODD + ULCR_STOP_1)
#define UART_7O2 (unsigned char)(ULCR_CHAR_7 + ULCR_PAR_ODD + ULCR_STOP_2)
#define UART_8N1 (unsigned char)(ULCR_CHAR_8 + ULCR_PAR_NO + ULCR_STOP_1)
#define UART_8N2 (unsigned char)(ULCR_CHAR_8 + ULCR_PAR_NO + ULCR_STOP_2)
#define UART_8E1 (unsigned char)(ULCR_CHAR_8 + ULCR_PAR_EVEN + ULCR_STOP_1)
#define UART_8E2 (unsigned char)(ULCR_CHAR_8 + ULCR_PAR_EVEN + ULCR_STOP_2)
#define UART_8O1 (unsigned char)(ULCR_CHAR_8 + ULCR_PAR_ODD + ULCR_STOP_1)
#define UART_8O2 (unsigned char)(ULCR_CHAR_8 + ULCR_PAR_ODD + ULCR_STOP_2)
//definitions for FIFO control settings
#define UART_FIFO_OFF (0x00)
#define UART_FIFO_1 (unsigned char)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG1)
#define UART_FIFO_4 (unsigned char)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG4)
#define UART_FIFO_8 (unsigned char)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG8)
#define UART_FIFO_16 (unsigned char)(UFCR_FIFO_ENABLE + UFCR_FIFO_TRIG16)
/*****************************************************************************
*
* Description:
* Initialize UART #0/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 initUart0(unsigned short div_factor, unsigned char mode, unsigned char fifo_mode);
void initUart1(unsigned short div_factor, unsigned char mode, unsigned char 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 #0)
*
****************************************************************************/
void uart0SendChar(unsigned char charToSend);
void uart1SendChar(unsigned char charToSend);
/*****************************************************************************
*
* Description:
* Output routine that adds extra line feeds at line breaks.
*
* Params:
* [in] charToSend - The character to print (to uart #0)
*
****************************************************************************/
void uart0SendCh(unsigned char charToSend);
void uart1SendCh(unsigned char charToSend);
/*****************************************************************************
*
* Description:
* Print NULL-terminated string to uart #0.
*
* Params:
* [in] pString - Pointer to NULL-terminated string to be printed
*
****************************************************************************/
void uart0SendString(unsigned char *pString);
void uart1SendString(unsigned char *pString);
/*****************************************************************************
*
* Description:
* Print a fixed number of bytes (as opposed to NULL-terminated string).
*
* Params:
* [in] pBuff - The character to print (to uart #0)
* [in] count - Number of characters to print
*
****************************************************************************/
void uart0SendChars(unsigned char *pBuff, unsigned short count);
void uart1SendChars(unsigned char *pBuff, unsigned short count);
/*****************************************************************************
*
* 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 uart0TxEmpty(void);
unsigned char uart1TxEmpty(void);
/*****************************************************************************
*
* Description:
* Removes all characters in the UART transmit queue.
*
****************************************************************************/
void uart0TxFlush(void);
void uart1TxFlush(void);
/*****************************************************************************
*
* Description:
* Blocking function that waits for a received character.
*
* Return:
* The received character.
*
****************************************************************************/
unsigned char uart0GetCh(void);
unsigned char uart1GetCh(void);
/*****************************************************************************
*
* 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 uart0GetChar(unsigned char *pRxChar);
unsigned char uart1GetChar(unsigned char *pRxChar);
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -