查看: 3806|回复: 3

[求助] 如何初始化freescale K21的UART0?

[复制链接]

该用户从未签到

11

主题

22

帖子

0

新手上路

Rank: 1

积分
69
最后登录
1970-1-1
发表于 2013-9-5 11:11:00 | 显示全部楼层 |阅读模式
小弟遇到一个问题,不知道如何初始化k21板子的UART0口,用替他们的原始程序时,当向其发送数据时候,数据寄存器UART0_D一直都不发生变化,想知道如何设置?
void uart_init (UART_MemMapPtr uartch, int sysclk, int baud)
{
    register uint16 sbr, brfa;
    uint8 temp;
   
 /* Enable the clock to the selected UART */   
    if(uartch == UART0_BASE_PTR)
  SIM_SCGC4 |= SIM_SCGC4_UART0_MASK;
    else
     if (uartch == UART1_BASE_PTR)
   SIM_SCGC4 |= SIM_SCGC4_UART1_MASK;
     else
      if (uartch == UART2_BASE_PTR)
       SIM_SCGC4 |= SIM_SCGC4_UART2_MASK;

    /* Make sure that the transmitter and receiver are disabled while we
     * change settings.
     */
    UART_C2_REG(uartch) &= ~(UART_C2_TE_MASK
    | UART_C2_RE_MASK );
                               
//      UART_C2_REG(uartch)=0xff;                         
    /* Configure the UART for 8-bit mode, no parity */
    UART_C1_REG(uartch) = 0x0; /* We need all default settings, so entire register is cleared */
   
    /* Calculate baud settings */
    sbr = (uint16)((sysclk*1000)/(baud * 16));
       
    /* Save off the current value of the UARTx_BDH except for the SBR field */
    temp = UART_BDH_REG(uartch) & ~(UART_BDH_SBR(0x1F));
   
    UART_BDH_REG(uartch) = temp |  UART_BDH_SBR(((sbr & 0x1F00) >> 8));
    UART_BDL_REG(uartch) = (uint8)(sbr & UART_BDL_SBR_MASK);
  
    /* Determine if a fractional divider is needed to get closer to the baud rate */
    brfa = (((sysclk*32000)/(baud * 16)) - (sbr * 32));
   
    /* Save off the current value of the UARTx_C4 register except for the BRFA field */
    temp = UART_C4_REG(uartch) & ~(UART_C4_BRFA(0x1F));
   
    UART_C4_REG(uartch) = temp |  UART_C4_BRFA(brfa);   
    /* Enable receiver and transmitter */
 UART_C2_REG(uartch) |= (UART_C2_TE_MASK
    | UART_C2_RE_MASK );
}
char uart_getchar (UART_MemMapPtr channel)
{
    /* Wait until character has been received */
 
    while (!(UART_S1_REG(channel) & UART_S1_RDRF_MASK));
   
    /* Return the 8-bit data from the receiver */
    return UART_D_REG(channel);
}

/** 
 * \brief   Wait for space in the UART Tx FIFO and then send a character
 * \author  
 * \param    channel  UART channel to send to
*  \param    ch      character to send
 * \return   none
 * \todo   
 * \warning
 */
void uart_putchar (UART_MemMapPtr channel, char ch)
{
    /* Wait until space is available in the FIFO */
    while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
   
    /* Send the character */
    UART_D_REG(channel) = (uint8)ch;
 }
不知道传参怎么弄
我知道答案 目前已有2人回答
回复

使用道具 举报

  • TA的每日心情
    难过
    2021-12-15 16:01
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]初来乍到

    305

    主题

    4701

    帖子

    0

    中级会员

    Rank: 3Rank: 3

    积分
    377
    最后登录
    2023-8-16
    发表于 2013-9-5 15:40:45 | 显示全部楼层

    RE:如何初始化freescale K21的UART0?

    IO口配置没有?
    该会员没有填写今日想说内容.
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    11

    主题

    22

    帖子

    0

    新手上路

    Rank: 1

    积分
    69
    最后登录
    1970-1-1
     楼主| 发表于 2013-9-5 15:50:04 | 显示全部楼层

    回复:如何初始化freescale K21的UART0?

    回复第 2 楼 于2013-09-05 15:40:45发表:
    IO口配置没有? 

    管脚都配置好了啊,就是不能够接受发送数据,UART0_D寄存器一直都没有反应
    回复 支持 反对

    使用道具 举报

    该用户从未签到

    1

    主题

    23

    帖子

    0

    注册会员

    Rank: 2

    积分
    64
    最后登录
    1970-1-1
    发表于 2013-9-5 16:14:10 | 显示全部楼层

    回复:如何初始化freescale K21的UART0?

    是不是缺了
    SIM_SOPT2 |= SIM_SOPT2_UART0SRC(1);//select PLLFLL as uart0 clock source
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /3 下一条

    Archiver|手机版|小黑屋|恩智浦技术社区

    GMT+8, 2025-7-20 15:39 , Processed in 0.089705 second(s), 23 queries , MemCache On.

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.

    快速回复 返回顶部 返回列表