Created
March 20, 2021 22:58
-
-
Save Tamakichi/f7a19fd0bd817cbbca5085e5f7fb9c76 to your computer and use it in GitHub Desktop.
Raspberry Pi Pico 7セグ4桁ダイナミック表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 4桁高輝度緑色7セグメントLED表示器(アノードコモン) OSL40562-IG の制御 | |
// 2021/03/20 たま吉さん | |
// 利用パーツ | |
// 4桁7セグメントLED OSL40562-IG x 1 | |
// 桁制御用トランジスタ 2SA1015L-GR x 4 | |
// LED電流制御用抵抗 330Ω x 8 | |
// トランジスタベース抵抗 10kΩ x 4 | |
// | |
#include <stdio.h> | |
#include "pico/stdlib.h" | |
// タイマー割り込み間隔 | |
#define INTERVAL 4 | |
// デジタルピンの定義 | |
#define DIG1 2 | |
#define DIG2 3 | |
#define DIG3 4 | |
#define DIG4 5 | |
#define SEGA 6 | |
#define SEGB 7 | |
#define SEGC 8 | |
#define SEGD 9 | |
#define SEGE 10 | |
#define SEGF 11 | |
#define SEGG 12 | |
#define SEGP 13 | |
// 桁表示ON・OFF | |
#define DIG_ON 0 | |
#define DIG_OFF 1 | |
// セグメント表示ON・OFF | |
#define SEG_ON 0 | |
#define SEG_OFF 1 | |
// セグメントビット定義 | |
#define BIT_A 0b10000000 | |
#define BIT_B 0b01000000 | |
#define BIT_C 0b00100000 | |
#define BIT_D 0b00010000 | |
#define BIT_E 0b00001000 | |
#define BIT_F 0b00000100 | |
#define BIT_G 0b00000010 | |
#define BIT_P 0b00000001 | |
// フォントの定義 | |
const uint8_t font[12] = { | |
BIT_A|BIT_B|BIT_C|BIT_D|BIT_E|BIT_F, // 0 | |
BIT_B|BIT_C, // 1 | |
BIT_A|BIT_B|BIT_G|BIT_E|BIT_D, // 2 | |
BIT_A|BIT_B|BIT_G|BIT_C|BIT_D, // 3 | |
BIT_F|BIT_B|BIT_G|BIT_C, // 4 | |
BIT_A|BIT_F|BIT_G|BIT_C|BIT_D, // 5 | |
BIT_A|BIT_F|BIT_E|BIT_D|BIT_C|BIT_G, // 6 | |
BIT_A|BIT_B|BIT_C, // 7 | |
BIT_A|BIT_B|BIT_C|BIT_D|BIT_E|BIT_F|BIT_G, // 8 | |
BIT_A|BIT_B|BIT_C|BIT_D|BIT_F|BIT_G, // 9 | |
0, // ブランク | |
BIT_G, // - | |
}; | |
// 桁制御ピン | |
const uint8_t pin_dig[4] = { | |
DIG1, DIG2, DIG3, DIG4, | |
}; | |
// セグメント制御ピン | |
const uint8_t pin_seg[8] = { | |
SEGA, SEGB, SEGC, SEGD, SEGE, SEGF, SEGG, SEGP, | |
}; | |
// 表示用データ | |
volatile uint8_t digit[4]; | |
volatile uint8_t cnt = 0; | |
// 指定桁の表示 | |
void showAt(uint8_t n) { | |
// 全桁の消去 | |
for (uint8_t i = 0; i < 4; i++) { | |
gpio_put(pin_dig[i], n==i ? DIG_ON:DIG_OFF); | |
} | |
//gpio_put(pin_dig[n], DIG_ON); | |
// n桁のセグメントへの出力 | |
for (uint8_t i = 0; i < 8; i++) { | |
gpio_put(pin_seg[i], digit[n] & (0x80>>i) ? SEG_ON:SEG_OFF); | |
} | |
} | |
// タイマー割り込み呼び出し関数 | |
bool repeating_timer_callback(struct repeating_timer *t) { | |
showAt(cnt); | |
cnt++; | |
if (cnt == 4) { | |
cnt = 0; | |
} | |
return true; | |
} | |
// データのクリア | |
void cls() { | |
digit[0] = 0; | |
digit[1] = 0; | |
digit[2] = 0; | |
digit[3] = 0; | |
} | |
// 数値の設定 | |
void setNumber(uint16_t n, uint8_t dt) { | |
if (n>9999) | |
return; | |
cls(); | |
uint8_t dec = 0; | |
// 4桁目 | |
if (n >= 1000) { | |
digit[0] =font[ n / 1000]; | |
n = n % 1000; | |
dec = 1; | |
} | |
// 3桁目 | |
if (n >= 100) { | |
digit[1] = font[n / 100]; | |
n = n % 100; | |
dec = 1; | |
} else if (dec) { | |
digit[1] = font[0]; | |
} | |
// 2桁目 | |
if (n >= 10) { | |
digit[2] = font[n / 10]; | |
n = n % 10; | |
dec = 1; | |
} else if(dec) { | |
digit[2] = font[0]; | |
} | |
// 1桁目 | |
digit[3] = font[n]; | |
// 小数点 | |
if(dt && dt <=4) { | |
digit[4-dt] |= BIT_P; | |
} | |
} | |
int main() { | |
stdio_init_all(); | |
// 桁ピンの初期化 | |
for (uint8_t i = 0; i < 4; i++) { | |
gpio_init(pin_dig[i]); | |
gpio_set_dir(pin_dig[i], GPIO_OUT); | |
gpio_put(pin_dig[i], DIG_OFF); | |
} | |
// セグメントピンの初期化 | |
for (uint8_t i = 0; i < 8; i++) { | |
gpio_init(pin_seg[i]); | |
gpio_set_dir(pin_seg[i], GPIO_OUT); | |
gpio_put(pin_seg[i], SEG_OFF); | |
} | |
// 表示データの初期化 | |
cls(); | |
// LEDダイナミック点灯用割り込み設定&タイマー割り込み実行開始 | |
repeating_timer timer; | |
add_repeating_timer_ms(INTERVAL, repeating_timer_callback, NULL, &timer); | |
uint16_t n = 0; | |
while(1) { | |
setNumber (n, (uint8_t)(n%10 ? 0:1)); | |
sleep_ms(200); | |
n++; | |
if (n>9999) n = 0; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment