best view IE mode 800x600 Text Size Medium

Home
แผนที่เว็บไซต์

 

 

 

บทความ

การใช้งาน Visual Basic กับไมโครคอนโทรลเลอร์ MCS51
ตอน Serial port กับ Philips's P89C51RD+/RD2

    เนื้อหาครั้งนี้เป็นการนำเสนอการใช้งาน Visual Basic ติดต่อกับไมโครคอนโทรลเลอร์ MCS51 ในตระกูลของ Philips (P89C51RD2) โดยแบ่งโค้ดออกเป็น 2 ส่วนคือ

  1. ส่วนของ Visual Basic ซึ่งทำงานบนเครื่องคอมพิวเตอร์และติดต่อกับไมโครคอนโทรลเลอร์ผ่านทาง Serial port เพื่อส่งค่าให้กับไมโครคอนโทรลเลอร์ เพื่อนำไปควบคุม LED ตามที่โปรแกรมบนคอมพิวเตอร์ต้องการ
     
  2. ส่วนของ MCS51 ส่วนนี้จะรับข้อมูลมาจาก Visual Basic เพื่อนำค่าที่ส่งมาไปควบคุม LED ตามเงื่อนไขที่ได้กำหนดไว้แล้ว โดยก่อนที่จะรับคำสั่งจาก Visual Basic นั้นจะต้องตรวจสอบการเชื่อมต่อกันก่อนระหว่าง ว่าโปรแกรมบน Visual Basic สามารถควบคุม LED ได้หรือไม่ด้วย protocol ที่ได้กำหนดขึ้นมาไว้ล่วงหน้าแล้ว (โค้ดในไมโครคอนโทรลเลอร์เขียนด้วยภาษาซี โดยใช้ Keil C-Compiler)

โปรแกรม VB LED Control

    การทำงานของโปรแกรมจะเป็นลักษณะควบคุม LED D0-D7 โดยที่ก่อนที่จะใช้งานได้จะต้องคลิกที่ปุ่มคำสั่ง Connect ก่อนเพื่อติดต่อกับบอร์ดทดลอง ลักษณะของโค้ดจะเป็นดังนี้

' Use COM1.
MSComm1.CommPort = Text1.Text
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "A"      ' ส่งตัวอักษร A ออกไป
' Wait for data to come back to the serial port.
Do
  DoEvents
  Buffer$ = MSComm1.Input
Loop Until Buffer$ = "A"      ' รออักษร A กลับมา

MSComm1.Output = "S"     ' ส่งตัวอักษร S ออกไป
Do
  DoEvents
  Buffer$ = MSComm1.Input
Loop Until Buffer$ = "S"      ' รออักษร S กลับมา

MSComm1.Output = "T"    ' ส่งตัวอักษร T ออกไป
Do
  DoEvents
  Buffer$ = MSComm1.Input
Loop Until Buffer$ = "T"     ' รออักษร T กลับมา


Style = vbOKOnly + vbInformation ' Define buttons.
Title = "Test LED" ' Define title.
Msg = "CONNECT....."
MsgBox Msg, Style, Title

     ส่วนโค้ดควบคุม LED ก็เพียงแค่ส่งค่าตาม LED ที่ต้องการออกไป ดังโค้ดข้างล่าง

If MSComm1.PortOpen = True Then
   If Command1.Caption = "ON" Then
      Shape1(0).BackColor = &H80
      Command1.Caption = "OFF"
   Else
      Shape1(0).BackColor = &HFF
      Command1.Caption = "ON"
   End If
      MSComm1.Output = "0"      ' เปิด LED ตำแหน่งที่ 0 (D0 บนบอร์ด NX-51 V2.0 )
End If

    โค้ดควบคุม LED ดวงอื่น ๆ ก็จะมีลักษณะโค้ดเช่นเดียวกัน เพียงแต่เปลี่ยนค่าที่จะส่งออกไป เช่น ถ้าจะควบคุม LED ตำแหน่งที่ 5 ก็เพียงเขียนคำสั่ง

