//receive serial character from serial port
char mygetchar(void)
{
char c;
while(!RI);
RI =0;
c = SBUF;
return SBUF;
}
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// -=-=-=-=- Main Program -=-=-=-=-=-=-=
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
void main()
{
// -=-=- Intialize variables -=-=-=
pos = 0;
// -=-=- Intialise Serial Port -=-=-=
//Sets up MCU to use 9600 bps @ 11.059 MHz Crystal
SCON = 0x52; // 8-bit UART mode
TMOD = 0x20; // timer 1 mode 2 auto reload
TH1= 0xfd; // 9600 8-n-1
TR1 = 1; // run timer1
// -=-=- Program Loop -=-=-=
while(1)
{
ch = mygetchar(); //loop till character received
if(ch==0x0A) // if received character is , 0x0A, 10 then process buffer
{
pos = 0; // buffer position reset for next reading
// extract data from serial buffer to 8 bit integer value
// convert data from ASCII to decimal
read1 = ((sbuffer[1]-'0')*100) + ((sbuffer[2]-'0')*10) +(sbuffer[3]-'0');
read2 = ((sbuffer[6]-'0')*100) + ((sbuffer[7]-'0')*10) +(sbuffer[8]-'0');
read3 = ((sbuffer[11]-'0')*100) + ((sbuffer[12]-'0')*10) +(sbuffer[13]-'0');
// Do whatever you wish to do with this sensor integer variables
// Show on LCD or Do some action as per your application
// Value of variables will be between 0-255
} else { //store serial data to buffer
sbuffer[pos] = ch;
pos++;
}
} // end while
}// end main
Did you get tired of the responses you got in your other thread?
Code tags man!
Why don't you explain what you want to do instead. That looks like someone was trying to recreate parseInt(). The entire thing may be able to be replaced with a single function call.
A literal conversion, compiles but untested. Also added buffer overflow checking. You will NOT be able to use the serial port to connect to a PC to see the results as the code stands.
char sbuffer[30], ch;
byte pos, read1, read2, read3;
void setup()
{
Serial.begin(9600);
// -=-=- Intialize variables -=-=-=
pos = 0;
}
// -=-=- Program Loop -=-=-=
void loop()
{
while(Serial.available() > 0)
{
ch = Serial.read(); //loop till character received
if(ch == 0x0A) // if received character is <LF>, 0x0A, 10 then process buffer
{
pos = 0; // buffer position reset for next reading
// extract data from serial buffer to 8 bit integer value
// convert data from ASCII to decimal
read1 = ((sbuffer[1]-'0')*100) + ((sbuffer[2]-'0')*10) +(sbuffer[3]-'0');
read2 = ((sbuffer[6]-'0')*100) + ((sbuffer[7]-'0')*10) +(sbuffer[8]-'0');
read3 = ((sbuffer[11]-'0')*100) + ((sbuffer[12]-'0')*10) +(sbuffer[13]-'0');
// Do whatever you wish to do with this sensor integer variables
// Show on LCD or Do some action as per your application
// Value of variables will be between 0-255
}
else
{ //store serial data to buffer
sbuffer[pos++] = ch;
if(pos > sizeof(sbuffer))
{
pos = 0;
}
}
} // end while
}
Riva:
A literal conversion, compiles but untested. Also added buffer overflow checking. You will NOT be able to use the serial port to connect to a PC to see the results as the code stands.
// -=-=- Program Loop -=-=-=
void loop()
{
while(Serial.available() > 0)
{
ch = Serial.read(); //loop till character received
if(ch == 0x0A) // if received character is , 0x0A, 10 then process buffer
{
pos = 0; // buffer position reset for next reading
// extract data from serial buffer to 8 bit integer value
// convert data from ASCII to decimal
read1 = ((sbuffer[1]-'0')*100) + ((sbuffer[2]-'0')*10) +(sbuffer[3]-'0');
read2 = ((sbuffer[6]-'0')*100) + ((sbuffer[7]-'0')*10) +(sbuffer[8]-'0');
read3 = ((sbuffer[11]-'0')*100) + ((sbuffer[12]-'0')*10) +(sbuffer[13]-'0');
// Do whatever you wish to do with this sensor integer variables
// Show on LCD or Do some action as per your application
// Value of variables will be between 0-255
}
else
{ //store serial data to buffer
sbuffer[pos++] = ch;
if(pos > sizeof(sbuffer))
{
pos = 0;
}
}
} // end while
}
Thanq So Much Riva your updated code helped us alot