Address

Author Topic: Help with XOR?  (Read 4239 times)

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Help with XOR?
« on: November 19, 2012, 10:11:21 AM »
Hello all, I'm back with a question.  I am working on getting an Arduino to read a voltage input and report that read back via a serial protocol.  I have all the necessary parts working except for one.  While talking to the radio vendor about the output format they advised I needed to do an XOR on all the bytes as a checksum at the end.  I have found examples online about how to do XOR on Arduino, but I can't say that I actually understand what is going on.


Could someone explain in simple term, non-programmer here, what an XOR is and how it is implemented?

f00bard

  • FamiLAB Member
  • Newbie
  • *
  • Posts: 0
  • Da Real MVP
    • View Profile
Re: Help with XOR?
« Reply #1 on: November 19, 2012, 01:15:17 PM »
In English, XOR or "exclusive OR" can be expressed as such: given two inputs, the output is true if one but not both of the inputs are true.  The truth table, where inputs are A and B:

A | B | Out
------------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0

This is often used, like in your application, for calculating a parity bit.  Think of it as binary addition where you're discarding the result of the carry operation.  1 + 0 = 1.  1 + 1 = 0 with a carry of 1.  Throw out the carry and you're left with 0.

This is in contrast to an OR operation where the output is true if either or both of the inputs are true.

When XORing two values, make sure they're of the same type/width.  So if you have a 16-bit integer make sure the other variable or constant you're XORing with is also 16-bits wide.

Hope this helps!

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #2 on: November 19, 2012, 01:36:02 PM »
I think I have it figured out, but now my UNO is acting up on me and giving me an out of sync message.  1 step forward 2 steps back...:)

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #3 on: November 20, 2012, 10:23:11 AM »
OK, so I need another nudge.  I have the string that I need to transmit properly formatted on the Arduino and I understand how the XOR checksum works on paper.  My problem is how to I take my string "V;RB0475;" and manipulate it so I can perform a XOR per byte.

On paper I do "56 XOR 3B XOR 52 XOR 42 XOR 30 XOR 34 XOR 37 XOR 35 XOR 3B" which give me a binary value after processing that I convert back to a byte and attach to the end of the string.

How do I take the string and break it into individual bytes to do the Bitwise XOR and generate the checksum?

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #4 on: November 20, 2012, 01:18:58 PM »
Let me add some more. the string I have is currently ASCII. I think I need to change it to a char array them perform the XOR against the values in the array.


Does that sound sane?




aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #5 on: November 20, 2012, 01:45:29 PM »
And for reference here is what I have working so far.  I have removed all the stuff I've tried because I wasn't getting anywhere quickly. The sections in bold are what I am working on one at a time.



/*
  Hall_Effect_Read
  Reads an analog input on pin A5, converts it to voltage, multiplies by 100 to remove "." which is an unsupported character by the radio.
  Calculates a checksum using XOR and appends values to the end of the string.


 */
 //clear existing setup
void setup() {
  ///init serial comms
  Serial.begin(9600);
}


// Loop for testing, final revision will be run once per power cycle
void loop() {
  //turn pin 9 on to full power
  analogWrite(9, 255);
  //wait 5ms for sensor to settle
  delay(5);
  // read the input on analog pin A5:
  int sensorValue = analogRead(A5);
  // Convert the analog reading to a voltage and multiply by 100 to remove decimal point:
  float voltage = sensorValue * (5.0 / 1023.0) * 100;
  //Setup message format (probably a more efficient way to do this
  //convert voltage to something other than float value since I can't add the float value to a string (errors out)
  word voltvalue = voltage;
  //set lead in string for radio protocol
  String Header = "V;RB0";
  //add voltage
  String Read = Header + voltvalue;
  //add final seperator before checksum bit
  String Transmission = Read + ";";
   //turn pin 9 off
  analogWrite(9, 0);
  // print out the message minus checksum (FOR TESTING PURPOSES REMOVE A WHEN COMPLETE)
  Serial.println(Transmission);
  //calculate Checksum
 
  //add checksum to transmission
 
  //transmit message serially
 
  //delay 10 seconds between reads REMOVE WHEN COMPLETE
  delay(10000);
}

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #6 on: December 05, 2012, 08:33:18 AM »
Code: [Select]
So I got it to work for the most part (see below), still need to do some serial stuff and test connectivity when I get radios in.


I have a question regarding serial. I have the Serial speed set to 9600 8,N,1 which is default.  When I use an Arduino UNO with the processor removed to monitor the serial output of the chip on a breadboard I have to set it to 4800bps to read the output. Is this due to the way ARduino does math? (i.e. 4800 TX + 4800RX = 9600bps in setup). 


Also I have a XProtolab that I was trying to use as a protocol sniffer (UART) and all I get on it is garbage mostly when sniffing the traffic. Any ideas what may be happening there from those who may have used it?



/*
  Hall_Effect_Read
  Reads an analog input on pin A5, converts it to voltage, multiplies by 100 to remove "." which is an unsupported character by the radio.
  Calculates a checksum using XOR and appends values to the end of the string.


 */
 //clear existing setup
void setup() {
  ///init serial comms
  Serial.begin(9600);
 }


// Loop for testing, final revision will be run once per power cycle
void loop() {
  //turn pin 9 on to full power
  analogWrite(9, 255);
  //wait 5ms for sensor to settle
  delay(5);
  // read the input on analog pin A5:
  int sensorValue = analogRead(A5);
  // Convert the analog reading to a voltage and multiply by 100 to remove decimal point:
  float voltage = sensorValue * (5.0 / 10.23);
   //turn pin 9 off
  analogWrite(9, 0);
  //build array
  char voltread[10];
  sprintf (voltread, "V;RB0%03i;",(int)voltage);
  //add checksum to transmission
  byte checksum = 0;
    for(int i = 0; i < 9; i++) checksum ^= voltread;
  char transmission[13];
  sprintf (transmission, "%s%x;", voltread, checksum);
  Serial.println(transmission);
  //transmit message serially
  //need serial parameters still
  //delay X seconds between reads REMOVE WHEN COMPLETE
  delay(3000);
}

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #7 on: January 15, 2013, 10:47:00 AM »
Well I finally got a radio in house for testing and so far it's a complete failure...:)

I think the problem is with serial comms.  Does anyone see anything wrong with the below setup?

Radio is expecting UART 1200bps 7,E,1 for incoming communications.

I used the above sketch but changes the Serial parameters to Serial.begin(1200,SERIAL_7E1);
I then wired the comms cable from the radio to pin 3 of the Atmega 328P chip which is TXD.

My understanding is this pin speaks UART natively.

The radio however is reporting that it is not seeing any reading when activated.

I have an XProtolab that I was trying to use as a UART sniffer, but I can't seem to get it to properly decode the signal from the Atmega.  The UNO can decode it fine and present the transmission in the serial monitor, but the Xprotolab only gets garbage.

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Help with XOR?
« Reply #8 on: January 15, 2013, 11:11:46 PM »
Guessing here, but you never know :)

Are you also sharing a ground between the radio or xprotolab and the ATMEGA ?

Did you confirm that the radio is expecting TTL level (0-5v), not RS-232 levels?



aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #9 on: January 16, 2013, 08:33:31 AM »
I do have them all sharing a ground and according to the manufacturer it is TTL 0-5V.

I did receive confirmation last night that the radio is compiled to expect 1200bps, 8,N,1 signal but they only look at the first 7 bits.  I'm not sure if this means I have to modify how I send my data or not. I switched the serial.begin line to just 1200 without the other parameters, but still don't get anything.

Gabriel from Gabotronics confirmed last night as well that the XProtolab will only decode UART at 8,N,1 so I have some more testing to do there.

Can you use an UNO without the chip as a TTL2USB converter if you connect the TX/RX pins to the breadboarded chip?

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Help with XOR?
« Reply #10 on: January 16, 2013, 04:01:35 PM »
Based on that spec, I'd think that you need to be sending 8,N,1, then letting them ignore the 8th bit.

I've got a USB saleae logic analyzer that should do it, you are welcome to use it if it would help.

Not sure about the Uno question...seems feasible, but never tried it and don't know what's going on there since they replaced the FTDI chip. You could also use a USB FTDI cable if you have one laying around...



aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #11 on: January 17, 2013, 08:40:07 AM »
Thanks, I have a friend with an Oscilliscope stopping by the office today so we can make sure the signal is clean and I don't have a grounding problem. I also have a couple of Max3232's on the way and should be here tomorrow. So hopefully I will make some headway here shortly.  I may still need to ask for advice later.

Also I figured out the 8,N,1 thing. They ignore the 8th bit because all the Ascii characters fit into the 1st 7 bits.

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #12 on: January 17, 2013, 02:16:14 PM »
OK, so made some headway.  Found out that the message can be decoded by the radio correctly and presented at the base station so I don't have to worry about that piece.  The problem is the chip isn't running the loaded sketch when on a breadboard and powered by the radio.  Power goes high, the TX pin on the chip goes high like it should, but a message is never transmitted.  If I power the chip separately and reset the arduino when the radio is expecting to hear a message everything works. This is done with the radio not providing power to the circuit.

1st thing I did was look at the voltage and it's 5.6vDC which is at the very top of what the chip is rated for. So I thought maybe is was too much voltage and  put a diode in-line which drops it to 4.86vDC. Same results.

Second I thought maybe I wasn't waiting for voltage to stabilize before trying to process and send the message so I increased my delay with the same result.

I'm kind of at a loss now.  I'm not sure if there is a reset issue going on, or if I'm missing something wiring wise.

aspazatak

  • FamiLAB Member
  • Jr. Member
  • *
  • Posts: 50
    • View Profile
Re: Help with XOR?
« Reply #13 on: January 24, 2013, 09:09:58 AM »
I got it working, turns out it required a crystal to run properly. All the stuff online shows breadboard projects with and without oscillators. Other posts say that it's only important for communications. Well for me it was required to get the sketch to run at all.

digitalman2112

  • Lab Dad (ex-treasurer)
  • FamiLAB Member
  • Hero Member
  • *
  • Posts: 1096
  • Work Hard, Play Hard!
    • View Profile
    • Raising Geeks
Re: Help with XOR?
« Reply #14 on: January 24, 2013, 10:35:17 AM »
This would make for an informative blog post if you have the time...