1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
|
<!-- kate: tab-width 2; indent-mode xml; -->
<chapter id="microbe">
<title>µbe;</title>
<sect1>
<title>Introduction and General Syntax</title>
<para>
<application>Microbe</application> compiles programs written in the custom language for PICs, as a companion program to &ktechlab;. The syntax has been designed to suit a &flowcode; program.
The syntax for running <command>microbe</command> from the command line is:
<programlisting>microbe [options] [input.microbe] [output.asm]</programlisting>
where options are:
</para>
<itemizedlist>
<listitem><para><function>--show-source</function> - Puts each line of µbe; source code as a comment in the assembly output before the assembly instructions themselves for that line.</para></listitem>
<listitem><para><function>--no-optimize</function> - Prevent optimization of the instructions generated from the source. Optimization is usually safe, and so this option is mainly used for debugging.</para></listitem>
</itemizedlist>
<para>
The .microbe input file must identify the target PIC by inserting the PIC name at the top of the .microbe file; ⪚ the name of a PIC16F84 is "P16F84".
<example><title>Simple complete µbe; program</title>
<programlisting role="correct">
P16F84
a = 0
repeat
{
PORTA = a
a = a + 1
}
until a == 5
end</programlisting>
</example>
</para>
<sect2 id="namingconventions">
<title>Naming conventions</title>
<para>
The following rules apply to variable names and labels:
<itemizedlist>
<listitem><para>They can only contain alphanumerical characters [a..z][A..Z][0..9] and the underscore "_".</para></listitem>
<listitem><para>They are case-sensitive.</para></listitem>
<listitem><para>They cannot start with a number.</para></listitem>
<listitem><para>They should not start with <quote>__</quote> (double underscore), as this is reserved for use by the compiler.</para></listitem>
</itemizedlist>
</para>
</sect2>
<sect2 id="bracingconventions">
<title>Bracing conventions</title>
<para>
Curly braces, {}, indicate the start and end of a code block.
They can appear anywhere before the start and after the end of the code block.
Examples of acceptable code blocks:
<programlisting role="correct">
statement1 {
some code
}</programlisting>
<programlisting role="correct">
statement2 {
other code }</programlisting>
<programlisting role="correct">
statement3
{
other code
}</programlisting>
<programlisting role="correct">
statement5 {
code block
} statement6</programlisting>
</para>
</sect2>
<sect2 id="commenting">
<title>Commenting</title>
<para>
Commenting is similar to C. // comments out the rest of the line. /* and */ denote a multiline comment.
<programlisting role="correct">
// This is a comment
x = 2
/* As is this
multiline comment */</programlisting>
</para>
</sect2>
<sect2 id="structure">
<title>Program Structure</title>
<para>
The PIC id must be inserted at the top of the program. The end of the main program is denoted with <quote>end</quote>. Subroutines must placed after <quote>end</quote>.
</para>
</sect2>
<sect2 id="subroutines">
<title>Subroutines</title>
<para>
A subroutine can be called from anywhere in the code. Syntax:
</para>
<programlisting role="correct">
sub SubName
{
// Code...
}</programlisting>
<para>The subroutine is called with <quote>call <replaceable>SubName</replaceable></quote>.</para>
</sect2>
</sect1>
<sect1 id="languagereference">
<title>µbe; language reference</title>
<sect2 id="if">
<title>if</title>
<para>Conditional branching.
Syntax:
<programlisting role="correct">if [expression] then [statement]</programlisting>
or
<programlisting role="correct">
if [expression] then
{
[statement block]
}</programlisting>
Similarly for else:
<programlisting role="correct">else [statement]</programlisting>
or
<programlisting role="correct">
else
{
[statement block]
}</programlisting>
</para>
<example><title>if</title>
<programlisting role="correct">
if porta.0 is high then
{
delay 200
}
else
{
delay 300
}</programlisting>
</example>
</sect2>
<sect2 id="alias">
<title>alias</title>
<para>Aliases one string to another. Syntax:
<programlisting role="correct">alias [from] [to]</programlisting>
</para>
</sect2>
<sect2 id="repeat">
<title>repeat</title>
<para>Executes the statement block repeatedly until expression evaluates to true.
The evaluation of the expression is performed after the statement block, so the statement block will always be executed at least once. Syntax:
<programlisting role="correct">
repeat
{
[statement block]
}
until [expression]</programlisting>
</para>
</sect2>
<sect2 id="while">
<title>while</title>
<para>
Similar to repeat, this repeatedly executes the statement block. However, the expression is evaluated before execution, not after. So if the expression evaluates to false on the first pass, then the statement block will not get executed.
Syntax:
<programlisting role="correct">
while [expression]
{
[statement block]
}</programlisting>
</para>
</sect2>
<sect2 id="goto">
<title>goto</title>
<para>
This causes execution of the code to continue at the next statement after the label specified.
Goto syntax:
<programlisting role="correct"><function>goto</function> [labelname]</programlisting>
Label syntax:
<programlisting role="correct"><function>labelname:</function></programlisting>
It is often considered good programming practice to avoid the use of goto. Use of control statements and subroutines will result in a much more readable program.
</para>
<example><title>goto</title>
<programlisting role="correct">
goto MyLabel
...
[MyLabel]:
// Code will continue at this point</programlisting>
</example>
</sect2>
<sect2 id="call">
<title>call</title>
<para>
Calls a subroutine.
Syntax:
<programlisting role="correct"><function>call</function> [SubName]</programlisting>
where <replaceable>SubName</replaceable> is the name of the subroutine to be called.
</para>
</sect2>
<sect2 id="delay">
<title>delay</title>
<para>
This causes the code execution to stop for the given period of time. The interval is in milliseconds.
Syntax:
<programlisting role="correct"><function>delay</function> [interval]</programlisting>
<note><para>At present, µbe; assumes that the PIC is operating at a frequency of 4MHz - &ie; each instruction takes 1 microsecond to execute. If this is not the case, the interval must be adjusted proportionately.</para></note>
</para>
</sect2>
<sect2 id="sevenseg">
<title>sevenseg</title>
<para>This is used to define the pin mapping for a (common cathode) seven segment display connected to the PIC. Syntax:
<programlisting role="correct"><function>sevenseg</function> [name] [a] [b] [c] [d] [e] [f] [g]</programlisting>
where [a]...[g] are the PIC pins to which the respective segments of the seven segment display are attached. The pins can be written either as PORTX.N or RXN.
</para>
<para>To display a number on the seven segment, the pin mapping is treated as a write only variable.
<example>
<title>Defining and outputting to a seven segment</title>
<programlisting role="correct">
sevenseg seg1 RB0 RB1 RB2 RB3 RB4 RB5 RB6
seg1 = x + 2</programlisting>
</example>
</para>
</sect2>
<sect2 id="keypad">
<title>keypad</title>
<para>This is used to define the pin mapping for a keypad connected to the PIC. Syntax:
<programlisting role="correct"><function>keypad</function> [name] [row 1] ... [row 4] [column 1] ... [column n]</programlisting>
where [row 1] ... [row 4] and [column 1] ... [column n] are the PIC pins to which the respective rows and columns of the keypad are attached (at the moment, the number of rows is not changeable). See <xref linkend="sevenseg"/> (above) for more information on pin mappings.
</para>
<para>The columns of the keypad should be pulled down via 100k resistors to ground. The row pins must be configured as outputs and the column pins as inputs. Once the keypad has been defined, it is treated as a read only variable.
<example>
<title>Defining and reading from a keypad</title>
<programlisting role="correct">
keypad keypad1 RB0 RB1 RB2 RB3 RB4 RB5 RB6
x = keypad1</programlisting>
</example>
</para>
<para>
By default, the values returned for a keypad are:
<itemizedlist>
<listitem><para>The value of the number if it is a numeric key (1 to 3 along top row; hexadecimal A to D down the fourth column and continuing for each extra column).</para></listitem>
<listitem><para>253 for the key in row 4, column 1.</para></listitem>
<listitem><para>254 for the key in row 4, column 3.</para></listitem>
</itemizedlist>
These values can be redefined by using the alias command, where the name of the key in row x, column y (rows and columns starting at 1), is Keypad_x_y. For example, to give the star key on a 4x3 keypad the value zero, the following alias would be used:
<example>
<title>Aliasing a keypad key to a value</title>
<programlisting role="correct">alias Keypad_4_1 0</programlisting>
</example>
</para>
</sect2>
</sect1>
<sect1 id="picio">
<title>PIC I/O</title>
<sect2 id="tristate">
<title>Port Direction</title>
<para>
The port direction is set by assigning a value to TRIS*, where * is the port letter. For example:
</para>
<example><title>Setting port directions</title>
<programlisting role="correct">TRISB = b'01111001'</programlisting>
</example>
<para>
The above sets pins RB1, RB2 and RB7 on PORTB as outputs, and the other pins on PORTB as inputs. In this example, b'01111001' is a binary representation of the output type. The 1 on the right represents an output on RB0, and the 0 on the left represents an input on RB7.
</para>
</sect2>
<sect2 id="ports">
<title>Port I/O</title>
<para>
The port can be treated as a variable. For example:
</para>
<example><title>Writing to a port</title>
<programlisting role="correct">x = PORTA</programlisting>
</example>
<para>
The above assigns the value of PORTA to the variable x.
</para>
</sect2>
<sect2 id="pins">
<title>Pin I/O</title>
<para>
Each pin on a port is obtained by prefixing the pin number by the port name; ⪚ Pin 2 (starting from Pin 0) on PORTA is known as
<emphasis>PORTA.0</emphasis>.
The syntax to set a pin state is:
<programlisting role="correct">PORTX.N = <emphasis>STATE</emphasis></programlisting>
where <emphasis>STATE</emphasis> can be <emphasis>high</emphasis> or <emphasis>low</emphasis>.
The syntax to test a pin state is:
<programlisting role="correct"><function>if</function> PORTX.N is <emphasis>STATE</emphasis> <function>then</function></programlisting>
Combining these examples, we have:
</para>
<example><title>Setting and testing pin state</title>
<programlisting role="correct">
TRISA = 0
TRISB = 255
<function>if</function> PORTA.3 is <function>high</function> <function>then</function>
{
PORTB.5 = <function>low</function>
}
<function>else</function>
{
PORTB = PORTA + 15
}</programlisting>
</example>
</sect2>
</sect1>
<sect1 id="variables">
<title>Variables</title>
<para>
All variables are 8-bit unsigned integers, giving a range from 0 to 255.
<application>µbe;</application> supports the typical unary operations (acting on one variable) and binary operations (acting on two variables) that are supported by the PIC. In addition, µbe; also supports division and multiplication.
</para>
<sect2 id="unary">
<title>Unary Operations</title>
<para>
<itemizedlist>
<listitem><para><emphasis>rotateleft x</emphasis> - Rotates the variable x left through carry.</para></listitem>
<listitem><para><emphasis>rotateright x</emphasis> - Rotates the variable x right through carry.</para></listitem>
<listitem><para><emphasis>increment x</emphasis> - Increments the variable x. If x has a value of 255, then x wraps round to 0.</para></listitem>
<listitem><para><emphasis>decrement x</emphasis> - Decrements the variable x. If x has a value of 0, then x wraps round to 255.</para></listitem>
</itemizedlist>
</para>
</sect2>
<sect2 id="arithmetic">
<title>Arithmetic</title>
<para>
Supported operations:
<itemizedlist>
<listitem><para><emphasis>Addition:</emphasis> x + y</para></listitem>
<listitem><para><emphasis>Subtraction:</emphasis> x - y</para></listitem>
<listitem><para><emphasis>Multiplication:</emphasis> x * y</para></listitem>
<listitem><para><emphasis>Division:</emphasis> x / y</para></listitem>
<listitem><para><emphasis>Binary XOR:</emphasis> x XOR y</para></listitem>
<listitem><para><emphasis>Binary AND:</emphasis> x AND y</para></listitem>
<listitem><para><emphasis>Binary OR:</emphasis> x OR y</para></listitem>
</itemizedlist>
</para>
</sect2>
<sect2 id="comparison">
<title>Comparison</title>
<para>
Supported operations:
</para>
<itemizedlist>
<listitem><para><emphasis>Equals:</emphasis> x == y</para></listitem>
<listitem><para><emphasis>Does not equal:</emphasis> x != y</para></listitem>
<listitem><para><emphasis>Is greater than:</emphasis> x > y</para></listitem>
<listitem><para><emphasis>Is less than:</emphasis> x < y</para></listitem>
<listitem><para><emphasis>Is greater than or equal to:</emphasis> x >= y</para></listitem>
<listitem><para><emphasis>Is less than or equal to:</emphasis> x <= y</para></listitem>
</itemizedlist>
<para>
For example:
</para>
<example><title>Comparison</title>
<programlisting role="correct">
<function>if</function> PORTA >= 5 <function>then</function>
{
...
}</programlisting>
</example>
</sect2>
</sect1>
<!--
<sect1 id="interrupts">
<title>Interrupts</title>
<para>
There are several types of events, and some of these take an optional parameter making
the condition under which the routine is called more specific.
<itemizedlist>
<listitem><para><emphasis>change <pin name></emphasis>
- Occurs when the state of the specified pin changes. Pin name is in the usual syntax of PORTX.n, ⪚ <programlisting>interrupt change PORTB.4</programlisting></para></listitem>
<listitem><para><emphasis>timer</emphasis> - ///TODO</para></listitem>
<listitem><para><emphasis>external</emphasis> - ///TODO</para></listitem>
</itemizedlist>
</para>
</sect1>
-->
</chapter>
|