Pages: [1]   Go Down
Author Topic: BINARY OUTPUT FORMAT  (Read 237 times)
0 Members and 1 Guest are viewing this topic.
Spain
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello, sorry for my ignorance, but I have little time working with arduino.

Quote
/**
* Read a card using a mfrc522 reader on your SPI interface
* Pin layout should be as follows (on Arduino Uno):
* MOSI: Pin 11 / ICSP-4
* MISO: Pin 12 / ICSP-1
* SCK: Pin 13 / ISCP-3
* SS: Pin 10
* RST: Pin 9
*/

#include <SPI.h>
#include <RFID.h>

#define SS_PIN 10
#define RST_PIN 9

RFID rfid(SS_PIN, RST_PIN);

// Setup variables:
    int serNum0;
    int serNum1;
    int serNum2;
    int serNum3;
    int serNum4;

void setup()
{
  Serial.begin(9600);
  delay(1000);
  SPI.begin();
  rfid.init();
}

void loop()
{
   
    if (rfid.isCard()) {
        if (rfid.readCardSerial()) {
            if (rfid.serNum[0] != serNum0
                && rfid.serNum[1] != serNum1
                && rfid.serNum[2] != serNum2
                && rfid.serNum[3] != serNum3
            ) {
                /* With a new cardnumber, show it. */
                Serial.println(" ");
                Serial.println("Card found");
                serNum0 = rfid.serNum[0];
                serNum1 = rfid.serNum[1];
                serNum2 = rfid.serNum[2];
                serNum3 = rfid.serNum[3];
               
                //Serial.println(" ");
                Serial.println("Cardnumber:");
                Serial.print("HEX: ");
      Serial.print(rfid.serNum[0],HEX);
                Serial.print(", ");
      Serial.print(rfid.serNum[1],HEX);
                Serial.print(", ");
      Serial.print(rfid.serNum[2],HEX);
                Serial.print(", ");
      Serial.print(rfid.serNum[3],HEX);
                Serial.println(" ");
                       
                Serial.println("BIN: ");
      Serial.println(rfid.serNum[0],BIN);
      Serial.println(rfid.serNum[1],BIN);
      Serial.println(rfid.serNum[2],BIN);
      Serial.println(rfid.serNum[3],BIN);
                Serial.println(" ");

             }
            else { }
          }
    }
//    rfid.halt();
}

I have this code to read the serial number of an RFID card and I need to format the output in CODE BINARI four characters. That is, for the output:

Card found
cardNumber:
HEX: 1E, D, 82, FE
BIN:
11110
1101
10000010
11111110

I would need:

BIN:
00011110
00001101
10000010
11111110

Someone can help me ..

thanks
Logged

Victoria, Australia
Offline Offline
Edison Member
*
Karma: 46
Posts: 1557
Electronics Engineer/Industrial Control
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.

http://forum.arduino.cc/index.php?topic=137395.0

http://forum.arduino.cc/index.php?topic=43791.0

https://github.com/l0ad/arduino-binaryconverter

Hope this helps.

Tom... smiley
Logged

Everything runs on smoke, let the smoke out, it stops running......VK3DMK

Spain
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Tom for your quick response, but can not find the solution in your post.
Once converted to binary I need four bits for each binary number (with leading zeros).
Logged

Norfolk UK
Offline Offline
Faraday Member
**
Karma: 69
Posts: 2557
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is a subroutine I use for displaying OOK data from RF remote controls. maybe you can adapt it to your needs.
Code:
void dec2binLong(unsigned long myNum, byte NumberOfBits) {
  if (NumberOfBits <= 32){
    myNum = myNum << (32 - NumberOfBits);
    for (int i=0; i<NumberOfBits; i++) {
      if (bitRead(myNum,31) == 1)
      Serial.print("1");
      else
      Serial.print("0");
      myNum = myNum << 1;
    }
  }
}
Logged


Online Online
God Member
*****
Karma: 57
Posts: 934
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

These functions are based on one of the examples in Tom's post.

Code:
void printBinaryNibble(byte value)
{
    value = value & 0b00001111;
    for (byte mask = 0x08; mask; mask >>= 1)
    {
        Serial.print((mask & value) ? '1' : '0');
    }
}

void printBinaryByte(byte value)
{
    for (byte mask = 0x80; mask; mask >>= 1)
    {
        Serial.print((mask & value) ? '1' : '0');
    }
}

void printBinaryUnsignedInt(unsigned int value)
{
    for (unsigned int mask = 0x8000; mask; mask >>= 1)
    {
        Serial.print((mask & value) ? '1' : '0');
    }
}

void setup()
{
   
    Serial.begin(115200);
    printBinaryNibble(0x7);
    Serial.println();
    printBinaryByte(0x97);
    Serial.println();
    printBinaryUnsignedInt(0x1997);
    Serial.println();
}

void loop()
{
   
}
Logged

Hackscribble.  Writing about making things.
arduino@hackscribble.com | www.hackscribble.com

Spain
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you very much for your help. It's just what I needed ...
Logged

Shelby Township, Disco, Michigan
Offline Offline
God Member
*****
Karma: 21
Posts: 724
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Short way to do it.
Code:
// Binary_Output

int value ;

void setup() {
  Serial.begin(9600);
  //   just cycle one time
  for (value = 0; value < 256; value++)
  {
    for (int j = 7; j >= 0; j--)
    {
      Serial.print(bitRead(value, j));
    }
    Serial.print(" ");
    Serial.println(value);
  }

}

void loop()
{
  value = 0;
}



Output:
00000001 1
00000010 2
00000011 3
00000100 4
00000101 5
00000110 6
00000111 7
00001000 8
00001001 9
00001010 10
00001011 11
00001100 12
00001101 13
00001110 14
00001111 15
00010000 16
00010001 17
00010010 18
00010011 19
00010100 20
00010101 21
00010110 22
« Last Edit: June 14, 2014, 10:43:55 am by steinie44 » Logged

Spain
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you steinie44
Logged

Pages: [1]   Go Up
Jump to: