主程序:
/*******************************************************************************
*File: main.C
*功能: 串口发送数据
*说明: 使用外部晶振,不使用PLL,Fpclk=Fcclk/4
*******************************************************************************/
#include "config.h"
/*******************************************************************************
*名称: DelayNS()
*功能: 长软件延时
*******************************************************************************/
void DelayNS(uint32 dly)
{ uint32 i;
for(;dly>0;dly--)
for(i=0;i<50000;i++);
}
/*******************************************************************************
*名称: UART0_Ini()
*功能: 初始化串口0.设置为8位数据位,1位停止位,无奇偶校验,波特率为9600
*******************************************************************************/
void UART0_Ini(void)
{ U0LCR=0x83; //DLAB=1,可设置波特率
U0DLL=0x12;
U0DLM=0x00;
U0LCR=0x03;
}
/*******************************************************************************
*名称: UART0_SendByte()
*功能: 向串口发送字节数据,并等待发送完毕
*******************************************************************************/
void UART0_SendByte(uint8 data)
{ U0THR=data; //发送数据
while((U0LSR&0x40)==0); //等待数据发送完毕
{
uint32 i;
for(i=0; i<5; i++);
}
}
/*******************************************************************************
*名称:UART0_SendStr()
*功能:向串口发送一字符串
*******************************************************************************/
void UART0_SendStr(uint8 const *str)
{ while(1)
{ if(*str=='\0')
{UART0_SendByte('\r');
UART0_SendByte('\n');
break;
}
UART0_SendByte(*str++); //发送数据
}
}
char UART0_RecvByte(void)
{ while(!(U0LSR&0x01));
return U0RBR;
}
/*******************************************************************************
*名称: main()
*功能: 向串口UART0发送字符串"Hello World!"
*******************************************************************************/
int main(void)
{ uint8 const SEND_STRING[]="HELLO WORLD!\n";
PINSEL0=0x00000005; //设置I/O连接到UART0
PINSEL1=0x00000000;
UART0_Ini();
UART0_SendStr(SEND_STRING);
DelayNS(10);
while(1)
{
UART0_SendByte(UART0_RecvByte());
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』