0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ATtinyなどで、74HC595などに対してshiftOut関数をレジスタ操作で再現する。

Posted at

ATtinyでshiftOutなどをレジスタ操作でどうにかする備忘録です。7セグメントLEDなどの制御にどうぞ。

コード

ラッチ・クロック・データピンは全部同一のPORTのピンに配置し、S_OUT_PORTにはそのPORTを記述してください。同一のPORTにできないなら、コードをいくらか書き足す必要があります。

Arduino
#define S_OUT_PORT PORTA

#define latchPin   PA0
#define clockPin   PA1
#define dataPin    PA2

// 略

void hc595_shiftout(uint8_t data) {
  // latchPinをLOWにする
  S_OUT_PORT &= ~(1 << latchPin);
  for (int i = 7; i >= 0; i--) {
    if (data & (1 << i)) {
      // dataPinをHIGHにする
      S_OUT_PORT |= (1 << dataPin);
    } else {
      // dataPinをLOWにする
      S_OUT_PORT &= ~(1 << dataPin);
    }
    // clockPinをHIGHにする
    S_OUT_PORT |= (1 << clockPin); 
    // clockPinをLOWにする
    S_OUT_PORT &= ~(1 << clockPin);
  }
  // latchPinをHIGHにする
  S_OUT_PORT |= (1 << latchPin); 
}

// こんな感じに...
hc595_shiftout(0b00001001);

PORTAの他のピン(PA3など)には影響はないので、特に気にせず使用できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?