在线时间2 小时
UID3441796
注册时间2017-12-15
NXP金币0
该用户从未签到
新手上路

- 积分
- 23
- 最后登录
- 2018-3-17
|
最近在使用MKE06Z,按例程进行uart初始化时,UART寄存器值总为0,初始化不成功,而同一工程中ADC、定时器、can等寄存器值均可设置,求高手指点!代码如下:
void uart_init(void)
{
UART_ConfigType sConfig = {0};
sConfig.u32SysClkHz = 20970000;
sConfig.u32Baudrate = 9600;
sConfig.sSettings.b9bitModeEn = 0;
sConfig.sSettings.bParityEn = 0;
UARTx_Init(UART0, &sConfig);
}
void UARTx_Init(UART_Type *pUART, UART_ConfigType *pConfig)
{
uint16_t u16Sbr;
uint8_t u8Temp;
uint32_t u32SysClk = pConfig->u32SysClkHz;
uint32_t u32Baud = pConfig->u32Baudrate;
uint8_t b9bitModeEn = pConfig->sSettings.b9bitModeEn;
uint8_t bParityEn = pConfig->sSettings.bParityEn;
uint8_t bParityType = pConfig->sSettings.bParityType;
/* Sanity check */
ASSERT((pUART == UART0) || (pUART == UART1) || (pUART == UART2));
/* Enable the clock to the selected UART */
if (pUART == UART0)
{
SIM->SCGC |= SIM_SCGC_UART0_MASK;
}
#if defined(CPU_KE02) | defined(CPU_KE06)
else if (pUART == UART1)
{
SIM->SCGC |= SIM_SCGC_UART1_MASK;
}
else
{
SIM->SCGC |= SIM_SCGC_UART2_MASK;
}
#endif
/* Make sure that the transmitter and receiver are disabled while we
* change settings.
*/
pUART->C2 &= ~(UART_C2_TE_MASK | UART_C2_RE_MASK );
/* Configure the UART for 8-bit mode, no parity */
pUART->C1 = 0;
//8-bit mode config
if (b9bitModeEn)
{
//if not the default 8-bit mode, then enable 9-bit mode
pUART->C1 |= UART_C1_M_MASK;
}
//parity check config
if (bParityEn)
{
pUART->C1 |= UART_C1_PE_MASK;
if (bParityType)
{
//if not defult even parity type, enable odd parity type
pUART->C1 |= UART_C1_PT_MASK;
}
}
/* Calculate baud settings */
u16Sbr = (((u32SysClk)>>4) + (u32Baud>>1))/u32Baud;
/* Save off the current value of the UARTx_BDH except for the SBR field */
u8Temp = pUART->BDH & ~(UART_BDH_SBR_MASK);
pUART->BDH = u8Temp | UART_BDH_SBR(u16Sbr >> 8);
pUART->BDL = (uint8_t)(u16Sbr & UART_BDL_SBR_MASK);
/* Enable receiver and transmitter */
pUART->C2 |= (UART_C2_TE_MASK | UART_C2_RE_MASK );
}
最佳答案
如果能收发就是正常的,那就IDE的问题,换IDE
|
|