Pages: [1]   Go Down
Author Topic: Software Serial & hardware serial at the same time  (Read 987 times)
0 Members and 1 Guest are viewing this topic.
China
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
I love Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi ! I see the Arduino Software Serial Interface , and I make it work . but when I try to use "hardware serial" and  "Software Serial"  at the same time , only "Software Serial" work , is there somebody can make them work at the same time ? or it is impossibility?

my code:

Code:
#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
  beginSerial(9600);
}

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));
    delay(100);
    printString("hello!");
}
Logged

China
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
I love Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi! I make them work at the same time now!  smiley-grin  the "SWread()" should add a overtime detect.
Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool.  Can you post the revised version of the code?
Logged

China
Offline Offline
Jr. Member
**
Karma: 0
Posts: 60
I love Arduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I only read 10 times in Software Serial , if on data come, try next loop, no still wait . these code just fit my purpose .
hope it is useful.

Code:
#include <ctype.h>

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

int flag,i;

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
  beginSerial(9600);
  flag=0;
}

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;
  for(i=0;i<10;i++){

    if (digitalRead(rx) == LOW && flag==0) {
      delayMicroseconds(halfBit9600Delay);
      for (int offset = 0; offset < 8; offset++) {
        delayMicroseconds(bit9600Delay);
        val |= digitalRead(rx) << offset;
      }
      //wait for stop bit + extra
      delayMicroseconds(bit9600Delay);
      delayMicroseconds(bit9600Delay);
      flag=1;
    }
    return val;
  }
}

void loop()
{
  SWval = SWread();
  SWprint(toupper(SWval));
  delay(100);
  printString("hello!");
}
Logged

Pages: [1]   Go Up
Jump to: