Go Down

Topic: Comunicating ADC+ Arduino DUE with the protocol 1-wire (Read 17473 times) previous topic - next topic

graynomad

#15
Mar 20, 2014, 12:50 am Last Edit: Mar 20, 2014, 12:52 am by Graynomad Reason: 1
Your "diagram" looks correct but your terminology is not. A PIC (or any other CPU for that matter) does not have RS-232, it has a UART that talks async serial. RS-232 is a voltage level spec (as is RS-485) that has nothing to do with the format of the data or anything like that.

Quote
I think that I must use two MAX485 for adapt the signals between protocols. One of them in the out of the PIC and the other one in the entrance of the Arduino Due's RS232.
Yes for two devices you need two RS-485 transceivers, one at each end. Every extra device added needs another transceiver.

The MAX485 is a common choice and of course it works OK, but I don't think it's available in a 3v3 version which you need for the Due end. Check that out, if not try a LTC1480 or similar that does run at 3v3.

Quote
This is the best and easy protocol that I could use.
RS-485 is not a protocol, a protocol is code that implements a set of rules that determine the format of the data being transferred and optionally how to handle errors etc. You will have to write the protocol. That said yes I think RS-485 is the best method.

Quote
I can connect others PIC in the bus

Again, why a PIC? Not that there's anything wrong with PICs but you won't get much (or any) help here with them.

______
Rob


Rob Gray aka the GRAYnomad www.robgray.com

Due88

Thanks Graynomad! I am learning more in this forum  :)

The part of the pics is solved (that part is not a big problem now). I will send from Pics to Arduino a value type Float. For example, the smaller value that I can obtain with the measure of the Pic's ADC(10bits) is 5V/1024 =0,00488V. The smaller value that Arduino will receive by serial port is that.   Will the arduino's serial port receive packet of the 8bits? I will send to Arduino the value in string form by pic's serial port. If later I need send by arduino's serial port (to other platform, for example my laptop)  that smaller value I can use a simple "print()" ?

I wish that you understand that explanation. Thanks for your responses.

graynomad

Quote
Will the arduino's serial port receive packet of the 8bits?

It will receive whatever you send to it.

Quote
I will send to Arduino the value in string form by pic's serial port.

Then that's what it will receive (subject to faults in the line etc).

Quote
If later I need send by arduino's serial port (to other platform, for example my laptop)  that smaller value I can use a simple "print()" ?

Yes, you can just echo the same data or change it as you see fit.

I won't ask again why you are using a PIC, I'll just assume there is a good reason.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Due88

Thanks a lot  Grainomad and the other users with your help. I am continuing with this project and developing it.  :) :)

I am using pics because the idea is comunicate the Arduino and the sensors with a only bus. I will use the pics' ADC to make the conversion (Analog to digital) and I will save problems (probably this isn't the best  metod).

graynomad

Quote
I am using pics because the idea is comunicate the Arduino and the sensors with a only bus.
That's a reason to use a processor of any kind and a reasonable decision. I'm just curious as to the choice of a PIC because you won't get much (any) help here with it. So if you are already proficient with PICs fair enough, but if you have to learn a processor anyway it might as well be an AVR.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Due88

#20
Mar 29, 2014, 11:07 pm Last Edit: Mar 29, 2014, 11:10 pm by Due88 Reason: 1
Yes, I will use AVR. I think that is a good idea to start with microcontrollers. Now, I am programing the Arduino Due. I am trying to do a program with this algorithm:

The Arduino will receive instructions from my laptop by serial port. The laptop will send two thinks. For example, if I want that the sensors send me its information (temperature information) I will send to Arduino the following string: "$ADQ,Pxx,CHx" where the variable [ x ] is a number 0 to 9.  Example: If I want receive sensor's information (microcontroller 1 's channel0)  I will sent to Arduino: "$ADQ,P01,CH0" and Arduino send this information to microcontrollers connected by bus RS485 and Arduino will wait for resply from microcontrollers. The other think that laptop can send to arduino is the option of  automatic mode.

