LuckyFace's Systems Lifescience

bit Math 본문

Systems Neuroscience/ARDUINO functions

bit Math

LuckyFace 2017. 4. 19. 14:58

// portD (pin 0-7)

#define odorPin0 2 // Normally-open valve that transmit fresh air

#define odorPin1 3 // cue 1

#define odorPin2 4 // cue 2

#define odorPin3 5 // cue 3

#define odorPin4 6 // cue 4

#define odorPin5 7 // final valve


// portB (pin 8-13)

#define rewardPin 1

#define punishmentPin 0

#define noRewardPin 2 // used for cheetah notification

#define sensorPin 4 // pin 12

#define laserPin 5 // pin 13



 // Pin setup

    DDRD = B11111110; // Pin 7, 6, 5, 4, 3, 2, 1, 0. Pin 0 and 1 is reserved for serial communication.

    DDRB = B00101111; // Pin xx, xx, 13, 12, 11, 10, 9, 8. 

    PORTD &= B00000011; // reset pin 2-7

    PORTB &= B11010000; // reset pin 8-11 and 13 (pin 12 is sensor)

}


   아두이노 코드(혹은 C++)에서 bit math라는게 있는데..

1.  8개의 true or false data를 하나의 바이트로 저장하고 싶거나

2.  각각의 독립된 비트를 켜고 끄는 컨트롤을 하고 싶거나 (꺄아)

3.  2의 제곱으로 이뤄진 산술적 계산을 하고 싶거나...


이럴때 쓰면 좋다고 되어있다. 우리의 경우 수많은 solenoid valve를 크고 꺼야 하기 때문에 2의 이유로 적합한 상태!


처음에 정의한대로 portD는 2~7까지 각각 valve가 설정되어있고, (0과 1은 serial통신을 위해 빠져 있음)

PortB 는 0부터 5까지 설정되어있다.



자 첫번째 Bitwise AND 는 binary로 표시했을때 각 자리가 1로 일치하면 1로 output하고 그렇지 않으면 0으로 output하는 함수!


 int a =  92;    // in binary: 0000000001011100
 int b = 101;    // in binary: 0000000001100101
 int c = a & b;  // result:    0000000001000100, or 68 in decimal.


예를 들면 위와 같은 것. Bitwise OR 은 | 이 기호로 사용함.


bitwise XOR이라는 것도 있음. exclusive OR이란 뜻! 이것은 비교하는 두개의 bit이 다른 경우에 1을 변환. 함수는 ^을 씀.


  int x = 12;     // binary: 1100
  int y = 10;     // binary: 1010
  int z = x ^ y;  // binary: 0110, or decimal 6

bitwise NOT도 있음. 이것은 0을 1로, 1을 0으로 바꿔주는 것, ~ 기호로 사용한다.


bit Shift operator도 있네. 이것은 binary code를 좌우로 옮겨주는 역할


    int a = 5;        // binary: 0000000000000101
    int b = a << 3;   // binary: 0000000000101000, or 40 in decimal
    int c = b >> 3;   // binary: 0000000000000101, or back to 5 like we started with


얘를 이용해도 재밌는 코드를 짤수 있겠네.


C++에서는 이걸 짧게 변환하는 코드를 제공함


    int x = 1;  // binary: 0000000000000001
    x <<= 3;    // binary: 0000000000001000
    x |= 3;     // binary: 0000000000001011 - because 3 is 11 in binary
    x &= 1;     // binary: 0000000000000001
    x ^= 4;     // binary: 0000000000000101 - toggle using binary mask 100
    x ^= 4;     // binary: 0000000000000001 - toggle with mask 100 again


따라서 우리코드에 있는 &=는 bitwise and 였던 것! 



아두이노 홈페이지에서 제공하는 예제를 살펴보자!


만약 digital pin 2~13까지를 ouput으로 하고

그중에 11,12,13만 high라고 하고 다른 pin을 low로 하고 싶다면


void setup()
    {
        int pin;
        for (pin=2; pin <= 13; ++pin) {
            pinMode (pin, OUTPUT);
        }
        for (pin=2; pin <= 10; ++pin) {
            digitalWrite (pin, LOW);
        }
        for (pin=11; pin <= 13; ++pin) {
            digitalWrite (pin, HIGH);
        }
    }

이렇게 하겠지. ++pin은 pin=pin+1을 줄인것

pinMode는 각 pin의 숫자를 output혹은 input으로 설정할수 있게 해주는 함수다. 즉, pinMode(13, OUTPUT) 이렇게 하면 13번 pin을 ouput으로 설정하겠다는 이야기. digitalWrite은 은 그 pin의 값을 0 혹은 1로 만들어주는 함수. 하지만 이 함수를 control register 함수인 DDRD, DDRB를 이용하면 다음과 같이 표현이 가능하다.


  void setup()
    {
        // set pins 1 (serial transmit) and 2..7 as output,
        // but leave pin 0 (serial receive) as input
        // (otherwise serial port will stop working!) ...
        DDRD = B11111110;  // digital pins 7,6,5,4,3,2,1,0
        // set pins 8..13 as output...
        DDRB = B00111111;  // digital pins -,-,13,12,11,10,9,8
        // Turn off digital output pins 2..7 ...
        PORTD &= B00000011;   // turns off 2..7, but leaves pins 0 and 1 alone
        // Write simultaneously to pins 8..13...
        PORTB = B00111000;   // turns on 13,12,11; turns off 10,9,8
    }
DDRD로 하면 D port에서 digital pin 1부터 7까지를  output(1값), 0은 input, DDRB로 가면 8부터 13까지를 모두 output으로 사용하겠다는 말이 됨.

그 뒤에 PORTD/PORTB함수로 초기 setting을 해주면 된다.

이렇게 해야하는 중요한 이유중 하나가 digitalWrite(10,high) 하고 digitalWrite(11,high)이렇게 하면 두 output신호간의 시간차가 발생하게 되지만 PORTB |= B00001100 이렇게 해주면 동시에 켜지게 됨!!! 

'Systems Neuroscience > ARDUINO functions' 카테고리의 다른 글

State conversion in trials  (0) 2017.04.19
sensor  (0) 2017.04.19
Serial 통신(2)  (0) 2017.04.19
void setup과 Serial 통신  (0) 2017.04.18
boolean and String, char  (0) 2017.04.18
Comments