MSP430 - G2553之串口操作

#include <msp430.h>

void delay1s(void) 
{
  int t = 1000;
  while (t--)
    __delay_cycles(1000);    
}

#define UART_TXD 0x02                                  // TXD on P1.1 (Timer0_A.OUT0)
                                              
#define UART_TBIT (1000000 / 9600)                     // 9600 Baud, SMCLK = 1MHz
                                                       // Globals for transmit UART communication
unsigned int txData;                                   // UART internal variable for TX

void TimerA_UART_tx(unsigned char byte);               // Function prototypes
void TimerA_UART_print(char *string);

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;                            // Stop watchdog timer
  DCOCTL = 0x00;                                       // Set DCOCLK to 1MHz
  BCSCTL1 = CALBC1_1MHZ;
  DCOCTL = CALDCO_1MHZ;

  P1OUT = UART_TXD;                                    // Initialize P1.1
  P1SEL = UART_TXD;                                    // Timer function for TXD pin
  P1DIR = UART_TXD;                                    // Set TXD pin to output
                                                       // Timer_A for transmit UART operation
  TA0CCTL0 = OUT;                                      // Set TXD Idle as Mark = '1'
  TA0CCTL1 = SCS + CM1 + CAP;                          // Sync, Neg Edge, Capture
  TA0CTL = TASSEL_2 + MC_2;                            // SMCLK, start in continuous mode

  P2DIR |= (BIT3 | BIT4);						//Set P1.0 and P1.6 to outputs
  
  _BIS_SR(GIE);                                        // Enable CPU interrupts

  while (1) {
    TimerA_UART_print("G2553 TimerA UART\r\n");          // Send test message 
    TimerA_UART_print("READY.\r\n");
    P2OUT ^= 0x18;
    delay1s();
  }
}

void TimerA_UART_tx(unsigned char byte)              // Outputs one byte using the Timer_A UART
{              
  while (TACCTL0 & CCIE);                              // Ensure last char got TX'd

  TA0CCR0 = TAR;                                       // Current state of TA counter
  TA0CCR0 += UART_TBIT;                                // One bit time till first bit
  txData = byte;                                       // Load transmit data, e.g. 'A'=01000001
  txData |= 0x100;                                     // Add mark stop bit, e.g. 101000001
  txData <<= 1;                                        // Add space start bit, e.g. 1010000010
  TA0CCTL0 = OUTMOD0 + CCIE;                           // Set TXD on, enable counter interrupt
}

void TimerA_UART_print(char *string) {                 // Prints a string using the Timer_A UART
  while (*string)
    TimerA_UART_tx(*string++);
}

#pragma vector = TIMER0_A0_VECTOR                      // Timer_A UART - Transmit ISR

__interrupt void Timer_A0_ISR(void) {
  static unsigned char txBitCnt = 10;
  TA0CCR0 += UART_TBIT;                                // Add Offset to CCRx

  if (txBitCnt == 0) {                                 // All bits TXed?
    TA0CCTL0 &= ~CCIE;                                 // All bits TXed, disable interrupt
    txBitCnt = 10;                                     // Re-load bit counter
  }
  else {
    if (txData & 0x01)
      TA0CCTL0 &= ~OUTMOD2;                            // TX Mark '1'
    else
      TA0CCTL0 |= OUTMOD2;                             // TX Space '0'
  }
  txData >>= 1;                                        // Shift right 1 bit (low bits TX'ed first)
  txBitCnt--;
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值