Stuck making part of my code work for testing input
Hi everyone, I am new to the forums but not new to the site.
I have this little project for school that we are using our microcontroller boards with.
The project was to take the code given to us (95% of what you see here) that was an odd and even program, to modify it so that 'e' will turn it on, and 'd' will disable it.
I got that part working initially, but in the last step we had to check so that only numbers 0-9, 'e', and d are accepted.
My instructor is known for lots of bad ways of coding, like the while(1) loops, so don't eat me up for those.
I started with a error function (now commented out), that I replaced the character input with at first, but it was giving me problems so I tried making it work just inside the main function.
its telling me that the while(character == "1"... is always true as one of the compile errors along with the while loop.
I look at my code and still think its right. If anything other then d is entered, it should test the odd/even if a numbered is entered, or loop back and ask to enter another number if a false character is given. D should disable it with the else if statement.
Can you help me please, thanks
Code:
/* Serial Communication exercise on the Axiom CME11E9 EVBU and PC. */
/* A character is entered on the keyboard in terminal mode and it */
/* is returned to the terminal by the EVB over COM1 with a message */
/* stating whether it is even or odd and prompting another entry. */
/* */
/****************************************************************************/
/* Includes (libraries) */
/****************************************************************************/
#include <hidef.h>
/****************************************************************************/
/* Definitions */
/****************************************************************************/
#define baud *(volatile unsigned char *)0x102B /* baud rate reg. */
#define sccr1 *(volatile unsigned char *)0x102C /* sci cont. reg. 1 */
#define sccr2 *(volatile unsigned char *)0x102D /* sci cont. reg. 2 */
#define scsr *(volatile unsigned char *)0x102E /* sci status reg. */
#define scdat *(volatile unsigned char *)0x102F /* sci data reg. */
#define tdre 0x80 /* transmit mask bit */
#define rdrf 0x20 /* receive mask bit */
/*****************************************************************************/
/* Declarations (variables and function prototypes) */
/*****************************************************************************/
char input(void);
void onsci(void);
/*void error(void);*/
void output(char);
void outcrlf(void);
void outstr(char *strg);
char Msg1[] = "Enter a number...";
char Msg2[] = "Enter another number..";
char Msg3[] = "... is an odd number";
char Msg4[] = "... is an even number";
char Msg5[] = "The Evaluator Is Off, Press 'e' To Enable It";
char Msg6[] = "The Evaluator Is On, Press 'd' To Disable It";
char Msg7[] = "You entered an invalid character, please try again!";
char character;
/*****************************************************************************/
/* Main */
/*****************************************************************************/
void main(void)
{
EnableInterrupts;
onsci();
outstr(Msg5);
outcrlf();
while(1)
{
character = input();
if (character == 'e')
{
outstr(Msg6);
outcrlf();
outstr(Msg1);
outcrlf();
while(character != 'd')
{
character = input();
output(character);
if (character == '0' || '1' || '2' || '3' || '4' || '5'|| '6' || '7' || '8' || '9')
{
if ((character & 0x01) == 0 )
{
outstr(Msg4);
}
else
{
outstr(Msg3);
}
outcrlf();
outstr(Msg2);
outcrlf();
outcrlf();
}
}
}
else if (character = 'd')
{
main();
}
}
}
/****************************************************************************/
/****************************************************************************/
/* Locally Defined Functions */
/****************************************************************************/
/*void error(void)
{
character = input();
while(character != '1' || '2' || '3' || '4' || '5'|| '6' || '7' || '8' || '9' || 'e' || 'd')
{
outstr(Msg7);
outcrlf();
character = input();
}
}
*/
/****************************************************************************/
void outstr(char *strg) /* Output a string of characters */
{ /* String ends in NULL */
char character; /* Exits loop when onechar = NULL */
while (character = *strg)
{
output(character);
*strg++;
}
}
/****************************************************************************/
char input(void) /* Read a character from the sci */
{ /* wait for rx data then get it */
while ((scsr & rdrf) == 0 ){};
return scdat;
}
/****************************************************************************/
void onsci(void) /* Initialize SCI for 9600 baud with 8 MHz xtal. */
{
baud = 0x30;
sccr1 = 0x00;
sccr2 = 0x0C;
}
/****************************************************************************/
void output(char character) /* output one character */
{ /* wait for tx data reg. empty, then */
while ((scsr & tdre) == 0){};
scdat = (character & 0x7F); /* clear MSB (parity bit) and send. */
}
/****************************************************************************/
void outcrlf(void) /* output a carriage return and line feed */
{
output(0x0D);
output(0x0A);
}
/****************************************************************************/
/* End of module. */
/****************************************************************************/