The laptop too will send to Arduino if I want automatic mode. If I want automatic mode Arduino will send information request by serial port to microcontrollers. Example: if want that Arduino send automatically the strings "$ADQ,Pxx,CHx" by Serial port I need send to Arduino --> Automatic mode = 1. If Automatic mode is 0 Arduino will receive the string  from laptop and will retransmit this information to microcontrollers.

Arduino will receive sensors' information from microcontrollers by serial port and only will retransmit the received strings to laptop by serial port. Example: If Arduino send by bus the following string: "$ADQ,P01,CH0" the microcontroller 1 will respond with the following string :"$ADQ,P01,CH0,n" where  [ n ] is the result of the ADC's conversion in the microcontroller (ADC 10 bits).

I need use two serial ports from Arduino: the first SERIAL0 to connect laptop with arduino and the second SERIAL1 to connect the bus (with microcontrollers) and Arduino. Fisrt, I will send  always the string of automatic mode (AutomaticMode = 0/1) and later if I want manual mode (AutomaticMode=0) I will send the string that I want send (for example "$ADQ,P01,CH0").

I have done some think but I don't know if I am going by good way.


void setup() {

 int AutomaticMode=0;
 int CALL=0;
 
 boolean FlagUART= false;
 
 char DataReceivedMODE[2]={0};
 char DataReceivedSERIAL0[13]={0};
 char DataReceivedSERIAL1[19]={0};

 Serial.begin(9600);
 Serial1.begin(9600);
 
}


void loop() {
 
 CALL= UART0();
 CALL= AnalyzeSensors();
 CALL= UART1();
}



void UART0(){

while(Serial.available()){

if(FlagUART==false){
 
 FlagUART=true;
 
  DataReceivedMODE= Serial.read();
   
   Switch (DataReceivedMODE){
     
     case '1':
     
     AutimaticMode= 0;
     
     break;
     
     case default:
     
     AutomaticMode= 1;
     
     break;
     
     }
   }

else if {FlagUART=true||AutimaticMode==1){
 
 FlagUART=false;
 
 DataReceivedSERIAL0=Serial.read();

  }

else{

 FlagUART=false;

}

 }


}

 return(0);

}


void AnalyzeSensors(){
 
  if (AutomaticMode==1){
     
    Serial1.print("$ADQ,P00,CH0");
    delay(1);
    Serial1.print("$ADQ,P01,CH0");
    delay(1);
    Serial1.print("$ADQ,P02,CH0");
    delay(1);
    Serial1.print("$ADQ,P03,CH0");
    delay(1);
    Serial1.print("$ADQ,P04,CH0");
    delay(1);
    Serial1.print("$ADQ,P05,CH0");
    delay(1);
    Serial1.print("$ADQ,P06,CH0");
    delay(1);
    Serial1.print("$ADQ,P07,CH0");
    delay(1);
    Serial1.print("$ADQ,P08,CH0");
    delay(1);
    Serial1.print("$ADQ,P09,CH0");
    delay(1);
    Serial1.print("$ADQ,P10,CH0");
    delay(1);
   
  }
   
   else{
   
   
   Serial1.print(DataReceivedSERIAL0);
   delay(1);
   
    }

return(0);

}

void UART1() {

while(Serial1.available()){

DataReceivedSERIAL1=Serial1.read();
 
}
 return(0);
 
}



graynomad

#21
Mar 29, 2014, 11:42 pm Last Edit: Mar 30, 2014, 01:00 am by Graynomad Reason: 1
Please use CODE tags and provide code that at least compiles, have you tried that code or is it just a collection of semi-random instructions?

For example all the variable declarations in setup() are local to setup(), they are not global, so all references to them from the rest of the code causes errors.

Code: [Select]
CALL= UART0();
  CALL= AnalyzeSensors();
  CALL= UART1();


These functions are void functions, they do not return a value. And even if they did you do not use CALL anyway. Then there's

Code: [Select]
     AutimaticMode= 0;
            break;
            case default:
            AutomaticMode= 1;


AutimaticMode or AutomaticMode?

You should at least try to provide working code, or at least code that compiles.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Due88

