evg
Процедура для расчета baud rate в микроконтроллерах MSP430 и пример инициализации UART для MSP430F1XX.

#define CLK_P 4000000 // MC freq in Hz
#define BAUD_RATE 57600 // baud rate

union  int_2char{
  unsigned int i;
  struct {
    unsigned char c1,c2;
  }s;
} i2c;

void set_baud(long Baud, int u)
{
	int umctl, i;
	int cc, k;
	float dc, ac;

	i2c.i = (int)((float)CLK_P / (float)Baud);
	if(u == 1) { 
		UBR11 = i2c.s.c2;
		UBR01 = i2c.s.c1;
	} else
	{
		UBR10 = i2c.s.c2;
		UBR00 = i2c.s.c1;
	}
	cc = 0;
	dc = ((float)CLK_P / (float)Baud) - (float)i2c.i;
	ac = dc;
	umctl = 0;
	for(i = 0; i < 8; i++){
		ac = ac + dc;
		k = (int)ac;
		if(k > cc)
			umctl |= (1 << i);
		cc = k;
	}
	if(u == 1) UMCTL1 = umctl;
	else UMCTL0 = umctl;

}


void init_uart1(void)
{
    _DINT();  // disable interrupts
    UCTL1 |= SWRST; // soft reset
    set_baud(BAUD_RATE, 1); // set baud rate
    ME2 |= (UTXE1 + URXE1); // UART1 module enable
    P3DIR |= BIT6;
    P3SEL |= (BIT6 + BIT7); //UART1 on
    UCTL1 = CHAR + PEV;
    UTCTL1 = SSEL1;
    URCTL1 = 0;
    UCTL1 &= ~SWRST; // clear soft reset bit
    IE2 |= (URXIE1+UTXIE1); // enable interrupts for UART1
    _EINT(); // enable interrupts
}

void init_uart0(void)
{
	_DINT();  // disable interrupt
    UCTL0 |= SWRST; // soft reset
    set_baud(BAUD_RATE, 0);
    ME1 |= (UTXE0 + URXE0); // UART0 module enable
    P3DIR |= BIT4;
    P3SEL |= (BIT5 + BIT4);//UART0 on
    UCTL0 = CHAR + PEV;
    UTCTL0 = SSEL1;
    URCTL0 = 0;
    UCTL0 &= ~SWRST; //clear soft reset bit
    IE1 |= (URXIE0 + UTXIE0);
	_EINT();
}

Коментарии