3 deleted 2 characters in body
source | link

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 08.833 uS while 104/12 gives 08.877 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()  
{   
  byte val = 0;   
  while (digitalRead(rx));   
  //wait for start bit   
  if (digitalRead(rx) == LOW) {   
    delayMicroseconds(halfBit9600Delay);   
    for (int offset = 0; offset < 8; offset++) {   
     delayMicroseconds(bit9600Delay);   
     val |= digitalRead(rx) << offset;   
    }   
    //wait for stop bit + extra    
    delayMicroseconds(bit9600Delay);     ....hard wired two stop bits   
    delayMicroseconds(bit9600Delay);     ....or 1 stop and 1 bit time gap   
    return val;    
  }  
}  

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 0.83 uS while 104/12 gives 0.87 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()  
{   
  byte val = 0;   
  while (digitalRead(rx));   
  //wait for start bit   
  if (digitalRead(rx) == LOW) {   
    delayMicroseconds(halfBit9600Delay);   
    for (int offset = 0; offset < 8; offset++) {   
     delayMicroseconds(bit9600Delay);   
     val |= digitalRead(rx) << offset;   
    }   
    //wait for stop bit + extra    
    delayMicroseconds(bit9600Delay);     ....hard wired two stop bits   
    delayMicroseconds(bit9600Delay);     ....or 1 stop and 1 bit time gap   
    return val;    
  }  
}  

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 8.3 uS while 104/12 gives 8.7 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()  
{   
  byte val = 0;   
  while (digitalRead(rx));   
  //wait for start bit   
  if (digitalRead(rx) == LOW) {   
    delayMicroseconds(halfBit9600Delay);   
    for (int offset = 0; offset < 8; offset++) {   
     delayMicroseconds(bit9600Delay);   
     val |= digitalRead(rx) << offset;   
    }   
    //wait for stop bit + extra    
    delayMicroseconds(bit9600Delay);     ....hard wired two stop bits   
    delayMicroseconds(bit9600Delay);     ....or 1 stop and 1 bit time gap   
    return val;    
  }  
}  
2 Formatted code
source | link

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 0.83 uS while 104/12 gives 0.87 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); ....hard wired two stop bits
    delayMicroseconds(bit9600Delay); ....or 1 stop and 1 bit time gap
    return val;
  }
}

int SWread()  
{   
  byte val = 0;   
  while (digitalRead(rx));   
  //wait for start bit   
  if (digitalRead(rx) == LOW) {   
    delayMicroseconds(halfBit9600Delay);   
    for (int offset = 0; offset < 8; offset++) {   
     delayMicroseconds(bit9600Delay);   
     val |= digitalRead(rx) << offset;   
    }   
    //wait for stop bit + extra    
    delayMicroseconds(bit9600Delay);     ....hard wired two stop bits   
    delayMicroseconds(bit9600Delay);     ....or 1 stop and 1 bit time gap   
    return val;    
  }  
}  

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 0.83 uS while 104/12 gives 0.87 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); ....hard wired two stop bits
    delayMicroseconds(bit9600Delay); ....or 1 stop and 1 bit time gap
    return val;
  }
}

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 0.83 uS while 104/12 gives 0.87 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()  
{   
  byte val = 0;   
  while (digitalRead(rx));   
  //wait for start bit   
  if (digitalRead(rx) == LOW) {   
    delayMicroseconds(halfBit9600Delay);   
    for (int offset = 0; offset < 8; offset++) {   
     delayMicroseconds(bit9600Delay);   
     val |= digitalRead(rx) << offset;   
    }   
    //wait for stop bit + extra    
    delayMicroseconds(bit9600Delay);     ....hard wired two stop bits   
    delayMicroseconds(bit9600Delay);     ....or 1 stop and 1 bit time gap   
    return val;    
  }  
}  
    Post Migrated Here from electronics.stackexchange.com
1
source | link

This Arduino sketch is a wonderful opportunity for you to learn the very basics of serial communication.
You should be very careful scaling the constants given in any baud rate program (whether using a hardware or software UART) as they are normally off by some small percentage, and without understanding the implications you may eventually introduce timing errors. There is a great description of the timing sensitivities in this Picaxe note.

The table below shows the actual bit times for various baud rates: enter image description here

Notice here that for 9600 baud the sketch uses a constant which will use a 100 uS delay function for the bit times. From the table above you can see that this is slightly inaccurate, and the more correct delay would be 104 uS. This is an error of about 4%.

Would this error prevent successful reception of characters in the sketch? Single characters with inter character spaces more than 1 character time, no ....but if you sent a string of characters end to end with no gaps this sketch would fail on receive.

Scaling the constant for 19200 baud --> 100/2 gives 50 but the bit time for 19200 is actually 52 uS, so again an error, this time of about 1%. If we scaled the constant (100) for 115200 --> 100/12 gives 0.83 uS while 104/12 gives 0.87 uS for an error 4.5% or close to 0%.

There is IMO an error in this sketch for the serial receive routine, since it's hard wired to expect 2 stop bits. Any concatenated characters separated by only one stop bit will fail (you miss the second character start bit).

int SWread()
{
  byte val = 0;
  while (digitalRead(rx));
  //wait for start bit
  if (digitalRead(rx) == LOW) {
    delayMicroseconds(halfBit9600Delay);
    for (int offset = 0; offset < 8; offset++) {
     delayMicroseconds(bit9600Delay);
     val |= digitalRead(rx) << offset;
    }
    //wait for stop bit + extra
    delayMicroseconds(bit9600Delay); ....hard wired two stop bits
    delayMicroseconds(bit9600Delay); ....or 1 stop and 1 bit time gap
    return val;
  }
}