Click here to Skip to main content
Licence 
First Posted 14 Apr 2004
Views 30,641
Bookmarked 8 times

8-bits paralel port..C# version..

By | 21 Apr 2004 | Article
8-bits paralel port..C# version..It's done with mod(%) approach..

Sample Image - paralelport_MODapproach.jpg

Introduction

This article is about the paralel ports and its basic implementation on the 8 Leds (Light Emiting Diodes). Just try the program and see how the buttons' colors are changing according to the entered input into the textbox..I have to remind you the basic logic:

+-----------------

            181

 

21 x 0 =0

20 x 1 =1

22 x 1 =4

23 x 0 =0

27 x 1 =128

26 x 0 =0

25 x 1 =32

24 x 1 =16

1 0 1 1 0 1 0 1

This means that if you enter 181 as input into the textbox and click "Enter the data" then first, third, fourth, sixth and eighth leds will light on..Why??..Inside 181 there is 1x 128 so 1. button will light on...181-128=53...Inside 53 there is no 1x64 but there is 1x32  so second button will NOT light on but third will...etc. Any way!..The code is in the following..examine it try to understand...have a nice programming day.

here is the code:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.Button button3;
  private System.Windows.Forms.Button button4;
  private System.Windows.Forms.Button button5;
  private System.Windows.Forms.Button button6;
  private System.Windows.Forms.Button button7;
  private System.Windows.Forms.Button button8;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label5;
  private System.Windows.Forms.Label label6;
  private System.Windows.Forms.Label label7;
  private System.Windows.Forms.Label label8;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button9;
  private System.Windows.Forms.Button button10;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.button2 = new System.Windows.Forms.Button();
   this.button3 = new System.Windows.Forms.Button();
   this.button4 = new System.Windows.Forms.Button();
   this.button5 = new System.Windows.Forms.Button();
   this.button6 = new System.Windows.Forms.Button();
   this.button7 = new System.Windows.Forms.Button();
   this.button8 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.label5 = new System.Windows.Forms.Label();
   this.label6 = new System.Windows.Forms.Label();
   this.label7 = new System.Windows.Forms.Label();
   this.label8 = new System.Windows.Forms.Label();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button9 = new System.Windows.Forms.Button();
   this.button10 = new System.Windows.Forms.Button();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.BackColor = System.Drawing.Color.Black;
   this.button1.Location = new System.Drawing.Point(8, 16);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(56, 40);
   this.button1.TabIndex = 0;
   //
   // button2
   //
   this.button2.BackColor = System.Drawing.Color.Black;
   this.button2.Location = new System.Drawing.Point(80, 16);
   this.button2.Name = "button2";
   this.button2.Size = new System.Drawing.Size(56, 40);
   this.button2.TabIndex = 1;
   //
   // button3
   //
   this.button3.BackColor = System.Drawing.Color.Black;
   this.button3.Location = new System.Drawing.Point(152, 16);
   this.button3.Name = "button3";
   this.button3.Size = new System.Drawing.Size(56, 40);
   this.button3.TabIndex = 2;
   //
   // button4
   //
   this.button4.BackColor = System.Drawing.Color.Black;
   this.button4.Location = new System.Drawing.Point(224, 16);
   this.button4.Name = "button4";
   this.button4.Size = new System.Drawing.Size(56, 40);
   this.button4.TabIndex = 3;
   //
   // button5
   //
   this.button5.BackColor = System.Drawing.Color.Black;
   this.button5.Location = new System.Drawing.Point(296, 16);
   this.button5.Name = "button5";
   this.button5.Size = new System.Drawing.Size(56, 40);
   this.button5.TabIndex = 4;
   //
   // button6
   //
   this.button6.BackColor = System.Drawing.Color.Black;
   this.button6.Location = new System.Drawing.Point(368, 16);
   this.button6.Name = "button6";
   this.button6.Size = new System.Drawing.Size(56, 40);
   this.button6.TabIndex = 5;
   //
   // button7
   //
   this.button7.BackColor = System.Drawing.Color.Black;
   this.button7.Location = new System.Drawing.Point(440, 16);
   this.button7.Name = "button7";
   this.button7.Size = new System.Drawing.Size(56, 40);
   this.button7.TabIndex = 6;
   //
   // button8
   //
   this.button8.BackColor = System.Drawing.Color.Black;
   this.button8.Location = new System.Drawing.Point(512, 16);
   this.button8.Name = "button8";
   this.button8.Size = new System.Drawing.Size(56, 40);
   this.button8.TabIndex = 7;
   //
   // label1
   //
   this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label1.ForeColor = System.Drawing.Color.DarkBlue;
   this.label1.Location = new System.Drawing.Point(16, 72);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(40, 16);
   this.label1.TabIndex = 8;
   this.label1.Text = "Led 8";
   //
   // label2
   //
   this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label2.ForeColor = System.Drawing.Color.DarkBlue;
   this.label2.Location = new System.Drawing.Point(88, 72);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(40, 16);
   this.label2.TabIndex = 9;
   this.label2.Text = "Led 7";
   //
   // label3
   //
   this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label3.ForeColor = System.Drawing.Color.DarkBlue;
   this.label3.Location = new System.Drawing.Point(160, 72);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(40, 16);
   this.label3.TabIndex = 10;
   this.label3.Text = "Led 6";
   //
   // label4
   //
   this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label4.ForeColor = System.Drawing.Color.DarkBlue;
   this.label4.Location = new System.Drawing.Point(232, 72);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(40, 16);
   this.label4.TabIndex = 11;
   this.label4.Text = "Led 5";
   //
   // label5
   //
   this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label5.ForeColor = System.Drawing.Color.DarkBlue;
   this.label5.Location = new System.Drawing.Point(304, 72);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(40, 16);
   this.label5.TabIndex = 12;
   this.label5.Text = "Led 4";
   //
   // label6
   //
   this.label6.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label6.ForeColor = System.Drawing.Color.DarkBlue;
   this.label6.Location = new System.Drawing.Point(376, 72);
   this.label6.Name = "label6";
   this.label6.Size = new System.Drawing.Size(40, 16);
   this.label6.TabIndex = 13;
   this.label6.Text = "Led 3";
   //
   // label7
   //
   this.label7.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label7.ForeColor = System.Drawing.Color.DarkBlue;
   this.label7.Location = new System.Drawing.Point(448, 72);
   this.label7.Name = "label7";
   this.label7.Size = new System.Drawing.Size(40, 16);
   this.label7.TabIndex = 14;
   this.label7.Text = "Led 2";
   //
   // label8
   //
   this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(162)));
   this.label8.ForeColor = System.Drawing.Color.DarkBlue;
   this.label8.Location = new System.Drawing.Point(520, 72);
   this.label8.Name = "label8";
   this.label8.Size = new System.Drawing.Size(40, 16);
   this.label8.TabIndex = 15;
   this.label8.Text = "Led 1";
   //
   // textBox1
   //
   this.textBox1.AcceptsTab = true;
   this.textBox1.BackColor = System.Drawing.Color.DarkBlue;
   this.textBox1.ForeColor = System.Drawing.Color.Yellow;
   this.textBox1.Location = new System.Drawing.Point(16, 104);
   this.textBox1.Name = "textBox1";
   this.textBox1.Size = new System.Drawing.Size(224, 20);
   this.textBox1.TabIndex = 16;
   this.textBox1.Text = "";
   //
   // button9
   //
   this.button9.ForeColor = System.Drawing.Color.DarkBlue;
   this.button9.Location = new System.Drawing.Point(8, 136);
   this.button9.Name = "button9";
   this.button9.Size = new System.Drawing.Size(112, 48);
   this.button9.TabIndex = 17;
   this.button9.Text = "Enter The Data";
   this.button9.Click += new System.EventHandler(this.button9_Click);
   //
   // button10
   //
   this.button10.ForeColor = System.Drawing.Color.DarkBlue;
   this.button10.Location = new System.Drawing.Point(136, 136);
   this.button10.Name = "button10";
   this.button10.Size = new System.Drawing.Size(112, 48);
   this.button10.TabIndex = 18;
   this.button10.Text = "Reset the Leds";
   this.button10.Click += new System.EventHandler(this.button10_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(576, 189);
   this.Controls.Add(this.button10);
   this.Controls.Add(this.button9);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.label8);
   this.Controls.Add(this.label7);
   this.Controls.Add(this.label6);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button8);
   this.Controls.Add(this.button7);
   this.Controls.Add(this.button6);
   this.Controls.Add(this.button5);
   this.Controls.Add(this.button4);
   this.Controls.Add(this.button3);
   this.Controls.Add(this.button2);
   this.Controls.Add(this.button1);
   this.MaximizeBox = false;
   this.Name = "Form1";
   this.Text = "ParalelPort (LEDs)";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button9_Click(object sender, System.EventArgs e)
  {
   int num;
   if(textBox1.Text=="")
   {
    MessageBox.Show("Please enter the number..","NUMBER");
    return;
   }
   num=Convert.ToInt32(textBox1.Text);
   
   if(num>255 || num<0)
   {
    MessageBox.Show("Please enter number between 0 - 255..","0 - 255");
    textBox1.Text="";
    return;
   }

   int x1=num%128; // if 169 is entered x1=41
   int x2=num/128; // if 169 is entered x2=1
   if(x2==1)
    this.button1.BackColor=System.Drawing.Color.White;
   
   num=x1;
   x1=num%64;
   x2=num/64;
   if(x2==1)
    this.button2.BackColor=System.Drawing.Color.White;
   
 
   num=x1;
   x1=num%32;
   x2=num/32;
   if(x2==1)
    this.button3.BackColor=System.Drawing.Color.White;

   num=x1;
   x1=num%16;
   x2=num/16;
   if(x2==1)
    this.button4.BackColor=System.Drawing.Color.White;

   num=x1;
   x1=num%8;
   x2=num/8;
   if(x2==1)
    this.button5.BackColor=System.Drawing.Color.White;
   
   num=x1;
   x1=num%4;
   x2=num/4;
   if(x2==1)
    this.button6.BackColor=System.Drawing.Color.White;
   
   num=x1;
   x1=num%2;
   x2=num/2;
   if(x2==1)
    this.button7.BackColor=System.Drawing.Color.White;
   
   num=x1;
   x1=num%1;
   x2=num/1;
   if(x2==1)
    this.button8.BackColor=System.Drawing.Color.White;
   
   

  }

  private void button10_Click(object sender, System.EventArgs e)
  {
   textBox1.Text="";
   this.button1.BackColor=System.Drawing.Color.Black;
   this.button2.BackColor=System.Drawing.Color.Black;
   this.button3.BackColor=System.Drawing.Color.Black;
   this.button4.BackColor=System.Drawing.Color.Black;
   this.button5.BackColor=System.Drawing.Color.Black;
   this.button6.BackColor=System.Drawing.Color.Black;
   this.button7.BackColor=System.Drawing.Color.Black;
   this.button8.BackColor=System.Drawing.Color.Black;
  }
 }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

mehmetydm

Web Developer

Turkey Turkey

Member

Graduated from EMU as a Computer Engineer, working for a state association as a programmer/developer.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questioncompiling Pinmembervane40721:42 1 Jul '11  
GeneralMy vote of 1 Pinmember_Nic_4:11 8 Feb '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 22 Apr 2004
Article Copyright 2004 by mehmetydm
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid