Macros used in programs
Examples given in the following sections of this chapter often use macros WAIT, WAITX and PRINT, so they will be explained in more detail.
Macros WAIT, WAITX
File Wait.inc contains two macros WAIT and WAITX. Through these macros it is possible to assign time delays in different intervals. Both macros use the overflow of counter TMR0 as a basic interval. By changing the prescaler we can change the length of the overflow interval of the counter TMR0.
If we use the oscillator (resonator) of 4MHz, for prescaler values 0, 1, and 7 that divide the basic clock of the oscillator, the interval followed by an overflow of timer TMR0 will be 0.512, 1.02 and 65.3 mS. Practically, that means that the biggest delay would be 256x65.3mS which is equal to 16.72 seconds.
In order to use macros in the main program it is necessary do declare variables wcycle and prescWAIT as
will be done in examples which follow in this chapter.
Macro WAIT has one argument. The standard value assigned to prescaler of this macro is 1 (1.02mS), and it can not be changed.
WAIT timeconst_1
timeconst_1 is number from 0 to 255. By multiplying that number with the overflow time period we get the total amount of the delay: TIME=timeconst_1 x 1.02mS.
Example: WAIT .100
Example shows how to make a delay of 100x1.02mS, or total of 102mS.
Unlike macro WAIT, macro WAITX has one more argument that can assign prescaler value. Macro WAITX has two arguments:
Timeconst_2 is number from 0 to 255. By multiplying that number with the overflow time period we get the total amount of the delay:
TIME=timeconst_1 x 1.02mS x PRESCext
PRESCext is number from 0 to 7 which sets up the relationship between a clock and timer TMR0.
Example: WAITX .100,7
Example shows how to make a delay of 100x65.3 mS, or total of 653mS.
Macro PRINT
Macro PRINT is found in Print.inc file. It makes it easy to show a string of data on one of the output devices such as : LCD, RS232, matrix printer...etc. The easiest way to form a series is by using a dt (define table) directive. This instruction stores a series of data into program memory as a group of retlw instructions whose operand is data from the
string.
How one such sequence is formed by using dt instruction is shown in the following example:
org 0x00
goto Main
String movwf PCL
String1 dt
"this is 'ASCII' string"
String2 dt
"Second string"
End
Main
movlw .5
call String
:
First instruction after label Main writes the position of a member of the
string in w register. We jump with instruction call onto label string where position of a member of the
string is added to the value of the program counter: PCL=PCL+W. Next we will have in the program counter an address of retlw instruction with the desired member of the
string. When this instruction is executed, member of the string will be in w register, and address of the instruction that executed after the call instruction will be in the program counter. End label is an elegant way to mark the address at which the
string ends.
Macro PRINT has five arguments:
PRINT macro Addr, Start, End, Var, Out
Addr is an address where one or more strings (which follow one by
one) begin.
Start is an address of the first member of the string
End is an address where the string ends
Var is the variable which has a role of showing (pointing ) the members of the
string
Out is an argument we use to send the address of existing subprograms
assigned to output devices such as : LCD, RS-232, etc.
Macro PRINT writes out a string of ASCII caracters for 'MikroElektronika' on LCD display.
The string takes up one part of program memory beginning at address 0x03.
|