Gửi dữ liệu từ giao diện về PIC hiển thị LCD giao tiếp qua COM (Visual Basic 2010)

Ø Phần Design:


Phần kết quả:




Ø Code CCS:
#include <16F887.h>
#include <lcd4bit.c>
#include <stdlib.h>
#include <string.h>
#FUSES NOWDT,NOPROTECT,NOLVP,XT,PUT
#use delay(clock=4000000)
#use rs232(baud=9600,parity=n, xmit= pin_c6,rcv=pin_c7,bits=8)
unsigned int connect,disconnect,temp=0;
char c;
#INT_RDA
void rda_isr()
 {  
      c = getc();
     if(c=='$')
      {
      temp = 2;
      }
     If(temp == 1)
      {
      LCD_Putchar(c);
      }
switch (c)
  {
    case '&':
    {
     connect =1;
     break;
     }
     case '#':
     {
     disconnect =1;
     break;
     }
     case '@':
     {
     temp =1;
     set_tris_c(0x80);
     LCD_Putchar("\f");
     break;
      }
    }
}
void main(void)
{
   enable_interrupts(global);
   enable_interrupts(int_rda);
   delay_ms(100);
   set_tris_c(0x80);
   LCD_Init();
  while(true)
    {
      If(connect ==1)
       {
       lcd_Putchar("KET NOI THANH CONG");
       connect=0;
       }
      If(disconnect ==1)
       {
       lcd_Putchar("DA NGAT KET NOI");
       disconnect =0;
       }
     }
}

FILE LCD4bit.c :
#include <stddef.h>
#define LCD_RS          PIN_E1
//#define LCD_RW          PIN_E1
#define LCD_EN          PIN_E0

#define LCD_D4          PIN_B5
#define LCD_D5          PIN_B4
#define LCD_D6          PIN_B3
#define LCD_D7          PIN_A2

// misc display defines-
#define Line_1          0x80
#define Line_2          0xC0
#define Clear_Scr       0x01

// prototype statements
#separate void LCD_Init ( void );// ham khoi tao LCD
#separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro
#separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi len LCD
#separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD
#separate void LCD_PulseEnable ( void );// Xung kich hoat
#separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data
// D/n Cong
#use standard_io ( B )
#use standard_io (A)

//khoi tao LCD**********************************************
#separate void LCD_Init ( void )
    {

    LCD_SetData ( 0x00 );
   // delay_ms(200);       /* wait enough time after Vdd rise >> 15ms */
    output_low ( LCD_RS );// che do gui lenh
    LCD_SetData ( 0x03 );   /* init with specific nibbles to start 4-bit mode */
    LCD_PulseEnable();
    LCD_PulseEnable();
    LCD_PulseEnable();
    LCD_SetData ( 0x02 );   /* set 4-bit interface */
    LCD_PulseEnable();      /* send dual nibbles hereafter, MSN first */
    LCD_PutCmd ( 0x2C );    /* function set (all lines, 5x7 characters) */
    LCD_PutCmd ( 0b00001100);    /* display ON, cursor off, no blink */
    LCD_PutCmd ( 0x06 );    /* entry mode set, increment & scroll left */
    LCD_PutCmd ( 0x01 );    /* clear display */
    }

#separate void LCD_SetPosition ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
    LCD_SetData ( swap ( cX ) | 0x08 );
    LCD_PulseEnable();
    LCD_SetData ( swap ( cX ) );
    LCD_PulseEnable();
    }

#separate void LCD_PutChar ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
        output_high ( LCD_RS );
        LCD_PutCmd( cX );
        output_low ( LCD_RS );
    }

#separate void LCD_PutCmd ( unsigned int cX )
    {
    /* this subroutine works specifically for 4-bit Port A */
    LCD_SetData ( swap ( cX ) );     /* send high nibble */
    LCD_PulseEnable();
    LCD_SetData ( swap ( cX ) );     /* send low nibble */
    LCD_PulseEnable();
    }
#separate void LCD_PulseEnable ( void )
    {
    output_high ( LCD_EN );
    //delay_us(3);         // was 10
    output_low ( LCD_EN );
    //delay_ms(3);         // was 5
    }

