MSP430 uart

From Teknologisk videncenter
Revision as of 13:02, 4 December 2018 by Heth (talk | contribs) (Header file)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


MSP430F5529

Polled operation example

Source file

#include <msp430.h>
#include <stdio.h>
#include <stdarg.h>
#include "UARTpoll.h"

// USART poll
// Using USCI_A0 as UART 
// Setting: 9600 baud, 8 bit, no parity

//Initialize USCI_A0 as UART - polled operation
long count;

char bufout[256];
void uart_init( ) {

  P4SEL = BIT4+BIT5;                        // P3.4,5 = USCI_A0 TXD/RXD
  UCA1CTL1 |= UCSWRST;                      // **Put state machine in reset**
  UCA1CTL1 |= UCSSEL_1;                     // CLK = ACLK
  UCA1BR0 = 0x03;                           // 32kHz/9600=3.41 (see User's Guide)
  UCA1BR1 = 0x00;                           //
  UCA1MCTL = UCBRS_3+UCBRF_0;               // Modulation UCBRSx=3, UCBRFx=0
  UCA1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
   
}

//cin - Character in
// Poll USCI_A0 receive register until character received.
// Parameters: none
// Returns: unsigned char - character received
unsigned char cin( void ) {
  while ( !(0x01 & UCA1IFG) );
  return(UCA1RXBUF);
}
//cin_nonblocked
//If character received - return it
//Else return 0x00 - No character received
 unsigned char cin_nonblocked( void ) {
  if ( 0x01 & UCA1IFG ) {
    return(UCA1RXBUF);
  } else {
    return(0x00);
  }
}
//cou - Character out
// Transmit character poll USCI_A0 transmit register until ready.
// Parameters: Character to transmit
// Returns: none
void cout( unsigned char c ) {
  while ( !(0x02 & UCA1IFG) && (count++ > 0) );
  UCA1TXBUF = c;
}
int putchar( int charout ) {
  cout( (char) charout );
  return(charout);
}

int __read( void ) {
  int charin;
  charin = (int) cin();
  if ( (charin == 0x0d ) || (charin == 0x0a) ) { //  CR or LF
    putchar( 0x0d );    // Output CR+LF
    putchar( 0x0a );
    return( 0x0a );     // Return LF
  } else {  // Else other character 
    putchar( charin );
    return( charin );
  }
} 


// Console cursor to position x,y
void console_gotoxy(int x, int y) {
  printf("\033[%u;%uH",x,y);

}
void console_cls( void ) {
  printf("%s%s",CLS,HOME);
}
void console_reset( void ) {
  printf("\033c");
}
//Set textcolor 0 - 255
void console_normaltext() {
  printf("\033[0m");
}
//Set textcolor 0 - 255
void console_textcolor( unsigned char color) {
  printf("\033[38;5;%um", color );
}

//Set background color 0 - 255
void console_backgroundcolor( unsigned char color) {
  printf("\033[48;5;%um", color );

Header file

extern void uart_init( );
extern unsigned char cin( void );
extern unsigned char cin_nonblocked( void );
extern void cout( unsigned char c );
extern int putchar( int charout );
//extern int getchar( void );
extern int __read( void );
extern void console_gotoxy(int x, int y);
extern int printfxy(int x, int y, const char* format, ...);
extern void console_cls( void );
extern void console_reset( void );
extern void console_normaltext();
extern void console_textcolor( unsigned char color);
extern void console_backgroundcolor( unsigned char color);

//Some vt100 Escape sequences
#define OFF             "\033[0m"
#define BOLD            "\033[1m"
#define LOWINTENS       "\033[2m"
#define ITALIC          "\033[3m"
#define UNDERLINE       "\033[4m"
#define BLINK           "\033[5m"
#define REVERSE         "\033[7m"

// POSITIONING
#define CLS             "\033[2J"       // Esc[2J Clear entire screen
//#define GOTOXY(x,y)    "\033[x;yH"     // Esc[Line;ColumnH
#define GOTOXY(x,y)    "[x;y]"     // Esc[Line;ColumnH
#define HOME            "\033[H"

#define CUD(x)          "\033[xB"      // Move cursor up n lines

//ASC-II box drawing symbols - single line
#define BOXV      179      // Vertical Line |
#define BOXVL     180      // Vertical line left line -|
#define BOXUPC    191      // Upper rigth corner -.
#define BOXLLC    192      // Lower left corner |_
#define BOXHU     193      // Horizontal and up _|_
#define BOXHD     194      // Horizontal and down -.-
#define BOXVR     195      // Vertical line right line |-
#define BOXH      196      // Horizontal line -
#define BOXX      197      // Intersection -|-