今日やること: 7セグメントLEDを4つ使って、カウントアップするカウンタをつくります。
キーワード: C言語、GPIO、wiringPi、7セグメントLED、ラズベリーパイ、ビギナーズボード、Rabyy
ハードウェア
こちら↓のように、ハードウェアを接続します。
[回路図]
(*ビギナーズボードを使えばハードウェアの配線は不要です。)
ちなみに以前、ラズパイ+ブレッドボードで上の回路を組んだことがあります。その時の様子は以下。
ごちゃごちゃ(´・ω・`)
ソフトウェア
今回もC言語でプログラムを書きます。
以下のプログラムのソースコードをコピペして、適当なところに適当な名前で保存します。
ここでは /home/pi/sample_programs_woodbox/ というディレクトリに countup_7seg.c という名前で保存するとしましょう。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
/***************************************************************************** * Description: 7セグメントLEDでアップカウントターをつくる * Last Update: 2015/10/01 mariko.N @woodbox (thanks to hiroshi.K) * * This software is a free software and there is NO WARRANTY. * サンプルなのでご自由にどうぞ。ただし保証はしませんm(。・ω・。)m *****************************************************************************/ #include <stdio.h> #include <wiringPi.h> #include <pthread.h> #define PINS_LEN 8 #define DIGIT_LEN 4 const int VIEWS_LEN = 11; //GPIO number (A, B, C, D, E, F, G, D.P) const int pins[PINS_LEN] = {16, 18, 20, 22, 23, 17, 19, 21}; const int digit[DIGIT_LEN] = {12, 13, 14, 15}; //LED setting (A, B, C, D, E, F, G, D.P) const int number_views[14][8] = { {1, 1, 1, 1, 1, 1, 0, 0}, // 0 {0, 1, 1, 0, 0, 0, 0, 0}, // 1 {1, 1, 0, 1, 1, 0, 1, 0}, // 2 {1, 1, 1, 1, 0, 0, 1, 0}, // 3 {0, 1, 1, 0, 0, 1, 1, 0}, // 4 {1, 0, 1, 1, 0, 1, 1, 0}, // 5 {1, 0, 1, 1, 1, 1, 1, 0}, // 6 {1, 1, 1, 0, 0, 1, 0, 0}, // 7 {1, 1, 1, 1, 1, 1, 1, 0}, // 8 {1, 1, 1, 1, 0, 1, 1, 0}, // 9 {0, 0, 0, 0, 0, 0, 0, 1}, // dot {1, 1, 0, 0, 0, 1, 1, 0}, // ueO {0, 0, 1, 1, 1, 0, 1, 0}, // shitaO {0, 0, 0, 0, 0, 0, 0, 0} // none }; int seg7[DIGIT_LEN]; int dot = 0; void *updateView(void *); /***************************************************************************** * Function: main *****************************************************************************/ int main(void) { time_t timer; struct tm *local; int i = 0; int count = 0; //wiringPi initialize if (wiringPiSetupGpio() < 0) return 1; //set digits output for (i = 0 ; i < DIGIT_LEN ; i++) { pinMode(digit[i], OUTPUT); digitalWrite(digit[i], LOW); } //set pins output for (i = 0 ; i < PINS_LEN ; i++) { pinMode(pins[i], OUTPUT); } pthread_t thread1; pthread_create(&thread1, NULL, updateView, (void *)NULL); dot=2; while(1) { //set the digit3 seg7[3] = count % 10; //set the digit2 seg7[2] = (count / 10) % 10; //set the digit1 seg7[1] = (count / 100) % 10; //set the digit0 seg7[0] = (count / 1000) % 10; //wait 100ms delay(100); count++; } return 0; } /***************************************************************************** * Function: updateView * Description: update 7segment LED *****************************************************************************/ void *updateView(void *args) { static int currdigit; int i; while(1){ //turn off all digit for(i = 0; i < DIGIT_LEN; i++){ digitalWrite(digit[i], LOW); } //turn on selected pins for (i=0; i<PINS_LEN; i++) { if (number_views[seg7[currdigit]][i] == 1) { digitalWrite(pins[i], HIGH); } else { digitalWrite(pins[i], LOW); } //turn on dot if(currdigit == dot){ digitalWrite(pins[7], HIGH); } } //turn on selected digit digitalWrite(digit[currdigit], HIGH); //currdigit currdigit = (currdigit+1) & 0x03; delay(2); } } |
プログラムをコンパイルして実行します。
1 2 |
pi@raspberrypi ~ $ gcc -o countup_7seg countup_7seg.c -lwiringPi -lpthread pi@raspberrypi ~ $ sudo ./countup_7seg |
7セグメントLEDがカウントを開始しました。
お疲れ様です。
おまけ
1.ダイナミック点灯
今回、7セグメントLEDを4個使うにあたり、「ダイナミック点灯」ということを行ってます。
4ケタの数字、同時に点灯しているように見えますが、実は、人間の目には見えない速さで1ケタずつ順番に表示しています。残像が見えてるのですね。GPIOの数と消費電力を節約するための知恵です。
もっと詳しく書きたいのですが、力尽きてきたので続きは「ダイナミック点灯」でググってください(´・ω・`)
2.7セグメントLEDを選ぶときに注意すること
アノードコモンか、カソードコモンか。データシートを見てこれだけは必ずチェックしてください。
間違えると動きません。さらに端子が多いので半田付けした後に発覚すると面倒くさいことになります。
【参考】
半田付け日記 Rabyyで7セグLEDを光らせる: https://ryusakura.wordpress.com/page/2/
以上!