0

Serial communication
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial communication

by jglamphear on Thu Apr 09, 2009 8:20 pm

I'm trying to write some code to allow an Arduino to communicate with Handy Cricket bus devices (see http://handyboard.com/cricket/tech/bus.shtml).

The bus devices respond to a simple serial protocol comprised of a 100 us pre-start bit, a 10 us start bit, eight 10 us data bits and finally a 10 us command bit, which seems simple enough to implement with code, but to no avail. Do the for loops require too much time to allow for accurate timing? Unfortunately I don't have an oscilloscope to look at the actual signal.

Any insights would be much appreciated.

---
// Handy Cricket serial bus transmit

int tx = 7; // transmit pin 7

void setup() {
pinMode(13, OUTPUT); // LED monitor
pinMode(tx, OUTPUT);
digitalWrite(tx, HIGH);
}

void bcmd(int val) { // send bus command
byte mask;
digitalWrite(tx, LOW); // pre-start delay
delayMicroseconds(100);
digitalWrite(tx, HIGH); // start bit
delayMicroseconds(10);
for (mask = 1; mask > 0; mask <<= 1) {
if (val & mask) {
digitalWrite(tx, HIGH);
}
else {
digitalWrite(tx, LOW);
}
delayMicroseconds(10);
}
digitalWrite(tx, LOW); // cmd bit
delayMicroseconds(10);
digitalWrite(tx, HIGH);
}

void bdata(int val) { //send serial data
Serial.println(val, BIN);
byte mask;
digitalWrite(tx, LOW); // pre-start delay
delayMicroseconds(100);
digitalWrite(tx, HIGH); // start bit
delayMicroseconds(10);
for (mask = 1; mask > 0; mask <<= 1) {
if (val & mask) {
digitalWrite(tx, HIGH);
}
else {
digitalWrite(tx, LOW);
}
delayMicroseconds(10);
}
digitalWrite(tx, HIGH); // data bit
delayMicroseconds(10);
}

void loop() {
if (digitalRead(14) == HIGH) {
bcmd(16);
delay(100);
bdata(highByte(85));
delay(100);
bdata(lowByte(85));
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
}
}

jglamphear
 
Posts: 8
Joined: Sat Feb 21, 2009 4:32 pm

Re: Serial communication

by westfw on Thu Apr 09, 2009 9:58 pm

Loops are pretty fast, but digitalWrite() itself takes about 3.4 to 5 microseconds. A routine that has to time 10us, accurate within a microsecond or so (in order not to have drifted past a bit-edge after 10 bits), is going to be difficult to write in a high level language using "opaque" functions when you don't know exactly what's inside them. Especially without a scope. And it may break going from one compiler (or library) version to another, and will almost certainly break in Arduino going from a PWM pin to a non-PWM pin (because digitalWrite) is about 50% slower on PWM pins.)

westfw
 
Posts: 1603
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: Serial communication

by mtbf0 on Fri Apr 10, 2009 6:16 am

Code: Select all | TOGGLE FULL SIZE
//  digital pin 7 is bit 7 of PORTD
#define TX PD7

// think of me as an ellipsis ...

  PORTD |= (1 << TX);
this assignment to PORTD will always take 1/16us on a 16MHz arduino. it compiles to a single sbi, (set bit immediate), instruction. if you declare TX as an int, as in your code, it will not work. that will require a memory reference to get TX, then a loop with a bunch of 16 bit adds, then a read from PORTD, followed by or'ing the result of the shift, (bunch of adds, actually), with the contents of PORTD, followed by a write to PORTD. a real mess.

i really love my arduinos. they save me from a lot of grief hardware wise. but all the helper functions, especially pinMode, digitalRead and digitalWrite are real cycle hungry. you pass them an int, (not a byte, an int), they do a bunch of compares to figure out what port to use, (pin < 8, PORTD. 7 < pin < 14, PORTB. pin > 13, PORTC), then go through the torture outlined above to actually figure out which bit you want and access the proper data direction register, port register or pin register.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Please be positive and constructive with your questions and comments.