170 likes | 370 Views
UART. 下載. uart1 echo.rar. Baudrate 計算. 1000000/19200=52.08 0x34 UBR0 0x34 UBR1 0 U1BR0 = 0x34; // 1MHz 19200 U1BR1 = 0x00; // 1MHz 19200 U1MCTL = 0x6B; // 1MHz 19200 modulation. RS-232 接線. MSP430 實驗板. PC. 7 Seg LED. uart1. uart0.
E N D
下載 • uart1 echo.rar
Baudrate計算 • 1000000/19200=52.08 0x34 • UBR0 0x34 • UBR1 0 • U1BR0 = 0x34; // 1MHz 19200 • U1BR1 = 0x00; // 1MHz 19200 • U1MCTL = 0x6B; // 1MHz 19200 modulation
RS-232接線 MSP430 實驗板 PC 7 Seg LED uart1 uart0
for (;;) • { • while (!(U1TCTL & TXEPT)); // Confirm no TXing before --> LPM3 • _DINT(); // Disable interrupts for flag test • _NOP(); • U1TXBUF = 't'; // char to TXBUF0
睡眠狀態 • if (!(U1TCTL & SSEL0)) • _BIS_SR(LPM0_bits + GIE); // RX'ing char, LPM, int's active • else • _BIS_SR(LPM3_bits + GIE); // Enter LPM3, int's active
中斷程式--接收 • #pragma vector=USART1RX_VECTOR • __interrupt void usart1_rx (void) • { • if ((IFG2 & URXIFG1)) // Test URXIFG0 • { • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready? • U1TXBUF = U1RXBUF; // RXBUF0 to TXBUF0 • _BIC_SR_IRQ(LPM3_bits); // Exit LPM3 after reti • U1TCTL |= SSEL0; // SSEL0 = 1, no RX activity • } else // Start edge • { • U1TCTL &= ~URXSE; // Clear URXS signal • U1TCTL |= URXSE; // Re-enable edge detect • _BIC_SR_IRQ(SCG1 + SCG0); // DCO reamins on after reti • U1TCTL &= ~SSEL0; // SSEL0= 0, RX activity • } • }
Baudrate計算 • 1000000/38400=26.04 0x1A • UBR0 0x1A • UBR1 0 • U1BR0 = 0x1A; // 1MHz 38400 • U1BR1 = 0x00; // 1MHz 19200 • U1MCTL = 0x6B; // 1MHz 19200 modulation
發送一串文字 • U1TXBUF = 't'; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready? • U1TXBUF = 'e'; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready? • U1TXBUF = 's'; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready? • U1TXBUF = 't'; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready? • U1TXBUF = ' '; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready?
Baudrate計算 • 1000000/115200=8.58 8 • UBR0 0x8 • UBR1 0 • U1BR0 = 8; // 1MHz 115200 • U1BR1 = 0x00; // 1MHz 19200 • U1MCTL = 0x6B; // 1MHz 19200 modulation
接收副程式 • char recv_uart1( void ) • { • if ((IFG2 & URXIFG1)) // Test URXIFG0 • { • return U1RXBUF; • } • return 0; • }
利用接收副程式接收資料 • temp=0; • while(temp==0) • { • temp=recv_uart1(); • } • U1TXBUF = temp; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready?
檢查是否輸入1 • while(temp==0) • { • temp=recv_uart1(); • } • U1TXBUF = temp; // char to TXBUF0 • while (!(IFG2 & UTXIFG1)); // USART1 TX buffer ready? • if (temp==0x31){ • }