วันจันทร์ที่ 14 มกราคม พ.ศ. 2562

PLC (Programmable Logic Controller) with Microchip Pic Microcontroller

An year ago i made a plc (programmable logic controller) using microchip pic microcontroller. Its a 16 bit digital input and output programmable logic controller. Plc programming is done using a standard ladder logic language. The software that is used for plc programming and ladder logic design is an open source compiler written by Jonathan Westhues called LDMicro. Microchip pic microcontroller that i used in the project is an 8-bit pic16f877a.  

Core PLC (Programmable Logic Controller) board

Core plc board is comprised of 16 digital inputs and 16 digital outputs. Inputs are opto isolated. Optocouplers are used to protect the circuit/processor from heavy loads damage. Diodes are used at inputs from reverse polarity protection. At output side i used 16 relays for switching heavy loads. Relays are rated as +12v. They are derived from Uln2803 Ic.Uln2803 is an 8-channel darling-ton array high output current ic. Uln2803 can sink 500mA of current from a 50V power supply. It has built in fly-back diodes for driving coils and prevents from back efm damage. Relay coils are directly connected to the uln2803 output and does not need the fly back diodes, since they are build inside ic. Core plc board processor is pic16f877a working at 20MHz. Whole board is powered using a +12v dc supply. LM7805 regulator is used to convert the +12v to +5v for power supply to microcontroller, opto couplers and uln2803 relay driver.

For programming the board/microcontroller/pic16f877A pickit 2 programmer is made on the board. Pickit 2 is a programmer by microchip used to program its pic microcontrollers. Pickit 2 uses icsp interface to program the target microcontroller. Icsp interface of pickit 2 programmer is connected with the pic16f877a microcontroller using DIP switches. When ever a new logic is desired to be downloaded in the processor, switch on the dip switches. Dip switches are installed to prevent the controller pins from interference by the pickit 2 programmer circuit. I have a separate tutorial on pickit 2 programmer made at home. Visit the link for more information
Microchip Pickit 2 Programmer made at home - Diy Project
The Resistors on the front side of the pcb that are not aligned are added after the pcb printing. These resistors are added as pull-ups to the input pins. While testing the circuit i found that the input pins of pic16f877 floats so i added the pull-up resistors. On pcb back i added some extra tin/lead on the traces to remove any resistance created in them during the etching process.  
pic microcontroller plc back pcb
Microchip pic microcontroller plc- 16 digital input/output with on board pic microcontroller programmer pickit 2

Circuit/Block diagram and Pcb of Core PLC board.

                              Plc with microchip pic microcntroller block diagram
Plc with microchip pic microcntroller Circuit diagram- Made in Proteaus

​PLC status board

Status lcd board purpose is to show the status of the plc inputs and outputs. Four 74ls574 8-bit latches on status board are used to store the input and output status of plc. Two latches store input status and the remaining two store the output status. A 40x4 character lcd on status board is used to show the plc status visually. Active pin is shown as up arrow and the non active pin is shown as down arrow. Status lcd core processor is 89c51 microcontroller. Port-0 of 89c51 has a common 8-bit bus, shared with all the latches. Latches are activated sequentially to obtain the stored result and prevent from data collusion. Latches data is processed and then displayed on the 40x4 character lcd. Lcd is connected to port-1 of 89c51 microcontroller. Lcd is interfaced with 89c51 in 8-bit mode. Status board works on +12v dc.

Some tutorials that will help you in understanding the interfaces and working of the status board.
  • 74ls574 data/address Latches with 89c51 microcontroller. 
  • Character Lcd in 8-bit mode with microcontroller
  • Displaying custom character on lcd using 89c51 microcontroller​ 
  • Pic16f877 microcontroller PLC (Programmable Logic Controller)PCB back
  • Pic16f877 microcontroller PLC (Programmable Logic Controller) Input Output Status board
  • Picture
  • Plc Status board CircuitBelow pictures shows the status of a process that is controlled using the plc microcontroller processor. Circuit of status board is made in proteaus. Pcb of the status board is also designed in proteaus. Download the status board microcontroller code from the links given at the bottom of the post. Code is written using keil uvision-4.

  • Pic microcontroller plc status board
  • Microchip pic plc status board circuit diagram- Made in Proteaus
  • Ladder logic design and processor selection

    Below is a simple ladder logic program written in ldmicro. Microcontroller for which the ladder design is made is shown below with the clock frequency on which the microcontroller is operating. I used an external 20Mhz crystal in core plc board. So i selected the crystal frequency of 20Mhz in ldmicro. On the left side a simple code flow is shown. We write code in ldmicro, test the logic. Compile the logic and extract the hex file. This hex file is then loaded to the pic16f877a using pikit-2 programmer. 

