Monday 3 September 2018

8051: 16 switch interface code



Description:

            16 switches are connected in this diagram, port2 connected as the 4pin input and 4 pin output.16 switches are connected in matrix method.Port2 connected with switches and port3 blinks the corresponding binary numbers. In this program, r represents row and c represents column, port2 first set as HIGH and the r1 (first row) set as 0 and c1(first column) set 0,and other columns set as 1 the first switch pressed port3 blinks the corresponding binary value and vice versa.


Code:

#include<reg51.h>
sbit r1=P2^0;
sbit r2=P2^1;
sbit r3=P2^2;
sbit r4=P2^3;
sbit c1=P2^4;
sbit c2=P2^5;
sbit c3=P2^6;
sbit c4=P2^7;
void main()
{
            while(1)
            {
                        P2=0XFF;
                        r1=0;
                        while(c1==0&& c2==1&& c3==1&& c4==1)
                        {
                                    P3=1;
                        }
                        while(c2==0&& c1==1&& c3==1&& c4==1)
                        {
                                    P3=2;
                        }
                        while(c3==0&&c1==1&&c2==1&&c4==1)
                        {
                                    P3=3;
                        }
                        while(c4==0 && c1==1&& c2==1&& c3==1)
                        {
                                    P3=4;
                        }
                        P2=0XFF;
                        r2=0;
                        while(c1==0&& c2==1&& c3==1&&c4==1)
                        {
                                    P3=5;
                        }
                        while(c2==0&&c1==1&&c3==1&&c4==1)
                        {
                                    P3=6;
                        }
                        while(c3==0&&c1==1&&c2==1&&c4==1)
                        {
                                    P3=7;
                        }
                        while(c4==0&&c1==1&&c2==1&&c3==1)
                        {
                                    P3=8;
                        }
                        P2=0XFF;
                        r3=0;
                        while(c1==0&& c2==1&& c3==1&&c4==1)
                        {
                                    P3=9;
                        }
                        while(c2==0&&c1==1&&c3==1&&c4==1)
                        {
                                    P3=10;
                        }
                        while(c3==0&&c1==1&&c2==1&&c4==1)
                        {
                                    P3=11;
                        }
                        while(c4==0&&c1==1&&c2==1&&c3==1)
                        {
                                    P3=12;
                        }
                        P2=0XFF;
                        r4=0;
                        while(c1==0&&c2==1&& c3==1&&c4==1)
                        {
                                    P3=13;
                        }
                        while(c2==0&&c1==1&&c3==1&&c4==1)
                        {
                                    P3=14;
                        }
                        while(c3==0&&c1==1&&c2==1&&c4==1)
                        {
                                    P3=15;
                        }
                        while(c4==0&&c1==1&&c2==1&&c3==1)
                        {
                                    P3=16;
                        }                                                                                                             
            }
}



Circuit Diagram:




No comments:

Post a Comment