Sorry. I am working it and I'm thinking, I have a doubt now... If I want use a Switch with the data received by serial port I can use this data directly? For example,   Arduino will receive by serial port "1" or "0" and will select one case. The following code is correct?

DataReceivedMODE= Serial.read();
   
   Switch (DataReceivedMODE){
     
     case '1':
      AutomaticMode= 0;
      break;
     
     case default:
     AutomaticMode= 1;
     break;
 }

graynomad

#23
Mar 30, 2014, 03:22 am Last Edit: Mar 30, 2014, 03:23 am by Graynomad Reason: 1
If you only have those two possibilities like that then an if() is more appropriate

Code: [Select]
if (DataReceivedMODE == '1')
   AutomaticMode= 0;
else
   AutomaticMode= 1;


or even better (but more cryptic)

Code: [Select]
AutomaticMode = DataReceivedMODE == '1' ? 0 : 1

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Due88

#24
Mar 30, 2014, 12:05 pm Last Edit: Mar 30, 2014, 12:07 pm by Due88 Reason: 1
Thanks!! I am learning a lot. Other problem solved  :). But now I am trying receive correctly the datas from serial port. For example, If I need receive from serial port 0 the string "$ADQ,P00,CH0" I was reading that the function Serial.read() only returns the first byte available in the buffer. My string have 12 bytes (without count the null character). If I work with the variable [char DataReceivedUART0[13];]   and I do DataReceivedUART0=Serial.read();  I only will receive the first byte available in serial port? or willl I need use a bucle for if I need save 12bytes ("$ADQ,P00,CH0")? -->  

for(i=0; i<13; i++){
DataReceivedUART0=Serial.read();
}


When one byte is read with serial.read() in serial port this byte, later, is removed?

Thanks a lot!!!

graynomad

Code: [Select]
for(i=0; i<13; i++){
DataReceivedUART0=Serial.read();
}


Notice anything missing? I bet that's not your actual code. That's why we insist on people using CODE TAGS. (the # button in the tool bar when editing your post)

I suspect your problem is simple but we need to see the code properly presented without missing parts.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Due88

#26
Mar 30, 2014, 12:58 pm Last Edit: Mar 30, 2014, 01:03 pm by Due88 Reason: 1
This is the code. Sorry.  :) This code have errors that I don't know what is. (Errors  like  dd:36: error: ISO C++ forbids comparison between pointer and integer).

Code: [Select]
char DataReceivedMODE[2]={0};
char DataReceivedSERIAL0[13]={0};
char DataReceivedSERIAL1[18]={0};
int AutomaticMode=0;
int i=0;
boolean FlagUART= false;


void setup() {
 
 Serial.begin(9600);
 Serial1.begin(9600);
 
}


void loop() {
 
 UART0();
 AnalyzeSensors();
 UART1();
 
}


void UART0(){  //This function receve the datas from my laptop (Mode and Resquest sensor's infromation)

while(Serial.available()){

if(FlagUART==false){
 
 FlagUART=true;
 
 
    if(DataReceivedMODE == '1'){
     
      AutomaticMode= 1;
     
      }
     
     else{
       
       AutomaticMode= 0;
     
      }
 
 }

else if (FlagUART=true||AutomaticMode==1){
 
 FlagUART=false;
 
 for (i=0;i<13;i++){
 
 DataReceivedSERIAL0[i]=Serial.read();
  }
}

else{

 FlagUART=false;

}

 }

 

}



void AnalyzeSensors(){
 
  if (AutomaticMode==1){
     
    Serial1.print("$ADQ,P00,CH0");
    delay(1);
    Serial1.print("$ADQ,P01,CH0");
    delay(1);
    Serial1.print("$ADQ,P02,CH0");
    delay(1);
    Serial1.print("$ADQ,P03,CH0");
    delay(1);
    Serial1.print("$ADQ,P04,CH0");
    delay(1);
    Serial1.print("$ADQ,P05,CH0");
    delay(1);
    Serial1.print("$ADQ,P06,CH0");
    delay(1);
    Serial1.print("$ADQ,P07,CH0");
    delay(1);
    Serial1.print("$ADQ,P08,CH0");
    delay(1);
    Serial1.print("$ADQ,P09,CH0");
    delay(1);
    Serial1.print("$ADQ,P10,CH0");
    delay(1);
   
  }
   
   else{
   
   
   Serial1.print(DataReceivedSERIAL0);
   delay(1);
   
    }



}


