Friday, 12 May 2017

Interfacing Software with PIC Microcontroller Part1


Electronics design have gone far to the extent we can control any gadget either by web, smartphones or computer. In this article, I will be controlling my LEDs from the computer software created using Visual Studio 2015 in C#.  The Part1 of this project is to design the software and to use it to control LEDs in Proteus Simulating Software before the real world circuit Assembling. Please note that the application of this project is very wide as it can be used to control Home Appliances from PC, Wireless Robotic Car control or Industrial Automation (PLC).

The project is divided into four parts:

  •  The Visual Studio Code (Computer Software)
  •  The MikroC Code (Microcontroller Programming)
  •  The Proteus Simulation Circuit Design
  •  The Serial Port Virtual Emulator 


Visual Studio Code

Visual studio IDE  serves a very big role in software developing especially for embedded system applications, The code is compatible with visual studio 2010 - 2015. 

 

The code for the software is written below, just copy and paste it into your Visual C# project


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SoftwareControl
{
    public partial class Form1 : Form
    {
        int statusLED1 = 0, statusLED2 = 0, statusLED3 = 0;
        public Form1()
        {
         
            InitializeComponent();
       
        }
   

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if(serialPort1.IsOpen== true)
            {
                serialPort1.Close();
                button4.Text = "Connect";
               
            }
            else
            {
                serialPort1.BaudRate = 9600;
                serialPort1.PortName = comboBox1.SelectedItem.ToString();
                serialPort1.Open();
                button4.Text = "Disconnect";

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                serialPort1.Write("1");
                if (statusLED1 == 0)
                {
                    statusLED1 = 1; button1.BackColor = Color.Yellow;
                }
                else
                {
                    statusLED1 = 0; button1.BackColor = Color.WhiteSmoke;
                }
               
            }
            else
            {
                MessageBox.Show("Connect to Com Port First", "Click Connect");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                serialPort1.Write("2");

                if (statusLED2 == 0)
                {
                    statusLED2 = 1; button2.BackColor = Color.Yellow;
                }
                else
                {
                    statusLED2 = 0; button2.BackColor = Color.WhiteSmoke;
                }
            }
            else
            {
                MessageBox.Show("Connect to Com Port First", "Click Connect");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == true)
            {
                serialPort1.Write("3");
                if (statusLED3 == 0)
                {
                    statusLED3 = 1; button3.BackColor = Color.Yellow;
                }
                else
                {
                    statusLED3 = 0; button3.BackColor = Color.WhiteSmoke;
                }
            }
            else
            {
                MessageBox.Show("Connect to Com Port First", "Click Connect");
            }
        }
    }
}



Here is the software design below

                                  



MikroC Programming

MikroC compiler is one of the most easiest and fastest microcontroller C compiler with many inbult libraries for users. i used this compiler for this project so that it will be very easy for my readers to understand the code, you can download the compiler Here.
 Please note that the compiler has a demo limit but you can message me how to crack it.

 



The PIC16F628A is used int this project and you can just copy and paste the code to your project.


              unsigned char Recieved=0;
void main() {
              TrisA = 0x00; PortA =0x00;
              TrisB = 0x06; PortB =0x00;
              CMCON=0x07;
              UART1_Init(9600);
             
              while(1)
              {
               RCREG =0x00;
               while(RCREG ==0x00);
               Recieved = RCREG;
                if(Recieved == '1') PortB.B3 = ~PortB.B3;
                if(Recieved == '2') PortB.B4 = ~PortB.B4;
                if(Recieved == '3') PortB.B5 = ~PortB.B5;
              }
             
}


Proteus Simulation

Proteus Professional software is a circuit simulating software which can be download here, note that the project is designed with Proteus 7.10. 

 





Virtual Serial Port Emulation Software (VSPE)

In order to communicate between the Computer software and the microcontroller on proteus then we need an emulator to create a virtual COM Port that didnt expect on our computer. 

VSPE (Virtual Serial Port Emulator) is a software which provides us the utility to access multiple com ports on the same PC. We can create new serial ports virtually and can connect them as we want. For example I can create 2 serial COM ports and if I pair them then they will be connected with each other. So that I can run two applications communicating with each other using different com ports.

The best application of this software is for the simulation of electronic circuits, which are interfacing with the any serial devices like GSM modem, RFID, Bluetooth, PC etc.  We can create any circuit in Proteus ISIS simulation software. 

Then we can run it and also give inputs such as switches, signal generators etc. but to communicate with serial devices we can not use it directly. We need to create the hardware and then need to interface it with our PC physically. Then access it via Hyperterminal, RealTerm, Terminal or any software which can communicate with COM port. But this is not a reliable solution. So here the VSPE comes in picture. So the idea would be to:
Create circuit in the proteus. Then open any terminal app such as Hyeper terminal or realterm. Here I am taking example as Real Term. Choose choose two different comports in Proteus and Real Term. So that for our proteus circuit we can monitor and control serial data from our PC itself virtually.
Here I am showing an example of simplest tutorial on VSPE: “Connecting 2 com ports and testing them.

  1. Download VSPE software as per your PC configuration 64 bit or 32 bit:       http://www.eterlogic.com/Products.VSPE.htm
  2. Install them. Open VSPE. Click the “Create New device..” button 
  3. Select pair option from the drop down list. Click next.                                
  4. Select available com ports as per your PC. Here I am taking example as COM1 and COM2. You can choose any. But remember later on you need to select same ports while using them in your applications. Click finish.      
  5. Then you should come back to home screen of VSPE. You should see notification that COM1 and COM2 Running. If not so you press the green coloured Play button and you will get notification below: 
    6. then minimize.


Simulating the Project

click on the software drop down menu to select comport and click on Connect button.

                          

 Run your circuit diagram on Proteus and set the ComPort for it and click OK.

                          


Running Project for testing

 



or visit our youtube link below   https://youtu.be/mtBO_LAg058

you can also download project from here

 

thank you for reading.






No comments:

Post a Comment