该函数展示了如何通过串口展示相关信息。
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdint.h>
#include "M051.h"
#include "Register_Bit.h"
#include "Uart.h"
/*en:************************************************************************************
Description: Initialize UART.
****************************************************************************************/
/*中:************************************************************************************
描述: 初始化UART
****************************************************************************************/
void UART_Init(void)
{
P3_MFP = P3_MFP & (~(P31_TXD0 | P30_RXD0)) | (TXD0 | RXD0); //配置P3.1和P3.0为UART功能//en:Set P3.1 and P3.0 to UART0 function
IPRSTC2 |= UART0_RST; //复位UART模块//en:Reset UART0 module
IPRSTC2 &= ~UART0_RST; //UART模块从复位状态恢复到正常工作状态//en:Resume UART0 to normal mode
APBCLK |= UART0_CLKEN; //使能UART时钟//en:Enable UART0 clock
CLKSEL1 = CLKSEL1 & (~UART_CLK) | UART_12M; //选择外部12M作为UART时钟源//en:Select external 12M as UART0 clock source
CLKDIV &= ~(15<<8); //设置UART时钟除频值为0//en:UART clock no division
UA0_FCR |= TX_RST; //复位发送FIFO//en:Reset transmit FIFO
UA0_FCR |= RX_RST; //复位接收FIFO//en:Reset receive FIFO
UA0_LCR &= ~PBE; //校验位禁止//en:None parity
UA0_LCR = (UA0_LCR & (~WLS)) | WL_8BIT; //数据宽度为8位//en:Data width 8 bits
UA0_LCR &= NSB_ONE; //1位停止位//en:1 bit stop
UA0_BAUD |= DIV_X_EN; //模式2:DIV_X_EN = 1//en:Mode2:DIV_X_EN = 1
UA0_BAUD |= DIV_X_ONE; //模式2:DIV_X_ONE =1//en:Mode2:DIV_X_ONE = 1
UA0_BAUD |= ((12000000 / 115200) - 2); //设置波特率为115200,模式2波特率=UART_CLK/(UA_BAUD+2)//en:Set BaudRate to 115200,BaudRate==UART_CLK/(UA_BAUD+2) in mode 2
}
/*en:************************************************************************************
Parameter: c: char to be sent.
Description: Send a char to PC.
****************************************************************************************/
/*中:************************************************************************************
参数 : c: 要发送到字符
描述: 发送一个字符到PC
****************************************************************************************/
void Send_Data_To_PC (uint8_t c)
{
while((UA0_FSR&TX_FULL) != 0); //发送FIFO满时等待//en:Wait until UART transmit FIFO is not full
UA0_THR = (uint8_t) c; //通过UART0发送一个字符//en:Transmit a char via UART0
}
/*en:************************************************************************************
Return: A char.
Description: Get a char from PC.
****************************************************************************************/
/*中:************************************************************************************
返回值: 一个字符
描述: 从PC获取一个字符
****************************************************************************************/
uint8_t Receive_Data_From_PC(void)
{
while((UA0_FSR&RX_EMPTY) != 0); //等字符//en:Wait until an avaliable char present in RX FIFO
return ((uint8_t)UA0_RBR); //返回接收到的字符//en:Return received char
}
void Show_Pass(void)
{
printf ("\n");
printf ("\n");
printf (" * * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * \n");
printf (" * * * * * \n");
printf ("\n");
printf ("\n");
}
void Show_Fail(void)
{
printf ("\n");
printf ("\n");
printf (" * * \n");
printf (" * * \n");
printf (" * * \n");
printf (" * * \n");
printf (" * * \n");
printf (" * \n");
printf (" * * \n");
printf (" * * \n");
printf (" * * \n");
printf (" * * \n");
printf (" * * \n");
printf ("\n");
printf ("\n");
}
|