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 install XCSB on your computer
- You need to run the XCSB compiler
- You need to create a function called main
- You need to create some variables to use
- You need to understand about expressions and assignments
- You need to understand the syntax of the if and goto statements
- You need to understand how to access chip peripherals (special registers)
- You need to create a dummy intserv function
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 cannot leave information in a local variable between invokations of a specific function and expect it to remain unchanged (as you would with global variables) - if in doubt use globals until you understand the difference
- declaring a local variable with the same name as a global variable will hide the global variable and the local variable will take priority. This is not a fault, it is actually a very important feature of many modern languages. It allows the programmer to create a variable for local use without worrying whether on not it is already defined and how using it may affect some other piece of code that is already using it. Creating a local variable protects the rest of the system from your new code (if you don't go out of your way to break things)
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 * 4The 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 endifIf 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 + 1The 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 = 0x20To read the contents of one of the registers simply use it as part of an expression.e.g. x = PORTAThe fileshwreg-p16f84.h hwreg-p16f628.h hwreg-p16f876.h hwreg-p16f877.hare specific to the corresponding PIC processors, you should copy the relevent one to the file hwreg.hYou 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