本例程为中断服务程序收发定长数据。
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 = TRUE;
// if (vUART2TxProcess.sync == emType_Sync) {
// funcptr fun = (funcptr)(vUART2TxProcess.syncfun);
// (*fun)();
// }
}
}
}
2 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
网友评论