9 of 172
ZBasic Language Reference 2 ZX Microcontroller Family ' alternately blink the LEDs Do ' turn on the red LED for one half second Call PutPin(redLed, zxOutputLow) Call Delay(0.5) Call PutPin(redLed, zxOutputHigh) ' turn on the green LED for one half second Call PutPin(grnLed, zxOutputLow) Call Delay(0.5) Call PutPin(grnLed, zxOutputHigh) Loop End Sub The first two lines above define some constant values. It is a good idea to use constant values like this instead of using the value explicitly in many different places. One benefit of doing so is that it makes the program somewhat easier to understand assuming, of course, that the name chosen for the constant is suggestive of its intended use or meaning. A second benefit is that if you later wish to change the value, you can do so easily by modifying it in only one place. This way you’ll avoid the common error of changing a constant value in some places but not in others. It is important to note that alphabetic case is not significant in ZBasic. You may type keywords, variable names, etc. in upper case, lower case or mixed case and it makes no difference. The subroutine Main() is the same as the subroutine main(). Many programmers use alphabetic case to improve readability. Some even use it to remind them of the visibility attribute of the subroutine, constant, etc. One convention for this is to begin all public names with an upper case letter and begin all private names with a lower case letter. You’re free to adopt these conventions or not as you see fit. The public Main() routine is where execution begins when you run a program. That’s why every program must have a public subroutine Main(). In this program, the first two lines of Main() utilize a call to a System Library subroutine that sets the state of an I/O pin on the processor. The first parameter that is provided to the PutPin() subroutine is the pin number that we wish to configure and we’ve used the previously defined constants to do so. The second parameter to the PutPin() subroutine is a value that indicates whether the pin should be an output or an input and, additionally, its characteristics. In this case, we’ve used a built-in constant zxOutputHigh which both configures the pin to be an output and sets it to be at a logic high level. The LEDs that are present on the processor are illuminated when the pin to which they are attached is at logic low level. The effect of these first two lines, then, is to make the pins associated with both LEDs outputs and to make sure the LEDs are off. The next part of the code is the block beginning with Do and ending with Loop. This construct is useful for continually repeating a sequence of statements. The logic flow is that each of the statements inside the Do...Loop construct is executed in turn. When the Loop statement is reached, control transfers back to the top of the Do loop and the process repeats. ZBasic has several different kinds of control structures for looping, each with different useful characteristics. There are also control structures to allow you to write statements that are executed only if certain conditions prevail. The set of allowable statements is described in detail later in this manual. Inside of the Do loop, there are two sequences of statements that perform similar functions. The first three lines of code within the Do loop turn on the red LED for a brief period of time. This is done by first setting the corresponding pin to a logic low, then calling the System Library subroutine to delay for a half second and finally setting the LED pin to a logic high. The second sequence does the same for the green LED. A subroutine is nothing more than a collection of statements that can be executed by invoking the subroutine name. Although it is possible to implement all of the logic of your application inside the subroutine Main(), this is usually ill advised except for fairly simple applications like the example above. It is usually better to decompose your application into logical elements and to implement the functionality of each element using a subroutine or a function. The difference between a subroutine and a function is that a function returns a value. Because of this, a function can be used wherever a value may be used.