main.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * This file is part of the MSP430 hardware UART example.
  3. *
  4. * Copyright (C) 2012 Stefan Wendler <sw@kaltpost.de>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /******************************************************************************
  20. * Hardware UART example for MSP430.
  21. *
  22. * Stefan Wendler
  23. * sw@kaltpost.de
  24. * http://gpio.kaltpost.de
  25. *
  26. * Echos back each character received. Blinks green LED in main loop. Toggles
  27. * red LED on RX.
  28. *
  29. * Use /dev/ttyACM0 at 9600 Bauds (and 8,N,1).
  30. *
  31. * Note: RX-TX must be swaped when using the MSPg2553 with the Launchpad!
  32. * You could easliy do so by crossing RX/TX on the jumpers of the
  33. * Launchpad.
  34. ******************************************************************************/
  35. #include <msp430g2553.h>
  36. #include "uart.h"
  37. void uart_rx_isr(unsigned char c) {
  38. uart_putc(c);
  39. P1OUT ^= BIT0; // toggle P1.0 (red led)
  40. }
  41. /**
  42. * Main routine
  43. */
  44. int main(void)
  45. {
  46. WDTCTL = WDTPW + WDTHOLD; // Stop WDT
  47. BCSCTL1 = CALBC1_1MHZ; // Set DCO
  48. DCOCTL = CALDCO_1MHZ;
  49. P1DIR = BIT0 + BIT6; // P1.0 and P1.6 are the red+green LEDs
  50. P1OUT = BIT0 + BIT6; // All LEDs off
  51. uart_init();
  52. // register ISR called when data was received
  53. uart_set_rx_isr_ptr(uart_rx_isr);
  54. __bis_SR_register(GIE);
  55. uart_puts((char *)"\n\r***************\n\r");
  56. uart_puts((char *)"MSP430 harduart\n\r");
  57. uart_puts((char *)"***************\n\r\n\r");
  58. uart_puts((char *)"PRESS any key to start echo example ... ");
  59. unsigned char c = uart_getc();
  60. uart_putc(c);
  61. uart_puts((char *)"\n\rOK\n\r");
  62. volatile unsigned long i;
  63. while(1) {
  64. P1OUT ^= BIT6; // Toggle P1.6 output (green LED) using exclusive-OR
  65. i = 50000; // Delay
  66. do (i--); // busy waiting (bad)
  67. while (i != 0);
  68. }
  69. }