- uint8_t ComInBuf[128];
- uint8_t ComOutBuf[128];
- uint8_t ComOrderData[9]; //接收到的命令
- uint8_t ComOrderDataBuf[9]; //接收到的命令缓存
- uint8_t SendDataNum; //需要发送的实际数据个数
- uint8_t ComdataRead=0; //com串口数据已读取
- uint8_t ComdataReady=0; //com串口数据接收完成
- uint8_t ComOrdertype=0; //串口接收到的命令类型
- uint8_t Comreturn; //软件判断串口返回给软件的数据,并回复给MCU确认上一返回数据是否正确接收,1 true ;0 fault
- /*
- -
- --函数名:Uart1串口初始化
- --功 能:
- -
- */
- void Uart1Init(void)
- {
- STR_UART_T sParam;
- // Set UART Pin
- DrvGPIO_InitFunction(E_FUNC_UART1);
- SYSCLK->CLKSEL1.UART_S = 0;
- /*
- UART_S
- UART 时钟源选择
- 00 = 时钟源为外部 4~24 MHz 晶振时钟
- 01 = 时钟源为 PLL 时钟
- 11 = 时钟源为内部 22.1184 MHz 振荡器时钟
- */
-
- // UART Setting
- sParam.u32BaudRate = 115200;
- sParam.u8cDataBits = DRVUART_DATABITS_8;
- sParam.u8cStopBits = DRVUART_STOPBITS_1;
- sParam.u8cParity = DRVUART_PARITY_NONE; //无检验
- sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
- // Select UART Clock Source From 12Mhz
- //DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0); E_SYS_UART_CLKSRC
-
- DrvUART_Open(UART_PORT1,&sParam);
- // DrvUART_EnableInt(UART_PORT1,DRVUART_RDAINT & DRVUART_RDAINT,COM_INT_HANDLE);
- DrvUART_EnableInt(UART_PORT1, DRVUART_RDAINT, Uart1_ISR); //使能设置串口中断
- // UART1->IER.RDA_IEN = 1; //接收中断使能
- // UART1->IER.THRE_IEN = 0; //发送中断禁止
- NVIC_SetPriority (UART1_IRQn, (1<<__NVIC_PRIO_BITS) - 4); //设置串口中断优先级(0~3,0最高,3最低)
- }
- /**
- --函数名:Uart1_ISR 串口1中断函数
- --功 能:串口数据接收与发送
- **/
- void Uart1_ISR(uint32_t u32IntStatus)
- {
- uint8_t bInChar[1];
- static uint8_t comsendcnt=0; //发送数据计数
- static uint16_t comreccnt=0; //接收数据计数
-
- if(u32IntStatus& DRVUART_RDAINT) //接收中断
- {
- while(UART1->ISR.RDA_IF==1)
- {
- if(ComdataRead==1)
- {
- ComdataRead=0;
- comreccnt=0;
- }
- DrvUART_Read(UART_PORT1,bInChar,1);
- if(comreccnt<128) ComInBuf[comreccnt]= bInChar[0]; //将接收到的数据复制到缓存区
- comreccnt++;
- if(comreccnt>=128) comreccnt=128;
- ResetTMR0Start(); //复位串口接收超时定时器0
- }
- }
- if(u32IntStatus & DRVUART_THREINT) //发送中断 //DrvUART.H修改DRVUART_THREINT定义为BIT9
- {
- if(UART1->ISR.THRE_IF)
- {
- /*
- THRE_INT
- 发送保持寄存器空中断标志 (Read Only).
- 如果 THRE_IEN 和 THRE_IF 都被置1,该位置 1。
- 1 = THRE中断产生
- 0 = 无 THRE中断产生
- */
- if(comsendcnt<SendDataNum) //实际需要发送的数据个数
- {
- //DrvUART_Write(UART_PORT1,&ComOutBuf[comsendcnt],1);
- UART1->DATA =ComOutBuf[comsendcnt]; //test
- comsendcnt++;
- }
-
- else //数据发送完毕
- {
- if(UART1->FSR.TE_FLAG)
- {/*
- TE_FLAG
- 发送空标志位 (Read Only)
- 当 TX FIFO (UA_THR) 为空,而且最后一个字节的 STOP 位已发送,该位由硬件置位。
- 当 TX FIFO 不为空或最后一个字节传输未完成,该位自动清除。
- */
-
- comsendcnt=0; //重置发送计数
- UART1->IER.THRE_IEN = 0; //关闭中断使能,需要发送数据时开启
- // UART1->DATA =0;
- }
- }
- }
-
- }
- }
- /*
- //Uart1发送数据
- //
- */
- void SendCOMdata(uint8_t *buf,uint8_t datanum)
- {
- uint8_t i;
- for(i=0;i<datanum;i++)
- {
- ComOutBuf[i]=buf[i];
- }
- SendDataNum = datanum;
- TMR1Delay(1);//1ms延时
- UART1->IER.THRE_IEN = 1; //开启中断使能,需要发送数据时开启
-
- }
|