main.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <msp430g2553.h>
  2. // Hardware-related definitions
  3. #define UART_TXD 0x02 // TXD on P1.1 (Timer0_A.OUT0)
  4. #define UART_TBIT (1000000 / 9600) // 9600 Baud, SMCLK = 1MHz
  5. // Globals for transmit UART communication
  6. unsigned int txData; // UART internal variable for TX
  7. void TimerA_UART_tx(unsigned char byte); // Function prototypes
  8. void TimerA_UART_print(char *string);
  9. void main(void){
  10. WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
  11. DCOCTL = 0x00; // Set DCOCLK to 1MHz
  12. BCSCTL1 = CALBC1_1MHZ;
  13. DCOCTL = CALDCO_1MHZ;
  14. P1OUT = UART_TXD; // Initialize P1.1
  15. P1SEL = UART_TXD; // Timer function for TXD pin
  16. P1DIR = UART_TXD; // Set TXD pin to output
  17. // Timer_A for transmit UART operation
  18. TA0CCTL0 = OUT; // Set TXD Idle as Mark = '1'
  19. TA0CCTL1 = SCS + CM1 + CAP; // Sync, Neg Edge, Capture
  20. TA0CTL = TASSEL_2 + MC_2; // SMCLK, start in continuous mode
  21. _BIS_SR(GIE); // Enable CPU interrupts
  22. TimerA_UART_print("G2553 TimerA UART\r\n"); // Send test message
  23. TimerA_UART_print("READY.\r\n");
  24. }
  25. void TimerA_UART_tx(unsigned char byte) { // Outputs one byte using the Timer_A UART
  26. while (TACCTL0 & CCIE); // Ensure last char got TX'd
  27. TA0CCR0 = TAR; // Current state of TA counter
  28. TA0CCR0 += UART_TBIT; // One bit time till first bit
  29. txData = byte; // Load transmit data, e.g. 'A'=01000001
  30. txData |= 0x100; // Add mark stop bit, e.g. 101000001
  31. txData <<= 1; // Add space start bit, e.g. 1010000010
  32. TA0CCTL0 = OUTMOD0 + CCIE; // Set TXD on, enable counter interrupt
  33. }
  34. void TimerA_UART_print(char *string) { // Prints a string using the Timer_A UART
  35. while (*string)
  36. TimerA_UART_tx(*string++);
  37. }