6272 Embedded Controller I/Oktober 2016

From Teknologisk videncenter
Jump to: navigation, search

C

MSP430

Opgaver

MSP430/MSP430G2 Serial 1 project

Få stdio bibliotek til at virke

UART_poll.c

Indsæt følgende nederst i UART_poll.c

int putchar( int charout ) {
  cout( (char) charout );
  return(charout);
}

int getchar( 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 );
  }
}

UART_poll.h

Indsæt følgende linier nederst i UART_poll.h

extern int putchar( int charout );
extern int getchar( void );

Main.c

Indsæt følgende include sætning øverst i main.c

#include <stdio.h>

Lysdioder port definitioner

#define SMALL_BOARD  // Using MSP430G2553
//#define BIG_BOARD // Using MSP430F5529

#ifdef SMALL_BOARD
unsigned char volatile * const PORT1SEL  = (unsigned char *) 0x0026;
unsigned char volatile * const PORT1SEL2 = (unsigned char *) 0x0041;
unsigned char volatile * const PORT1IN   = (unsigned char *) 0x0020;
unsigned char volatile * const PORT1OUT  = (unsigned char *) 0x0021;
unsigned char volatile * const PORT1DIR  = (unsigned char *) 0x0022;
#endif
#ifdef BIG_BOARD
unsigned char volatile * const PORT1SEL  = (unsigned char *) 0x020a;
unsigned char volatile * const PORT1IN   = (unsigned char *) 0x0200;
unsigned char volatile * const PORT1OUT  = (unsigned char *) 0x0202;
unsigned char volatile * const PORT1DIR  = (unsigned char *) 0x0204;
unsigned char volatile * const PORT4SEL  = (unsigned char *) 0x022b;
unsigned char volatile * const PORT4IN   = (unsigned char *) 0x0221;
unsigned char volatile * const PORT4OUT  = (unsigned char *) 0x0223;
unsigned char volatile * const PORT4DIR  = (unsigned char *) 0x0225;
#endif


#ifdef SMALL_BOARD
  *PORT1SEL  &= ~(0x41);  // Clear bit 1 and 6
  *PORT1SEL2 &= ~(0x41);  // Clear bit 1 and 6
  *PORT1DIR  |= 0x41;     // Port 1 direction: bit 1 and 6 as output 
  *PORT1OUT  &= ~0x01;    // Port 1 bit 1=1 (Red LED on) bit 6 = 0 (Green LED off)
  
  while(1) {
     {
       for( tmp=0; tmp <= 50000 ; tmp++);
      *PORT1OUT  ^= 0x41;  // Invert port 1 bit 1 (RED) and bit 6 (GREEN)
      if ( *PORT1IN 
    }
  }
#endif
#ifdef BIG_BOARD
  *PORT1SEL  &= ~(0x01);  // Clear bit 1 
  *PORT1DIR  |= 0x01;     // Port 1 direction: bit 1 as output 
  *PORT1OUT  |= 0x01;    // Port 1 bit 1=1 (Red LED on)
  *PORT4SEL  &= ~(0x80);
  *PORT4DIR  |= 0x80;
  *PORT4OUT  &= ~(0x80);
  while(1) {
     {
       for( tmp=0; tmp <= 50000 ; tmp++);
      *PORT1OUT  ^= 0x1;  // Invert port 1 bit 1 (RED) 
      *PORT4OUT  ^= 0x80; // Invert port 5 bit 8 (GREEN) 
    }
  }
#endif

STRIN eksempel

#define ENTER 13
#define NULL 0
unsigned int strin( unsigned char *buf, int sizeofbuf ) {
  int i = -1; // så vi starter på 0
  
  
  if ( sizeofbuf == 0) { // hvis intet return 0
    return(0);
  }
  
  sizeofbuf--;
  
  do
  {
    i++;
    buf[i] = cin();
    cout(buf[i]);
  } while ((buf[i] != ENTER) && (i < sizeofbuf--)); // if enter is not and sizeobuf is less then 20, set last to null
  buf[i]=NULL;
  return (i);
}