📄 main_uart1ok.c
字号:
#define IN_MAIN
#include "config.h"
#pragma import(__use_no_semihosting_swi) //don't delete this line
int ACK; //I2C应答标志位
/*********************************************************************************************************
** Function name: IRQ_Exception
**
** Descriptions: interrupt exceptional handler , change it as needed
** don't delete this function
********************************************************************************************************/
void IRQ_Exception(void)
{
}
void Del(uint32 dly)
{ uint32 i;
for(;dly>0;dly--)
for(i=0;i<10;i++);
}
/* 定义串口模式设置数据结构 */
typedef struct UartMode
{
uint8 datab; // 字长度,5/6/7/8可选
uint8 stopb; // 停止位,1/2可选
uint8 parity; // 奇偶校验位,0-无校验,1-奇校验,2-偶校验
}UARTMODE;
uint8 rcv_buf0; // UART0数据接收缓冲区
uint8 rcv_buf1;
volatile uint8 rcv_new0; // UART0接收新数据标志
volatile uint8 rcv_new1; // UART1接受新数据标志
//extern uint8 Wt_cat(uint8,uint8,uint8);
//extern uint8 Rd_cat(uint8,uint8,uint8 *);
/*******************************************************************************************************
** 函数名称 :IRQ_UART0()
** 函数功能 :串口0接收中断服务程序
** 入口参数 :无
** 出口参数 :无
********************************************************************************************************/
void IRQ_UART0 (void)
{
rcv_new0= 1; // 设置接收到新的数据标志
rcv_buf0=U0RBR;
VICVectAddr = 0x00; // 中断处理结束
}
void IRQ_UART1 (void)
{
rcv_new1 = 1;
rcv_buf1 = U1RBR; // 读取FIFO的数据,并清除中断
VICVectAddr = 0x00; // 中断处理结束
}
/*******************************************************************************************************
** 函数名称 :UART0_SendByte()
** 函数功能 :向串口0发送1字节数据
** 入口参数 :dat 要发送的数据
** 出口参数 :无
*******************************************************************************************************/
void UART0_SendByte (uint8 dat)
{
U0THR = dat; // 要发送的数据
}
void UART1_SendByte (uint8 dat)
{
U1THR = dat; // 要发送的数据
}
/*******************************************************************************************************
** 函数名称 :UART0_SendBuf()
** 函数功能 :向串口发送8字节数据
** 入口参数 :无
** 出口参数 :无
********************************************************************************************************/
void UART0_SendBuf (void)
{
UART0_SendByte(rcv_buf1);
while ((U0LSR & 0x20) == 0); // 等待数据发送完毕
}
void UART1_SendBuf (void)
{
UART1_SendByte(rcv_buf0);
while ((U1LSR & 0x20) == 0); // 等待数据发送完毕
}
/*******************************************************************************************************
** 函数名称 :UART0_Init()
** 函数功能 :串口初始化,设置工作模式和波特率。
** 入口参数 :baud 波特率
** set 模式设置(UARTMODE数据结构)
** 出口参数 :1-初始化成功, 0-初始化失败
*********************************************************************************************************/
int8 UART0_Init (uint32 baud, UARTMODE set)
{
uint32 bak;
/* 参数过滤 */
if ((baud ==0 ) || (baud > 115200)) return (0);
if ((set.datab <5) || (set.datab > 8)) return (0);
if ((set.stopb == 0) || (set.stopb > 2)) return (0);
if (set.parity > 4) return (0);
/* 设置串口波特率 */
U0LCR = 0x80; // DLAB = 1
bak = (Fpclk >> 4) / baud; //Fpclk >> 4 即除以16
U0DLM = bak >> 8;
U0DLL = bak & 0xFF;
/* 设置串口模式 */
bak = set.datab - 5; // 设置字长
if (set.stopb == 2) bak |= 0x04; // 判断是否为2位停止位
if (set.parity != 0)
{
set.parity = set.parity - 1;
bak |= 0x08;
}
bak |= set.parity << 4; // 设置奇偶校验
U0LCR = bak;
return (1);
}
int8 UART1_Init (uint32 baud, UARTMODE set)
{
uint32 bak;
/* 参数过滤 */
if ((baud ==0 ) || (baud > 115200)) return (0);
if ((set.datab <5) || (set.datab > 8)) return (0);
if ((set.stopb == 0) || (set.stopb > 2)) return (0);
if (set.parity > 4) return (0);
/* 设置串口波特率 */
U1LCR = 0x80; // DLAB = 1
bak = (Fpclk >> 4) / baud; //Fpclk >> 4 即除以16
U1DLM = bak >> 8;
U1DLL = bak & 0xFF;
/* 设置串口模式 */
bak = set.datab - 5; // 设置字长
if (set.stopb == 2) bak |= 0x04; // 判断是否为2位停止位
if (set.parity != 0)
{
set.parity = set.parity - 1;
bak |= 0x08;
}
bak |= set.parity << 4; // 设置奇偶校验
U1LCR = bak;
return (1);
}
int Main(void)
{ UARTMODE set;
TargetInit(VPBDIV_DATA, PLLCFG_DATA, MAMTIM_DATA); // don't delete
while((PLLSTAT & (1 << 10)) == 0); // can delete
set.datab = 8;
set.stopb = 1;
set.parity = 0;
rcv_new0 = 0;
rcv_new1 = 0;
PINSEL0 = 0x00050005; // 设置I/O连接到UART0 UART1
UART0_Init(9600, set); // 串口初始化
UART1_Init(9600, set);
U0FCR = 0x01; // 使能FIFO,并设置触发点为1字节
U0IER = 0x01; // 允许RBR中断,即接收中断
U1FCR = 0x01;
U1IER = 0x01;
IRQEnable(); // 使能IRQ中断
SetISR(6,0,(uint32)IRQ_UART0);
SetISR(7,1,(uint32)IRQ_UART1);
VICIntEnable = 1 << 0x06; // 使能UART0中断
VICIntEnable = 1 << 0x07;
/*****
// 启动 GSM900
IODIR|=1<<19;
IOCLR=1<<19; //关机
Del(300);
IOSET=1<<19;
*******/
while (1)
{
if (rcv_new0 == 1)
{
rcv_new0 =0;
UART1_SendBuf();
}
if(rcv_new1==1)
{
rcv_new1=0;
UART0_SendBuf();
}
//feed_cat();
}
return 0;
}
/*****************************the end************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -