频道分类

Delphi Microchip串行通信示例

作者:admin 来源: 日期:2020/2/10 19:23:28 人气: 标签:

 
下面的程序和硬件用于本文介绍的过程。

1- Delphi 微笑

2- nrcomm作为串行通讯组件  http://www.deepsoftware.ru/nrcomm/。Delphi可以使用其他串行通信结构/组件。  

3- Labcenter Proteus VSM:简要地说,这是一个不错的程序,用于在计算机环境中进行电路运行,运行,创建pcb和gerber输出。通过该程序,可以设计和仿真包括不同制造商的各种微控制器系列的模拟,数字或混合电路,或者进行PCB设计。我通常使用模拟部分。https://www.labcenter.com/simulation/

4-虚拟串行端口驱动程序:通过此应用程序,我们可以通过在计算机上创建虚拟端口并将它们彼此连接来相互通信两个不同的程序。在我们的示例中,通过虚拟端口将Delphi和proteus仿真电路相互连接,我们执行了与实际操作相同的操作。这样,我们加快了测试和开发过程。https://www.eltima.com/products/vspdxp

5- CCS Pic C编译器:我们在其中编写了程序的编译器,该程序将在我们选择的微芯片系列中以mcu运行。http://www.ccsinfo.com/

6-在示例应用程序中,首选Microchip公司的18F4520处理器。http://www.microchip.com/wwwproducts/en/pıc18f4520

7-带128x64像素KS0108控制器的图形LCD,作为图形LCD。(这与NT7108有很多对应关系)
https://www.direnc.net/cn/wg12864b-mavi
https://www.sparkfun.com/products/710



首先,我们需要设置通信环境。
我们创建了虚拟串行端口,COM1和COM2端口,并将它们彼此连接。Delphi应用程序使用Com1端口仿真电路使用Com2端口。我们的示例电路图如下。这是Delphi编写的应用程序界面,这是我们应用程序的仿真电路和屏幕截图。Delphi端的代码。不太复杂。由于这些说明,可能看起来有点长。

unit Unit1;

{


/******************************************************************************
*
*   Delphi - microchip grafik lcd seri haberleşme örnek uygulama
*
******************************************************************************
* Dosya Adı:       DelphiCanGrafik.exe
* Derleyici:       10.2
* Şirket   :
*
* Yazılım Lisans Şartları
*
*
*
* Yazan                Tarih         Açıklama
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* frmman            2017.09.27      Temel Sürüm
******************************************************************************
*/

Comport Ayarları:

Baudrate     : 57600
ByteSize     : 8
Parity       : None
StopBits     : 1
StreamProtol : None

örnek haberleşme protokolü ASCII tabanlı şekilde tasarlanmıştır.
Aşağıda kullanılan haberleşme paket detayları açıklanmıştır.

A- Temel Paket Yapısı
#CMDPRM <-

#    : paket başlangıç karakteri başka yerde bu karakterin kullanılmaması gerekmektedir
CMD  : XX 2 byte komut (ASCII 00...99 arası)
PRM  : CMD içeriği, data max uzunluk: RX_BUFFER_SIZE - 4
<-   : char(13)  paket sonu


1-PC_LED  : Devrede bulunan C4 pinine bağlı LED kontrolü

#011<-  led yak         cevap pc : #011<-
#010<-  led söndür      cevap pc : #010<-


2-BUTTON_STATE Tek taraflı data mcu -> pc şeklinde  void main() de

                     #020<-     button bırakıldı
                     #021<-     buttona basıldı


3-Resim Göster : CPU rom alanına yerleştirilen 2 adet resim datalarını ekranda gösterme

#030<- // LOGO_DELPHI
#031<- // LOGO_DELPHICAN

4-Gelen metni ekrana Yaz  : PC tarafından gönderilen text mesajlar lcd ekranına yazılır.
  Türkçe karakter gösterim sorunu standart kullanımda vardır. Bu problemin çözümü özel font
  kullanımı ile çözülebilmektedir.

#04xxxxxxx<-        cevap pc : #04<-


5-Bar animasyon   : 000..100 arası bir değerin frafik lcd de bar şeklinde gösterimi sağlanmaktadır.
#05XXX<-        cevap pc : #05<-
 XXX: 000..100 arası


}
interface

uses
 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, nrcommbox, nrclasses,
 nrcomm, Vcl.ExtCtrls, Vcl.Buttons;

type
 TForm1 = class(TForm)
   nrComm1: TnrComm;
   nrDeviceBox1: TnrDeviceBox;
   CheckBox1: TCheckBox;
   btn_led_yak: TButton;
   TimerCevap: TTimer;
   Memo1: TMemo;
   btn_led_sondur: TButton;
   btn_tanimsiz_paket: TButton;
   btn_logo_delphi: TBitBtn;
   btn_MCU: TSpeedButton;
   btn_logo_delphican: TBitBtn;
   EditMetin: TEdit;
   btn_metin_gonder: TBitBtn;
   ScrollBar1: TScrollBar;
   Label1: TLabel;
   btn_scroolbar_gonder: TButton;
   Label2: TLabel;
   Label3: TLabel;
   procedure CheckBox1Click(Sender: TObject);
   procedure nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
     Received: Cardinal);
   procedure TimerCevapTimer(Sender: TObject);
   procedure btn_led_yakClick(Sender: TObject);
   procedure btn_led_sondurClick(Sender: TObject);
   procedure btn_logo_delphiClick(Sender: TObject);
   procedure btn_logo_delphicanClick(Sender: TObject);
   procedure btn_metin_gonderClick(Sender: TObject);
   procedure btn_tanimsiz_paketClick(Sender: TObject);
   procedure btn_scroolbar_gonderClick(Sender: TObject);
   procedure ScrollBar1Change(Sender: TObject);
   procedure FormCreate(Sender: TObject);
 private
   { Private declarations }
 public
 procedure SEND_CMD(data : ansistring; timeout:integer);
   { Public declarations }
 end;

const

btn_on  : AnsiString ='#021'+#13;
btn_off : AnsiString ='#020'+#13;


var
 Form1: TForm1;
 seri_gelen : ansistring;

implementation

{$R *.dfm}


{-------------------------------------------------------------------------------
 Delphi - Microchip Send Data
 # CMDPRM + chr(13)

 paket başı ve paket sonu karakterleri eklenerek data gönderilir
 giden komut cevabı TimerCevap.interval := timeout; ile onTimer eventinde işleniyor
 Gerçek uygulamalarda, dış dünyadan gelen haberleşme paketi bazı durumlarda birden fazla
 nrComm1AfterReceive eventi oluşturmaktadır. Paket bütünlüğü sağlamak ve giden komut
 cevabını beklemek için timeout mekanizması kullanılabilir.
-------------------------------------------------------------------------------}
procedure TForm1.SEND_CMD(data : ansistring; timeout:integer);
var i:integer;
cmd: ansistring;
begin


 try
     nrComm1.Active:=true;

     // gelen paket bufferini temizle
     seri_gelen:='';

     cmd := '#' + data + #13;
     nrcomm1.SendString(cmd);

     TimerCevap.Enabled := false;
     TimerCevap.interval:= timeout;
     TimerCevap.Enabled := true;

     memo1.Lines.Add('TX: ' + cmd);
 except
       Begin
           Application.MessageBox ('Seri Porta bağlanılamıyor.' + #13#10 +
               'Lütfen ayarları kontrol ederek tekrar deneyin.', 'HATA', MB_OK Or MB_ICONERROR);
           Exit;
       End;
   raise;
 end;
end;



{-------------------------------------------------------------------------------
 Seri port gelen data okuma
-------------------------------------------------------------------------------}
procedure TForm1.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
 Received: Cardinal);
var i:integer;

begin

     for i:=0 to Received-1  do
     begin
         seri_gelen := seri_gelen + (PAnsiChar(Buffer)[i]);
     end;


     // mcu dan gelen butona basma olayı burada yakalanıyor.
     // buton konumu herhangi bir zamanda değişebilir.
     if (pos(btn_off,seri_gelen)>0) then   // Kartdaki Buton Bırakıldı  (yükselen kenar)
     begin
       btn_MCU.Down := false;
       seri_gelen :='';
       btn_MCU.Caption :='MCU BTN OFF' ;
     end;


     if (pos(btn_on ,seri_gelen)>0) then // Kartdaki Butona Basıldı  (düşen kenar)
     begin
       btn_MCU.Down := true ;
       seri_gelen :='';
       btn_MCU.Caption :='MCU BTN ON' ;
     end;



end;

{-------------------------------------------------------------------------------
 Gelen Cevapları işleme
-------------------------------------------------------------------------------}
procedure TForm1.TimerCevapTimer(Sender: TObject);
begin
    TimerCevap.Enabled := false;
    if seri_gelen = '' then
      memo1.Lines.Add('RX: ' + 'Komut cevabı gelmedi')
    else
     memo1.Lines.Add('RX: ' + seri_gelen);


    seri_gelen:='';
end;

{-------------------------------------------------------------------------------
 DelphiCan Logosunu Ekranda Gösterme
-------------------------------------------------------------------------------}
procedure TForm1.btn_logo_delphicanClick(Sender: TObject);
begin
 SEND_CMD('031',100);
end;

{-------------------------------------------------------------------------------
 Delphi Logosunu Ekranda Gösterme
-------------------------------------------------------------------------------}
procedure TForm1.btn_logo_delphiClick(Sender: TObject);
begin
 SEND_CMD('030',100);
end;

{-------------------------------------------------------------------------------
 Edit deki texti LCD ekrana yazma
-------------------------------------------------------------------------------}
procedure TForm1.btn_metin_gonderClick(Sender: TObject);
begin
 SEND_CMD('04'+editmetin.Text,500);
end;

{-------------------------------------------------------------------------------
 Protokol dışında komut gönderme  - tanımsız komut
-------------------------------------------------------------------------------}
procedure TForm1.btn_tanimsiz_paketClick(Sender: TObject);
begin
  SEND_CMD('63RASTGELE',100);
end;

{-------------------------------------------------------------------------------
 scrollbar.position değerini gönderme
-------------------------------------------------------------------------------}
procedure TForm1.btn_scroolbar_gonderClick(Sender: TObject);
var data:ansistring;
begin
 Label1.Caption := inttostr(ScrollBar1.Position);

 data := format('%.3d',[ScrollBar1.Position]);
 SEND_CMD('05'+data,300);
end;

procedure TForm1.ScrollBar1Change(Sender: TObject);
begin
 Label1.Caption := inttostr(ScrollBar1.Position);
end;

{-------------------------------------------------------------------------------
 LED Söndür
-------------------------------------------------------------------------------}
procedure TForm1.btn_led_sondurClick(Sender: TObject);
begin
 SEND_CMD('010',100);
end;

{-------------------------------------------------------------------------------
 LED Yak
-------------------------------------------------------------------------------}
procedure TForm1.btn_led_yakClick(Sender: TObject);
begin
 SEND_CMD('011',100);
end;

{-------------------------------------------------------------------------------
 Seri Port on/off
-------------------------------------------------------------------------------}
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
   nrComm1.Active := CheckBox1.Checked;
   nrDeviceBox1.Enabled := not CheckBox1.Checked;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 memo1.Clear;
end;

end.

MCU 侧头部分的代码
#include <18F4520.h>

#device ADC=10

#FUSES NOWDT                    //No Watch Dog Timer

#use delay(internal=16MHz)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, stream=UART)



#DEFINE SIZE_IMG  3072 

#DEFINE LOGO_DELPHI   ( getenv("PROGRAM_MEMORY") - 3072 )
#DEFINE LOGO_DELPHICAN  LOGO_DELPHI - SIZE_IMG


#ROM LOGO_DELPHI =
{
0x40,0x80,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xE0,
0xF0,0xF0,0xF8,0xFC,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x7E,0xFE,0xFC,0xFC,0xFC,0xF8,0xF0,0xF0,
0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0x7F,0xFF,0x5F,0x1F,0x1F,0x0F,0x07,0x07,0x07,0x83,0x77,0x3F,0x05,0x00,
0x01,0x00,0x01,0x00,0x41,0xE0,0x51,0x0C,0x07,0x03,0x01,0x00,0x01,0x01,0x01,0x83,
0x07,0xC3,0xC7,0xE7,0xF7,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xC0,0xE0,0xFC,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0xFF,0xC7,0x03,
0x01,0x00,0x00,0x03,0x57,0xA0,0x00,0xC0,0x40,0x60,0x74,0xBF,0x71,0xB0,0x50,0x30,
0x10,0x38,0x14,0x33,0x71,0x60,0x60,0xE0,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC,0xF0,0xC0,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xF0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x6F,0x41,0x80,0x01,0x07,
0x14,0xF0,0x70,0x3C,0xD7,0x73,0x15,0x0D,0x05,0x02,0x01,0x01,0x01,0x00,0x00,0x00,
0x00,0x80,0xC0,0xC0,0x70,0x70,0x70,0x38,0x1D,0x0F,0x07,0x07,0x17,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF5,0xF0,0x10,0x20,0x41,0x02,
0x57,0xFF,0x55,0x3E,0xF7,0xA0,0x00,0x00,0x00,0x00,0x10,0x38,0x7C,0xEC,0xC4,0xE6,
0xE7,0xE3,0xF1,0xE0,0xF0,0xF0,0xF0,0xF8,0xF0,0xF8,0xFC,0xF8,0xFC,0xFC,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x07,0x1F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xFC,0xF4,0xE4,
0xC5,0xE3,0xD7,0xFE,0xF1,0xE7,0xDD,0xEC,0xC0,0xE0,0xF0,0xE0,0xF1,0xE0,0xF5,0xEF,
0xFD,0xF3,0xC7,0x07,0x17,0x1F,0x1F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x1F,0x03,0x01,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xF4,0xE0,0xC0,0x80,0xC1,0xE3,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,
0x07,0x07,0x07,0x0F,0x1F,0x3F,0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x3F,0x3F,0x3F,0x1F,0x0F,0x07,0x07,
0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00

}

#ROM LOGO_DELPHICAN =
{
0x40,0x80,
0xFF,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
0x01,0x01,0x01,0x01,0x01,0x01,0xFD,0xC5,0xC5,0xC5,0xC5,0xFD,0x01,0x01,0x01,0xFD,
0x8D,0x8D,0x8D,0x8D,0xFD,0x01,0x01,0x01,0xFD,0xCD,0xB5,0xB5,0xCD,0xFD,0x01,0xFF,
0x03,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x03,
0x00,0x00,0x04,0x04,0xFC,0xFC,0xFC,0x04,0x04,0x04,0x04,0x04,0x0C,0x0C,0x08,0x18,
0x78,0xF0,0xE0,0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x00,0x02,0x02,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0xFE,0xFE,0xFE,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x8C,0x8C,0x8C,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0xE0,0xF0,0x38,0x18,0x0C,0x0C,0x04,0x04,0x04,0x04,0x0C,0x0C,
0x7C,0x78,0x78,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
0xE0,0xFF,0x7F,0x1F,0x00,0x00,0xE8,0xFE,0xFF,0x17,0x11,0x10,0x10,0x11,0x1F,0x1F,
0x0E,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x01,0x01,0xFF,0xFF,0xFF,0x03,0x03,
0x01,0x01,0x03,0x87,0xFE,0xFE,0x78,0x00,0x00,0x00,0xFF,0xFF,0xFF,0x03,0x03,0x01,
0x03,0xFF,0xFE,0xFC,0x00,0x00,0x00,0x01,0x01,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
0x00,0x00,0x3F,0xFF,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xC0,0xC0,0xC0,0x86,0xC7,0xE7,0x61,0x21,0x30,0x31,0xFF,0xFF,0xFE,0x00,0x00,
0x01,0x03,0xFF,0xFF,0xFF,0x03,0x03,0x01,0x01,0x03,0xFF,0xFE,0xFC,0x00,0x00,0x00,
0x00,0x04,0x04,0x04,0x07,0x07,0x07,0x04,0x04,0x04,0x04,0x06,0x06,0x02,0x03,0x03,
0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x06,0x04,0x04,0x06,0x06,0x02,
0x02,0x00,0x04,0x04,0x07,0x07,0x07,0x04,0x04,0x00,0x00,0xFF,0xFF,0xFF,0x06,0x04,
0x04,0x06,0x06,0x03,0x03,0x01,0x00,0x00,0x04,0x04,0x07,0x07,0x07,0x04,0x04,0x04,
0x04,0x07,0x07,0x07,0x04,0x04,0x00,0x04,0x04,0x07,0x07,0x07,0x04,0x04,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x06,0x0C,0x0C,0x08,0x08,0x08,0x08,0x0C,0x0C,
0x07,0x07,0x03,0x00,0x01,0x03,0x07,0x06,0x04,0x06,0x06,0x03,0x07,0x07,0x04,0x04,
0x04,0x04,0x07,0x07,0x07,0x04,0x04,0x00,0x04,0x04,0x07,0x07,0x07,0x04,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x02,0x03,0x03,0x03,0x02,0x02,
0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x01,0x01,0x01,0x01,0xF1,0xF1,0x01,0xE1,0x71,0xE1,0x01,0xF1,0xF1,0x01,
0xF1,0xF1,0x01,0xE1,0x71,0xE1,0x01,0xF1,0xF1,0x01,0xF1,0xF1,0x01,0xE1,0x71,0xE1,
0x01,0xF1,0xF1,0x01,0x01,0x01,0x01,0xE1,0xF1,0x11,0x21,0xFD,0xFD,0x01,0xE1,0xF1,
0x91,0x91,0xF1,0xE1,0x01,0xFD,0xFD,0x01,0xF1,0xF1,0x21,0x11,0xF1,0xE1,0x01,0xFD,
0xFD,0x21,0x11,0xF1,0xE1,0x01,0xF5,0xF5,0x01,0xE1,0xF1,0x11,0x11,0x31,0x21,0x01,
0x21,0x91,0xD1,0x51,0xF1,0xE1,0x01,0xF1,0xF1,0x21,0x11,0xF1,0xE1,0x01,0x01,0x01,
0x01,0xE1,0xF1,0x11,0x11,0x31,0x21,0x01,0xE1,0xF1,0x11,0x11,0xF1,0xE1,0x01,0xF1,
0xF1,0x21,0x11,0xF1,0xF1,0x21,0x11,0xF1,0xE1,0x01,0x01,0x01,0x01,0x01,0xFF,0x00,
0x00,0x7F,0x40,0x40,0x40,0x40,0x40,0x47,0x47,0x43,0x40,0x43,0x47,0x47,0x40,0x40,
0x40,0x47,0x47,0x43,0x40,0x43,0x47,0x47,0x40,0x40,0x40,0x47,0x47,0x43,0x40,0x43,
0x47,0x47,0x40,0x40,0x46,0x46,0x40,0x43,0x47,0x44,0x42,0x47,0x47,0x40,0x43,0x47,
0x44,0x44,0x46,0x42,0x40,0x47,0x47,0x40,0x4F,0x5F,0x42,0x44,0x47,0x43,0x40,0x47,
0x47,0x40,0x40,0x47,0x47,0x40,0x47,0x47,0x40,0x43,0x47,0x44,0x44,0x46,0x42,0x40,
0x43,0x47,0x44,0x44,0x47,0x47,0x40,0x47,0x47,0x40,0x40,0x47,0x47,0x40,0x46,0x46,
0x40,0x43,0x47,0x44,0x44,0x46,0x42,0x40,0x43,0x47,0x44,0x44,0x47,0x43,0x40,0x47,
0x47,0x40,0x40,0x47,0x47,0x40,0x40,0x47,0x47,0x40,0x40,0x40,0x40,0x40,0x7F,0x00

}


主单片机程序
/******************************************************************************

*
*   DELPHİ CAN grafik lcd seri port örnek uygulama
*
******************************************************************************
* Dosya Adı:       MainDelphiCanGrafik.c
* İşlemci  :       18F4520
* Derleyici:       5.074
* Şirket   :       
*
* Yazılım Lisans Şartları
*  
*
*
* Yazan                Tarih         Açıklama
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* frmman            2017.09.27      Temel Sürüm
******************************************************************************  
*/

#include <MainDelphiCanGrafik.h>




//haberleşme ile ilgili değişkenler
#define RX_BUFFER_SIZE 100            // bir pakette max bu kadar byte veri alabiliriz 
char com_buffer[RX_BUFFER_SIZE];      // pc den gelen verilerin saklandığı buffer
int  com_sayac = 0;                   // gelen data sayaç 
int1 paket_geldi = false;             // bir paket geldi


// grafik lcd ye text yazmak için kullanaılan genel değişken
char Lcd_Text[RX_BUFFER_SIZE];  


// donanımsal tanımlamalar
#define BTN_INP   PIN_A0   // giriş buton basılı:H
#define LED_TX    PIN_C1   // çıkış data gönderme ledi
#define LED_RX    PIN_C2   // çıkış data alma ledi
#define LED_RUN   PIN_C3   // 500ms peryotlarla yanıp sönen sistem ok ledi
#define LED_PC    PIN_C4   // pc den kontrol edilen led


// CPU data gönderme ve alma sırasında, haberleşme trafiğini gösteren ledlerin
// yanma zamanları
#define TX_LED_SET 20 // 20*10ms = 200ms yansın
#define RX_LED_SET 20 // 20*10ms = 200ms yansın

// zaman ölçümü ile ilgili değişkenler
int time_tx_led = 0;
int time_rx_led = 0;
int time_500ms = 0;


// standart kütüphaneler
#include "hdm64gs12.c"
#include "graphics.c"



/*------------------------------------------------------------------------------
         Grafik LCD RESİM
Ekranda belirlenen koordinatta resim gösterir
x: Resmin x koordinatı
y: Resmin y koordinatı
adres: Resmin program hafızasındaki yeri
Ekrani_Temizle: True resimden önce ekrani temizle
------------------------------------------------------------------------------*/

void Resim(int x,int y,int16 adres,int1 Ekrani_Temizle) //(int16 adres) 
{
int   resim_yukseklik, resim_genislik;
int   data;
int   x1, xi, yi;
int   temp_x, temp_y;
int16 resim_adres, t_resim_adres, resim_y_adres_artirma;
int1  chip, chip_degistir = true;
int   a=0;

 if (Ekrani_Temizle) glcd_fillScreen(OFF);
 
 resim_yukseklik = read_program_eeprom(adres);
 resim_genislik  = read_program_eeprom(adres+2);

 t_resim_adres = adres + 4;                         // resim dataları 4 byte sonra başlıyor
 resim_adres   = t_resim_adres;
 resim_y_adres_artirma = (int16)resim_genislik * 2 ;// bir satır taraması bitince sonraki satır adres başlangıcı
 

   temp_x = x + resim_genislik;    if (temp_x > 128) temp_x = 128;
   temp_y = y + resim_yukseklik;   if (temp_y > 64)  temp_y = 64;    
   
   for (yi = y; yi < temp_y; )
   {
    a++;
    chip_degistir = true;
    
         for (xi=x; xi < temp_x; xi++)
         {
            if (xi<=63)  { chip = 0;  x1 = xi; }
            else         { chip = 1;  x1 = 64 - xi; }
             
            if (xi == 64) chip_degistir = true;
        
              if (chip_degistir)
              {
               chip_degistir = false;
               
               output_low(GLCD_DI);  // Set for instruction
               bit_clear(x1,7);      // Clear the MSB. Part of an instruction code
               bit_set(x1,6);        // Set bit 6. Also part of an instruction code
              
               glcd_writeByte(chip, x1);   
               glcd_writeByte(chip, (yi/8 & 0b10111111) | 0b10111000); 
           
               output_low(GLCD_DI);           // Set for instruction
               glcd_writeByte(chip, x1);       // Set the horizontal address
              
               output_high(GLCD_DI);          // Set for data
              }

            data = read_program_eeprom(resim_adres);
            resim_adres += 2;
            glcd_writeByte(chip, data);   // Write the pixel data
         }
     
     
     resim_adres   = t_resim_adres + ( resim_y_adres_artirma * a );
     
    yi+=8; 
   }
 
}


/*-----------------------------------------------------------------------------
BAR ANİMASYON  Delphi tarafındaki scroolbar animasyonu
min : 0
max : 100 
-----------------------------------------------------------------------------*/
void Bar_animasyon(int value)
{
  if (value > 100) value = 100;
  
  glcd_fillScreen(OFF);
  
  sprintf(Lcd_Text,"%u",value);
  glcd_text57(58,15,Lcd_Text,1,1);   
  
  glcd_rect(13, 28, 113, 41, NO, ON);  
  glcd_bar(13, 35, value+13, 35, 10, 1);
  

}

/*-----------------------------------------------------------------------------
TIMER 2 KESME 10ms
-----------------------------------------------------------------------------*/
#INT_TIMER2
void  TIMER2_isr(void) 
{
// --- 500ms peryotlarla yapılack işler
if (++time_500ms > 50)
{
  time_500ms = 0;
  output_toggle(LED_RUN);
}

// 10ms peryotlarla yapılacak işler

if (time_tx_led > 0) { output_high(LED_TX); time_tx_led--;} else output_low(LED_TX);
if (time_rx_led > 0) { output_high(LED_RX); time_rx_led--;} else output_low(LED_RX);
 
}

/*-----------------------------------------------------------------------------
 RX BUFFER TEMİZLE
-----------------------------------------------------------------------------*/
void rx_buffer_temizle()
{
int ix;

for (ix = 0; ix < RX_BUFFER_SIZE ; ix++) com_buffer[ix] = 0;

com_sayac = 0;
paket_geldi = false;

}


/*-----------------------------------------------------------------------------
SERI DATA ALMA KESME
-----------------------------------------------------------------------------*/
#INT_RDA
void  RDA_isr(void) 
{
char gelen;
  gelen = getc();
  
  if (gelen == '#') com_sayac = 0; // gelen paket başlangıç ise indexi başa al
  
  com_buffer[com_sayac] = gelen;
  
  com_sayac++; 
  if (gelen == 13)  paket_geldi = true; // paket sonu karakteri. paket geldi
  
  // buffer taşması olursa gelen paketi iptal et
  if (com_sayac >= RX_BUFFER_SIZE)
  {
    disable_interrupts(INT_RDA);
    rx_buffer_temizle();
    enable_interrupts(INT_RDA);
  }
  

  
  time_rx_led = RX_LED_SET;
}



/*-----------------------------------------------------------------------------
 CMD char to int dönüşümü
 Delphi deki strtoint fonksiyonu benzeri iki basamaklı string değer 8bitlik 
 int değere dönüştürme
-----------------------------------------------------------------------------*/  
int cmd_chartoint()
{
int d,h,l = 0;

  h = com_buffer[1] -'0';
  l = com_buffer[2] -'0';
  d = (h * 10)  + l;

  return(d);
}
/*-----------------------------------------------------------------------------
 HABERLEŞME KONTROL
 gelen paket burada işlenir

örnek haberleşme protokolü ASCII tabanlı şekilde tasarlanmıştır. Aşağıda 
haberleşme paket detayı açıklanmıştır.

#CMDPRM <-

#    : paket başlangıç karakteri başka yerde bu karakterin kullanılmaması gerekmektedir
CMD  : XX 2 byte komut
PRM  : CMD içeriği data max uzunluk: RX_BUFFER_SIZE - 4
<-   : char(13)  paket sonu


PC_LED

#011<-  led yak         cevap pc : #011<-
#010<-  led söndür      cevap pc : #010<-


BUTTON_STATE Tek taraflı mcu-> pc şeklinde  main() de

                     #020<-     button bırakıldı
                     #021<-     buttona basıldı


Resim Göster

#030<- // LOGO_DELPHI
#031<- // LOGO_DELPHICAN

Gelen metni ekrana Yaz
#04xxxxxxx<-        cevap pc : #04<-
  
  
Bar animasyon
#05XXX<-        cevap pc : #05<-   
 XXX: 000..100 arası
-----------------------------------------------------------------------------*/ 
void haberlesme_kontrol()
{
int cmd;
int prm;
  if (paket_geldi == false)  return; // paket gelmedi çık

  disable_interrupts(INT_RDA); // gelen paket işlenirken data almayı durdur
  
  cmd = cmd_chartoint();

  switch (cmd)
  {
   case 1:  // cmd led yak söndür
         if (com_buffer[3] =='0') output_low(LED_PC);
         if (com_buffer[3] =='1') output_high(LED_PC);
         time_tx_led = TX_LED_SET;
         fprintf(UART,"%s", com_buffer);
      break;
  
   case 3:  // cmd resim göster
         if (com_buffer[3] =='0') resim(0,0,LOGO_DELPHI,true);
         if (com_buffer[3] =='1') resim(0,0,LOGO_DELPHICAN,true);
         time_tx_led = TX_LED_SET;
         fprintf(UART,"%s", com_buffer);
      break;   
  
  
   case 4:  // cmd gelen metni lcd de göster
        glcd_fillScreen(OFF);
        memcpy(&Lcd_Text[0],&com_buffer[3],com_sayac-4);
        Lcd_Text[com_sayac-4] = 0;
        glcd_text57(0,0,Lcd_Text,1,1);
        
        
         time_tx_led = TX_LED_SET;
         fprintf(UART,"#04\r");
      break;   
  
    
   case 5:  // cmd bar animasyon
        
        prm = ((com_buffer[3] -'0') * 100) +
              ((com_buffer[4] -'0') * 10)  +
               (com_buffer[5] -'0');
               
         Bar_animasyon(prm);
         time_tx_led = TX_LED_SET;
         fprintf(UART,"#05\r");
      break;   
       
    
  
    default:
        time_tx_led = TX_LED_SET;
        fprintf(UART,"%s TANIMSIZ PAKET\r\n", com_buffer);
        
        // lcd ekrana hatalı paket içeriğini yaz
        glcd_fillScreen(OFF);
        sprintf(Lcd_Text,"TANIMSIZ PAKET");
        glcd_text57(5,20,Lcd_Text,1,1);
        glcd_text57(5,40,com_buffer,1,1);
        
      break;
  }
  



 rx_buffer_temizle(); // gelen paket işlendi buffer temizle
 
 enable_interrupts(INT_RDA);
}





/*-----------------------------------------------------------------------------
MAIN
-----------------------------------------------------------------------------*/
void main()
{
int1 btn_status = false ;

  setup_timer_2(T2_DIV_BY_16,241,13);      //774 us overflow, 10,0 ms interrupt

  glcd_init(ON);

  enable_interrupts(INT_TIMER2);
  enable_interrupts(INT_RDA);
  enable_interrupts(GLOBAL);


  // açılış animasyon
  resim(0,0,LOGO_DELPHICAN,true);delay_ms(500);
  resim(0,0,LOGO_DELPHI,true); delay_ms(500);
  resim(0,0,LOGO_DELPHICAN,true);delay_ms(500);
  resim(0,0,LOGO_DELPHI,true); delay_ms(500);
  resim(0,0,LOGO_DELPHICAN,true);delay_ms(500);

  while(TRUE)
  {

     haberlesme_kontrol();
     
     
     // button basma yükselen kenar
     if (input(BTN_INP) && (!btn_status))
     {
       btn_status = true;
       
         time_tx_led = TX_LED_SET;
         fprintf(UART,"#021\r");        
     }
     
     //button bırakma düşen kenar
     if (!input(BTN_INP) && (btn_status))
     {
       btn_status = false;
       
         time_tx_led = TX_LED_SET;
         fprintf(UART,"#020\r");        
     }
     

  }

}