uart.c

Go to the documentation of this file.
00001 /*
00002  * File:        uart.c
00003  * Purpose:     Serial interface functions
00004  * Author:      Peter Ivanov
00005  * Modified by:
00006  * Created:     2007-05-19 11:31:29
00007  * Last modify: 2007-12-27 14:12:46 ivanovp {Time-stamp}
00008  * Copyright:   (C) Peter Ivanov, 2007
00009  * Licence:     GPL
00010  */
00011 /**
00012  * \file uart.c
00013  * \brief Serial interface functions
00014  * \author Peter Ivanov
00015  */
00016 #include <msp430x44x.h>
00017 #include <msp430/basic_clock.h>
00018 #include <msp430/system_clock.h>
00019 #include <stdarg.h>
00020 #include <stdlib.h>
00021 #include <stdio.h>
00022 #include <string.h>
00023 #include <sys/types.h>
00024 #include "uart.h"
00025 
00026 /**
00027  * Initialize UART (RS232) port: 9600 baud, 8n1.
00028  */
00029 void UART_init (void)
00030 {
00031     //UART ini
00032     U1TCTL = SSEL0;                     // UCLOCK = ACLK
00033     U1BR0 = 0x03;                       // BAUD RATE = 9600
00034     U1BR1 = 0x00;
00035     U1MCTL = 0x4a;                      // with modulation
00036     U1CTL = CHAR;                       // Start 8bit 1stop, N
00037     ME2 = UTXE1 | URXE1;                // enable RX and TX
00038     P4SEL |= BIT0 | BIT1;               // select UART pins
00039     P4DIR |= BIT0;                      // port direction for TXD0
00040     P4DIR &= ~BIT1;                     // port direction for RXD0
00041     //IE1 |= URXIE0;                    // Enable USART0 RX interrupt
00042     //_EINT();                          // Enable interrupts
00043     //U1CTL &= ~SWRST;                  //reset UART logic
00044 }
00045 
00046 /**
00047  * Send ony byte via UART.
00048  *
00049  * @param c Character to send.
00050  */
00051 void UART_putChar (char c)              // UART1 Transmit Subroutine
00052 {
00053     while ((IFG2 & UTXIFG1) == 0);      // Wait for ready U1TXBUF
00054     U1TXBUF = c;                        // send data
00055 }
00056 
00057 /**
00058  * Check UART's input buffer.
00059  *
00060  * @return TRUE: if input buffer contains received data.
00061  */
00062 char UART_getCharIsReady ()
00063 {
00064     return (IFG2 & URXIFG1) != 0;
00065 }
00066 
00067 /**
00068  * Receive one byte via UART. This function waits for the input (blocking).
00069  *
00070  * @return Received character.
00071  */
00072 char UART_getChar ()
00073 {
00074     while (!UART_getCharIsReady ());
00075     return U1RXBUF;
00076 }
00077 
00078 /**
00079  * Write string buffer to UART.
00080  *
00081  * @param buf String buffer to send.
00082  * @param length Length of string buffer.
00083  */
00084 void UART_write (const char *buf, int length)
00085 {
00086     int i;
00087     for (i = 0; i < length; i++)
00088     {
00089         UART_putChar (buf[i]);
00090     }
00091 }
00092 
00093 /**
00094  * Printf function which uses UART interface to send.
00095  * Example:
00096 <pre>
00097 UART_printf ("Number: %02i\r\n", number);
00098 </pre>
00099  *
00100  * @param fmt Printf format string. Example: "Value: %02i\r\n"
00101  */
00102 void UART_printf (const char *fmt, ...)
00103 {
00104     static va_list valist;
00105     char buf[UART_PRINTF_BUF_SIZE];
00106     
00107     va_start (valist, fmt);
00108     vsnprintf (buf, sizeof (buf), fmt, valist);
00109     va_end (valist);    
00110     UART_write (buf, strlen (buf));
00111 }

Generated on Thu Dec 27 14:21:51 2007 for Sample MSP430-449STK2 project by  doxygen 1.5.1