2021年12月4日 星期六

8-13 dip

 int seg7_pin[] = {2, 3, 4, 5, 6, 7, 8};

int dip_sw[] = {9, 10, 11, 12};

int weight[] = {8, 4, 2, 1};

int seg7_table[] = {~0xc0, ~0xf9, ~0xa4, ~0xb0, ~0x99, ~0x92, ~0x82, ~0xf8, ~0x80, ~0x90,

                    ~0x88, ~0x8c, ~0xc6, ~0xa1, ~0x86, ~0x8e//寫是寫共陽,加個毛毛蟲(是bitwise),不是!(為真變假,假變真

                   };

int i;

int number;

void setup()

{

  for (int a = 0; a < 7; a++)

  {

    pinMode(seg7_pin[a], OUTPUT);

  }

  Serial.begin(9600);

}

void tx(byte rx)

{

  int x;

  for (x = 0; x < 7; x++)

  {

    if (rx % 2 == 1)

      digitalWrite(seg7_pin[x], HIGH);//板子是共陰,給高電位七段顯示器才會亮

    else

      digitalWrite(seg7_pin[x], LOW);

    rx = rx / 2;

  }

}

void loop()

{

  number = 0;

  for (i = 0; i < 4; i++)

  {

    if (digitalRead(dip_sw[i]) == HIGH)//    if (digitalRead(dip_sw[i] == HIGH))括號括錯了

      number = number + weight[i];//記得權值加中括號

      Serial.println(number);

  }

  tx(seg7_table[number]);

}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...