Home | Forum | DAQ Fundamentals | DAQ Hardware | DAQ Software Input Devices | Data Loggers + Recorders | Books | Links + Resources |
AMAZON multi-meters discounts AMAZON oscilloscope discounts A high-level language program produces machine code from a program written in a form that is close to ordinary English. The many small steps of machine code and assembler are replaced by keywords that call up ready-made routines. Some languages use an interpreter, and others use a compiler. Two widely used high-level languages suitable for programming microelectronic devices are BASIC and C. Ladder logic is often used with PLCs. Just as assembler is one stage removed from machine code, so the high-level languages are one stage removed from assembler. There are many high-level languages, a few of them very widely used, and several that have more specialist applications. CPUs can not understand high level languages, but there are several programs that allow us to write a program in a high-level language and then have it turned into machine code. In this SECTION we look at three such programs, using C, BASIC and ladder logic. The main difference between assembler and high-level languages is that assembler is related to machine code on a one-to-one basis. Each line of an assembler program corresponds to one fetch-execute cycle of the CPU. Because a CPU operates in short simple steps, programming in assembler can be very tedious and repetitive. With a high-level language, a single command may be the equivalent of several tens of fetch-execute cycles. Example: A program written in C might contain this line: area = length * width; Two variables, labeled length and width are to be multiplied together to obtain the value of a third variable labeled area. This command sends the CPU into a long sequence of fetch-execute cycles in which it loads the variables length and width, multiplies them together (a fairly complicated operation for a CPU that can only add or subtract) and finally store the result as area. A high-level language program includes an extensive range of ready made machine code routines for programming complicated sequences (such as the multiplication routine mentioned above) so that the programmer does not have to work them out every time they are needed. Although programming in assembler may be complex and tedious at times, it does have the advantage that the user is always fully aware of what the CPU is doing. This can lead to efficient programming carefully tailored to the project in hand. The ready-made routines of a high-level language save much work, but are not necessarily efficient. In order to make them usable in a wide range of programming situations, they may be longer (and therefore slower) than an equivalent routine specially written to fit into one particular program. If maximum speed is essential, a programmer may use a high-level language for most of the program but use assembler when writing routines that need to run at maximum speed. To be able to create a source program in a high-level language, you need the appropriate language program. This is the program that accepts the statements you type into your computer, and displays them on the screen. It allows you to edit your program, to check it for errors and to save it. ================= ASCII code This is the American Standard Code for Information Interchange and is the standard way of coding letters, numerals and punctuation and also a number of printer control commands. It is an 8-bit binary code with a range of values from 0 to 255. The first 32 values are the control characters, such as 'carriage return', 'bell' (produces a beep in modern computers but used to ring an actual bell in the old Teletype machines), and 'end of file'. Code 32 is a space. Codes 48 to 57 are the numerals 0 to 9, then there are some punctuation marks. Capital letters of the alphabet begin at code 65 and lower-case letters at 97. The codes in between these blocks are used for punctuation. ================== The form a program is saved in varies with the language. Most versions of BASIC, for example, use an interpreter program. The BASIC language program saves your source program not as machine code but as a sequence of bytes. Some of the bytes represent BASIC commands while others represent numbers and text. The numbers and text are represented by ASCII code. In this form, the program is meaningless to a CPU, which understands only opcodes and operands. To be understood by the CPU, the BASIC source program must first be interpreted. When you run the source program the interpreter takes over. It goes through the source program line by line, and command by command and turns it into machine code. It sends this to the CPU. The CPU has to do four things at once: • Read a line of the source program. • Use the interpreter program to find out what the line means. • Turn it into machine code. • Obey the machine code. The need to perform four tasks simultaneously makes running an interpreted BASIC program relatively slow. In addition, it is wasteful for the CPU to have to interpret a line every time it is used. Imagine a program in which the same sequence of calculations is repeated 100 times, with the CPU jumping back to the start every time it finishes. The lines of that section of the program are interpreted 100 times even though they always produce the same result. It would be far quicker if the lines were interpreted just once. This is the function of a compiler. A compiler is a program that takes a program written in a high-level language and converts it directly into machine code. It does this in just one session and the machine code is stored in memory or on a disk. When the compiled program is run, the CPU has only to read the machine code and obey it. This is much faster than having to interpret every line as it comes to it. C is a compiled language. There are also compiled versions of BASIC. Compiling is a complicated process that takes the CPU an appreciable time. Unfortunately, the program can not be tested and debugged until after it has been compiled. If any errors are found, the programmer has to go back to the source program, correct the errors, recompile the program, and test again. A compiled program will usually take much longer to write and test than an interpreted program but, once it is done, it runs much faster. Compiled programs make use of a collection of ready-made standard machine code routines that are held in library files. Some of the routines from the library files are requested by the programmer when writing the source code. When the program is compiled, these routines are added to the compiled program by a program known as a linker. The RAM addresses are added and checked by a loader program. The final version of the program is then ready to run. We will now look in more detail at a some of the high-level languages that are useful for programming microelectronic systems. C compilers C is one of the most popular languages for programming microprocessors and microcontrollers. Since the C compiler has to be matched to the features of the particular type of CPU it instructs, there are many versions of it. However, the American National Standards Institute has drawn up a set of rules for the way in which C programs are written and most compilers follow these rules. These compilers are known as ANSI C compilers. There is also a version of C with additional features and this is known as C ++. There is also a version of C ++ called Visual C ++. This is a Windows program, primarily intended for writing applications based on a graphical user interface. It is more difficult to learn than a simple C compiler program. There are rules for typing in a C program and these rules must be strictly followed if the program is to be compiled without error messages appearing. As with an assembler, all variables used in the program must be declared, preferably at the beginning of the program. Example: Here is the sample program of SECTION 7, re-written in C: /* Example 1 re-written in C */ int counter int trial #define BODY 37 main() { counter = BODY; trial = ++counter; After the initial declarations and a definition, the main program is very short and consists almost entirely of readable (and memorable) words. It is enclosed in braces {} but, as this is only a section of the sample program, the final brace is not shown in the listing above The program declares two variables, counter and trial as integer variables. Note that we do not have to specify where these variables are to be stored or even whether they are to be stored in a register of the CPU or in a location in RAM. The compiler takes care of all the details and always knows where to find the values of counter and trial when it needs them. The program also defines a constant, BODY. This will hold its value for the whole program. We have typed its name in capitals to indicate (to people looking at the program) that this is a constant, not a variable. As in several other languages, calculations are performed by assigning a value to a variable. In C, this is done by using the 'equals' symbol. In the first line of the main program, variable counter is assigned the value of the constant BODY. We have already assigned the value 37 to BODY, so BODY holds 37. The first line of the main program then assigns the value of BODY to counter. Then both BODY and counter hold 37. Perhaps the only item that needs an explanation is the 'double plus' on the last line of the sample. This line means 'increment counter and assign its new value to trial'. The operation could also be programmed as: counter = counter +1; trial = counter; The sample listed above does not have any label indicating the program line that the CPU is to jump back to later. This is because C does not have this feature. There are several other ways of making a program section repeat, but we will leave descriptions of these until later. Most C compiler programs comprise several sections for handling the different stages of production of the final machine code file. The first section to use is the Editor, which is rather like a word processor. When the program above, or a sufficiently large part of it, has been completely typed in, using the Editor, you click on the 'Compile' button. The compiler will attempt to compile your program, but will report any errors you have made that prevent it from compiling. If there are errors, you return to the Editor to correct them. When all errors have been eliminated, the program compiles the complete listing into machine code and produces a new file with the .C extension. This is an executable file that can then be run, perhaps on the computer or after downloading into the ROM of a microcontroller. It can also be saved on to disk. There is a lot more to writing programs in C than this example illustrates, but it is enough to help you compare C with the other languages described here. There is more on C in SECTION 11. BASIC The acronym BASIC stands for Beginners All-purpose Symbolic Instruction Code. It is probably the easiest language to learn, so it is ideal for beginners, but this has led many people to reject its use for 'serious programming'. However, the fact remains that one can do almost anything with BASIC. Moreover, several of the recent prototyping systems use BASIC as their programming language. A notable one is the BASIC Stamp. Its two versions, Stamp 1 and Stamp II, program the PIC16C56 and the PIC16C57 microcontrollers respectively, using a special Stamp version of BASIC. We shall return to Stamp BASIC later, but first we will look at the original BASIC. There have been many versions of BASIC written for different computers. This lack of standardization contrasts with the strict definition of C by ANSI. This is one reason why BASIC has been less popular with the professional programmers, who like to be able to write a program on one model of computer and quickly adapt it for running on other computers. The original BASICs differ from most other languages in that the program lines are numbered. Line numbers are keyed in as the program is being written. Conventionally, the lines are numbered in tens. This allows extra lines to be inserted in the program later, without having to disturb the earlier numbering. Here is the sample program written in BASIC. Example: 10 REM Example 1 written in BASIC 20 body = 37 30 counter = body 40 counter = counter + 1 50 trial = counter Line 10 begins with REM, which stands for 'Remark'. The CPU ignores everything on the line following the REM statement. The remainder of this program sample is easily read and understood, provided that we remember that the '=' symbol means 'assign the value of the variable, constant, or expression on the right to the variable on the left'. Setting out a BASIC program is much simpler than a C program. It is not necessary to begin by defining variables and constants, though you can do so if you prefer to. If you do not define variables, BASIC sets aside locations in memory for them the first time each variable is used. There is no use of punctuation marks at the end of program lines in BASIC. Program lines just end with a carriage return (ENTER on the keyboard) instead of a semicolon. There is no use of braces for limiting sections of the program. In most versions of BASIC, line numbers are used instead of labels. Example: If, when the CPU gets to line 250, we want it to jump back to line 40 to increment counter, we can use this statement at line 250, later in the program: 250 GOTO 40 Many programmers prefer to avoid using GOTO, as it may lead to sloppy programming that is difficult to understand later. Instead, they would use a properly constructed program loop, as we shall describe later. The same program written in PBASIC for the Stamp 2, looks different from the program above. Part of the reason is that the microcontroller has a limited amount of RAM (only 2 K) so the program must be as compact as possible. One obvious difference is that there are no line numbers. This means that there must be labels on certain lines: 'Example 1 written in PBASIC counter var byte trial var byte body CON 37 counter = body again: counter = counter + 1 trial = counter The program begins with a title and, in PBASIC, we use an apostrophe to indicate a comment. It is so much easier to understand what is happening in PBASIC programs that it is rarely necessary to append remarks to every line. Note the label again: beginning the line to which the CPU is to return later. Later in the program the CPU could be sent back to this line by the statement: GOTO again As in the previous program, it probably would be better not to use GOTO. A properly constructed loop should be used instead. PBASIC has a useful feature for debugging programs. To find the values of all the variables after the program has been executed, we append these debugging statements to the program: DEBUG DEC body DEBUG DEC counter DEBUG DEC trial This instructs the CPU computer to obtain the decimal values of the variables and the constant and send them back to the computer along the programming lead. The computer displays them on the screen in decimal form. As soon as the program has run, a panel appears on the computer monitor, with these numbers on it: 37 38 38 Debug commands can be inserted anywhere in a program to show the values of variables at any stage. The Stamp is primarily intended for use in control or measurement systems. It has a number of features that fit it for this, and therefore PBASIC includes a number of commands for using these features. These include: BUTTON debounce a button and jump to a different part of the program. PULSOUT output a timed pulse. DTMF generate DTMF dialing codes. POT read the value of a potentiometer, thermistor or any other sensor with variable resistance. These and other built-in routines save the programmer much time. Visual BASIC is one of the relatively new languages that exploit the facilities of the Microsoft Windows environment. It has most of the keywords of conventional BASIC and a great many more of its own. Most of these are aimed at providing the user with the means of setting up the complicated and visually effective windows, menus, radio buttons, sliders and other graphic devices that are associated with Windows programs. For those wanting to write their own Windows utilities and games programs, Visual BASIC is an ideal platform. Unfortunately, its keywords do not include the INP and OUT functions of conventional BASIC. To access the ports of the computer requires additional programs which may be downloaded from the World Wide Web, but there is no guarantee that these will always be available. Ladder logic Ladder logic is derived from relay logic, which was widely used in industrial control systems. The plant was controlled by a system of hard-wired relays for switching on lamps, motors and other items of equipment. The diagrammatic form of the program is based on the symbols formerly used in USA for relays and associated devices. Actuators are represented by circles. FIG. 1 shows part of a PLC 'program'. With the control unit in programming mode, the diagram is entered on the screen. The keyboard of the control unit has special keys for each of the relay logic symbols. If a PC is being used for programming, certain keys are allotted to display the symbols. The program 'lines' are numbered (the example shown is line 46) and the symbols are each identified with a number or code which indicates memory locations corresponding to terminals on the input or output cards. The system used on Mitsubishi Programmable logic controllers play an important role in industrial and other control systems. Many of them are programmed by keying a statement list into a PC. The statement list is much like a program written in BASIC, though it has its own keywords and syntax. Most PLCs can also be programmed in ladder logic. This is not so much a high-level language, as a visual way of representing a sequence of control instructions. It was designed to be programmed by electrical engineers rather than by software experts.
Symbols marked 'X' are inputs, and that marked 'Y' is an output. The numbers refer to the I/O cards that are wired to the corresponding sensors and actuator.
PLCs uses the 'X' prefix for inputs and the 'Y' prefix for outputs. Annotation on the diagram helps the user to understand how the system functions. Reading across from left to right in FIG. 1, we see that, if the receptacle is in place (a proximity sensor detects this and location X1 goes logic high), we can trace a continuous path across its symbol. Then, if the temperature is correct (another continuous path if it is) we can continue across to the symbol representing the pump motor. This complete the path from left to right. 'Current' flows along the path and pumping begins. The diagram shows an alternative path, through a manually operated switch. This means that an operator can start the pump, provided the receptacle is in place, but it is not necessary for the temperature to be correct. FIG. 2 sets out an elementary program for controlling the entrance to a car park. In the first line of the excerpt shown here, if an optical sensor detects that a car is waiting to enter, and the barrier is down (as sensed by a limit switch) and the program is in Phase A (barrier down, ready for car to arrive), a memory location (M41) in the controller is set to a '1'. The next line is line 70. Note that the lines need not be numbered consecutively. This makes it easy to interpose additional lines if necessary as the program is developed. In line 70, if Phase A is operative and the barrier is up, this marks the end of Phase A and the beginning of Phase B (barrier raised, car drives in). M42 is set to indicate this. In line 75, the first symbol inverts the action of the signal from the sensor by using a the symbol of a relay with normally closed contacts. These are open if the car is there. If the car is not waiting at the barrier, and the barrier is up and it is Phase B, then the flag is set to allow the barrier to be lowered. In line 80 if the flag to raise the barrier is 1 (this flag was set or not set in line 65), and the flag to lower the barrier is 0 (set or not set in line 75), and the barrier is not up, the barrier lifting motor is started (a Y output). As in machine code and assembler, the program proceeds in many small steps. Problems on high-level languages 1. What are the main differences between programming in assembler and programming in a named high-level language? 2. Explain the difference between a compiler and an interpreter. 3. Describe the main features of a named high-level language. 4. What is ASCII code? Give some examples. 5. Explain what is meant by assignment. 6. What is ladder logic and in what applications is it used? Related Articles -- Top of Page -- Home |
Updated: Thursday, May 18, 2017 10:10 PST