QUICK START

XC Structured Basic (XCSB) is a simple language to learn and use. However unlike a program written for a PC which can easily be debugged on the PC, a program written for a small microcontroller such as a PIC can be very difficult to debug. The trick is to start with small simple code for the PIC and to expand on it. You are probably anxious to dive right in and write your first XCSB program for the PIC. To get you started as quickly as possible the basic information that you will need has been collected together here for you.

You need to create a function called main

The XCSB compiler recognises the user function called main as the program entry point. This means that of all the code you write, you indicate how the program should be started by putting your start code into a function which you must name main. Your main function is then responsible for initialising variables, on-chip peripherals and calling other functions.

You need to create some variables to use

Many BASIC implementations do not offer a local variable facility relying instead purly on globals. You must be aware of the differences between local and global variables and the way they are defined in XCSB. The XCSB compiler optimises the way it uses memory, sharing local variables between functions when there is no possibility of a clash.

You need to understand about expressions and assignments

The XCSB compiler is an optimising compiler. This means that it organises machine code instructions and memory accesses in such a way as to improve execution speed and reduce memory usage. As a consequence of this some types of expressions are evaluated in the destination (where the result will be ultimately stored). This can involve producing partial results which modify the destination in seperate steps. Where the destination is an on-chip peripheral this may produce strange effects.

Initially, if in doubt only use assignments between on-chip peripherals and program variables. Do not use other types of expressions involving on-chip peripherals.

e.g.
	// this simple assignment is guarented to be OK
	PORTA = x

	// this is potentially a problem if PORTA is not defined as a PORT
	PORTA = x * 4

The PORT data type eliminates this potential problem but it should not be used by default for normal variables since it prevents the compiler from generating optimised code when such variables are used by the programmer. An unoptimised executable can require more than twice as much space as an optimised executable and run at a fraction of the speed.

An assignment is an expression and can be used anywhere where an expression is valid. It is possible to confuse a test for equality with an assignment expression.

  • The assignment operator uses one '=' character on its own as the assignment operator.
  • The equals operator uses two '=' characters side by side - thus '=='

PASCAL gets around the confusion by using ':=' for asignment but given that assignment is much more widely used in a program than the test for equality it makes sence to place the burden on the equals operator rather than the asignment operator. So XCSB uses the C language convention.

If you use assignment where you mean test for equality you may find that your program always behaves as though the condition is true.

e.g.
	// this is always true
	if x = 1 then
	endif

	// this is always false
	if x = 0 then
	endif

If you use test for equality where you mean assignment you will find that your program mysteriously loses the value you think you have assigned to a variable or on-chip peripheral.

e.g.
	// this will evaluate x+1 and throw the result away
	x == x + 1

	// this will increment x by 1
	x = x + 1

The logical AND operator (which is && ) and the logical OR operator (which is || ) are early out operators. They are wonderful things but DO NOT use them until you have studied them and are happy you understand them.

You need to understand the syntax of the IF and GOTO statements

As a very minimum if you understand these two types of flow control statements you can start producing programs to do useful things. The other XCSB statements, although important, are not vital to your early use of the language and its compiler.

You need to understand how to access chip peripherals (special registers)

The file hwreg.h contains the names and addresses of the microcontroller specific hardware registers. To write a value into one of the registers simply assign a value to it.
e.g.
	PORTA = 0x20
To read the contents of one of the registers simply use it as part of an expression.
e.g.

	x = PORTA
The files
	hwreg-p16f84.h
	hwreg-p16f628.h
	hwreg-p16f876.h
	hwreg-p16f877.h
are specific to the corresponding PIC processors, you should copy the relevent one to the file hwreg.h

You need to create a dummy intserv function

The XCSB compiler recognises the user function called intserv as the interrupt service request handler. This means that when a hardware interrupt occures this function is executed in order to handle the interrupt. At the very least you must create an intserv function that does nothing.

This is simply the two statements

proc intserv()
endproc

 
Back to main XCSB documentation