Show a number on a 7-segment LED display

back to main tutorial page

Dr Nathan Scott & Dr Hiroyuki Kagawa · July 2002

You should now have an AVR set up on a breadboard. In this section I will show you how to connect a 7-segment LED display to the AVR so that the AVR can show human users a decimal number. I will show you two ways to do this, first by directly connecting the AVR to the LED unit; and then (in the next tutorial) using a special purpose "LED driver" chip, which uses fewer of the available data pins on the AVR.

7セグLEDによる数字表示

戻る

ネーサン・スコット(訳:香川博之)  · 20027

これまでの作業でAVRをブレッドボードに取り付けることができた。ここでは,7セグLEDAVRにどのように接続するかを示す。これにより10進数を表示することができるようになる。まず,そのLEDAVRに直接接続する方法について説明する。次章ではAVRの数本の端子を利用するだけですむLED表示専用ICを使う方法について説明する。


7-segment LED units

A 7-segment LED unit is a group of 7 independant LEDs in one package. The LEDs can be turned on by an external circuit so that various patterns are displayed. They can show all the digits and certain letters of the Roman alphabet, for example "E", "H", "L" and "P" - so you could use several displays side-by-side to show "3215", "HELP" or "HELLO". LED displays are seen on some digital clocks, they are good because they can be read in a dark room. It may be difficult to read them in bright daylight, however.

Most LED display units have an 8th LED which is for the decimal point in a number.

Direct connection method

The AVR data pins can deliver up to 100mA to a load, and are powerful enough to drive an LED directly, as you have seen. A quick way to connect the AVR to the LED display is to drive each of the 8 LEDs from 8 data pins on the AVR. The AVR has 4 data ports, all of which have 8 pins and can be used for digital input and output (I/O), so we can drive up to 4 "digits" this way.

  • Implement the circuit shown in Figure 1 on your breadboard


7セグLEDユニット

7セグLEDユニットは一つのパッケージの中に独立した7個のLEDをもっている各LEDは外部電流によって点灯させることができるため,様々なパターンを表示できる。全ての数字と特定のアルファベット(例えばEHLP)を表示できるので,横に並べると「3215」,「HELP」や「HELLO」の表示ができる。LED表示器はデジタル時計に使われていたりする。暗い部屋でも読むことができるので,いい表示器である。しかし,明るい部屋では読みにくいかもしれない。

多くのLED表示ユニットは小数点を表示するために8個のLEDをもっている。

Direct connection method

AVRのデータ端子は負荷に対して100mAまでの電流を供給できるため,LEDを直接駆動することができる。AVRLED表示器を簡単に接続する方法は,表示機内の8個のLEDをそれぞれAVRの8個のデータ端子に接続することである。デジタルIOを利用できるので,この方法で4桁までの表示が可能になる。

  • ブレッドボード上で図1のように配線する。

Figure 1 Direct connection between the AVR and a common-anode 7-segment LED display such as the Agilent HDSP-5501

  • Now open the project "seven_seg.prj" using ICCAVRIDE. The source file "seven_seg.c" contains the following code:
  • ICCAVRIDEで「seven_seg.prj」というプロジェクトファイルを開く。「seven_seg.c」というソースファイルは下記のように記述されている。

 

/*
Code for a crash course in AVR programming
Example "seven_seg.c"
Dr Nathan Scott, July 2002
 
This program drives an external 7-segment common-anode LED display
directly using PORTC
*/
 
 
#include "environment.h"
#include "delay.h"
 
void Idle()
// ignore this for now, it is used by the UART code in a later example
{
}
 
void main()
{
    char i;
 
    // set up control over PORTC
    PORTC = 0; // put a definite start value into the register for PORTC
    // - it's best not to leave this to chance
 
    DDRC = 0xFF; // set the direction of all the pins of PORTC to "output".
 
    while (1) // endless loop, will run forever
    {
        for (i = 0; i < 0xFF; i++)
        {
            PORTC = i; // this will turn on a unique combination
            // of the 8 LEDs in the display! However only a few such
            // combinations will be recognisable as numbers!
            DelayMs(100); // human beings need time to read
        }
    }
}

Figure 2 Example program to make a confusing display

  • Compile the project and download the hex file to the AVR.
  • As soon as the project is downloaded, your 7-segment display should start running rapidly and it should show every possible combination of the 8 LEDs. It will be a confusing blur.
  • プロジェクトをコンパイルし,HEXファイルををAVRに転送する。
  • データがAVRに転送されるとすぐに,7セグ表示器が動作し,8個のLEDで表示可能ないろいろなパターンがごちゃごちゃと表示される。

Exercises

  • Change the program "seven_seg.c" so that the display shows only the numbers 0 to 9 in a loop. (hint: you will need to work out which bit patterns correspond to which numbers. Then send the bit patterns to PORTC in order).
  • Do you know why we have the 220 resistors in Figure 1?
  • Suppose that we had a "common cathode" 7-segment LED display instead of our "common anode" one? What changes would you have to make to (a) the wiring diagram of Figure 1, and (b) the program you wrote?

Return to the main tutorial page

演 習

  • seven_seg.c」を修正し,0~9を繰返し表示するようにする。(ヒント:数字に対応したビットパターンを作る必要がある。そして,PORTCポートに順番に出力する。)
  • どうして図1で220抵抗器が必要かわかるか?
  • 今使っている「アノード・コモン7セグLED」の代わりに「カソード・コモン」を利用するとしたら,(a)図1の回路図をどのように変更しねければならないか。また,(b)プログラムをどのようにするか。

メインページに戻る


Dr Nathan Scott · nscott@mech.uwa.edu.au