Go Down

Topic: Software serial in 04 (Read 564 times) previous topic - next topic

christian

Hi there I am trying to drive a motor with a Little Step-U
Unipolar Stepper Motor Controller http://www.tla.co.nz/lit_step.html

Can someone please explain how to use the new software serial in 04?

Antonio

Same question for me.

I've found some interesting files in Arduino libraries, like "uartsw.c" and "uartswconf.h". But no way to use them in my program for the moment.

Any idea?

mellis

See this thread for some info:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1147888882

hdeweyh

#3
Aug 08, 2006, 08:25 pm Last Edit: Aug 08, 2006, 09:18 pm by hdeweyh Reason: 1
I just got the following code working based on the examples in the FAQ. Only weird thing is it sends two bytes for every byte, one is the correct byte one is  dec 192. Consistently over and over!

#define BAUDRATE 9600
// the width of a single bit of serial data in microseconds
// in each second (1000000 microseconds) there are a number of bits equal
// to the baud rate (e.g. 9600), so each bit lasts for 1000000 / baudrate
// microseconds
int width = 1000000 / BAUDRATE;
int fudge = -25;
int TXfudge = -20;
int rx = 6;
int tx = 7;

void setup() {
 pinMode(rx,INPUT);
 pinMode(tx,OUTPUT);
 digitalWrite(tx,HIGH);
 digitalWrite(13,HIGH);
}
void SWprint(int data)
{
 int mask;
 //startbit
 digitalWrite(tx,LOW);
 delayMicroseconds(width + TXfudge);
 for (mask = 0x01; mask; mask <<= 1) {
   if (data & mask){ // choose bit
     digitalWrite(tx,HIGH); // send 1
   }
   else{
     digitalWrite(tx,LOW); // send 1
   }
   delayMicroseconds(width + TXfudge);
 }
 //stop bit
 digitalWrite(tx, HIGH);
 delayMicroseconds(width + TXfudge);
}
int SWread()
{
 int val = 0;
 // one byte of serial data (LSB first)
 // ...--\    /--\/--\/--\/--\/--\/--\/--\/--\/--...
 //       \--/\--/\--/\--/\--/\--/\--/\--/\--/
 //      start  0   1   2   3   4   5   6   7 stop

 while (digitalRead(rx));

 // confirm that this is a real start bit, not line noise
 if (digitalRead(rx) == LOW) {
   // frame start indicated by a falling edge and low start bit
   // jump to the middle of the low start bit
   delayMicroseconds(width / 2);
     
   // offset of the bit in the byte: from 0 (LSB) to 7 (MSB)
   for (int offset = 0; offset < 8; offset++) {
     // jump to middle of next bit
     delayMicroseconds(width + fudge);
     
     // read bit
     val |= digitalRead(rx) << offset;
   }
     
   delayMicroseconds(width + fudge);
 }
 else
 {
   val=0;
 }
 return val;
}
void loop()
{
   int SWval;
   SWval = SWread();
   SWprint(SWval);
}

-Heather

mellis

Are you sending between two Arduino boards?  If so, you might try sending to something else with a hardware serial port (like a Wiring board) to see if the problem is in the sending or the receiving.  

hdeweyh

I am sending between Arduino and a PC. I have tested the code using hardware serial on half (first HW TX with SW RX then HW RX with SW TX) and the problem is definitely in the sending.

-h

Go Up