Advertisement
uas_arduino

Untitled

Jan 25th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Accepts 8 bits of data and outputs each bit to it's corresponding pin (pins array)
  2. void writeByte(byte data, byte[] pins){
  3.     // Loop through each of the 8 bits (start at zero, end at 7)
  4.     for(byte a = 0; a < 8; a++){
  5.         // shift the data to the right by 'a' bits and check to see if the first bit is set
  6.         // consider if data = 0x75 (01110101 in binary)
  7.         // the first iteration of the for loop will shift 01110101 by 0 (since a is 0 to start with)
  8.         //   since the first bit is a 1, pins[0] will output a HIGH
  9.         // the second iteration of the for loop will shift 01110101 by 1 (since a is now 1)
  10.         //   after the shift, the data will be 0111010 (notice there are now only 7 bits!)
  11.         //   since the first bit is now a 0, pins[1] will output a LOW
  12.         if((data >> a) & 0x01){
  13.             digitalWrite(pins[a], HIGH);
  14.         }else{
  15.             digitalWrite(pins[a], LOW);
  16.         }
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement