📄 main.c
字号:
/****************************************Copyright (c)**************************************************
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#include "config.h"
#define UART_BPS 19200 // 定义通讯波特率
uint8 const SEND_STRING[] = "Hello World123321!\r\n";
uint8 RxBuff[100];
uint8 RxBu=0;
uint8 RxLth = 0;
uint8 RxFlag = 0;
uint32 i=0;
/****************************************************************************
* 名 称:DelayNS()
* 功 能:长软件延时
* 入口参数:dly 延时参数,值越大,延时越久
* 出口参数:无
****************************************************************************/
void DelayNS(uint32 dly)
{
for(; dly>0; dly--)
{
for(i=0; i<5000; i++);
}
}
/****************************************************************************
* 名 称:UART1_Ini()
* 功 能:初始化串口1。设置为8位数据位,1位停止位,无奇偶校验,波特率为19200
* 入口参数:无
* 出口参数:无
****************************************************************************/
void UART1_Init(void)
{
uint16 Fdiv;
PINSEL0 = 0x55550005; //p0.8~15设置I/O连接到UART1
U1LCR = 0x83; // DLAB = 1,可设置波特率
Fdiv = (Fpclk / 16) / UART_BPS; // 设置波特率
U1DLM = Fdiv / 256;
U1DLL = Fdiv % 256;
U1LCR = 0x03; // 禁止访问分频因子寄存器// 且设置为8,1,n //
U0IIR |=0x80;
U0FCR = 0x87; // 使能FIFO,并设置触发点为8字节
U1IER = 0x01; // 允许RBR中断,即接收中断
}
/****************************************************************************
* 名 称:UART0_SendByte()
* 功 能:向串口发送字节数据,并等待发送完毕。
* 入口参数:data 要发送的数据
* 出口参数:无
****************************************************************************/
void UART1_SendByte(uint8 data)
{ U1LCR = 0x03;
U1THR = data; // 发送数据
while( (U1LSR&0x40)==0 ); // 等待数据发送完毕
}
/****************************************************************************
* 名 称:UART1_SendStr()
* 功 能:向串口发送一字符串
* 入口参数:srt 要发送的字符串的指针
* 出口参数:无
****************************************************************************/
void UART1_SendStr(uint8 const *str)
{
while(1)
{
if( *str == '\0' ) break;
UART1_SendByte(*str++); // 发送数据
}
}
/****************************************************************************
* 名 称:UART1_SendStr()
* 功 能:向串口发送一字符串
* 入口参数:srt 要发送的字符串的指针
* 出口参数:无
****************************************************************************/
void __irq UART1_revStr()
//void UART1_revStr()
{
uint8 temp;
uint8 iir;
iir = U1IIR;///清中断
switch(iir & 0x0f)
{
case 0x06://线状态中断
break;
case 0x02://发送中断
break;
case 0x04://接收中断
case 0x0c://字符超时中断
while((U1LSR&0x01)==1)
{
RxBu = U1RBR;
RxBuff[i]=RxBu;
RxFlag = 1;
i++;
if(i>18)
break;
}
break;
default:
temp = U1LSR;
temp = U1RBR;
break;
}
VICVectAddr = 0x00; // 中断处理结束
}
/****************************************************************************
* 名 称:main()
* 功 能:向串口UART0发送字符串"Hello World!"
****************************************************************************/
int main(void)
{
uint8 data=0;
PINSEL2 = 0x00000034;
IO3DIR = 0x000001ff;
IO3CLR = 0x000001ff;
IO3SET = 0x000001ff;
UART1_Init();
/* */
VICIntSelect=0x00000000;
VICDefVectAddr=(uint32)UART1_revStr;
VICIntEnable=7<<5;
UART1_SendStr(SEND_STRING);
i=0;
while(1)
{
//UART1_revStr();
if(RxFlag==1)
{
data=RxBu;
IO3CLR = 0xf00001ff;
IO3SET = data;//;<<28
RxFlag=0;
UART1_SendStr(&RxBu);
}
//UART1_SendStr(SEND_STRING);
}
return(0);
}
/*********************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -