0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ジャンクのカウンター基板

0
Last updated at Posted at 2026-11-17

どっかで買ったパーツのジャンク袋にカウンター基板が入っていました。なんらかの機械に組み込まれていたものを取り外したものかもしれません。

写真(2026-11-16 6.45) #2.jpg

10年くらい部品箱の肥やしになっていました。

使われているマイコンはAT90S1200で初期のAVRです。Flashは1Kバイトです。7セグはLucky Light ElectronicsのKW4-561ASAです。トランジスタはBC547でした。

ATTINY2313が互換チップで手持ちがあったので、これでI2Cで制御できるようにしてみました。環境はPlatformIOのCLIです。

#include <Wire.h>

#define I2C_ADDR 0x20

volatile uint8_t digits[4] = {0, 0, 0, 0};
uint8_t currentDigit = 0;

void receiveEvent(int howMany) {
  uint8_t addr = Wire.read();
  if (addr > 4)
    addr = 0;
  for (uint8_t i = addr; i < 4 && Wire.available(); i++) {
    digits[i] = Wire.read();
  }
  while(Wire.available())
    Wire.read();
}

void setup() {
  // 桁選択を出力に設定
  DDRD |= 0b01100101;
  PORTD |= 0b01100101; // PNPトランジスタOFF(HIGH) & dp消灯(HIGH)

  DDRB |= 0b01011111;
  PORTB |= 0b010111111;
  DDRA |= 0b0000011;
  PORTA |= 0b00000011;

  Wire.begin(I2C_ADDR);
  Wire.onReceive(receiveEvent);

  // Timer0 CTCモード設定 (約2msごとに割り込み)
  TCCR0A = (1 << WGM01);
  TCCR0B = (1 << CS01) | (1 << CS00); // 分周比 64 (8MHz動作時)
  OCR0A = 200;
  TIMSK |= (1 << OCIE0A);
}

char keta[4] = {0, 2, 6, 5};

// ダイナミック点灯用タイマー割り込み
ISR(TIMER0_COMPA_vect) {
  // 1. ゴースト防止のため全桁OFF (PB0-PB3をHIGHに)
  PORTD |= 0b01100101;

  // 2. 次の桁のセグメントデータをPD0-PD6へ出力
  uint8_t pattern = ~digits[currentDigit];
  PORTB = (pattern & 0b01011111) | 0b10100000;
  PORTA = ((pattern >> 7) & 1) | ((pattern >> 4) & 2);

  // 3. 該当桁のPNPトランジスタをON (LOW)
  PORTD &= ~(1 << keta[currentDigit]);

  currentDigit = (currentDigit + 1) & 0x03;
}

void loop() {
}

数か所パターンをカットしてつなぎなおしました。ATTINY2313はデフォルトで8MHzで動いているものと思ったのですが、8分周されて1MHzな事をすっかり忘れていてはまりました。fuseを焼き直して、8分周を外しました。

I2Cの19,17ピンは7segにつながれていたので、未使用の4,5ピンに変えました。

Timer0の割り込みでダイナミック点灯しているのですが、うまく動きません。PORTBの設定の時にI2Cのポートは1にする必要がありました。

ドットはもともと接続されてませんでしたが、制御できるようにしました。

I2Cのプロトコルは先頭がアドレスで、後はビットパターンです。並びが一般的な物と違います。

7SEG.png

FreeBSD/mipsのmrubyから操作してみます。

t = BsdIic.new(0)

i = 0
loop do
  t.write(0x20, [0x00, 1 << i, 2 << i, 4 << i,8 << i])
  i = (i  + 1) & 7
  sleep 1
end

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?