Go Down

Topic: Software Serial & hardware serial at the same time (Read 1 time) previous topic - next topic

fallen

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: [Select]

#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!");
}

fallen

Hi! I make them work at the same time now!  :D  the "SWread()" should add a overtime detect.

mellis

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

fallen

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: [Select]
#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!");
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy