Modifying SoftwareSerial timing to work with new digitalRead() and digitalWrite() functions.

This commit is contained in:
David A. Mellis 2007-04-20 23:18:25 +00:00
parent d277488310
commit 81b0054893
2 changed files with 14 additions and 11 deletions

View File

@ -46,14 +46,16 @@ SoftwareSerial::SoftwareSerial(uint8_t receivePin, uint8_t transmitPin)
void SoftwareSerial::begin(long speed) void SoftwareSerial::begin(long speed)
{ {
_baudRate = speed; _baudRate = speed;
_bitPeriod = 1000000 / _baudRate;
digitalWrite(_transmitPin, HIGH);
delayMicroseconds( _bitPeriod); // if we were low this establishes the end
} }
int SoftwareSerial::read() int SoftwareSerial::read()
{ {
int val = 0; int val = 0;
int width = 1000000 / _baudRate; int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50);
int fudge = -8;
//int fudge = -16;
// one byte of serial data (LSB first) // one byte of serial data (LSB first)
// ...--\ /--\/--\/--\/--\/--\/--\/--\/--\/--... // ...--\ /--\/--\/--\/--\/--\/--\/--\/--\/--...
@ -66,18 +68,18 @@ int SoftwareSerial::read()
if (digitalRead(_receivePin) == LOW) { if (digitalRead(_receivePin) == LOW) {
// frame start indicated by a falling edge and low start bit // frame start indicated by a falling edge and low start bit
// jump to the middle of the low start bit // jump to the middle of the low start bit
delayMicroseconds(width / 2); delayMicroseconds(bitDelay / 2 - clockCyclesToMicroseconds(50));
// offset of the bit in the byte: from 0 (LSB) to 7 (MSB) // offset of the bit in the byte: from 0 (LSB) to 7 (MSB)
for (int offset = 0; offset < 8; offset++) { for (int offset = 0; offset < 8; offset++) {
// jump to middle of next bit // jump to middle of next bit
delayMicroseconds(width + fudge); delayMicroseconds(bitDelay);
// read bit // read bit
val |= digitalRead(_receivePin) << offset; val |= digitalRead(_receivePin) << offset;
} }
delayMicroseconds(width + fudge); delayMicroseconds(_bitPeriod);
return val; return val;
} }
@ -90,7 +92,7 @@ void SoftwareSerial::print(uint8_t b)
if (_baudRate == 0) if (_baudRate == 0)
return; return;
int bitDelay = 1000000 / _baudRate - 15; int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
byte mask; byte mask;
digitalWrite(_transmitPin, LOW); digitalWrite(_transmitPin, LOW);
@ -110,7 +112,7 @@ void SoftwareSerial::print(uint8_t b)
delayMicroseconds(bitDelay); delayMicroseconds(bitDelay);
} }
void SoftwareSerial::print(char *s) void SoftwareSerial::print(const char *s)
{ {
while (*s) while (*s)
print(*s++); print(*s++);
@ -167,7 +169,7 @@ void SoftwareSerial::println(char c)
println(); println();
} }
void SoftwareSerial::println(char c[]) void SoftwareSerial::println(const char c[])
{ {
print(c); print(c);
println(); println();

View File

@ -28,13 +28,14 @@ class SoftwareSerial
uint8_t _receivePin; uint8_t _receivePin;
uint8_t _transmitPin; uint8_t _transmitPin;
long _baudRate; long _baudRate;
int _bitPeriod;
void printNumber(unsigned long, uint8_t); void printNumber(unsigned long, uint8_t);
public: public:
SoftwareSerial(uint8_t, uint8_t); SoftwareSerial(uint8_t, uint8_t);
void begin(long); void begin(long);
int read(); int read();
void print(char); void print(char);
void print(char[]); void print(const char[]);
void print(uint8_t); void print(uint8_t);
void print(int); void print(int);
void print(unsigned int); void print(unsigned int);
@ -43,7 +44,7 @@ class SoftwareSerial
void print(long, int); void print(long, int);
void println(void); void println(void);
void println(char); void println(char);
void println(char[]); void println(const char[]);
void println(uint8_t); void println(uint8_t);
void println(int); void println(int);
void println(long); void println(long);