灵动微MM32F003硬件最小系统 --- 串口循环发送,中断接

2020-11-26  本文已影响0人  黄开通

本例程为中断服务程序接收串口数据,当缓存达到10个字节后,利用循环发送接收到的10个字节数据。

1 中断服务程序

void UART2_IRQHandler(void)
{
    UART_TypeDef* UARTx = UART2;
    u8 temp;

// Receive packet
    if (UART_GetITStatus(UARTx, UART_IT_RXIEN) != RESET) {
        UART_ClearITPendingBit(UARTx, UART_IT_RXIEN);
        temp = UART_ReceiveData(UARTx);
        vtemp[vNo++] = temp;

        if ((vUART2RxProcess.Cnt) > 0) {
            *(u8*)(vUART2RxProcess.Ptr) = temp;
            vUART2RxProcess.Ptr++;
            vUART2RxProcess.Cnt--;
            if ((vUART2RxProcess.Cnt) == 0) {
                vUART2RxProcess.Complete = TRUE;
                UART_ITConfig(UARTx, UART_IT_RXIEN, DISABLE);
//                if (vUART2RxProcess.sync == emType_Sync) {
//                    funcptr fun = (funcptr)(vUART2RxProcess.syncRx);
//                   (*fun)();
//                }
            }

        }

    }
//// Send packet
//    if (UART_GetITStatus(UARTx, UART_IT_TXIEN) != RESET) {
//        UART_ClearITPendingBit(UARTx, UART_IT_TXIEN);
//
//      if (vUART2TxProcess.Cnt > 0) {
//            UART_SendData(UARTx, *(u8*)(vUART2TxProcess.Ptr));
//            vUART2TxProcess.Ptr++;
//            vUART2TxProcess.Cnt--;
//        }
//        else{
//            UART_ITConfig(UARTx, UART_IT_TXIEN, DISABLE);
//            while (!UART_GetFlagStatus(UARTx, UART_CSR_TXC));
//          vUART2TxProcess.Complete = 1;
////            if (vUART2TxProcess.sync == emType_Sync) {
////                funcptr fun = (funcptr)(vUART2TxProcess.syncfun);
////               (*fun)();
////            }
//        }

    //}
}

2 接收数据处理程序

int UART2_ReadBuf(u8* ptr, u16 count)
{

    if (!vUART2RxProcess.Process) {
        vUART2RxProcess.Process = TRUE;
        vUART2RxProcess.Complete = FALSE;
        vUART2RxProcess.Cnt = count;
        vUART2RxProcess.Ptr = ptr;
        if (emType_Polling == vUART2RxProcess.type) {

            //UART2_PollingRcvPacket_Block();
        } else if (emType_IT == vUART2RxProcess.type) {
            //   UART_ITConfig(UART2, UART_IT_RXIEN, DISABLE);
            UART_ITConfig(UART2, UART_IT_RXIEN, ENABLE);

        } else if (emType_DMA == vUART2RxProcess.type) {
            //DRV_UART2_DMA_RcvPacket();
        } else {
        }
        if (vUART2RxProcess.block == 1) {
            while (!vUART2RxProcess.Complete) {
                //          if (vUART2RxProcess.bTimeOut && (nTimeOutCnt >= vUART2RxProcess.TimeOut)) {
                //              return emRETURN_TimeOut;
                //          }
            }
            vUART2RxProcess.Process = FALSE;
            return 1;
        }
    }


    else {
        if (vUART2RxProcess.Complete) {
            vUART2RxProcess.Process = FALSE;
            return 1;
        } else {
            return 0;
        }

    }

    return 0;
}

3 main主程序流程

int main(void)
{
    int ret;
    InitSystick();

    Uart_ConfigInit(9600);
//    UART2_WriteBuf((u8*)printBuf, sprintf(printBuf,"sprintf ok\r\n"));
    UartSendGroup((u8*)printBuf, sprintf(printBuf, "sprintf ok\r\n"));          //pintf stdio data
    UartSendAscii("UartSendAscii\r\n");                                         //printf string
    UartSendAscii("Please input data ,end with '\\n' \r\n");

    while(1) {
        // delay(1000);

        ret = UART2_ReadBuf(RxBuf, 10);
        if(ret == 1) {
            memcpy(TxBuf, RxBuf, 10);
            UART2_WriteBuf(TxBuf, 10);
        }
    }
}

官方例程的修正已基本完成,需要程序的同学可以到百度网盘下载。
提取码:0o5i

上一篇下一篇

猜你喜欢

热点阅读