void UART1() { //This function receive the datas from microcontrollers

while(Serial1.available()){

for(i=0;i<18;i++){  
 
DataReceivedSERIAL1=Serial1.read();

}

Serial.print(DataReceivedSERIAL1);
 
}

 
}

graynomad

Code: [Select]
if(DataReceivedMODE == '1')

You are comparing an array with a char

Code: [Select]
if(DataReceivedMODE[0] == '1')

Will compile, as to the logic of the program I'll look at that.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Due88

#28
Mar 30, 2014, 01:18 pm Last Edit: Mar 30, 2014, 01:24 pm by Due88 Reason: 1
OK yes. Now it will compile! Thankss. But I have a question is possible simplify when I send by serial port the strings "$ADQ,P00,CH0", "$ADQ,P01,CH0" etc? And I don't know if I do correctly the assignation of  the received datas from serial port.


Code: [Select]
char DataReceivedMODE[2]={0};
char DataReceivedSERIAL0[13]={0};
char DataReceivedSERIAL1[18]={0};
int AutomaticMode=0;
int i=0;
boolean FlagUART= false;


void setup() {
 
 Serial.begin(9600);
 Serial1.begin(9600);
 
}


void loop() {
 
 UART0();
 AnalyzeSensors();
 UART1();
 
}


void UART0(){  //This function receve the datas from my laptop (Mode and Resquest sensor's infromation)

while(Serial.available()){

if(FlagUART==false){
 
 FlagUART=true;
 
 
    if(DataReceivedMODE[0] == '1'){
     
      AutomaticMode= 1;
     
      }
     
     else{
       
       AutomaticMode= 0;
     
      }
 
 }

else if (FlagUART=true||AutomaticMode==1){
 
 FlagUART=false;
 
 for (i=0;i<13;i++){
 
 DataReceivedSERIAL0[i]=Serial.read();
  }
}

else{

 FlagUART=false;

}

 }

 

}



void AnalyzeSensors(){
 
  if (AutomaticMode==1){
     
    Serial1.print("$ADQ,P00,CH0");
    delay(1);
    Serial1.print("$ADQ,P01,CH0");
    delay(1);
    Serial1.print("$ADQ,P02,CH0");
    delay(1);
    Serial1.print("$ADQ,P03,CH0");
    delay(1);
    Serial1.print("$ADQ,P04,CH0");
    delay(1);
    Serial1.print("$ADQ,P05,CH0");
    delay(1);
    Serial1.print("$ADQ,P06,CH0");
    delay(1);
    Serial1.print("$ADQ,P07,CH0");
    delay(1);
    Serial1.print("$ADQ,P08,CH0");
    delay(1);
    Serial1.print("$ADQ,P09,CH0");
    delay(1);
    Serial1.print("$ADQ,P10,CH0");
    delay(1);
   
  }
   
   else{
   
   
   Serial1.print(DataReceivedSERIAL0);
   delay(1);
   
    }



}


void UART1() { //This function receive the datas from microcontrollers

while(Serial1.available()){

for(i=0;i<18;i++){  
 
DataReceivedSERIAL1[i]=Serial1.read();

}

Serial.print(DataReceivedSERIAL1);
 
}
 
 
}

graynomad

Another problem

Code: [Select]
while(Serial1.available()) {
for(i=0;i<18;i++){  
DataReceivedSERIAL1[i]=Serial1.read();
}


Let's assume there was a single byte available so this code it executed. What makes you think there are 18 bytes? and anyway aren't there just 12 bytes in the received string?

Code: [Select]
if (Serial1.available() >= 12) {
for(int i=0; i<12; i++){  
DataReceivedSERIAL1[i]=Serial1.read();
}


Same in UART0().

Sorry, I still haven't thought about the logic of the program as I keep finding errors like these.

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Go Up