I've attached two versions of a function that will compute the modulator bits 
for the MSP430 UARTs.  The first version is what one might expect with little 
optimization.  The 2nd version has been optimized and avoid multiplications.

Mark


// version 1 - "direct way"
byte calc_mod(long freq, long baud, uint *p_div) {
  uint div;
  byte mod,j, m_tot;
  long p0,p1, e0,e1;
  
  div = freq/baud;
  if (p_div) *p_div = div;

  m_tot = 0;
  mod = 0;
  for(j=0;j<8;j++) {
    p0 = baud * ((j + 1) * div + m_tot) - (j+1) * freq;;
    p1 = p0 + baud;

    e0 = labs(p0);
    e1 = labs(p1);
    if (e0 > e1) {
      ++m_tot;
      mod |= (1<<j);
    } 
  } // for j
  return mod;
}




// smarter way
byte compute_modulator_bits_v5(long baud, long freq, uint *p_div) {
  uint div;
  byte mod, j, m_tot;

#if 1
  long p0, rem;
#else
  int p0, rem;

  while(baud >= 0x8000) {
    baud >>= 1;
    freq >>= 1;
  }
#endif

  div = freq/baud;
  rem = (baud * div) - freq;

  if (p_div) *p_div = div;

  m_tot = 0;
  mod = 0;
  p0 = 0;

  for(j=0;j<8;j++) {
    p0 += rem;
    if ((p0<<1) < -baud ) {
      ++m_tot;
      p0 += baud;
      mod |= (1<<j);
    } 
  } // for j

  return mod;
}


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Reply via email to