Step 9 - entering user code – Freescale Semiconductor Microcontrollers User Manual

Page 660

Advertising
background image

ColdFire V1 Full Chip Simulation Connection

FCS Tutorials

660

Microcontrollers Debugger Manual

Step 9 - Entering User Code

1. Open the user module MyGuessTheNumber.C

2. Insert the following code before the main routine:

#include <stdlib.h>
void PutChar(unsigned char c) {
while (AS1_SendChar(c) == ERR_TXFULL) {
// could wait a bit here
}
}
void PutString(const char* str) {
while (str[0] != '\0') {
PutChar(str[0]);
str++;
}
}

void GuessTheNumber(void) {
int ran = rand() / (RAND_MAX / 9);
AS1_Init();

PutString("Guess a Number between 0 and 9\n");
PutString("Number: ");
for (;;) {
unsigned char c;
if (AS1_RecvChar(&c) == ERR_OK) {
PutChar(c); PutChar(' ');
if(c < '0' || c > '9') {
PutString("not a number, try again\n");
} else if(c == ran + '0') {
PutString("\nCongratulation! You have found the number!");
PutString("\nGuess a new number\n");
ran = rand() / (RAND_MAX / 9);
} else if(c > ran + '0') {
PutString("lower\n");
} else {
PutString("greater\n");
}
PutString("Number: ");
} else {
// could wait a bit here
}
} // for
}

3. Call the function GuessTheNumber in the main routine.

Advertising