Comm Bet 8051 and Labview

20
7-1 Chapter 7 To communicate between LabVIEW and the single chip 8051 Overview Previous chapter we have completed a 8051 serial port experiment, this circuit already has the prototype with serial port interface of general remote instrument, as long as to expand I/ O port of 8051 in order to connect with lots of sensors or control systems. This is a ideal practice circuit for LabVIEW serial port communication, due to 8051 user friendly development, low price, and the flexibility of program design. The practice of the chapter is constructed under this criterion, to use internal timer of 8051 continuously accumulate the counter, and to make command interactive mode of LabVIEW read out value from serial port in order to simulate operation of a general instrument. Objective To writing an 8051 program starts the internal counter, and to construct an interface of interactive command simulate the output data of the general measuring system. On LabVIEW to design an interactive program communicates with 8051 in order to receive and display the data. Keyword Interactive command control Explanation of control Path Controls Text Controls Text Ring Explanation The control item of Text Ring, text input to be divided into a few items, and with the order of sequence number represents each item, starting from 0 to count. The user can arbitrarily add the contents of its item. Path Controls Text Controls Text Ring Explanation The indication item of Text Ring, with same function as control item explained above, only change their function from the control into the indication.

description

gd

Transcript of Comm Bet 8051 and Labview

Page 1: Comm Bet 8051 and Labview

7-1

Chapter 7 To communicate between LabVIEW and the single chip 8051

Overview

Previous chapter we have completed a 8051 serial port experiment, this circuit already has

the prototype with serial port interface of general remote instrument, as long as to expand I/ O

port of 8051 in order to connect with lots of sensors or control systems. This is a ideal practice

circuit for LabVIEW serial port communication, due to 8051 user friendly development, low price,

and the flexibility of program design. The practice of the chapter is constructed under this

criterion, to use internal timer of 8051 continuously accumulate the counter, and to make

command interactive mode of LabVIEW read out value from serial port in order to simulate

operation of a general instrument.

Objective

To writing an 8051 program starts the internal counter, and to construct an interface of

interactive command simulate the output data of the general measuring system.

On LabVIEW to design an interactive program communicates with 8051 in order to

receive and display the data.

Keyword

Interactive command control

Explanation of control Path Controls Text Controls Text Ring

Explanation

The control item of Text Ring, text input to be divided into a few items, and with the order of sequence number represents each item, starting from 0 to count. The user can arbitrarily add the contents of its item.

Path Controls Text Controls Text Ring

Explanation The indication item of Text Ring, with same function as control item explained above, only change their function from the control into the indication.

Page 2: Comm Bet 8051 and Labview

7-2

Path Controls Graph Inds Waveform Graph

Explanation Waveform Graph, can display the curve of chart from the numeric data of variable, and have the capability of memory function, almost the same as the paper tape for recording signals.

Explanation of function

Path All Functions Instrument I/O I/O Serial VISA Configure Serial Port

Explanation Initializes the serial port specified by VISA resource name to the specified settings

Input

VISA resource name specifies the resource to be opened. The parameters need initialization such as: the baud rate、flow control etc

Output If there is a bug, output the error code

Path All Functions Instrument I/O Serial VISA Write

Explanation Writes the data from write buffer to the device or interface specified by VISA resource name.

Input

VISA resource name specifies the resource to be opened.write buffer contains the data to be written to the device. error in describes error conditions that occur before this VI or function runs.

Output

dup VISA resource name is a copy of the VISA resource name that VISA functions return. return count contains the actual number of bytes written. error out contains error information.

Path All Functions Instrument I/O Serial ( VISA Read

Explanation

Reads the specified number of bytes from the device or interface specified by VISA resource name and returns the data in read buffer.

Input

VISA resource name specifies the resource to be opened. byte count is the number of bytes to be read. error in describes error conditions that occur before this VI or function runs. The default is no error.

Output Read in string and output of error codePath All Functions Numeric Conversion

Explanation Converts a string into an array of unsigned bytes.

Input string is the input string the function converts.

Output unsigned byte array is the output array.

Page 3: Comm Bet 8051 and Labview

7-3

Path All Functions Array Index Array

Explanation Returns the element or sub-array of n-dimension array at index.

Input n-dimension array can be an n-dimensional array of any type index 0..n-1 must be numeric. The number of index inputs matches the number of dimensions in n-dimension array.

Output element or subarray has the same type as the elements of n-dimension array.

Brief

In many situations, the communication model of command interactive mode has been

widely used in microcomputer-based automatic control system. The main function among them

is either the boot of control command or the feedback data return. For example, the

communication for the PC peripheral equipment also use command interactive mode for data

transmission, and the MODEM have a command set (AT command) to switch information each

other. This experiment will use the previous chapter 8051 serial port experiment to build a virtual

simulation control system between the PC and 8051, and use self-defined command set to set

communication channel.

Here shows the whole practice structure, and the function of this practice system is shown

as follows.

PC installed LabVIEW

& with serial port

8051 serial port experiment

Command

set

Interface Serial port

GUI interface

Page 4: Comm Bet 8051 and Labview

7-4

General speaking, the design of command is just a series of numeric figures composition,

the reason is just to take “information” derived to machine that did not understand the

information as we do. These kinds of communication mode for machine can save the cost of

communication, the shorter the better. The meaning seems to be the instructions of assembly

language.

The setting of our serial port is defined with each 8 bits as one unit of transmission;

therefore if the instruction set has the width of 8 bits we will have 256 sets instruction ( )2 2568 =

to assign, in the case of learning it is enough for use.

There is an example of simulation instruction sets to assign as following.

Sending Return

Name of instruction Hexadecimal Return value The meaning of return

Someone in? 01H F0H Yes!

How are you! 02H F1H Fine, thanks

Take the side dish 03H F2H Yes, please.

If you have chance to observe the contents of communication protocol of some automatic

control and measuring system, you will find it much alike the general dialogue of people most of

the time. In fact instrument is the same as people organization (certainly, the instrument is

designed by people), so between the instruments they sent message and received information

frequently, it is similar the example that we mentioned above. Except the message and

information are all restricted, they couldn't announce as freedom as people, they don’t speak too

much and couldn't create the new “words" that people didn’t save inside it. Therefore all these

message are assigned to the restricted numeric numbers to correspond, thus those instruments

use these numbers to communicate each other. In fact each value represented by the instruction

will vary the meaning for different systems; it depends on the user how to define it. Therefore,

the same value but at different systems could have different meaning to explain. OK, are you

ready? Start to do experiment.

Page 5: Comm Bet 8051 and Labview

7-5

Practice 7-1 Command interactive

How to let the LabVIEW interactive with 8051?

First, in order to explain the simple concept of instruction set we design a simple

instruction table command at 8051 inside.

Sending Return

Instruction Value Return value Meaning of return

01H On-line Is 8051 on-line? 3FH

None Without connected to 8051

Other value 00H No meaning

On the table the text of instruction is used for people understanding, and the computer only

recognize the value, return back is still a value. We just use LabVIEW human-machine interface

to transfer value into text in order to be understood by people.

How to plan an 8051 program?

What is doing inside the 8051 program? From the following flowchart it is easy to understand.

3FH?

Start

Is there any input?

no

yes

no

yes

Output 01H

Output 00H

Some initialization

Page 6: Comm Bet 8051 and Labview

7-6

This is the 8051 program that has less modified from program of chapter 6 could achieved

the goal.

The source code is as follows:

org 0h ;Program initial jmp init org 30h init: ;Main program entry point mov sp,#40h ;Reset stack call set_bps clr ri clr ti mov r0,#30h ;Setting relative addressing register start: jnb ri,$ ;Waiting for input clr ri mov a,sbuf cjne a,#3fh,a1 ;Is the input equal to 3FH? jmp a2 a1: mov sbuf,#00h ;Input ≠ 3FH,output 00H jnb ti,$ clr ti jmp start a2: mov sbuf,#01h ;Input = 3FH,output 01H jnb ti,$ clr ti jmp start set_bps: mov scon,#01010000B ;Setting serial port mov tmod,#00100000B ;Setting timer 1 mov th1,#FDH ;Setting timer 1 reset value,offer serial port communication rate setb tr1 ;Start timer 1 ret

Page 7: Comm Bet 8051 and Labview

7-7

How to construct LabVIEW interface?

1、Put a Text Ring on the Front Panel.

2、Please add the following new Items into Text Ring. It doesn't matter how many items

have we added, but remember that it must contain "Is 8051 on-line?" this one, it

means what we want to input the instruction.

3、To create a new Case Structure in the Block Diagram, and connect it with Text Ring

that has just create, and Add Case as many as Items of Text Ring, in order to one to

one correspond. Each Case need to put a String Constant. The display mode of

each String Constant should change to Hex Display, so it could display

hexadecimal format.

Page 8: Comm Bet 8051 and Labview

7-8

Please remember that the Case which correspond with the item "Is 8051 on-line?"

of Text Ring must put into the value 3FH , this is what the instruction we need. The

constants of the other Case can put any other value, remember don’t repeat with the

value 3FH .

4、Please add a Stacked Sequence Structured and put a VISA Configure Serial

Port.vi in it. To set the parameter of termination char is 1, which means the width

of the instruction set is one byte. Therefore we set the termination char as 1 and the

VISA resource name setting is pointed to your PC COM port for this experiment.

(Here is setted to COM1), setting the baud rate to 19200.

Page 9: Comm Bet 8051 and Labview

7-9

5、Adding a new page frame after the step 4, we put a VISA Write.vi on it. Connect the

“write buffer” parameter with the Case Structure output of step 3 together. Similarly

the port number setting is pointed to your PC COM port for this experiment. (Here is

setted to COM1).

6、Here is the last step of program block diagram. Put the VISA Read.vi on it first, set its

parameters, and remember to set “byte count” as 1, which means it will be read back

1 byte, and next the output of VI is connected to (String to byte array), why to

do so? Next steps will be explained. Now take out the first element from the new

transferred array.

String to byte array

Page 10: Comm Bet 8051 and Labview

7-10

7、In the step 6, the new create interface is an indicator item of Text Ring, inside this

Text Ring will be set a few items, and how to do those settings we has practiced in

the step 2. Here the purpose of Text Ring is how to display the return value that is

sent back from 8051 instruction set into the message that people can understand. As

you know what the “VISA Read.vi “read back is text constant, it must be transferred

into value so that the indicator item of Text Ring could display the constant correctly.

This is why we must convert VI (String to Number Conversion) in the step 6.

Page 11: Comm Bet 8051 and Labview

7-11

8、Finally put the correct message text inside the indicator items of Text Ring on the

Front Panel. What the return value of 8051 instruction is that 00H represents “No

meaning “and 01H represents “On-line”. Therefore the first item of “Text Ring”

indicator is displayed “No meaning ", and the second item of “Text Ring” indicator is

displayed “On-line". Please pay attention, it must be in order, otherwise the output

message will be upside down.

9、OK, let the 8051 serial port experiment and transmission line for ready. Run to

communicate, at first input any instruction that is not what we expect. Yeah! Really

the answer is no meaning.

Page 12: Comm Bet 8051 and Labview

7-12

10、Next input the instruction “Is 8051 on line?” and you will get the return

response as “On-line”. Bravo!! Success.

The end of practice 7-1

Page 13: Comm Bet 8051 and Labview

7-13

Practice 7-2 To simulate temperature measuring system

According to the structure of the above practice, this new practice simulates a temperature

measuring system to output the data by means of 8051 internal timer 0.

In the instruction sets of practice 1 please add a new instruction:

Sending Return instruction Hexadecimal value Return value Meaning of return

01H On-line Is 8051 on-line? 3FH

None Without connected to 8051Get the value of timer 0 2BH Arbitrary value The value inside Timer 0

Other value 00H No meaning

Now we can use 2BH to call 8051, we hope to get the value inside the timer 0. Therefore

we have to modify the 8051 source code to add the function of timer 0, and the response of

calling command 2BH .

Program of 8051

The program here is also modified from 8051 program of practice 1 to add extra

code.

3FH?

Start

Is there any input?

no

yes

no

yes

Output 01H

Output 00H

Initial ization

2BH?no

yes

Output the value inside timer 0

Page 14: Comm Bet 8051 and Labview

7-14

Source code of 8051 program is as follows:

How to construct LabVIEW interface?

The program of LabVIEW still need some modification.

1, Please add a new Waveform Chart on front panel.

org 0h ; Program start point jmp init org 30h init: ; Main program entry point mov sp,#40h ; Reset the stack call set_bps clr ri clr ti mov r0,#30h ; Set the relative addressing register start: jnb ri,$ ; Waiting for input clr ri mov a,sbuf cjne a,#3fh,a1 ;Input = 3FH or not? mov sbuf,#01h ;Input = 3FH,output 01H jnb ti,$ clr ti jmp start a1: cjne a,#2bh,a2 ;Input =2BH or not? mov a,tl0 ; Input = 3FH,output the value of timer 0 mov sbuf,a jnb ti,$ clr ti jmp start a2: mov sbuf,#00h ;Input the other value,output 00H jnb ti,$ clr ti jmp start set_bps: mov scon,#01010000B ;Setting serial port mov tmod,#00100010B ; Setting timer 1 mov th0,#00H ; Setting timer 0 reset value mov th1,#FDH ; Setting timer 1 reset value,offer serial port communication rate setb tr0 ; setb tr1 ;Start timer 0 setb tr1 ;Start timer 1 ret

Page 15: Comm Bet 8051 and Labview

7-15

Program of LabView

In the LabVIEW Just modify from practice 7-1 program

1、Create a Waveforms Graph in the Front Panel of Controls Graph Indicators

2、On the Text Control of the Text Ring, create the Control and Indicator item please

add a new choice item on each one to provide the space for new instruction.

Page 16: Comm Bet 8051 and Labview

7-16

3、Similarly in the Block Diagram we also add a new Case to put the new instruction :

2BH , and setting the baud rate to 19200.

4、Adding a new page frame after the step 4, we put a VISA Write.vi on it. Connect the

“write buffer” parameter with the Case Structure output of step 3 together. Similarly

the port number setting is pointed to your PC COM port for this experiment. (Here is

setted to COM1).

Page 17: Comm Bet 8051 and Labview

7-17

5、In the last page frame of Sequence, erase away the original object, and put into Case

icons. Setting those two Case, one is default, the other one is“+". ASCII code for

“+" is 2BH .The selecting input of Case is connected as follows.

6、At the default of new Case we put into the original block diagram of page

frame 2.

Page 18: Comm Bet 8051 and Labview

7-18

7、At the ” +” page frame of new Case we put into the following block diagram, and link

the Waveform Graph of step 1 into here.

8、OK, let the 8051 serial port experiment and transmission line to PC

for ready. Start to communicate, first input the query “Is 8051 on-line?” to check

whether 8051 exists or not.

Page 19: Comm Bet 8051 and Labview

7-19

9、Very Good. Next ask value of timer from 8051, that is input the query “get the value of

timer 0”. If there is a response, click the keypad one more times to get more data.

The end of practice 7-2

Page 20: Comm Bet 8051 and Labview

7-20

Exercise and discussion

1、In the practice 1, the instruction sets what we designed is regarded the return

value “00H” as no meaning. It is just for convenience to arrange it, we could

have another arrangement to reach the same goal. In many situations, people

use “FFH” as the return value for no meaning response. Try to change

LabVIEW block diagram in practice 1 and modify the program of single chip

serial port experiment circuit in order to take “FFH” as no meaning response.

2、In practice 2, we use timer 0 0f 8051 to simulate the output data generated by

the temperature measuring system. Can you try to connect it with a real

temperature senor into 8051 circuit to get real data of temperature?