Guest User

Shift register reading

a guest
Apr 2nd, 2017
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #define CLOCK 2
  2. #define DATA 3
  3.  
  4. volatile int master_count = 0; // universal count
  5.  
  6. volatile uint8_t in_buffer = 0;
  7.  
  8. bool secondByte = false;
  9.  
  10. int readingOffset = 2;
  11.  
  12. const byte numTable[] =
  13. {
  14.   B00000011, //0
  15.   B11111001, //1
  16.   B11010010, //2
  17.   B11101000, //3
  18.   B10011001, //4
  19.   B01001001, //5
  20.   B01000001, //6
  21.   B11111000, //7
  22.   B10000000, //8
  23.   B11111000 //9
  24. };
  25.  
  26.  
  27. void setup() {
  28.    pinMode(DATA, INPUT_PULLUP);
  29.    pinMode(CLOCK,INPUT);
  30.    
  31.    Serial.begin(250000);
  32.    Serial.println("Ready for sniffing...");
  33. }
  34.  
  35. void loop() {
  36.  
  37.   delayMicroseconds(300);
  38.  
  39.   master_count = 0;
  40.   in_buffer = 0;
  41.  
  42.  attachInterrupt(digitalPinToInterrupt(CLOCK), interrupt, RISING);  
  43.  // interrupt 0 digital pin 2 positive edge trigger
  44.    
  45.    while(1)
  46.    {
  47.       if(master_count > 7)
  48.        {
  49.          detachInterrupt(digitalPinToInterrupt(CLOCK));
  50.          // digitalWrite(DATA, LOW);
  51.          readingOffset++;
  52.          if(readingOffset >= 3){
  53.             printBIN(in_buffer);
  54.             readingOffset = 0;
  55.          }        
  56.          in_buffer = 0;
  57.          master_count=0;
  58.          
  59.          //delay(3);
  60.          break;      
  61.      }        
  62.    }
  63. }
  64.  
  65.  
  66. void interrupt() {
  67.        boolean read = digitalRead(DATA);
  68.        noInterrupts();
  69.        if(read==true)bitSet(in_buffer,(7-master_count));
  70.        master_count++;
  71.        interrupts();      
  72. }
  73.  
  74. String digitOne = "";
  75. String digitTwo = "";
  76. bool shouldPrint = false;
  77. int printed = 0;
  78. //function that also prints leading zeros
  79. String temp = "";
  80. void printBIN(volatile uint8_t* binary)
  81. {
  82.  
  83.   for(int byteIndex = 0; byteIndex < 8; byteIndex++){      
  84.       if(numTable[byteIndex] == binary){
  85.         if(!secondByte){
  86.           secondByte = true;
  87.           if(byteIndex > 10){
  88.               //return;
  89.           }        
  90.                    
  91.           if(digitTwo != String(byteIndex, DEC)){
  92.             digitTwo = String(byteIndex, DEC);
  93.             shouldPrint = true;
  94.           }          
  95.         }else{          
  96.           secondByte = false;
  97.           if(digitOne != String(byteIndex, DEC) && digitTwo != String(byteIndex, DEC)){
  98.             digitOne = String(byteIndex, DEC);
  99.             shouldPrint = true;
  100.           }
  101.         }
  102.         if(shouldPrint){          
  103.           if(printed == 1){            
  104.             if(temp.length() >= 2){
  105.               temp = "";
  106.             }else{
  107.               temp += String(digitOne + digitTwo);
  108.             }            
  109.             printed = 0;
  110.           }else{
  111.             if(temp.length() == 2){
  112.               temp = "";
  113.             }else{
  114.               temp += String(digitOne + digitTwo);
  115.             }            
  116.             printed++;
  117.           }          
  118.           shouldPrint = false;
  119.           if(temp.length() == 2){
  120.               Serial.println(temp);
  121.               temp = "";
  122.           }else{
  123.           }
  124.         }            
  125.       }else{
  126.        
  127.         if(binary == B11111111){
  128.           return;        
  129.         }
  130.       }
  131.   }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment