Has anyone tried this sketch?  Does it work?

Hello,

Has anyone tried this sketch and got it to work?

//Created August 15 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc

#include <ctype.h>

#define bit9600Delay 84  
#define halfBit9600Delay 42
#define bit4800Delay 188 
#define halfBit4800Delay 94 

byte rx = 6;
byte tx = 7;
byte SWval;

void setup() {
  pinMode(rx,INPUT);
  pinMode(tx,OUTPUT);
  digitalWrite(tx,HIGH);
  digitalWrite(13,HIGH); //turn on debugging LED
  SWprint('h');  //debugging hello
  SWprint('i');
  SWprint(10); //carriage return
}

void SWprint(int data)
{
  byte mask;
  //startbit
  digitalWrite(tx,LOW);
  delayMicroseconds(bit9600Delay);
  for (mask = 0x01; mask>0; mask <<= 1) {
    if (data & mask){ // choose bit
     digitalWrite(tx,HIGH); // send 1
    }
    else{
     digitalWrite(tx,LOW); // send 0
    }
    delayMicroseconds(bit9600Delay);
  }
  //stop bit
  digitalWrite(tx, HIGH);
  delayMicroseconds(bit9600Delay);
}

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); 
    delayMicroseconds(bit9600Delay);
    return val;
  }
}

void loop()
{
    SWval = SWread(); 
    SWprint(toupper(SWval));
}

I'm thinking either my rs232 to ttl circuit is incorrect or this sketch doesn't work. I've used both electrolytic and non-electrolytic capacitors with no luck. I've also tried two different transceivers (one was on a pcb itself and the other I had on my breadboard).

I was able to display non-sense characters so I changed the baud rate but that didn't help. Also, in the example files the SoftwareSerial file isn't there (which made me think maybe it doesn't work (lol)).

Do you guys think I should buy an RS232 to TTL pcb on ebay for $ 7.50 USD plus shipping? That way, I can quickly isolate the problem?

Please let me know what you honestly think. :slight_smile:


And if anyone has had a similar situation in the past
and got their project to work, PLEASE POST A SKETCH
IF POSSIBLE. THANKS.


Forgive me if I seem ignorant but is there a reason you're not using the SoftwareSerial library?

I didn't even know the library existed. I'll have to check it out. So what do you think, should I just buy the RS232 to TTL converter or should I try my transceiver circuit again except using the library for the sketch?

Tell us about your TTL to RS-232 circuit and we'll be able to help!

I am using a Sipex SP3232EBCP transceiver with four capacitors. The VCC is directly connected to 5V and Ground is directly connected to ground. The serial port connector is soldered like a loop-back connector like in the arduino.cc tutorial.

link: http://arduino.cc/en/Tutorial/ArduinoSoftwareRS232

For some reason, in hyperterminal all I receive are characters and not the "hi". Also when typing, there are wrong characters not regular letters or numbers.

I'm wondering if its my circuit or the program. Thats why I'm not sure if I should buy a RS232 to TTL converter or not.

I got it to work! I ended up using the SoftwareSerial Library. I also used the example. I feel so great. lol. :sunglasses:

THANKS FOR YOUR HELP YOU GUYS!!!

Well, that's good news! Sounds like your TTL-to-RS232 circuit was working OK after all?

Yep, the circuit worked after all. I have a question though. Do you know if there is anyway to set the baud rate for 115200? I think that 9600 is only possible, but I'm not sure.

The IDE goes up to 115200 and I've used sketches that communicate at that rate. You just set it in Serial.begin().

Thanks for the info. I'll try that later.

I have the same problem as you had. Could you post the code that you used to communicate?