Delphi - 7 Segment Display

Viewed 204

I am trying to solve a problem. I want to display multiple I/O functions (write_byte()) on a 7 segment display. I am using a PCF8574 iic. I tried to add two I/O functions together, but only one segment is turning red at a time. How can I make it happen, that multiple segments are turning red at the same time and stay red for a some time?

Here is my code

unit check;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, i2cUsb, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  private
    { private-declarationen }
  public
    { public-declarationen }
  end;

var
  Form1: TForm1;
  board, slave : Ti2cUsb;
  var1:integer;
  i: integer;

implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin

board := Ti2cUsb.Create;
slave := Ti2cUsb.Create;
//Comport Number COM4
var1 := board.Init(4);
Sleep(1000);
board.relais_on;
Sleep(1000);
board.relais_off;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

board.start_iic(False,64,'w');
{
// 7 Segment off:
board.wr_byte_iic($FF);
//all 7 Segments:
board.wr_byte_iic($7F);
board.wr_byte_iic($BF);
board.wr_byte_iic($DF);
board.wr_byte_iic($EF);
board.wr_byte_iic($F7);
board.wr_byte_iic($FB);
board.wr_byte_iic($FD);
board.wr_byte_iic($FE);
}

// Display off
        board.wr_byte_iic($FF);
        sleep(5000);


//number 0:
      board.wr_byte_iic($7F);
      board.wr_byte_iic($BF);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($FB);
      board.wr_byte_iic($FD);
      sleep(5000);

//number 1:
      board.wr_byte_iic($DF);
      board.wr_byte_iic($F7);
      sleep(5000);


//number 2:
      board.wr_byte_iic($BF);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($FE);
      board.wr_byte_iic($FD);
      board.wr_byte_iic($FB);
      sleep(5000);

//number 3:
      board.wr_byte_iic($BF);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($FE);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($FB);
      sleep(5000);

//number 4:
      board.wr_byte_iic($7F);
      board.wr_byte_iic($FE);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($EF);
      sleep(5000);

//number 5:
      board.wr_byte_iic($BF);
      board.wr_byte_iic($7F);
      board.wr_byte_iic($FE);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($FB);
      sleep(5000);

//number 6:
      board.wr_byte_iic($EF);
      board.wr_byte_iic($BF);
      board.wr_byte_iic($7F);
      board.wr_byte_iic($FD);
      board.wr_byte_iic($FB);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($FE);
      sleep(5000);

//number 7:
      board.wr_byte_iic($BF);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($F7);
      sleep(5000);

//number 8:
      board.wr_byte_iic($FF);
      board.wr_byte_iic($7F);
      board.wr_byte_iic($BF);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($FB);
      board.wr_byte_iic($FD);
      board.wr_byte_iic($FE);
      sleep(5000);

//number 9:
      board.wr_byte_iic($EF);
      board.wr_byte_iic($FE);
      board.wr_byte_iic($7F);
      board.wr_byte_iic($BF);
      board.wr_byte_iic($DF);
      board.wr_byte_iic($F7);
      board.wr_byte_iic($FB);
      sleep(5000);

// Display off
      board.wr_byte_iic($FF);



end;

end.

Thanks in advance!

1 Answers

When you are trying to write multiple segments, each write is replacing what you have written previously. You need to work out the number corresponding to the combination of segments you want to show, and then write that combined number, once.

There are a number of ways to do this. I think I would do something like this:


implementation
  const      ALL_OFF   = $ff;
  const      SEGMENT_1 = $20;      // ONE bit only set, the bit that needs to
  const      SEGMENT_2 = $08;      //     turned off for that segment

procedure SetNumberOne();
var
  ByteToWrite: Byte;
begin
  ByteToWrite := ALL_OFF Xor (SEGMENT_1 Or SEGMENT_2);
  board.wr_byte_iic(ByteToWrite);
end;

So you have constants marking the bits that need to be set off, you Or these together to get the correct combination of bits, then Xor with $FF to turn off the bits corresponding the segments you want to display.

Related