#separate void LCD_SetData ( unsigned int cX )
    {
    output_bit ( LCD_D4, cX & 0x01 );
    output_bit ( LCD_D5, cX & 0x02 );
    output_bit ( LCD_D6, cX & 0x04 );
    output_bit ( LCD_D7, cX & 0x08 );
    }

Code giao diện:

Imports System.IO.Ports
Public Class Form1
    Dim temp As Integer = 0
    Dim readBuffer As String = Nothing
    Dim lendPort As Integer
    Dim connect As Integer
    Dim ports As Array

    Delegate Sub settextcallback(ByVal [text] As String)
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ports = SerialPort.GetPortNames
        For i = 0 To UBound(ports)
            chonCOM.Items.Add(ports(i))
        Next
    End Sub

    Private Sub Button_connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_connect.Click
        If chonCOM.Text = "" Or chonbaudrate.Text = "" Then
            MsgBox(" Bạn chưa chọn cổng COM hoặc Baud Rate ")
        Else
            If connect = 0 Then
                connect = 1
                Button_connect.Text = " Disconnect "
                Button_connect.BackColor = Color.Blue
                Button_disconnect.BackColor = Color.Red
                MessageBox.Show("Kết nối thành công", "Thông Báo", MessageBoxButtons.OK)
                SerialPort1.PortName = chonCOM.Text
                SerialPort1.BaudRate = 9600
                Convert.ToInt16(chonbaudrate.Text)
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.DataBits = 8
                chonCOM.Enabled = False
                chonbaudrate.Enabled = False
                Button1.Enabled = False
                Button_senddata.Enabled = True
                SerialPort1.Open()
                SerialPort1.Write("&")
            Else
                connect = 0
                Button_connect.Text = " CONNECT "
                Button_connect.BackColor = Color.Red
                Button_disconnect.BackColor = Color.Blue
                MessageBox.Show("Đã ngắt kết nối", "Thông Báo", MessageBoxButtons.OK)
                SerialPort1.Close()
                chonCOM.Enabled = True
                chonbaudrate.Enabled = True
                Button1.Enabled = True
                Button_senddata.Enabled = False
            End If
        End If
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived  
                readBuffer = SerialPort1.ReadExisting()         
    End Sub

    Private Sub Button_disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_disconnect.Click
        If SerialPort1.IsOpen Then
            SerialPort1.Write("#")
            SerialPort1.DiscardInBuffer()
            SerialPort1.Close()
            MessageBox.Show(" Da Ngat Ket Noi", " Thong Bao", MessageBoxButtons.OK)
            Button_disconnect.BackColor = Color.Blue
            Button_connect.BackColor = Color.Red
            Button_connect.Enabled = True
            Button_disconnect.Enabled = True
        End If
    End Sub
    Public Sub DoUpdate(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If readBuffer = "&" Then
            MessageBox.Show("Ket noi thanh cong", "Thong bao", MessageBoxButtons.OK)
            Timer1.Enabled = False
            readBuffer = Nothing
        End If
    End Sub

    Private Sub Button_senddata_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_senddata.Click
        Timer1.Enabled = True
        SerialPort1.Write("@")
        temp = 1

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If temp = 1 Then
            SerialPort1.Write(txtsend.Text)
            temp = 2
        ElseIf temp = 2 Then
            SerialPort1.Write("$")
            Timer1.Enabled = False
            temp = 0
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If temp = 2 Then
            SerialPort1.Write("$")
            Timer2.Enabled = False
            temp = 0

        End If
        If temp = 1 Then
            SerialPort1.Write(txtsend.Text)
            Timer2.Enabled = False
            temp = 2
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If MsgBox("Do you want to exit?", MsgBoxStyle.YesNo + MsgBoxStyle.Question, "EXIT PROGRAM") = MsgBoxResult.Yes Then
            End
        End If
    End Sub
End Class

Cảm ơn các bạn đã quan tâm,Like và share Web để cập nhật các chương trình lý thú cho các bạn nhé.!!!

Previous
Next Post »