7-31-08
Ref. Sht. HG31-08
Explanation
of XCSB Template
OEES 215
| //---------------------------------------------------------------------- //---------------------------------------------------------------------- //Template (containing an AND gate program) //---------------------------------------------------------------------- //---------------------------------------------------------------------- ubyte global g_obyt, g_polarity, g_mode, g_p1 include "S215XC_Std_func_3.bas" //Include the S215 standard functions //---------------------------------------------------------------------- proc main() ubyte in0, in1, ibyt, out0 //Declare variables Initp(0, 1) //Do initialization while 1==1 do //Loop forever ibyt = GetInBts() //Get input byte in0 = SelBit(ibyt,0) //Put bit 0 into in0 in1 = SelBit(ibyt,1) //Put bit 1 into in1 if in0 == 1 then if in1 == 1 then out0 = 1 //If the computer reaches here, //both bit 0 and bit 1 are 1, //and the AND "gate" should output //a 1. else out0 = 0 endif else out0 = 0 endif Ob(out0, 0, 1) //Output whatever is in out0 to bit 0 done endproc //---------------------------------------------------------------------- |
| //---------------------------------------------------------------------- //---------------------------------------------------------------------- //Template (containing an AND gate program) The above line is the name of the program (Template). Anything preceded by two slashes (//) is a comment. Comments are notes to the programmer (a human being!) and are ignored by the compiler. The compiler is a program that converts a program written in something like XBasic, to machine language. Machine language consists of ones and zeroes that the microcontroller understands. The lines of dashes serve to divide the program into sections for readability. //---------------------------------------------------------------------- //---------------------------------------------------------------------- ubyte global g_obyt, g_polarity, g_mode, g_p1 The compiler needs to know how much memory space to reserve for each variable (for example, g_obyt is a variable that contains the byte that is to be outputted). A variable is like the X in algebra: it can contain any number that is put into it. The word ubyte tells the compiler that each variable will contain an unsigned byte (a mixture of eight ones and zeroes that will be treated as a positive number). The word global tells the compiler that these particular variables will be available for all functions to use. A function is a little program that is used by the main program. include "S215XC_Std_func_3.bas" //Include the S215 standard functions The include statement above tells the compiler to include the contents of the S215XC_Std_func_3.bas file, which is a file containing version 3 of the standard custom functions we'll be using in this class (OEES 215). //---------------------------------------------------------------------- proc main() "Procedure" is another name for "function," thus proc main means that we're now inside a function called main. The empty parentheses indicate that this function will not be producing any result for use by another function. (The other version of BASIC we're using uses the name "function" rather than "procedure," and most other languages use the name "function." Thus, we're sticking with the name "function," even though we have to use the word "proc" to keep the compiler happy. ubyte in0, in1, ibyt, out0 //Declare variables Again, this reserves space for each variable. Because the word global isn't present, these variables may only be used by the main function. Initp(0, 1) //Do initialization Initp is a custom function written for this class. It sets up other custom functions so that they're ready for use. while 1==1 do //Loop forever While statements cause the computer to repeat a block of instructions a certain number of times. The 1==1 tells the computer to repeat the block of instructions as long as one equals one, which is forever. (The double equal sign indicates that we're doing a comparison, rather than setting a variable equal to some number.) ibyt = GetInBts() //Get input byte GetInBts is a custom function. It gets whatever ones and zeroes are present on the input switches. The statement ibyt = GetInBts() tells the computer to put the ones and zeroes gotten by GetInBts into the variable ibyt. in0 = SelBit(ibyt,0) //Put bit 0 into in0 SelBit is a custom function that gets just the bit number requested. The statement in0 = SelBit(ibyt,0) tells the computer to get just bit zero from the ones and zeroes contained in ibyt, and then put the value of this bit into the variable in0. in1 = SelBit(ibyt,1) //Put bit 1 into in1 This time, bit 1 is obtained from ibyt. if in0 == 1 then The block of program lines starting with this first if statement and ending with the last endif statement are not actually part of the template. Rather, they are a sample program that shows how to use the template file. if in1 == 1 then out0 = 1 //If the computer reaches here, //both bit 0 and bit 1 are 1 The statement out0 = 1 tells the computer to put a 1 into the out0 variable. else out0 = 0 endif else out0 = 0 endif Ob(out0, 0, 1) //Output whatever is in out0 to bit 0 Ob is a custom function. The above statement tells the computer to send whatever number is in the variable out0 to LED zero. (LED stands for light emitting diode.) The 1 before the closing parentheses serves a special purpose to be explained later. Only in special cases will we be changing this 1 to a 0. done The word done indicates that this is the end of the block of statements that are to be repeated over and over again because of the while statement above. endproc This marks the end of the main procedure. //--------------------------------------------------------------------- |