本帖最后由 ansicheng 于 2018-8-24 23:05 编辑
以下代码,我测试,NU131,UART0正常使用,UART5不能使用,原因是什么,谁有个准确答案
int main()
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable Internal RC 22.1184MHz clock */
CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk;
/* Waiting for Internal RC clock ready */
while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk));
/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLK_S_Msk)) | CLK_CLKSEL0_HCLK_S_HIRC;
CLK->CLKDIV = (CLK->CLKDIV & (~CLK_CLKDIV_HCLK_N_Msk)) | CLK_CLKDIV_HCLK(1);
/* Set PLL to Power-down mode */
CLK->PLLCON |= CLK_PLLCON_PD_Msk;
/* Enable external XTAL 12MHz clock */
CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk;
/* Waiting for external XTAL clock ready */
while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_XTL12M_STB_Msk));
/* Set core clock as PLL_CLOCK from PLL */
CLK->PLLCON = PLLCON_SETTING;
while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_PLL_STB_Msk));
CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLK_S_Msk)) | CLK_CLKSEL0_HCLK_S_PLL;
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
//SystemCoreClockUpdate();
PllClock = PLL_CLOCK; // PLL
SystemCoreClock = PLL_CLOCK / 1; // HCLK
CyclesPerUs = PLL_CLOCK / 1000000; // For SYS_SysTickDelay()
/* Enable UART module clock */
CLK->APBCLK |= CLK_APBCLK_UART0_EN_Msk;
CLK->APBCLK1 |= CLK_APBCLK1_UART5_EN_Msk;
/* Select UART module clock source */
CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_UART_S_Msk)) | CLK_CLKSEL1_UART_S_HXT;
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set GPB multi-function pins for UART0 RXD and TXD */
SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk);
SYS->GPB_MFP |= SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
/* Set GPA multi-function pins for UART5 RXD and TXD */
SYS->GPA_MFP &= ~(SYS_GPA_MFP_PA0_Msk | SYS_GPA_MFP_PA1_Msk);
SYS->GPA_MFP |= SYS_GPA_MFP_PA0_UART5_TXD | SYS_GPA_MFP_PA1_UART5_RXD;
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset UART0 */
SYS->IPRSTC2 |= SYS_IPRSTC2_UART0_RST_Msk;
SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk;
/* Configure UART0 and set UART0 Baudrate */
UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 9600);
UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
/* Reset UART5 */
SYS->IPRSTC2 |= SYS_IPRSTC3_UART5_RST_Msk;
SYS->IPRSTC2 &= ~SYS_IPRSTC3_UART5_RST_Msk;
/* Configure UART5 and set UART5 Baudrate */
UART5->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 9600);
UART5->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
}
void SendChar_ToUART5()
{
char pu8TxBuf[6] = "abcde\n";
uint32_t u32Count, u32delayno;
for(u32Count = 0; u32Count != 6; u32Count++)
{
u32delayno = 0;
while((UART5->FSR & UART_FSR_TE_FLAG_Msk) == 0) /* Wait Tx empty and Time-out manner */
{
u32delayno++;
if(u32delayno >= 0x40000000)
return;
}
UART5->THR = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
}
}
|