MSComm1.Output = "5"      ' เปิด LED ตำแหน่งที่ 5 (D5 บนบอร์ด NX-51 V2.0 )

โค้ดในส่วนของ C51

#include <reg51f.h>

#define TRUE 1
#define FALSE 0

unsigned char HOOK;
sbit P0_0 = P0^0;
sbit P0_1 = P0^1;
sbit P0_2 = P0^2;
sbit P0_3 = P0^3;
sbit P0_4 = P0^4;
sbit P0_5 = P0^5;
sbit P0_6 = P0^6;
sbit P0_7 = P0^7;
sbit dri_p = P1^4;               ' บิตควบคุมการทำงานของ LED ทั้ง 8 ตำแหน่ง

// putbuf: write a character to SBUF or transmission buffer
void putbuf(char c)
{
    SBUF = c; // to SBUF to start transmission
}


void Display_LED(unsigned char led)
{
    switch(led)
{
    case '0':
    P0_0 = !P0_0;
    break;
    case '1':
    P0_1 = !P0_1;
    break;
    case '2':
    P0_2 = !P0_2;
    break;
    case '3':
    P0_3 = !P0_3;
    break;
    case '4':
    P0_4 = !P0_4;
    break;
    case '5':
    P0_5 = !P0_5;
    break;
    case '6':
    P0_6 = !P0_6;
    break;
    case '7':
    P0_7 = !P0_7;
    break;
}
}


//serial: serial receiver / transmitter interrupt
serial () interrupt 4 using 2                   // use registerbank 2 for interrupt
{
unsigned char c;

if(RI)                                                           // if receiver interrupt
{
    c = SBUF;                                            // read character
    RI = 0;                                                    // clear interrupt request flag
    switch(c){                                             // process character
      case 'A':
      putbuf('A');
      HOOK = 1;
      break;
      case 'S':
      putbuf('S');
      if(HOOK == 1) HOOK = 2;
      break;
      case 'T':
       putbuf('T');
       if(HOOK == 2) dri_p = 1;
       break;
       case 'Q':
       HOOK = 0;
       dri_p = 0;
       break;

       default: // read all other characters into inbuf
      if(HOOK == 2)
          Display_LED(c);
      break;

} // switch
}

if (TI)                               // if transmitter interrupt
{
      TI = 0;                       // clear interrupt request flag
}
}

// serial_init: initialize serial interface
void serial_init(void)
{
    SCON = 0x50;                  // mode 1: 8-bit UART, enable receiver
   TMOD |= 0x21;                 // 8-Bit auto reload, T0 16Bit
   TH1 = 0xFA;                      // 9600 bps Timer1 Default
   TL1 = 0xFA;
   IE = 0x90;                            // set En. EA, ES
   TR1 = 1;                              // timer 1 run
}

void main(void)
{
    serial_init();
    P0 = 0x00; // clear P0
    while(1);
}

 

Home Page
>> หากมีข้อเสนอและแนะนำส่ง email มาได้ที่ support@appsofttech.com  ครับ

 

Home Page

best view IE mode 1024x768 Text Size Medium
ผู้จำหน่ายไมโครคอนโทรลเลอร์และอุปกรณ์อิเล็กทรอนิกส์ทุกประเภท
Copyright © 2005 Appsofttech Co., Ltd.


บริษัท แอพซอฟต์เทค จำกัด 19/6 หมู่ 6 หมู่บ้านเอกวัฒนา ซ.เพชรเกษม 53 ถนนเพชรเกษม แขวงบางแค เขตบางแค กรุงเทพฯ  10160 Tel: 0-2413-3985-6, Fax: 0-2413-3165(auto)
Hotline 081-4850870, 081-4316541

[ วิธีสั่งซื้อ | บริษัท | Term of use | Privacy Policy ]
 
Contact email: info@appsofttech.com  Designed by : : Appsofttech Co.,Ltd.