How to make(build) a Calculator using Pic16f877 microcontroller

                                            Calculator build Using Pic16f877a microcontroller - Circuit Diagram
Calculator with Pic 16f877 microcontroller is a comprehensive project. Comprehensive in the way that code is lengthy and logical. It will be hard for newbies to understand the code and circuit components interface with pic microcontroller. I will try to explain each and every thing about code and circuit thoroughly. The project covers all the aspects of programming and interfacing of 16x2 character lcd and 4x4 numeric keypad and pic16f877 microcontroller. One should already know about 16x2 lcd working and 4x4 numeric keypad interface with microcontroller before beginning with the project. I have a good tutorial on 16x2 lcd pin out and working. You must first go with this tutorial if you don't know about 16x2 lcd pin out, working principle and interface with microcontrollers.

What pic microcontroller calculator offers?

I am going to build a 2-Digit calculator with pic microcontroller. Four functions addition, subtraction, multiplication and division(+,-,/,*) can be performed on single digit numbers using the calculator made with pic microcontroller. User Inputs the numbers and operator using 4x4 numeric keypad and pic microcontroller after manipulating the numbers displays result on 16x2 lcd.

Calculator with pic microcontroller - Working

When you turn on/power the system. A message will be displayed on the 16x2 lcd first row "Enter First No = ". You enter the number. As soon as you enter number it will be displayed on the 16x2 lcd screen. Then a second message will appear on 16x2 lcd first row asking to "Enter Operator = ". You entered the operator. Then a third message will prompt up saying "Enter Second No= ". You entered the second number. When you are done with entering the second number. Result will be displayed on 16x2 lcd. Calculator arithmetic operation result is shown below.
Calculator with pic16f877 microcontroller output
Calculator with pic16f877 microcontroller output

Pic16f877 microcontroller calculator - Project requirements

  • Pic16f877 Microcontroller
  • 16x2 lcd 
  • 4x4 keypad
  • Crystal 20 MHz
  • Potentiometer/variable resistor (For setting Lcd Contrast)
  • Bread board or PCB for Circuit Designing
  • Power Supply

Two digit calculator with pic microcontroller - Circuit diagram



Lcd data pins are connected to Port-B of PIC16f877 Microcontroller. Lcd Controlling pins RS(Register Select) and EN(Enable) are connected to Port-D Pin# 6 & 7. Keypad is interfaced with Port-C of pic microcontroller. Rows of keypad are connected to lower nibble of Port-C. Coulombs are connected to upper nibble of Port-C. Apply 5 volts at VDD & VCC Pins of microcontroller and 16x2 lcd display. Ground VSS Pins of pic microcontroller and lcd display. Crystal is connected to Pin# 13 & 14 of PIC16f877 microcontroller. Circuit diagram of pic microcontroller calculator project is given below.
CODE
#include< htc.h>
#define _XTAL_FREQ 20e6
#define rs RD6
#define en RD7
#define r0 RC0
#define r1 RC1
#define r2 RC2
#define r3 RC3
#define c0 RC4
#define c1 RC5
#define c2 RC6
#define c3 RC7

void lcdcmd  (unsigned char);
void lcddata (unsigned char);
void disp_num(float num);
int get_num  (char ch);
void lcdinit ();
char scan_key(void);

unsigned char  s[]={"ENTER 1 NO= "};
unsigned char s1[]={"ENTER 2 NO= "};
unsigned char s2[]={"OPERATOR = "};
unsigned char s3[]={"***RESULT***"};

void lcdinit(){
 __delay_ms(400);	lcdcmd(0x30);	__delay_ms(400);	lcdcmd(0x30);
__delay_ms(400);	lcdcmd(0x30);	__delay_ms(400);	lcdcmd(0x38);
    lcdcmd(0x0F);	lcdcmd(0x01);	lcdcmd(0x06);	lcdcmd(0x80);
}

void main (void)
  {
    TRISC=0xF0;        //Rows Output, Coulombs Input
    TRISB=0x00;        //Port-B as Output
    TRISD6=0;          //Port-D PIN-6 as Output
    TRISD7=0;          //Port-D PIN-7 as Output
    __delay_ms(400);
  
   unsigned int count=0;
   int k2,k1; 
   char ke,key,key1;
   lcdinit();        //Initializing Lcd

 while(1){   
      while(s[count]!='\0')     //Displaying String s on LCD
      {
        lcddata(s[count]);
        count++;
      }

ke=scan_key();   //Scan the First Digit
k2=get_num(ke);  //Converting Char into number
lcdcmd(0x01);    //Clear Lcd
count=0;

while(s2[count]!='\0')    //Displaying String s2 on LCD
{
lcddata(s2[count]);
count++;
}

key=scan_key();  //Scaning operator
lcdcmd(0x01);    //Cleat Lcd
count=0;

while(s1[count]!='\0')  //Displaying String s1 on LCD
{
lcddata(s1[count]);
count++;
}

key1=scan_key();  //Scan Second digit
k1=get_num(key1); //Converting Char into number
lcdcmd(0x01);     //Clear Lcd
lcdcmd(0x82);     //Start displying data on lcd at position Row=1 Coulomb=3
count=0;

while(s3[count]!='\0')  //Displaying String s3 on LCD
{
lcddata(s3[count]);
count++;
}
count=0;
lcdcmd(0xC0);   //Jump to second Line of Lcd
lcddata(ke);
lcddata(key);
lcddata(key1);
lcddata(' ');
lcddata('=');

switch(key)
{
case '+':
disp_num(k1+k2);
break;
case '-':
disp_num(k2-k1);
break;
case '*':
disp_num(k2*k1);
break;
case '/':
disp_num((float)k2/k1);
break;
}
}
}

void lcdcmd(unsigned char value)  //Sending Commands to Lcd
  {
    PORTB = value;      
    rs = 0;
    en = 1;            
    __delay_ms(100);
    en = 0;
    __delay_ms(100);    
  }
void lcddata(unsigned char value) //Sending Data to Lcd
  {
    PORTB = value;  
    rs = 1;
    en = 1;          
    __delay_ms(100);
    en = 0;
    __delay_ms(100);
  }
  


char scan_key()      //Scan the Pressed Key by user 
{
unsigned char c='s';
while(c!='a')
{
r0=0;r1=1;r2=1;r3=1;
if(c0==1 && r0==0){ lcddata('7');__delay_ms(500);return '7';c='a';}
if(c1==1 && r0==0){ lcddata('8');__delay_ms(500);return '8';c='a';}
if(c2==1 && r0==0){ lcddata('9');__delay_ms(500);return '9';c='a';}
if(c3==1 && r0==0){ lcddata('/');__delay_ms(500);return '/';c='a';}
r0=1;r1=0;r2=1;r3=1;
if(c0==1 && r1==0){ lcddata('4');__delay_ms(500);return '4';c='a';}
if(c1==1 && r1==0){ lcddata('5');__delay_ms(500);return '5';c='a';}
if(c2==1 && r1==0){ lcddata('6');__delay_ms(500);return '6';c='a';}
if(c3==1 && r1==0){ lcddata('*');__delay_ms(500);return '*';c='a';}
r0=1;r1=1;r2=0;r3=1;
if(c0==1 && r2==0){ lcddata('1');__delay_ms(500);return '1';c='a';}
if(c1==1 && r2==0){ lcddata('2');__delay_ms(500);return '2';c='a';}
if(c2==1 && r2==0){ lcddata('3');__delay_ms(500);return '3';c='a';}
if(c3==1 && r2==0){ lcddata('-');__delay_ms(500);return '-';c='a';}
r0=1;r1=1;r2=1;r3=0;
if(c1==1 && r3==0){ lcddata('0');__delay_ms(500);return '0';c='a';}
if(c3==1 && r3==0){ lcddata('+');__delay_ms(500);return '+';c='a';}
}
return 0;
}

int get_num(char ch)         //converting character into integer
{
switch(ch)
{
case '0': return 0; break;
case '1': return 1; break;
case '2': return 2; break;
case '3': return 3; break;
case '4': return 4; break;
case '5': return 5; break;
case '6': return 6; break;
case '7': return 7; break;
case '8': return 8; break;
case '9': return 9; break;
}
return 0;
}

void disp_num(float num)            //Displays calculated value on LCD
{
unsigned char UnitDigit  = 0;  //Contains unit digit of calculated value
unsigned char TenthDigit = 0;  //contains 10th digit of calculated value
unsigned char decimal = 0;
int j,numb;

j=(int)(num*10);
numb=(int)num;

if(numb<0)
{
numb = -1*numb;  // Make number positive
lcddata('-');	// Display a negative sign on LCD
}

TenthDigit = (numb/10);	        // Findout Tenth Digit

if( TenthDigit != 0)	        // If it is zero, then don't display
lcddata(TenthDigit+0x30);	// Make Char of TenthDigit and then display it on LCD

UnitDigit = numb - (TenthDigit*10);

lcddata(UnitDigit+0x30);	// Make Char of UnitDigit and then display it on LCD
lcddata('.');
decimal=(j%10)+0x30;             //Display If any value after Decimal Point
lcddata(decimal);
__delay_ms(3000);
lcdcmd(0x01);
}

Internet of Things (IoT)

Internet of Things หรือ IoT  Internet of Things (IoT)  คือ การที่อุปกรณ์อิเล็กทรอนิกส์ต่างๆ สามารถเชื่อมโยงหรือส่งข้อมูลถึงกันได้ด้วยอิ...