E5144 (lab 4)

8
Title: Counter Objective: To count number of data port from lowest to highest (D0-D7) using Visual Basic. Equipment’s: 1. Computer. 2. Visual Basic 6.0 3. Printer Cable 4. 8 LED 5. Resistor 1kΩ (8) 6. Protoboard Introduction: VISUAL BASIC is a high level programming language evolved from the earlier DOS version called BASIC. BASIC means Beginners' All purpose Symbolic Instruction Code. It is a fairly easy programming language to learn. The codes look a bit like English Language. On start up, Visual Basic 6.0 will display the dialog box as we can choose to start a new project, open an existing project or select a list of recently opened programs. A project is a collection of files that make up your application. There are various types of applications we could create, however, we shall concentrate on creating Standard EXE programs(EXE means executable program). Visual Basic Environments consists of the Blank Form window which can design application's interface , the project window displays the files that are created in the application and properties window which displays the properties of various controls and objects that are created in applications. It also includes a Toolbox that consists of all the controls essential for developing a VB Application. Controls are tools such as boxes, buttons, labels and other objects draw on a form to get input or display output. They also add visual appeal. To effectively control the VB program flow, we shall use If...Then...Else statement together with the conditional operators and logical operators.

description

politeknik

Transcript of E5144 (lab 4)

Page 1: E5144 (lab 4)

Title: Counter

Objective:

To count number of data port from lowest to highest (D0-D7) using Visual Basic.

Equipment’s:

1. Computer.2. Visual Basic 6.03. Printer Cable4. 8 LED5. Resistor 1kΩ (8)6. Protoboard

Introduction:

VISUAL BASIC is a high level programming language evolved from the earlier DOS version called BASIC. BASIC means Beginners' All purpose Symbolic Instruction Code. It is a fairly easy programming language to learn. The codes look a bit like English Language. On start up, Visual Basic 6.0 will display the dialog box as we can choose to start a new project, open an existing project or select a list of recently opened programs. A project is a collection of files that make up your application. There are various types of applications we could create, however, we shall concentrate on creating Standard EXE programs(EXE means executable program). Visual Basic Environments consists of the Blank Form window which can design application's interface , the project window displays the files that are created in the application and properties window which displays the properties of various controls and objects that are created in applications. It also includes a Toolbox that consists of all the controls essential for developing a VB Application. Controls are tools such as boxes, buttons, labels and other objects draw on a form to get input or display output. They also add visual appeal. To effectively control the VB program flow, we shall use If...Then...Else statement together with the conditional operators and logical operators.

The general format for the if...then...else statement is If conditions Then

VB expressions

Else

VB expressions

End If

* any If.. Then.. Else statement must end with End If. Sometime it is not necessary to use Else.

Page 2: E5144 (lab 4)

Steps:

Form

1. In program declare first Dim I As Integer at general.2. Create a button name it as cmdcount and caption count.3. Add a label name it as lblcount and caption 0 change the font size to 24.4. Click twice for the button and add this program inside.

Module5. Add module in your form with in port 32 by copying the source code for the lab before.6. Run the interface.

Question:

1. What is the code that instructs to move led lighting from the led to other led?ElseIf.

2. What does it mean by Dim?Dim is a declaration statement. It is an abbreviation for Declare in Memory.

3. Why we must put a declaration of i?‘I’ is the variable that can be change every time we want to count.

Page 3: E5144 (lab 4)

Exercise

Make a counter that can count number of data port from highest to lowest : D7-D0.

The answer.

The programming.

Dim i As IntegerPrivate Sub chkD0_Click()If LED0.FillColor = &HFFFFFF Then LED0.FillColor = &HFF& Else LED0.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD1_Click()If LED1.FillColor = &HFFFFFF Then LED1.FillColor = &HFF& Else LED1.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD2_Click()If LED2.FillColor = &HFFFFFF Then LED2.FillColor = &HFF& Else LED2.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD3_Click()If LED3.FillColor = &HFFFFFF Then LED3.FillColor = &HFF& Else LED3.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD4_Click()If LED4.FillColor = &HFFFFFF Then LED4.FillColor = &HFF& Else LED4.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD5_Click()If LED5.FillColor = &HFFFFFF Then

Page 4: E5144 (lab 4)

LED5.FillColor = &HFF& Else LED5.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD6_Click()If LED6.FillColor = &HFFFFFF Then LED6.FillColor = &HFF& Else LED6.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub chkD7_Click()If LED7.FillColor = &HFFFFFF Then LED7.FillColor = &HFF&Else LED7.FillColor = &HFFFFFFEnd IfEnd SubPrivate Sub cmdcount_Click()lblcount.Caption = lblcount.Caption - 1i = i + 1If i = 1 Then LED7.FillColor = &HFF& LED0.FillColor = &HFFFFFF ' off ElseIf i = 2 Then LED6.FillColor = &HFF& LED7.FillColor = &HFFFFFF ' off ElseIf i = 3 Then LED5.FillColor = &HFF& LED6.FillColor = &HFFFFFF ' off ElseIf i = 4 Then LED4.FillColor = &HFF& LED5.FillColor = &HFFFFFF ' off ElseIf i = 5 Then LED3.FillColor = &HFF& LED4.FillColor = &HFFFFFF ' off ElseIf i = 6 Then LED2.FillColor = &HFF& LED3.FillColor = &HFFFFFF ' off ElseIf i = 7 Then LED1.FillColor = &HFF& LED2.FillColor = &HFFFFFF ' off ElseIf i = 8 Then

Page 5: E5144 (lab 4)

LED0.FillColor = &HFF& LED1.FillColor = &HFFFFFF ' off i = 0 End IfEnd SubPrivate Sub cmdreset_Click()chkD7.Value = 0chkD6.Value = 0chkD5.Value = 0chkD4.Value = 0chkD3.Value = 0chkD2.Value = 0chkD1.Value = 0chkD0.Value = 0LED7.FillColor = &HFFFFFF

End SubPrivate Sub Form_Load()chkD7.Value = 0chkD6.Value = 0chkD5.Value = 0chkD4.Value = 0chkD3.Value = 0chkD2.Value = 0chkD1.Value = 0chkD0.Value = 0End Sub

Discussion.

Page 6: E5144 (lab 4)

In this lab, we want to make a program that can count up number from D0 until D7. We use the first lab exercise and do some renovation at the programming. We add button name cmdcount and the caption is count. It will count up. Then we will add label and name as lblcount and change the caption into 0. This label will changing when we click the count command for a few times.

In the programming, we put a new program under the line ‘private Sub cmdcount_Click()’ . We declare ‘i’ as the variable. This variable will stimulate when we give the changing by clicking the command box. We will make a few program to show the ‘ i ‘ will changing by state the number from 1 until 8. The number will count up when we click at the command. We use the ‘Elseif’ structure to show the other choices that will come out. As we have 8 LED, so there will be have 8 choices. We need to declare the general as Dim I As Integer because ‘i’ is integers variable starting from number 0 to 7. Dim is Declare for Memory that means ‘i’ is declare as the integers.

We save the program. Then we run the program to see the result. However, in this lab we are not using the module because if we using it the program cannot be run. We also remove the line ‘Out Val (“&H378”),128’because it not necessary for this program. We must know the correct order for the LED light up if not the LED will not light up.

If i = 1 Then

LED0.FillColor = &HFF&

LED7.FillColor = &HFFFFFF ' off

ElseIf i = 2 Then

LED1.FillColor = &HFF&

LED0.FillColor = &HFFFFFF ' off

From the programming above, if the variable is 1 so the LED0 will turn red and the LED 7 will turn white. This is happened as we set the choices for the LED lightning.

Conclusion.

We can conclude that we must declare first the general as the Dim i as integer so that the variable will come out as a number. We use elseif because we have more than one choices. So, that we can count the number as up or down.