This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Adding using getchr()

I was trying to make a program that would get a letter from the serial then add up all the numbers that follow it until the next letter appears in the serial port. I was using P2 for a visual aid only. Anyone know of a link I could go to so I could get on the right path.?

#include <reg51.h>
#include <stdio.h>

unsigned int getchr(void);
int tempval;
int result;
int X;


void main(void)
{

X = 0x00;
SCON = 0x50;
TMOD = 0x20;
TH1 = 0xFD; //9600 baud
TR1 = 1;
TI = 1;


while (1)
{

P2 = 0;

while (1)
{
tempval = getchr(); //GET CHAR FORM SERIAL PORT

if(tempval == 0x58 && result != 0x58) //CHECK FOR "X"
{result = 0x58;
tempval = 0x00;} // CLEAR tempval

if(result == 0x58 && tempval != 0x00)
{X = X +(tempval - 0x30);
// ADDS VALUE FROM X AND KEEPS ON ADDING EVEN IF A KEY
//WHEN SERIAL DATA ISN'T COMING IN.
tempval = 0x00;} // CLEAR tempval

if(result == 0x5A) //CHECK FOR "Z"
{X = 0x00; // CLEAR X
result =0x00;} // ZERO P2


P2 = X; //SEND X TO PORT P2

}
}
}


unsigned int getchr(void)
{
unsigned int chr, ch1;
ch1 = 0x00;
chr = 0x00;
ch1 = SBUF; //GET CHARACTER
chr = ch1 & 0x7f; //MASK OFF 8TH BIT
RI = 0; //CLEAR STATUS
return(chr);
}

0