Proglang For Microcontroller Pic
Proglang For Microcontroller Pic
0. Introduction
PIC is the name of a single-chip microcontroller designed and fabricated by Microchip Inc. It
features an ALU with basic operations for arithmetic, a 128 byte data memory, a 2048 word
program memory, and 2 I/O ports (PIC 16C84). Here we present a programming language that
is tailored to the PIC’s size and architecture. The challenge lay in postulating a design allowing
to abstract from the peculiarities of the particular architecture and overcome the tedium of
coding instruction by instruction with an assembler, and yet of letting the processor’s facilities
be sufficiently transparent, that no excessively inefficient programs will be produced.
The language’s syntax is formally defined in terms of Extended BNF. Short examples of most
syntactic constructs are provided to help explaining their meanings and to illustrate the use and
structure of the language.
3. Expressions
2
Expressions denote either a variable, a constant, or a computed value. The operators are
addition, subtraction for integers, and logical operators for sets. Expressions are said to be
evaluated, and the resulting value is of type INT or SET. Examples:
x x+5 x–y
u*v u+v u-v NofBits(x)
When applied to operands of type INT, + and – denote addition and subtraction respectively.
When applied to operands of type SET or BOOL, + denotes inclusive disjunction (or), * denotes
conjunction (and), - denotes exclusive disjunction (xor).
expression = (ident | constant) [operator (identifier | constant)] |
ident “(“ [expression] “)”.
operator = “+” | “-“ | “&”.
An expression may be a function call. If the function has a parameter, the actual (value)
parameter is enclosed in parentheses.
4. Conditions
A condition represents a computed truth value. It consists of either a conjunction or a
disjunction of terms, and a term is either a Boolean variable or its negation, or a comparison.
An asterisk stands for True. Examples:
x>y x=0 b ~u.2
x >= 0 & x < 10 x = 1 OR y = 1
The symbol “~” denotes negation, “#” inequality.
term = ident relation (ident | constant) | [“~”] ident [“.” index].
relation = “=” | “#” | “<” | “>” | “<=” | “>=”.
index = integer.
conjunction = term {“&” term}.
disjunction = term {OR disjunction}.
condition = conjunction | disjunction.
5. Statements
Statements denote actions, and are said to be executed. There are simple statements and
composite statements. The latter consist of components which are statements themselves.
Simple statements are assignments, procedure calls, and commands operating on a variable.
Assignments consist of an expression and a variable. Their execution consists of the evaluation
of the expression and the assignment of the resulting value to the variable. Examples of
assignments are:
x := y y := 0 x := x + 1 u := u * v
Procedure calls consist of the identifier denoting the procedure to be activated, possibly
followed by a parameter:
Output(100)
Examples of commands and queries acting on Boolean variables are:
! A.0 set A.0 to true
! ~B.3 set B.3 to false
? A.0 wait until A.0
? ~B.5 wait until not B.5
The queries are meaningful only if the operand is a port variable representing an input signal.
The two ports of the PIC controller are predefined and denoted by A and B.
Examples of commands acting on integer variables are:
3
6. Procedure Declarations
Procedures, also called subroutines, are sequences of statements that can be activated by
procedure calls. A procedure may have a single parameter. It may have local variables that are
visible only within the procedure. If a procedure has a result, it is called a function. Functions
can be used in assignments. Their result type is indicated by the type symbol in the heading of
the procedure, and the result is specified at its end.
Examples of a procedure and a function declaration are:
PROCEDURE Send(INT x); {B.6 = clock, B.7 = data}
INT n;
BEGIN n := 8;
REPEAT !~B.6;
IF x.0 THEN !B.7 ELSE !~B.7 END ;
!B.6; ROR x; DEC n
UNTIL n = 0
END Send
4
7. Modules
An entire PICL program is called a module. It consists of a heading, specifying its name,
constant and variable declarations, procedure and function declarations, and a sequence of
statements, the main body.
Module = MODULE ident ";"
[CONST {ident "=" constant ";"}]
[type {ident {"," ident} ";"}]
{ProcedureDeclaration ";"}
[BEGIN statseq] END ident ".".
The following sample program rotates a single zero bit among the flags x.0 – x.7.
MODULE Rotate;
CONST delay = 250;
INT x, d0, d1;
BEGIN x := 1;
!S.5; B := 0; !~S.5;
REPEAT ROL x; B := x; d1 := delay;
REPEAT DEC d1; d0 := delay;
REPEAT DEC d0
UNTIL d0 = 0;
DEC d1
UNTIL d1 = 0
END
END Rotate.
Note: The first line of the procedure body serves to configure the controller. A, B, S are
predeclared identifiers. A and B denote the controller’s input and output ports, and S denotes
the controller’s status register. Every bit of A and B must be configured, and is initially set to
input. In this example, all 8 ports B.0, B.1, … B.7 are set to output. The inner repetitions serve
as delays. Assuming that LEDs are connected to the ports, the lights rotate and indicate the
speed of the process.