LPC3131的SPI驱动程序,哪位给解释一下?

[复制链接]
2358|0
 楼主| redgalaxy 发表于 2010-8-27 15:01 | 显示全部楼层 |阅读模式
下面是LPC3131的SPI驱动程序,其中给DIV赋值的那句话,我没明白,哪位大侠给解释一下!
static STATUS spi_get_clock_div(UNS_32 target_clock,
                                UNS_32 *pDivSet)
{
  UNS_32 div, spi_clk, ps, div1;
  STATUS retvalue = _ERROR;
  /* The SPI clock is derived from the (main system oscillator / 2),
     so compute the best divider from that clock */
  spi_clk = cgu_get_clk_freq(CGU_SB_SPI_CLK_ID);
  /* Find closest divider to get at or under the target frequency.
     Use smallest prescaler possible and rely on the divider to get
     the closest target frequency */
  div = (spi_clk + target_clock / 2) / target_clock;//div为什么这样计算,没看明白?
  if ((div < SPI_MAX_DIVIDER) && (div > SPI_MIN_DIVIDER))
  {
    ps = (((div - 1) / 512) + 1) * 2;
    div1 = ((((div + ps / 2) / ps) - 1));
    /* write the divider settings */
    *pDivSet = SPI_SLV1_CLK_PS(ps) | SPI_SLV1_CLK_DIV1((div1));
    retvalue = _NO_ERROR;
  }
  return retvalue;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

25

主题

48

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部