Communications power up code

This code compiles in mspgcc and CodeBlocks. The timeouts are still based on the number of iterations rather than a given time period, which will be finalised as soon as the issue of whether of not to have an onboard clock is resolved. It has not yet been tested on the board.

#define DEBUG                /* Remove this if compiling with MSPGCC */
#define STANDALONE

#include <io.h>

void setpins(int n);
int readpins(int n);
void mcureport(int n);
void delay();
int powerup();

#ifdef DEBUG
  #include <stdio.h>
#endif

#ifdef STANDALONE
  int main() {
    powerup();
  }
#endif

int powerup() {
  int count, success, i, j, k, l, input;
  success = 0;
  k = 0;
  while ( k < 10 && success == 0 ) {        /* Begin 'very long timeout' loop */
    count = 0;
    j = 0;
    while ( j < 100 && success == 0 ) {     /* Begin 'long timeout' loop */
      setpins(170);                             /* Transmit signal 10101010 */
      i = 0;
      while ( i < 100 && success == 0 ) {   /* Begin 'short timeout' loop */
        #ifdef DEBUG
          printf("Beginning short timeout loop with i=%d, j=%d, k=%d, l=%d, success=%d, count=%d, input=%d\n", i, j, k, l, success, count, input);
        #endif
        delay();
        input = readpins(input);
        if (input == 204){                      /* Listen for input 11001100 */
          success = 1;
        }
        i = i + 1;
        count = count + 1;
      }
      j = j + 1;
    }
    k = k + 1;
    for (l = 0; l < 1000; l = l + 1) {
      delay();
    }
  }
  if ( success == 1 ) {
    setpins(204);
    mcureport(204);
  } else if ( success == 0 ) {
    setpins(170);
    mcureport(170);
  }
}

void setpins(int n) {
  #ifdef DEBUG
    printf("\nPin 3 (transmission) outputs value: %d", n);
  #else
    P3OUT=n;
  #endif
}

int readpins(int n) {
  #ifdef DEBUG
    printf("\nEnter value to pass to pin 2 (receive): ");
    scanf("%d", &n);
    return n;
  #else
    n=P2IN;
  #endif
}

void mcureport(int n) {
  #ifdef DEBUG
    printf("\nPin 1 (to MCU) outputs value: %d", n);
  #else
    P1OUT=n;
  #endif
}

void delay() {
  #ifndef DEBUG
    nop();
  #endif
}

The following code should test that all six ports can output data:

/* This code sends signals 0, 1, 2, ..., 254 and 255 sequentially to port 1, then port 2, all the way to port 6. */

#define DEBUG           /* Remove this line when compiling through MSPGCC */
#define STANDALONE

#include <stdio.h>
#include <io.h>

void setports(int portnumber, int portdata);
int readports(int n);
void mcureport(int n);
void delay();
int testports();

#ifdef STANDALONE
  int main() {
    testports();
  }
#endif

int testports() {
  int portdata, portnumber;
  for (portnumber = 1; portnumber <=6; portnumber = portnumber + 1) {
    for (portdata = 0; portdata <= 255; portdata = portdata + 1) {
      setports(portnumber, portdata);
    }
  }
}

void setports(int portnumber, int portdata) {
  #ifdef DEBUG
    printf("\nport %d outputs value: %d", portnumber, portdata);
  #else
    if (portnumber == 1) {
      P1OUT=portdata;
    } else if (portnumber == 2) {
      P2OUT=portdata;
    } else if (portnumber == 3) {
      P3OUT=portdata;
    } else if (portnumber == 4) {
      P4OUT=portdata;
    } else if (portnumber == 5) {
      P5OUT=portdata;
    } else if (portnumber == 6) {
      P6OUT=portdata;
    }
  #endif
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.