配置UART
void uart_Confi(u32 Baudrate,UART_TypeDef* UARTx)
{
UART_InitTypeDef UART_InitStructure;
if(UARTx==UART0){
SCU_APBPeriphClockConfig(__UART0, ENABLE); /* Enable UART0 Clock */
}
else if(UARTx==UART1){
SCU_APBPeriphClockConfig(__UART1, ENABLE); /* Enable UART0 Clock */
}
else if(UARTx==UART2){
SCU_APBPeriphClockConfig(__UART2, ENABLE); /* Enable UART0 Clock */
}
/* UARTx configuration -------------------------------------------------------*/
/* UARTx configured as follow:
- Word Length = 8 Bits //数据宽度 8bit
- One Stop Bit //停止位 1bit
- No parity //无奇偶校验
- BaudRate = 9600 baud //波特率 9600
- Hardware flow control enabled (RTS and CTS signals) //硬件流控制开启
- Receive and transmit enabled //开发送和接收
- Receive and transmit FIFOs are enabled //收发FIFO开启
- Transmit and Receive FIFOs levels have 8 bytes depth //收发FIFO各8个字节
*/
UART_InitStructure.UART_WordLength = UART_WordLength_8D;
UART_InitStructure.UART_StopBits = UART_StopBits_1;
UART_InitStructure.UART_Parity = UART_Parity_No ;
UART_InitStructure.UART_BaudRate = Baudrate;
UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
//UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_RTS_CTS;
UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
UART_InitStructure.UART_FIFO = UART_FIFO_Disable; //UART_FIFO_Enable;
//UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */
//UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; /* FIFO size 16 bytes, FIFO level 8 bytes */
UART_DeInit(UARTx);
UART_Init(UARTx, &UART_InitStructure);
UART_ITConfig(UARTx, UART_IT_Receive | UART_IT_Transmit, ENABLE); /*enable UART Rx Interrupt*/ // | UART_IT_Transmit
SCU_AHBPeriphClockConfig(__VIC,ENABLE); //使能VIC时钟
SCU_AHBPeriphReset(__VIC,DISABLE);
if(UARTx==UART0){
VIC_Config(UART0_ITLine, VIC_IRQ, 5);
VIC_ITCmd(UART0_ITLine,ENABLE); /*enable UART0 interrupt for data reception*/
UART_Cmd(UART0, ENABLE); /* Enable the UART0 */
}
else if(UARTx==UART1){
VIC_Config(UART1_ITLine, VIC_IRQ, 5);
VIC_ITCmd(UART1_ITLine,ENABLE); /*enable UART0 interrupt for data reception*/
}
else if(UARTx==UART2){
VIC_Config(UART2_ITLine, VIC_IRQ, 5);
VIC_ITCmd(UART2_ITLine,ENABLE); /*enable UART0 interrupt for data reception*/
}
}
UART0中断的处理
void UART0_IRQHandler(void)
{
//RX
if ((UART_GetITStatus(UART0, UART_IT_Receive)) == SET)
{
UART_ClearITPendingBit(UART0, UART_IT_Receive);
UART0_RX_ISR();
}
//TX
if ((UART_GetITStatus(UART0, UART_IT_Transmit)) == SET)
{
UART_ClearITPendingBit(UART0, UART_IT_Transmit);
UART0_TX_ISR();
}
}
void UART0_TX_ISR(void)
{
u8 Data;
if(FifoPop(&Uart0TxFifo,&Data))
{
UART0->DR = Data;
}
}
串口发送
u32 UartWrite(u8 Uart, u8* pBuffer, u32 BufferSize)
{
u32 Count = 0;
u8 Data;
UartFifo_t* pUartFifo;
UART_TypeDef * pUart;
switch(Uart)
{
case 0:
pUart = UART0;
pUartFifo = &Uart0TxFifo;
break;
case 1:
pUart = UART1;
pUartFifo = &Uart1TxFifo;
break;
case 2:
pUart = UART2;
pUartFifo = &Uart2TxFifo;
break;
default:
return(0);
}
if(!FifoPop(pUartFifo,&Data))
{
FifoPush(pUartFifo,*(pBuffer+Count));
FifoPop(pUartFifo,&Data);
Count++;
//UART_SendData(pUart,Data);
pUart->DR = Data;
}
if(BufferSize != 0)
{
for ( ; Count < BufferSize; ++Count)
{
if(!FifoPush(pUartFifo,*(pBuffer+Count)))
{
break;
}
}
}
return(Count);
}
出现的问题是执行完 UART_SendData(pUart,Data)或者pUart->DR = Data后,就不能执行下一步了,检测UART0->MIS,发现一直是UART_IT_Transmit,也就是它一直在死循环。 |