arduino의 계산기

4918 단어 계산기Arduino

개요



arduino에서 계산기, 시도했다.
8bit이므로 255까지
구구는 OK.

사진





샘플 코드


#define stb         10
#define clk         9
#define dio         8
uint8_t a = 0;
uint8_t b = 0;
uint8_t c = 0;

void printn(uint8_t v) {
    uint8_t w = v / 100;
    uint8_t x = (v - w * 100) / 10;
    uint8_t y = v % 10;
    uint8_t t;
    uint8_t a[] = {1, 0, 1, 1, 0, 1, 0, 1, 1, 1};
    uint8_t b[] = {1, 1, 1, 1, 1, 0, 0, 1, 1, 1};
    uint8_t c[] = {1, 1, 0, 1, 1, 1, 1, 1, 1, 1};
    uint8_t d[] = {1, 0, 1, 1, 0, 1, 1, 0, 1, 0};
    uint8_t e[] = {1, 0, 1, 0, 0, 0, 1, 0, 1, 0};
    uint8_t f[] = {1, 0, 0, 0, 1, 1, 1, 0, 1, 1};
    uint8_t g[] = {0, 0, 1, 1, 1, 1, 1, 0, 1, 1};
    uint8_t h[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    digitalWrite(stb, LOW);
    shiftOut(dio, clk, LSBFIRST, 0x40);
    digitalWrite(stb, HIGH);
    digitalWrite(stb, LOW);
    shiftOut(dio, clk, LSBFIRST, 0xc0);
    t = a[y] << 4;
    if (v > 9) t = t | a[x] << 5;
    if (v > 99) t = t | a[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = b[y] << 4;
    if (v > 9) t = t | b[x] << 5;
    if (v > 99) t = t | b[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = c[y] << 4;
    if (v > 9) t = t | c[x] << 5;
    if (v > 99) t = t | c[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = d[y] << 4;
    if (v > 9) t = t | d[x] << 5;
    if (v > 99) t = t | d[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = e[y] << 4;
    if (v > 9) t = t | e[x] << 5;
    if (v > 99) t = t | e[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = f[y] << 4;
    if (v > 9) t = t | f[x] << 5;
    if (v > 99) t = t | f[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = g[y] << 4;
    if (v > 9) t = t | g[x] << 5;
    if (v > 99) t = t | g[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    t = h[y] << 4;
    if (v > 9) t = t | h[x] << 5;
    if (v > 99) t = t | h[w] << 6;
    shiftOut(dio, clk, LSBFIRST, t);
    shiftOut(dio, clk, LSBFIRST, 0x01);
    digitalWrite(stb, HIGH);
}
void setup() {
    Serial.begin(9600);
    pinMode(stb, OUTPUT);
    pinMode(clk, OUTPUT);
    pinMode(dio, OUTPUT);
    digitalWrite(stb, LOW);
    shiftOut(dio, clk, LSBFIRST, 0x88);
    digitalWrite(stb, HIGH);
    printn(0);
    Serial.print("ok");
}
void loop() {
    uint8_t v;
    uint8_t btn = 20;
    digitalWrite(stb, LOW);
    shiftOut(dio, clk, LSBFIRST, 0x42);
    pinMode(dio, INPUT);
    v = shiftIn(dio, clk, LSBFIRST);
    if (v == 0x02)
    {
        btn = 1;
    }
    else if (v == 0x04)
    {
        btn = 7;
    }
    else if (v == 0x20)
    {
        btn = 2;
    }
    else if (v == 0x40)
    {
        btn = 8;
    }
    v = shiftIn(dio, clk, LSBFIRST);
    if (v == 0x02)
    {
        btn = 3;
    }
    else if (v == 0x04)
    {
        btn = 9;
    }
    else if (v == 0x20)
    {
        btn = 12;
    }
    else if (v == 0x40)
    {
        btn = 10;
    }
    v = shiftIn(dio, clk, LSBFIRST);
    if (v == 0x02)
    {
        btn = 0;
    }
    else if (v == 0x04)
    {
        btn = 4;
    }
    else if (v == 0x20)
    {
        btn = 15;
    }
    else if (v == 0x40)
    {
        btn = 5;
    }
    v = shiftIn(dio, clk, LSBFIRST);
    if (v == 0x02)
    {
        btn = 14;
    }
    else if (v == 0x04)
    {
        btn = 6;
    }
    else if (v == 0x20)
    {
        btn = 13;
    }
    else if (v == 0x40)
    {
        btn = 11;
    }
    pinMode(dio, OUTPUT);
    digitalWrite(stb, HIGH);
    if (btn < 10)
    {
    if (b > 9)
    {
      c = c * 10 + btn;
      printn(c);
    }
    else
    {
          a = a * 10 + btn;
          printn(a);
    }
    }
    else if (btn == 15)
    {
        a = 0;
    b = 0;
    c = 0;
        printn(a);
    }
  else if (btn == 10)
  {
    b = 10;
    printn(0);
  }
  else if (btn == 11)
  {
    b = 11;
    printn(0);
  }
  else if (btn == 12)
  {
    b = 12;
    printn(0);
  }
  else if (btn == 13)
  {
    b = 13;
    printn(0);
  }
  else if (btn == 14)
  {
    if (b == 10)
    {
      a = a + c;
    }
    else if (b == 11)
    {
      a = a - c;
    }
    else if (b == 12)
    {
      a = a / c;
    }
    else if (b == 13)
    {
      a = a * c;
    }
    printn(a);
    a = 0;
    b = 0;
    c = 0;
  }
    delay(300);
}






이상.

좋은 웹페이지 즐겨찾기